Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import { ModelType } from "@hasezoey/typegoose/lib/types";
  2. import { CrudRequest, CreateManyDto, GetManyDefaultResponse, CrudService } from "@nestjsx/crud";
  3. import { BadRequestException, NotFoundException } from "@nestjs/common";
  4. import { mongoose } from "@hasezoey/typegoose";
  5.  
  6. export class MongooseCrudService<T> extends CrudService<T> {
  7. constructor(public model: ModelType<T>) {
  8. super()
  9. }
  10.  
  11. buildQuery(req: CrudRequest) {
  12. let { limit = 10, page = 1, offset: skip = 0, filter = [], fields = [], sort = [], join = [], paramsFilter = [] } = req.parsed
  13. if (page > 1) {
  14. skip = (page - 1) * limit
  15. }
  16. const options = {
  17. page,
  18. skip,
  19. limit,
  20. sort: sort.reduce((acc, v) => (acc[v.field] = v.order === 'ASC' ? 1 : -1, acc), {}),
  21. populate: join.map(v => v.field),
  22. select: fields.join(' ')
  23. }
  24. const where = filter.reduce((acc, { field, operator, value }) => {
  25. let cond = null
  26. switch (operator) {
  27. case 'starts':
  28. cond = new RegExp(`^${value}`, 'i')
  29. break;
  30. case 'ends':
  31. cond = new RegExp(`${value}\$`, 'i')
  32. break;
  33.  
  34. case 'cont':
  35. cond = new RegExp(`${value}`, 'i')
  36. break;
  37. case 'excl':
  38. cond = { $ne: new RegExp(`${value}`, 'i') }
  39. break;
  40. case 'notin':
  41. cond = { $nin: value }
  42. break
  43. case 'isnull':
  44. cond = null
  45. break
  46. case 'notnull':
  47. cond = { $ne: null }
  48. break
  49. case 'between':
  50. const [min, max] = value
  51. cond = { $gte: min, $lte: max }
  52. break
  53. default:
  54. cond = { [`\$${operator}`]: value }
  55. }
  56. acc[field] = cond
  57. return acc
  58. }, {})
  59. const idParam = paramsFilter.find(v => v.field === 'id')
  60. return { options, where, id: idParam ? idParam.value : null }
  61. }
  62.  
  63. async getMany(req: CrudRequest) {
  64.  
  65. const { options, where } = this.buildQuery(req)
  66. const queryBuilder = this.model.find().setOptions({
  67. ...options
  68. }).where({
  69. ...where
  70. })
  71. options.populate.map(v => {
  72. queryBuilder.populate(v)
  73. })
  74.  
  75. const data = await queryBuilder.exec()
  76. if (options.page) {
  77. const total = await this.model.countDocuments(where)
  78. return this.createPageInfo(data, total, options.limit, options.skip)
  79. }
  80. return data
  81. }
  82. async getOne(req: CrudRequest): Promise<T> {
  83. const { options, where, id } = this.buildQuery(req)
  84. const queryBuilder = this.model.findById(id).setOptions({
  85. ...options
  86. }).where({
  87. ...where
  88. })
  89. options.populate.map(v => {
  90. queryBuilder.populate(v)
  91. })
  92.  
  93. const data = await queryBuilder.exec()
  94. return data
  95. }
  96. createOne(req: CrudRequest, dto: T): Promise<T> {
  97. throw new Error("Method not implemented.");
  98. }
  99. createMany(req: CrudRequest, dto: CreateManyDto<any>): Promise<T[]> {
  100. throw new Error("Method not implemented.");
  101. }
  102. updateOne(req: CrudRequest, dto: T): Promise<T> {
  103. throw new Error("Method not implemented.");
  104. }
  105. replaceOne(req: CrudRequest, dto: T): Promise<T> {
  106. throw new Error("Method not implemented.");
  107. }
  108. deleteOne(req: CrudRequest): Promise<void | T> {
  109. throw new Error("Method not implemented.");
  110. }
  111. throwBadRequestException(msg?: any): BadRequestException {
  112. throw new Error("Method not implemented.");
  113. }
  114. throwNotFoundException(name: string): NotFoundException {
  115. throw new Error("Method not implemented.");
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement