0.1.1 • Published 3 years ago

@nests/crud v0.1.1

Weekly downloads
12
License
MIT
Repository
-
Last release
3 years ago

crud(@nests/crud)

Nest.js CRUD for RESTful APIs 该包只是一个核心包,具体请看@nests/mongoose-crud

Table of Contents

Installation

npm

npm i @nestjs/swagger @nests/crud

yarn

yarn add @nestjs/swagger @nests/crud

Usage

Step 1:创建路由工厂

const findOne: Factory = () => ({
  name: 'findOne',
  main() {
    // ...
  },
});

路由工厂,需要返回满足 Route 接口的对象

export interface Route {
  name: string;
  main: Function;
  paramtypes?: Type<unknown>[];
  meta?: {
    method?: MethodDecorator[];
    param?: ParameterDecorator[];
  };
}
  • name:路由名称
  • main:函数主体
  • paramtypes:路由函数参数类型
  • meta
    • method:添加您想要的方法装饰器,例如 @Get()@ApiParam()
    • param:添加您想要的参数装饰器,例如 @Param()@Query()

Step 2:将路由添加到 Store

有以下两种方式

// 1,添加到全局的仓库中
GlobalStore.clear();
GlobalStore.put(FindOne, FindMany);

// 2,添加到局部的仓库中,每次生成 CRUD 后都会被销毁
const crud = new CRUD(options, target);
crud.put(findOne, findMany);

Step 3:实现装饰器

function Crud(options) {
  return target => {
    new CRUD(options, target).create();
  };
}

具体参考 Sample

Configuration

DTO configuration

配置路由的数据转换对象的选项

每一个 Key 都是 HTTP 请求方法(get,post...),Value 为 class 或者一个对象

Interface

export interface DynamicDto {
  body?: Type<unknown>;
  response?: Type<unknown>;
}

export type DtoConfig = Partial<Record<Method, Type<unknown> | DynamicDto>>;
  • body:HTTP 请求体
  • response:HTTP 响应体

Parameter configuration

配置 HTTP URL path to file 的选项

Interface

export interface ParamConfig {
  name: string;
  type: string | Function | Type<unknown> | [Function];
  pipes?: (PipeTransform | Type<PipeTransform>)[];
  propertyKey?: string;
}
  • name: URL path to file,默认为 id
  • type:URL path to file 的类型,例如,字符串,数字字符串,默认为 string
  • pipes: nestjs PipeTransform
  • propertyKey:数据库查询时,对应的键值

Helper

为方便使用,提供了路由工厂的帮助方法

Example

为方便使用 DTO 选项,从而实现 Dto 静态类

const findMany = ({ model, dto, query }) => {
  const cls = Dto.normalize(model, dto['get']);

  // ...
};
  • Dto
    • normalize:将参数规范化为 DynamicDto
    • bulk:生成 Bulk DTO(以下是该 DTO 的一些属性)
      • bulk:数据
    • pagination:生成 Pagination DTO(以下是该 DTO 的一些属性)
      • data:数据
      • total:数据总数
      • current:当前页码
      • pageCount: 页码数
      • pageSize: 每页条数
0.1.1

3 years ago

0.1.0

3 years ago

0.1.0-rc.1

4 years ago

0.1.0-rc.2

4 years ago

0.1.0-beta.2

4 years ago

0.1.0-beta.1

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago