@drax/crud-back 0.45.0 → 0.47.0

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.
@@ -175,11 +175,15 @@ class AbstractFastifyController extends CommonController {
175
175
  };
176
176
  this.eventEmitter.emitCrudEvent(eventData);
177
177
  }
178
+ async preCreate(request, payload) {
179
+ return payload;
180
+ }
178
181
  async create(request, reply) {
179
182
  try {
180
183
  request.rbac.assertPermission(this.permission.Create);
181
184
  const payload = request.body;
182
185
  this.applyUserAndTenantSetters(payload, request.rbac);
186
+ await this.preCreate(request, payload);
183
187
  let item = await this.service.create(payload);
184
188
  this.onCreated(request, item);
185
189
  return item;
@@ -188,6 +192,9 @@ class AbstractFastifyController extends CommonController {
188
192
  this.handleError(e, reply);
189
193
  }
190
194
  }
195
+ async preUpdate(request, payload) {
196
+ return payload;
197
+ }
191
198
  async update(request, reply) {
192
199
  try {
193
200
  request.rbac.assertPermission(this.permission.Update);
@@ -208,6 +215,7 @@ class AbstractFastifyController extends CommonController {
208
215
  //Definido el tenant/user en el create no debe modificarse en un update
209
216
  delete payload[this.tenantField];
210
217
  delete payload[this.userField];
218
+ await this.preUpdate(request, payload);
211
219
  let item = await this.service.update(id, payload);
212
220
  if (!item) {
213
221
  throw new NotFoundError();
@@ -219,6 +227,9 @@ class AbstractFastifyController extends CommonController {
219
227
  this.handleError(e, reply);
220
228
  }
221
229
  }
230
+ async preUpdatePartial(request, payload) {
231
+ return payload;
232
+ }
222
233
  async updatePartial(request, reply) {
223
234
  try {
224
235
  request.rbac.assertPermission(this.permission.Update);
@@ -239,6 +250,7 @@ class AbstractFastifyController extends CommonController {
239
250
  //Definido el tenant/user en el create no debe modificarse en un update
240
251
  delete payload[this.tenantField];
241
252
  delete payload[this.userField];
253
+ await this.preUpdatePartial(request, payload);
242
254
  let item = await this.service.updatePartial(id, payload);
243
255
  if (!item) {
244
256
  throw new NotFoundError();
@@ -250,6 +262,9 @@ class AbstractFastifyController extends CommonController {
250
262
  this.handleError(e, reply);
251
263
  }
252
264
  }
265
+ async preDelete(request, item) {
266
+ return item;
267
+ }
253
268
  async delete(request, reply) {
254
269
  try {
255
270
  request.rbac.assertPermission(this.permission.Delete);
@@ -267,6 +282,7 @@ class AbstractFastifyController extends CommonController {
267
282
  this.assertUser(item, request.rbac);
268
283
  }
269
284
  this.assertTenant(item, request.rbac);
285
+ await this.preDelete(request, item);
270
286
  await this.service.delete(id);
271
287
  this.onDeleted(request, item);
272
288
  reply.send({
@@ -483,10 +499,6 @@ class AbstractFastifyController extends CommonController {
483
499
  const filters = this.parseFilters(request.query.filters);
484
500
  this.applyUserAndTenantFilters(filters, request.rbac);
485
501
  const result = await this.service.groupBy({ fields, filters, dateFormat });
486
- // console.log("groupby fields",fields)
487
- // console.log("groupby dateFormat",dateFormat)
488
- // console.log("groupby filters",filters)
489
- // console.log("groupby result",result)
490
502
  return result;
491
503
  }
492
504
  catch (e) {
@@ -1,16 +1,16 @@
1
1
  import z from "zod";
2
2
  const ErrorBodyResponseSchema = z.object({
3
- statusCode: z.string(),
4
- error: z.string(),
5
- message: z.string(),
6
- i18nMessage: z.string()
7
- });
3
+ statusCode: z.string().optional(),
4
+ error: z.string().optional(),
5
+ message: z.string().optional(),
6
+ i18nMessage: z.string().optional()
7
+ }).passthrough();
8
8
  const ValidationErrorBodyResponseSchema = ErrorBodyResponseSchema.extend({
9
9
  inputErrors: z.array(z.object({
10
10
  field: z.string(),
11
11
  reason: z.string(),
12
12
  value: z.any().optional(),
13
13
  })).optional()
14
- });
14
+ }).passthrough();
15
15
  export default ErrorBodyResponseSchema;
16
16
  export { ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.45.0",
6
+ "version": "0.47.0",
7
7
  "description": "Crud utils across modules",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -22,10 +22,10 @@
22
22
  "author": "Cristian Incarnato & Drax Team",
23
23
  "license": "ISC",
24
24
  "dependencies": {
25
- "@drax/common-back": "^0.45.0",
26
- "@drax/common-share": "^0.45.0",
27
- "@drax/identity-share": "^0.45.0",
28
- "@drax/media-back": "^0.45.0",
25
+ "@drax/common-back": "^0.47.0",
26
+ "@drax/common-share": "^0.47.0",
27
+ "@drax/identity-share": "^0.47.0",
28
+ "@drax/media-back": "^0.47.0",
29
29
  "@graphql-tools/load-files": "^7.0.0",
30
30
  "@graphql-tools/merge": "^9.0.4",
31
31
  "mongoose": "^8.6.3",
@@ -45,5 +45,5 @@
45
45
  "tsc-alias": "^1.8.10",
46
46
  "typescript": "^5.6.2"
47
47
  },
48
- "gitHead": "b05b1b2c982bad40a6d93496266c652f77749279"
48
+ "gitHead": "65a17b06254404866deb9fb81daf42fbe3d2db3c"
49
49
  }
@@ -266,12 +266,16 @@ class AbstractFastifyController<T, C, U> extends CommonController {
266
266
  this.eventEmitter.emitCrudEvent(eventData)
267
267
  }
268
268
 
269
+ async preCreate(request: CustomRequest, payload:any){
270
+ return payload
271
+ }
269
272
 
270
273
  async create(request: CustomRequest, reply: FastifyReply) {
271
274
  try {
272
275
  request.rbac.assertPermission(this.permission.Create)
273
276
  const payload = request.body
274
277
  this.applyUserAndTenantSetters(payload, request.rbac)
278
+ await this.preCreate(request, payload as C)
275
279
  let item = await this.service.create(payload as C)
276
280
  this.onCreated(request, item)
277
281
  return item
@@ -280,7 +284,9 @@ class AbstractFastifyController<T, C, U> extends CommonController {
280
284
  }
281
285
  }
282
286
 
283
-
287
+ async preUpdate(request: CustomRequest, payload:any):Promise<C>{
288
+ return payload
289
+ }
284
290
 
285
291
  async update(request: CustomRequest, reply: FastifyReply) {
286
292
  try {
@@ -308,6 +314,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
308
314
  delete payload[this.tenantField]
309
315
  delete payload[this.userField]
310
316
 
317
+ await this.preUpdate(request, payload)
311
318
  let item = await this.service.update(id, payload as U)
312
319
 
313
320
  if (!item) {
@@ -322,6 +329,10 @@ class AbstractFastifyController<T, C, U> extends CommonController {
322
329
  }
323
330
  }
324
331
 
332
+ async preUpdatePartial(request: CustomRequest, payload:any):Promise<C>{
333
+ return payload
334
+ }
335
+
325
336
  async updatePartial(request: CustomRequest, reply: FastifyReply) {
326
337
  try {
327
338
  request.rbac.assertPermission(this.permission.Update)
@@ -348,6 +359,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
348
359
  delete payload[this.tenantField]
349
360
  delete payload[this.userField]
350
361
 
362
+ await this.preUpdatePartial(request, payload)
351
363
  let item = await this.service.updatePartial(id, payload as U)
352
364
  if (!item) {
353
365
  throw new NotFoundError()
@@ -359,6 +371,9 @@ class AbstractFastifyController<T, C, U> extends CommonController {
359
371
  }
360
372
  }
361
373
 
374
+ async preDelete(request: CustomRequest, item:T){
375
+ return item
376
+ }
362
377
 
363
378
 
364
379
  async delete(request: CustomRequest, reply: FastifyReply) {
@@ -384,7 +399,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
384
399
 
385
400
  this.assertTenant(item, request.rbac)
386
401
 
387
-
402
+ await this.preDelete(request, item)
388
403
  await this.service.delete(id)
389
404
 
390
405
  this.onDeleted(request, item)
@@ -652,10 +667,7 @@ class AbstractFastifyController<T, C, U> extends CommonController {
652
667
 
653
668
 
654
669
  const result = await this.service.groupBy({fields, filters, dateFormat})
655
- // console.log("groupby fields",fields)
656
- // console.log("groupby dateFormat",dateFormat)
657
- // console.log("groupby filters",filters)
658
- // console.log("groupby result",result)
670
+
659
671
  return result
660
672
  } catch (e) {
661
673
  this.handleError(e, reply)
@@ -1,11 +1,11 @@
1
1
  import z from "zod"
2
2
 
3
3
  const ErrorBodyResponseSchema = z.object({
4
- statusCode: z.string(),
5
- error: z.string(),
6
- message: z.string(),
7
- i18nMessage: z.string()
8
- });
4
+ statusCode: z.string().optional(),
5
+ error: z.string().optional(),
6
+ message: z.string().optional(),
7
+ i18nMessage: z.string().optional()
8
+ }).passthrough();
9
9
 
10
10
  const ValidationErrorBodyResponseSchema = ErrorBodyResponseSchema.extend({
11
11
  inputErrors: z.array(z.object({
@@ -13,7 +13,7 @@ const ValidationErrorBodyResponseSchema = ErrorBodyResponseSchema.extend({
13
13
  reason: z.string(),
14
14
  value: z.any().optional(),
15
15
  })).optional()
16
- });
16
+ }).passthrough();
17
17
 
18
18
  export default ErrorBodyResponseSchema
19
19
 
@@ -144,7 +144,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
144
144
  }
145
145
  }
146
146
 
147
- async findOneBy(field: string, value: string): Promise<T | null> {
147
+ async findOneBy(field: string, value: any): Promise<T | null> {
148
148
  try {
149
149
  let item: T = await this._repository.findOneBy(field, value);
150
150
  if (item && this.transformRead) {
@@ -158,7 +158,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
158
158
 
159
159
  }
160
160
 
161
- async findBy(field: string, value: string, limit: number = 1000): Promise<T[] | null> {
161
+ async findBy(field: string, value: any, limit: number = 1000): Promise<T[] | null> {
162
162
  try {
163
163
 
164
164
  let items: T[] = await this._repository.findBy(field, value, limit);