0.1.1 • Published 3 years ago

@nests/mongoose-crud v0.1.1

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

mongoose-crud(@nests/mongoose-crud)

Nest.js CRUD for RESTful APIs - Mongoose (更接近原生的 CRUD)

Table of Contents

Installation

npm

npm i @nestjs/swagger @nests/crud # install peer-dependency
npm i @nests/mongoose-crud

yarn

yarn add @nestjs/swagger @nests/crud # install peer-dependency
yarn add @nests/mongoose-crud

Usage

创建您的 Nest.js controller、service、module 并在您想要添加 CRUD 的 controller 上添加 CRUD 装饰器,以及在使相应的 service 继承 CrudService

@Crud({
  model: User,
})
@Controller('users')
export class UsersController {
  constructor(readonly service: UsersService) {}
}
@Injectable()
export class UsersService extends CrudService {
  constructor(@InjectModel(User.name) readonly model: Model<User>) {
    super(model);
  }
}

Options

CRUD 装饰器中的选项

Routes options

Example

如果您只想要 findOnefindMany 路由,您可以这样设置

@Crud({
  ...
  routes: {
    only: ['findOne', 'findMany'],
  }
})

Interface

export interface RoutesOptions {
  only?: RouteName[];
  exclude?: RouteName[];
  common?: Unit;
  findOne?: Unit & Hook;
  findMany?: Unit;
  createOne?: Unit & Hook & RetShallowObject;
  createMany?: Unit & RetShallowObject;
  updateOne?: Unit & Hook;
  replaceOne?: Unit & Hook;
  deleteOne?: Unit;
  deleteMany?: Unit;
}

Options

  • only:只实现某些路由如果为空或未定义 - 实现全部
  • exclude:剔除某些路由
  • common:给与实现的所有路由相同拦截器和装饰器
  • routes:为每个路由添加不同的拦截器和装饰器

Hook

以下路由提供 pre 钩子(可以是异步函数)

路由运行时间函数参数
findOne返回数据之前,函数的返回值会被当作路由的返回值数据库查询结果
createOne插入在数据库之前,函数的返回值会被插入数据库DTO
updateOne在数据库更新之前,函数的返回值会更新原来的数据DTO
replaceOne在数据库替换之前,函数的返回值会替换原来的数据DTO

钩子函数可以用来做一些数据转换,或者设置填充字段(Setting Populated Fields

Query options

Example

如果您需要对数据库查询结果进行排序,且限制最多可以查询 25 条数据,您可以这样设置

@Crud({
  ...
  query: {
    sort: { id: 1 },
    maxLimit: 25,
  }
})

Interface

export interface QueryOptions {
  field?: QueryFieldOptions;
  maxLimit?: number;
  sort?: QuerySortOptions;
  populate?: QueryPopulateOptions[];
}

Options

  • field:
    • disable:禁止查询的字段,GET 请求结果中,将不会包含这些字段
    • optional:可选的查询字段,默认情况下,不会出现在 GET 请求结果中,需要指定字段查询
  • maxLimit: 表示在 GET 请求中可以查询的最大结果数量
  • sort:Mongoose 查询,Key,Value 形式 Value 可以是 asc, desc, ascending, descending, 1, and -1.
  • populate:Mongoose Populate

Add route and overload route

对于添加路由来说,每个路由都是非常接近原生状态的 nestjs 代码,您只需要像往常一样即可 对于重载路由来说,如果您在控制器中写有于本库相同的路由(名字相同),那么该路由的优先级会高于程序提供的路由,总而言之,您只需要像往常一样即可

为了更接近原生,在某些处理采用了闭包的方式实现,从而不利于在其他路由中使用 如果您觉得某路由的 service 处理逻辑不符合您的开发需求,建议您覆盖该方法,在需求确定时,很大程度不太依赖于 CRUD options

URL search params

  • field:选择的字段

    Request URL:/users?field=id,username,

  • search:Mongoose 条件查询,JSON 形式,解析成功后,直接由 Mongoose 的 find 函数处理

    Request URL:/users?search={"id":1}

  • sort:Mongoose 查询排序,格式为 Key,ValueValue 只能是 1(正序)或 -1(倒序)

    Request URL:/users?sort=username,1&sort=id,-1

  • limit:可以查询的最大结果数量

    Request URL:/users?limit=10

  • page:查询的页码

    Request URL:/users?page=2

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.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago