@nestjs-extend/crud v1.0.3
NestJs CRUD
Links
Install
npm i @nestjs-extend/crud @nestjs/swagger
# OR
yarn add @nestjs-extend/crud @nestjs/swagger
Getting started
Step 1: 构建属于您的数据库查询选项接口以及请求接口。
import { DefaultQuery } from '@nestjs-extend/crud';
export interface QueryOptions extends DefaultQuery {
sort?: object;
}
export interface Request {
params: object;
query: object;
}
Step 2: 实现请求工厂, 将传入 nestjs 的请求, 和控制查询的选项。要求生成可直接在相应的数据库驱动程序中使用的请求对象。
import { RequestFactory as Factory, ControlOptions } from '@nestjs-extend/crud';
export class RequestFactory implements Factory<QueryOptions, Request> {
create(request: any, options: ControlOptions<QueryOptions>): Request {
return {
options,
params: request.params,
query: request.query,
};
}
}
Step 3: 实现 CRUD service。
import { CrudService } from '@nestjs-extend/crud'
export class MongooseCrudService extends CrudService {
constructor(protected readonly model: Model<any>) {
super();
}
// ....
Step 4: 定义配置并生成 @Crud()
装饰器。
import { CrudConfig } from '@nestjs-extend/crud';
import { QueryOptions } from './interfaces';
export const config: CrudConfig = {
routes: {
// 配置路由
},
factory: new RequestFactory(),
};
export const Crud = def<QueryOptions>(config);
Options
Model options
配置数据库模型, 数据转换对象相关的选项。
import { ApiProperty } from '@nestjs/swagger';
class User {
@ApiProperty()
username: string;
@ApiProperty()
password: string
}
class CreateUserDto {
// ...
}
@Crud({
model: User
dto: {
create: CreateUserDto
}
})
model: 必填, 数据库模型对象。
dto: 可选, 数据转换对象(DTO), 可以设置
create
,update
,replace
三种路由行为的 DTO, 皆为可选的。
Control options
以下选项会传入请求工厂, 您可以获取相应的选项, 用来帮助您生成请求对象,
Routes options
Interface
export type RouteName =
| 'findOne'
| 'findMany'
| 'createOne'
| 'createMany'
| 'updateOne'
| 'replaceOne'
| 'deleteOne';
export interface RoutingOptions {
interceptors?: (NestInterceptor | Function)[];
decorators?: MethodDecorator[];
}
export interface ExtraOptions {
[extraProps: string]: any;
}
export type RoutesOptions = {
[key in RouteName]?: RoutingOptions & ExtraOptions;
} & {
only?: RouteName[];
exclude?: RouteName[];
common?: RoutingOptions;
};
only: 可选, 表示仅仅实现哪些路由。如果为未定义或为空 - 实现全部路由。
exclude: 可选, 表示哪些路由不实现。如果
controller
中存在与RouteName
名字相同的属性, 又不会实现。common: 可选, 表示提供给已经实现的所有路由相同的选项。
Param options
Interface
export interface ParamOptions {
field?: string;
name?: string;
type?: string | Function | Type<unknown> | [Function];
pipes?: (Type<PipeTransform> | PipeTransform)[];
}
field: 可选, 表示资源字段, 默认为
id
。name: 可选, 请求对象中的键值, 默认值为填写的资源字段(field)。⊕
type: 可选, 将应用于
@ApiParam()
装饰器, 默认为string
。pipes: 可选, 用于转换或验证资源字段的类型。
⊕: 此属性的含义是这样没错, 但是传入的选项, 您写的是什么, 它就长什么样。😜 因为, 此选项将完整的传入请求工厂, 用于帮助您生成请求对象的, 具体的实现应有您来实现。您甚至还可以传入
service
中。
Example
如果您有一个名为 id 的资源字段, 它是 MD5 hash , 并且, 您想要它的键值为 post
, 您可以设置此 param 选项。
import { ParseMD5Pipe } from '@nestjs-extend/crud';
@Crud({
...
param: {
field: 'id',
name: 'post',
type: 'string',
pipes: [ParseMD5Pipe],
},
...
})
此选项是配置只有一个资源字段的动态路由(
/:id
), 一般情况下, 已经够用。如果想要多个资源字段的动态路由(/:id/:name
), 请自己实现 😜。
Query options
Interface
export interface DefaultQuery {
allow?: string[];
exclude?: string[];
limit?: number;
maxLimit?: number;
alwaysPaginate?: boolean;
}
allow: 可选, 允许在 GET 请求中接收的字段数组。如果为空或未定义 - 允许全部。
exclude: 可选, 将从 GET 响应中排除的字段数组。
limit: 可选, 将应用于数据库查询的默认限制。
maxLimit: 可选, 表示在 GET 请求中可以查询的最大结果数量, 默认为
100
。alwaysPaginate: 可选, 指定是否总是返回带有分页数据的对象, 默认为
true
。
Adding routes
import { UseInterceptors } from '@nestjs/common';
import { Req, CrudRequestInterceptor } from '@nestjs-extend/crud';
...
@UseInterceptors(CrudRequestInterceptor)
@Get('/:id/users')
async findUsers(@Req() req: any) {
// ...
}
此包导出
@Req()
,@Request()
装饰器, 都是获取用来请求工厂生成的结果。
CRUD config
自定义 CRUD。
def({
routes: {
findOne: {
query: [
{
name: 'filter',
type: 'string',
},
],
ok: { type: Dto },
},
},
factory: RequestFactory,
});
routes: 可选, 配置路由功能、OpenAPI。
query: 可选, 将应用于
@ApiQuery()
装饰器。ok: 可选, 将应用与
@ApiOkResponse()
装饰器。此选项也可是一个函数, 传入ModelOptions
作为参数。
factory: 必填, 用于生成请求对象的工厂。