@drax/crud-back 0.11.5 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/builders/CrudSchemaBuilder.js +349 -0
  2. package/dist/controllers/AbstractFastifyController.js +105 -176
  3. package/dist/index.js +15 -1
  4. package/dist/regexs/QueryFilterRegex.js +3 -0
  5. package/dist/repository/AbstractMongoRepository.js +114 -28
  6. package/dist/repository/AbstractSqliteRepository.js +131 -41
  7. package/dist/schemas/DeleteBodyResponseSchema.js +9 -0
  8. package/dist/schemas/ErrorBodyResponseSchema.js +16 -0
  9. package/dist/schemas/ExportBodyResponseSchema.js +9 -0
  10. package/dist/schemas/FindBySchema.js +6 -0
  11. package/dist/schemas/FindSchema.js +9 -0
  12. package/dist/schemas/IdParamSchema.js +6 -0
  13. package/dist/schemas/PaginateBodySchema.js +20 -0
  14. package/dist/schemas/PaginateSchema.js +17 -0
  15. package/dist/schemas/SearchSchema.js +5 -0
  16. package/dist/services/AbstractService.js +8 -5
  17. package/dist/zod/DeleteBodySchema.js +6 -0
  18. package/dist/zod/IdParamSchema.js +6 -0
  19. package/dist/zod/PaginateBodySchema.js +20 -0
  20. package/package.json +7 -6
  21. package/src/builders/CrudSchemaBuilder.ts +420 -0
  22. package/src/controllers/AbstractFastifyController.ts +149 -165
  23. package/src/index.ts +28 -0
  24. package/src/regexs/QueryFilterRegex.ts +4 -0
  25. package/src/repository/AbstractMongoRepository.ts +153 -40
  26. package/src/repository/AbstractSqliteRepository.ts +196 -72
  27. package/src/schemas/DeleteBodyResponseSchema.ts +12 -0
  28. package/src/schemas/ErrorBodyResponseSchema.ts +20 -0
  29. package/src/schemas/ExportBodyResponseSchema.ts +12 -0
  30. package/src/schemas/FindBySchema.ts +9 -0
  31. package/src/schemas/FindSchema.ts +13 -0
  32. package/src/schemas/IdParamSchema.ts +9 -0
  33. package/src/schemas/PaginateSchema.ts +21 -0
  34. package/src/schemas/SearchSchema.ts +8 -0
  35. package/src/services/AbstractService.ts +42 -33
  36. package/tsconfig.tsbuildinfo +1 -1
  37. package/types/builders/CrudSchemaBuilder.d.ts +2401 -0
  38. package/types/builders/CrudSchemaBuilder.d.ts.map +1 -0
  39. package/types/controllers/AbstractFastifyController.d.ts +6 -1
  40. package/types/controllers/AbstractFastifyController.d.ts.map +1 -1
  41. package/types/index.d.ts +10 -1
  42. package/types/index.d.ts.map +1 -1
  43. package/types/regexs/QueryFilterRegex.d.ts +4 -0
  44. package/types/regexs/QueryFilterRegex.d.ts.map +1 -0
  45. package/types/repository/AbstractMongoRepository.d.ts +6 -3
  46. package/types/repository/AbstractMongoRepository.d.ts.map +1 -1
  47. package/types/repository/AbstractSqliteRepository.d.ts +23 -8
  48. package/types/repository/AbstractSqliteRepository.d.ts.map +1 -1
  49. package/types/schemas/DeleteBodyResponseSchema.d.ts +20 -0
  50. package/types/schemas/DeleteBodyResponseSchema.d.ts.map +1 -0
  51. package/types/schemas/ErrorBodyResponseSchema.d.ts +60 -0
  52. package/types/schemas/ErrorBodyResponseSchema.d.ts.map +1 -0
  53. package/types/schemas/ExportBodyResponseSchema.d.ts +20 -0
  54. package/types/schemas/ExportBodyResponseSchema.d.ts.map +1 -0
  55. package/types/schemas/FindBySchema.d.ts +13 -0
  56. package/types/schemas/FindBySchema.d.ts.map +1 -0
  57. package/types/schemas/FindSchema.d.ts +19 -0
  58. package/types/schemas/FindSchema.d.ts.map +1 -0
  59. package/types/schemas/IdParamSchema.d.ts +11 -0
  60. package/types/schemas/IdParamSchema.d.ts.map +1 -0
  61. package/types/schemas/PaginateBodySchema.d.ts +61 -0
  62. package/types/schemas/PaginateBodySchema.d.ts.map +1 -0
  63. package/types/schemas/PaginateSchema.d.ts +41 -0
  64. package/types/schemas/PaginateSchema.d.ts.map +1 -0
  65. package/types/schemas/SearchSchema.d.ts +10 -0
  66. package/types/schemas/SearchSchema.d.ts.map +1 -0
  67. package/types/services/AbstractService.d.ts +5 -5
  68. package/types/services/AbstractService.d.ts.map +1 -1
  69. package/types/zod/DeleteBodySchema.d.ts +11 -0
  70. package/types/zod/DeleteBodySchema.d.ts.map +1 -0
  71. package/types/zod/IdParamSchema.d.ts +11 -0
  72. package/types/zod/IdParamSchema.d.ts.map +1 -0
  73. package/types/zod/PaginateBodySchema.d.ts +61 -0
  74. package/types/zod/PaginateBodySchema.d.ts.map +1 -0
@@ -0,0 +1,20 @@
1
+ import z from "zod"
2
+
3
+ const ErrorBodyResponseSchema = z.object({
4
+ statusCode: z.string(),
5
+ error: z.string(),
6
+ message: z.string(),
7
+ i18nMessage: z.string()
8
+ });
9
+
10
+ const ValidationErrorBodyResponseSchema = ErrorBodyResponseSchema.extend({
11
+ inputErrors: z.array(z.object({
12
+ field: z.string(),
13
+ reason: z.string(),
14
+ value: z.any().optional(),
15
+ })).optional()
16
+ });
17
+
18
+ export default ErrorBodyResponseSchema
19
+
20
+ export {ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema}
@@ -0,0 +1,12 @@
1
+ import z from "zod"
2
+
3
+ const ExportBodyResponseSchema = z.object({
4
+ url: z.string(),
5
+ rowCount: z.number(),
6
+ time: z.number(),
7
+ fileName: z.string(),
8
+ });
9
+
10
+ export default ExportBodyResponseSchema
11
+
12
+ export {ExportBodyResponseSchema}
@@ -0,0 +1,9 @@
1
+ import z from "zod"
2
+
3
+ const FindByParamSchema = z.object({
4
+ value: z.string(),
5
+ field: z.string(),
6
+ });
7
+
8
+
9
+ export {FindByParamSchema}
@@ -0,0 +1,13 @@
1
+ import z from "zod"
2
+ import QueryFilterRegex from "../regexs/QueryFilterRegex.js";
3
+
4
+
5
+ const FindQuerySchema = z.object({
6
+ orderBy: z.string().optional(),
7
+ order: z.enum(["asc", "desc"]).optional(),
8
+ search: z.string().optional(),
9
+ filters: z.string().regex(QueryFilterRegex).optional().describe("Format: field;operator;value|field;operator;value|..."),
10
+ });
11
+
12
+
13
+ export {FindQuerySchema}
@@ -0,0 +1,9 @@
1
+ import z from "zod"
2
+
3
+ const IdParamSchema = z.object({
4
+ id: z.string().min(1, "validation.required")
5
+ });
6
+
7
+ export default IdParamSchema
8
+
9
+ export {IdParamSchema}
@@ -0,0 +1,21 @@
1
+ import z from "zod"
2
+ import QueryFilterRegex from "../regexs/QueryFilterRegex.js";
3
+
4
+ const PaginateQuerySchema = z.object({
5
+ page: z.number().optional(),
6
+ limit: z.number().optional(),
7
+ orderBy: z.string().optional(),
8
+ order: z.enum(["asc", "desc"]).optional(),
9
+ search: z.string().optional(),
10
+ filters: z.string().regex(QueryFilterRegex).optional().describe("Format: field;operator;value|field;operator;value|..."),
11
+ });
12
+
13
+
14
+ const PaginateBodyResponseSchema = z.object({
15
+ page: z.number(),
16
+ limit: z.number(),
17
+ total: z.number(),
18
+ items: z.array(z.any())
19
+ });
20
+
21
+ export {PaginateQuerySchema, PaginateBodyResponseSchema}
@@ -0,0 +1,8 @@
1
+ import z from "zod"
2
+
3
+ const SearchQuerySchema = z.object({
4
+ search: z.string().optional(),
5
+ });
6
+
7
+
8
+ export {SearchQuerySchema}
@@ -1,6 +1,6 @@
1
1
  import {ZodErrorToValidationError} from "@drax/common-back"
2
2
  import {ZodError} from "zod";
3
- import type {ZodSchema} from "zod";
3
+ import type {ZodObject, ZodRawShape} from "zod";
4
4
  import type {
5
5
  IDraxPaginateOptions,
6
6
  IDraxPaginateResult,
@@ -17,7 +17,7 @@ import {IDraxFindOneOptions} from "@drax/crud-share/types/interfaces/IDraxFindOn
17
17
  abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
18
18
 
19
19
  protected _repository: IDraxCrudRepository<T, C, U>
20
- protected _schema?: ZodSchema | undefined
20
+ protected _schema?: ZodObject<ZodRawShape> | undefined
21
21
  protected _defaultOrder?: string | undefined
22
22
 
23
23
  transformCreate?: (data: C) => Promise<C>;
@@ -25,8 +25,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
25
25
  transformRead?: (data: T) => Promise<T>;
26
26
 
27
27
 
28
-
29
- constructor(repository: IDraxCrudRepository<T, C, U>, schema?: ZodSchema) {
28
+ constructor(repository: IDraxCrudRepository<T, C, U>, schema?: ZodObject<ZodRawShape>) {
30
29
  this._repository = repository
31
30
  this._schema = schema
32
31
  }
@@ -37,7 +36,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
37
36
  if (this._schema) {
38
37
  await this._schema.parseAsync(data)
39
38
  }
40
- if(this.transformCreate){
39
+ if (this.transformCreate) {
41
40
  data = await this.transformCreate(data)
42
41
  }
43
42
  const item: T = await this._repository.create(data)
@@ -56,7 +55,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
56
55
  if (this._schema) {
57
56
  await this._schema.parseAsync(data)
58
57
  }
59
- if(this.transformUpdate){
58
+ if (this.transformUpdate) {
60
59
  data = await this.transformUpdate(data)
61
60
  }
62
61
  const item: T = await this._repository.update(id, data)
@@ -73,6 +72,10 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
73
72
  async updatePartial(id: string, data: any): Promise<T> {
74
73
  try {
75
74
 
75
+ if (this._schema) {
76
+ await this._schema.partial().parseAsync(data)
77
+ }
78
+
76
79
  const item: T = await this._repository.updatePartial(id, data)
77
80
 
78
81
  return item
@@ -88,7 +91,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
88
91
  async delete(id: string): Promise<boolean> {
89
92
  try {
90
93
  const result: boolean = await this._repository.delete(id);
91
- if(!result){
94
+ if (!result) {
92
95
  throw new Error("error.deletionFailed");
93
96
  }
94
97
  return result;
@@ -102,7 +105,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
102
105
  async findById(id: string): Promise<T | null> {
103
106
  try {
104
107
  let item: T = await this._repository.findById(id);
105
- if(this.transformRead){
108
+ if (this.transformRead) {
106
109
  item = await this.transformRead(item)
107
110
  }
108
111
  return item
@@ -115,8 +118,8 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
115
118
  async findByIds(ids: Array<string>): Promise<T[]> {
116
119
  try {
117
120
  let items: T[] = await this._repository.findByIds(ids);
118
- if(this.transformRead){
119
- items = await Promise.all(items.map( item => this.transformRead(item)))
121
+ if (this.transformRead) {
122
+ items = await Promise.all(items.map(item => this.transformRead(item)))
120
123
  }
121
124
  return items
122
125
  } catch (e) {
@@ -128,7 +131,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
128
131
  async findOneBy(field: string, value: string): Promise<T | null> {
129
132
  try {
130
133
  let item: T = await this._repository.findOneBy(field, value);
131
- if(this.transformRead){
134
+ if (this.transformRead) {
132
135
  item = await this.transformRead(item)
133
136
  }
134
137
  return item
@@ -139,11 +142,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
139
142
 
140
143
  }
141
144
 
142
- async findBy(field: string, value: string): Promise<T[] | null> {
145
+ async findBy(field: string, value: string, limit: number = 1000): Promise<T[] | null> {
143
146
  try {
144
- let items: T[] = await this._repository.findBy(field, value);
145
- if(this.transformRead){
146
- items = await Promise.all(items.map( item => this.transformRead(item)))
147
+
148
+ let items: T[] = await this._repository.findBy(field, value, limit);
149
+ if (this.transformRead) {
150
+ items = await Promise.all(items.map(item => this.transformRead(item)))
147
151
  }
148
152
  return items
149
153
  } catch (e) {
@@ -156,8 +160,8 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
156
160
  async fetchAll(): Promise<T[]> {
157
161
  try {
158
162
  let items: T[] = await this._repository.fetchAll();
159
- if(this.transformRead){
160
- items = await Promise.all(items.map( item => this.transformRead(item)))
163
+ if (this.transformRead) {
164
+ items = await Promise.all(items.map(item => this.transformRead(item)))
161
165
  }
162
166
  return items
163
167
  } catch (e) {
@@ -169,9 +173,10 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
169
173
 
170
174
  async search(value: string, limit: number = 1000, filters: IDraxFieldFilter[] = []): Promise<T[]> {
171
175
  try {
176
+
172
177
  let items: T[] = await this._repository.search(value, limit, filters);
173
- if(this.transformRead){
174
- items = await Promise.all(items.map( item => this.transformRead(item)))
178
+ if (this.transformRead) {
179
+ items = await Promise.all(items.map(item => this.transformRead(item)))
175
180
  }
176
181
  return items
177
182
  } catch (e) {
@@ -183,17 +188,19 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
183
188
 
184
189
  async paginate({
185
190
  page = 1,
186
- limit = 5,
191
+ limit = 10,
187
192
  orderBy = this._defaultOrder,
188
- order = false,
193
+ order = "asc",
189
194
  search = '',
190
195
  filters = []
191
196
  }: IDraxPaginateOptions): Promise<IDraxPaginateResult<T>> {
192
197
  try {
198
+
199
+
193
200
  const pagination = await this._repository.paginate({page, limit, orderBy, order, search, filters});
194
201
 
195
- if(this.transformRead){
196
- pagination.items = await Promise.all(pagination.items.map( item => this.transformRead(item)))
202
+ if (this.transformRead) {
203
+ pagination.items = await Promise.all(pagination.items.map(item => this.transformRead(item)))
197
204
  }
198
205
 
199
206
  return pagination;
@@ -208,10 +215,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
208
215
  orderBy = '',
209
216
  order = false,
210
217
  search = '',
211
- filters = []
218
+ filters = [],
219
+ limit = 0,
212
220
  }: IDraxFindOptions): Promise<T[]> {
213
221
  try {
214
- let items = await this._repository.find({orderBy, order, search, filters});
222
+
223
+ let items = await this._repository.find({orderBy, order, search, filters, limit});
215
224
  return items;
216
225
  } catch (e) {
217
226
  console.error("Error find", e)
@@ -221,11 +230,11 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
221
230
  }
222
231
 
223
232
  async findOne({
224
- search = '',
225
- filters = []
226
- }: IDraxFindOneOptions): Promise<T> {
233
+ search = '',
234
+ filters = []
235
+ }: IDraxFindOneOptions): Promise<T> {
227
236
  try {
228
- let item = await this._repository.findOne({ search, filters});
237
+ let item = await this._repository.findOne({search, filters});
229
238
  return item;
230
239
  } catch (e) {
231
240
  console.error("Error findOne", e)
@@ -246,12 +255,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
246
255
  destinationPath: string): Promise<IDraxExportResult> {
247
256
  try {
248
257
 
249
- let cursor:any
250
- let exporter:any
258
+ let cursor: any
259
+ let exporter: any
251
260
 
252
- if(this._repository.findCursor){
261
+ if (this._repository.findCursor) {
253
262
  cursor = await this._repository.findCursor({orderBy, order, search, filters});
254
- }else{
263
+ } else {
255
264
  cursor = await this._repository.find({orderBy, order, search, filters});
256
265
  }
257
266