@dnax/core 0.8.14 → 0.8.15

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 (2) hide show
  1. package/driver/mongo/rest.ts +213 -210
  2. package/package.json +1 -1
@@ -158,9 +158,12 @@ class useRest {
158
158
  let result = {
159
159
  docs: [],
160
160
  };
161
+
161
162
  let useHook = options?.useHook ?? this.#useHook;
162
163
  let sharedData = {};
163
164
  let col = getCollection(collection, this.#tenant_id);
165
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
166
+
164
167
  if (col?.hooks?.beforeOperation && useHook) {
165
168
  await col.hooks.beforeOperation({
166
169
  io: Cfg.io,
@@ -233,36 +236,7 @@ class useRest {
233
236
  let col = getCollection(collection, this.#tenant_id);
234
237
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
235
238
 
236
- if (col?.customApi?.insertOne && useCustomApi) {
237
- let result = await col?.customApi?.insertOne({
238
- io: Cfg.io,
239
- session: sessionStorage(),
240
- data: data,
241
- rest: new useRest({
242
- useHook: false,
243
- tenant_id: this.#tenant_id,
244
- useCustomApi: false,
245
- }),
246
- });
247
- return resolve(result);
248
- }
249
-
250
- // Processing before validation
251
- data = await setUUID(data, col);
252
- data = await randomCode(data, col);
253
- data = await hashPasswordAuto(data, col);
254
- data = transformAllDate(data);
255
- data = deepSetId(col, data);
256
-
257
- var { valid, output, error } = this.validator(collection, data);
258
- if (!valid) fn.error(error, 400);
259
- data = {
260
- ...data,
261
- ...(output || {}),
262
- };
263
-
264
- //console.log(data);
265
-
239
+ // before operation
266
240
  if (col?.hooks?.beforeOperation && useHook) {
267
241
  await col.hooks.beforeOperation({
268
242
  sharedData: sharedData,
@@ -279,6 +253,7 @@ class useRest {
279
253
  });
280
254
  }
281
255
 
256
+ //before Insert
282
257
  if (col?.hooks?.beforeInsert && useHook) {
283
258
  await col.hooks.beforeInsert({
284
259
  sharedData: sharedData,
@@ -295,6 +270,36 @@ class useRest {
295
270
  });
296
271
  }
297
272
 
273
+ if (col?.customApi?.insertOne && useCustomApi) {
274
+ let result = await col?.customApi?.insertOne({
275
+ io: Cfg.io,
276
+ session: sessionStorage(),
277
+ data: data,
278
+ rest: new useRest({
279
+ useHook: false,
280
+ tenant_id: this.#tenant_id,
281
+ useCustomApi: false,
282
+ }),
283
+ });
284
+ return resolve(result);
285
+ }
286
+
287
+ // Processing before validation
288
+ data = await setUUID(data, col);
289
+ data = await randomCode(data, col);
290
+ data = await hashPasswordAuto(data, col);
291
+ data = transformAllDate(data);
292
+ data = deepSetId(col, data);
293
+
294
+ var { valid, output, error } = this.validator(collection, data);
295
+ if (!valid) fn.error(error, 400);
296
+ data = {
297
+ ...data,
298
+ ...(output || {}),
299
+ };
300
+
301
+ //console.log(data);
302
+
298
303
  await this.#tenant.database.db?.collection(collection).insertOne(
299
304
  formatData(data, {
300
305
  action: "insertOne",
@@ -341,34 +346,6 @@ class useRest {
341
346
  let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
342
347
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
343
348
 
344
- if (col?.customApi?.insertMany && useCustomApi) {
345
- let result = await col?.customApi?.insertMany({
346
- io: Cfg.io,
347
- session: sessionStorage(),
348
- data: data,
349
- rest: new useRest({
350
- useHook: false,
351
- tenant_id: this.#tenant_id,
352
- useCustomApi: false,
353
- }),
354
- });
355
- return resolve(result);
356
- }
357
-
358
- for await (let d of data) {
359
- // Processing before validation
360
- d = await setUUID(d, col);
361
- d = await randomCode(d, col);
362
- d = await hashPasswordAuto(d, col);
363
- d = deepSetId(col, d);
364
- var { valid, output, error } = this.validator(collection, d);
365
- if (!valid) fn.error(error, 400);
366
- d = {
367
- ...d,
368
- ...(output || {}),
369
- };
370
- }
371
-
372
349
  if (col?.hooks?.beforeOperation && useHook) {
373
350
  await col.hooks.beforeOperation({
374
351
  sharedData: sharedData,
@@ -385,6 +362,7 @@ class useRest {
385
362
  });
386
363
  }
387
364
 
365
+ //beforeInsert
388
366
  if (col?.hooks?.beforeInsert && useHook) {
389
367
  await col.hooks.beforeInsert({
390
368
  sharedData: sharedData,
@@ -401,6 +379,34 @@ class useRest {
401
379
  });
402
380
  }
403
381
 
382
+ if (col?.customApi?.insertMany && useCustomApi) {
383
+ let result = await col?.customApi?.insertMany({
384
+ io: Cfg.io,
385
+ session: sessionStorage(),
386
+ data: data,
387
+ rest: new useRest({
388
+ useHook: false,
389
+ tenant_id: this.#tenant_id,
390
+ useCustomApi: false,
391
+ }),
392
+ });
393
+ return resolve(result);
394
+ }
395
+
396
+ for await (let d of data) {
397
+ // Processing before validation
398
+ d = await setUUID(d, col);
399
+ d = await randomCode(d, col);
400
+ d = await hashPasswordAuto(d, col);
401
+ d = deepSetId(col, d);
402
+ var { valid, output, error } = this.validator(collection, d);
403
+ if (!valid) fn.error(error, 400);
404
+ d = {
405
+ ...d,
406
+ ...(output || {}),
407
+ };
408
+ }
409
+
404
410
  await this.#tenant.database.db?.collection(collection).insertMany(
405
411
  formatData(data, {
406
412
  collection: collection,
@@ -496,21 +502,6 @@ class useRest {
496
502
  };
497
503
 
498
504
  let col = getCollection(collection, this.#tenant_id);
499
-
500
- if (col?.customApi?.find && useCustomApi) {
501
- let result = await col?.customApi?.find({
502
- io: Cfg.io,
503
- session: sessionStorage(),
504
- params: params,
505
- rest: new useRest({
506
- useHook: false,
507
- tenant_id: this.#tenant_id,
508
- useCustomApi: false,
509
- }),
510
- });
511
- return resolve(result);
512
- }
513
-
514
505
  if (col?.hooks?.beforeOperation && useHook) {
515
506
  await col.hooks.beforeOperation({
516
507
  sharedData: sharedData,
@@ -543,6 +534,20 @@ class useRest {
543
534
  });
544
535
  }
545
536
 
537
+ if (col?.customApi?.find && useCustomApi) {
538
+ let result = (await col?.customApi?.find({
539
+ io: Cfg.io,
540
+ session: sessionStorage(),
541
+ params: params,
542
+ rest: new useRest({
543
+ useHook: false,
544
+ tenant_id: this.#tenant_id,
545
+ useCustomApi: false,
546
+ }),
547
+ })) as any;
548
+ return resolve(result);
549
+ }
550
+
546
551
  result.docs = await this.#tenant.database.db
547
552
  ?.collection(collection)
548
553
  .aggregate(formatData(buildPipeline(params, col)), {
@@ -591,21 +596,7 @@ class useRest {
591
596
  let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
592
597
 
593
598
  let col = getCollection(collection, this.#tenant_id);
594
-
595
- if (col?.customApi?.findOne && useCustomApi) {
596
- let result = await col?.customApi?.findOne({
597
- io: Cfg.io,
598
- session: sessionStorage(),
599
- id: id,
600
- params: params,
601
- rest: new useRest({
602
- useHook: false,
603
- tenant_id: this.#tenant_id,
604
- useCustomApi: false,
605
- }),
606
- });
607
- return resolve(result);
608
- }
599
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
609
600
 
610
601
  if (col?.hooks?.beforeOperation && useHook) {
611
602
  await col.hooks.beforeOperation({
@@ -640,6 +631,21 @@ class useRest {
640
631
  });
641
632
  }
642
633
 
634
+ if (col?.customApi?.findOne && useCustomApi) {
635
+ let result = await col?.customApi?.findOne({
636
+ io: Cfg.io,
637
+ session: sessionStorage(),
638
+ id: id,
639
+ params: params,
640
+ rest: new useRest({
641
+ useHook: false,
642
+ tenant_id: this.#tenant_id,
643
+ useCustomApi: false,
644
+ }),
645
+ });
646
+ return resolve(result);
647
+ }
648
+
643
649
  let params_: findParam = {};
644
650
  if (typeof id === "string") {
645
651
  params_ = {
@@ -704,6 +710,40 @@ class useRest {
704
710
  let col = getCollection(collection, this.#tenant_id);
705
711
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
706
712
 
713
+ if (col?.hooks?.beforeOperation && useHook) {
714
+ await col.hooks.beforeOperation({
715
+ sharedData: sharedData,
716
+ id: id,
717
+ io: Cfg.io,
718
+ c: this.#c,
719
+ driver: "mongodb",
720
+ action: "updateOne",
721
+ update: update,
722
+ session: sessionStorage(),
723
+ rest: new useRest({
724
+ useHook: false,
725
+ tenant_id: this.#tenant_id,
726
+ }),
727
+ });
728
+ }
729
+
730
+ if (col?.hooks?.beforeUpdate && useHook) {
731
+ await col.hooks.beforeUpdate({
732
+ sharedData: sharedData,
733
+ c: this.#c,
734
+ id: id,
735
+ driver: "mongodb",
736
+ io: Cfg.io,
737
+ action: "updateOne",
738
+ update: update,
739
+ session: sessionStorage(),
740
+ rest: new useRest({
741
+ useHook: false,
742
+ tenant_id: this.#tenant_id,
743
+ }),
744
+ });
745
+ }
746
+
707
747
  if (col?.customApi?.updateOne && useCustomApi) {
708
748
  let result = await col?.customApi?.updateOne({
709
749
  io: Cfg.io,
@@ -746,40 +786,6 @@ class useRest {
746
786
  if (!valid) fn?.error(error, 400);
747
787
  }
748
788
 
749
- if (col?.hooks?.beforeOperation && useHook) {
750
- await col.hooks.beforeOperation({
751
- sharedData: sharedData,
752
- id: id,
753
- io: Cfg.io,
754
- c: this.#c,
755
- driver: "mongodb",
756
- action: "updateOne",
757
- update: update,
758
- session: sessionStorage(),
759
- rest: new useRest({
760
- useHook: false,
761
- tenant_id: this.#tenant_id,
762
- }),
763
- });
764
- }
765
-
766
- if (col?.hooks?.beforeUpdate && useHook) {
767
- await col.hooks.beforeUpdate({
768
- sharedData: sharedData,
769
- c: this.#c,
770
- id: id,
771
- driver: "mongodb",
772
- io: Cfg.io,
773
- action: "updateOne",
774
- update: update,
775
- session: sessionStorage(),
776
- rest: new useRest({
777
- useHook: false,
778
- tenant_id: this.#tenant_id,
779
- }),
780
- });
781
- }
782
-
783
789
  if (typeof id == "string") {
784
790
  result.doc = await this.#tenant.database.db
785
791
  ?.collection(collection)
@@ -841,26 +847,6 @@ class useRest {
841
847
 
842
848
  let sharedData = {};
843
849
  let useHook = options?.useHook ?? this.#useHook;
844
-
845
- //@ts-expect-error
846
- update = omit(update, omitUpdate);
847
-
848
- if (update.$set) {
849
- // update.$set = deepSetId(col, update.$set);
850
- update.$set = deepSetId(col, update.$set);
851
- update.$set = transformAllDate(update.$set);
852
- update.$set = await hashPasswordAuto(update.$set, col);
853
- var { valid, output, error } = this.validator(
854
- collection,
855
- update.$set,
856
- {
857
- partial: true,
858
- }
859
- );
860
- // update.$set = output;
861
- if (!valid) fn.error(error, 400);
862
- }
863
-
864
850
  if (col?.hooks?.beforeOperation && useHook) {
865
851
  await col.hooks.beforeOperation({
866
852
  sharedData: sharedData,
@@ -895,6 +881,25 @@ class useRest {
895
881
  });
896
882
  }
897
883
 
884
+ //@ts-expect-error
885
+ update = omit(update, omitUpdate);
886
+
887
+ if (update.$set) {
888
+ // update.$set = deepSetId(col, update.$set);
889
+ update.$set = deepSetId(col, update.$set);
890
+ update.$set = transformAllDate(update.$set);
891
+ update.$set = await hashPasswordAuto(update.$set, col);
892
+ var { valid, output, error } = this.validator(
893
+ collection,
894
+ update.$set,
895
+ {
896
+ partial: true,
897
+ }
898
+ );
899
+ // update.$set = output;
900
+ if (!valid) fn.error(error, 400);
901
+ }
902
+
898
903
  result.doc = await this.#tenant.database.db
899
904
  ?.collection(collection)
900
905
  .findOneAndUpdate(
@@ -957,6 +962,40 @@ class useRest {
957
962
  let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
958
963
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
959
964
 
965
+ if (col?.hooks?.beforeOperation && useHook) {
966
+ await col.hooks.beforeOperation({
967
+ sharedData: sharedData,
968
+ ids: ids,
969
+ c: this.#c,
970
+ driver: "mongodb",
971
+ io: Cfg.io,
972
+ action: "updateMany",
973
+ update: update,
974
+ session: sessionStorage(),
975
+ rest: new useRest({
976
+ useHook: false,
977
+ tenant_id: this.#tenant_id,
978
+ }),
979
+ });
980
+ }
981
+
982
+ if (col?.hooks?.beforeUpdate && useHook) {
983
+ await col.hooks.beforeUpdate({
984
+ sharedData: sharedData,
985
+ c: this.#c,
986
+ ids: ids,
987
+ driver: "mongodb",
988
+ io: Cfg.io,
989
+ action: "updateMany",
990
+ update: update,
991
+ session: sessionStorage(),
992
+ rest: new useRest({
993
+ useHook: false,
994
+ tenant_id: this.#tenant_id,
995
+ }),
996
+ });
997
+ }
998
+
960
999
  if (col?.customApi?.updateMany && useCustomApi) {
961
1000
  let result = await col?.customApi?.updateMany({
962
1001
  io: Cfg.io,
@@ -997,40 +1036,6 @@ class useRest {
997
1036
  if (Array.isArray(ids)) {
998
1037
  if (!ids?.length) throw new contextError("List of id required", 400);
999
1038
 
1000
- if (col?.hooks?.beforeOperation && useHook) {
1001
- await col.hooks.beforeOperation({
1002
- sharedData: sharedData,
1003
- ids: ids,
1004
- c: this.#c,
1005
- driver: "mongodb",
1006
- io: Cfg.io,
1007
- action: "updateMany",
1008
- update: update,
1009
- session: sessionStorage(),
1010
- rest: new useRest({
1011
- useHook: false,
1012
- tenant_id: this.#tenant_id,
1013
- }),
1014
- });
1015
- }
1016
-
1017
- if (col?.hooks?.beforeUpdate && useHook) {
1018
- await col.hooks.beforeUpdate({
1019
- sharedData: sharedData,
1020
- c: this.#c,
1021
- ids: ids,
1022
- driver: "mongodb",
1023
- io: Cfg.io,
1024
- action: "updateMany",
1025
- update: update,
1026
- session: sessionStorage(),
1027
- rest: new useRest({
1028
- useHook: false,
1029
- tenant_id: this.#tenant_id,
1030
- }),
1031
- });
1032
- }
1033
-
1034
1039
  await this.#tenant.database.db?.collection(collection).updateMany(
1035
1040
  {
1036
1041
  _id: {
@@ -1102,21 +1107,6 @@ class useRest {
1102
1107
 
1103
1108
  let col = getCollection(collection, this.#tenant_id);
1104
1109
  let sharedData = {};
1105
-
1106
- if (col?.customApi?.deleteOne && useCustomApi) {
1107
- let result = await col?.customApi?.deleteOne({
1108
- io: Cfg.io,
1109
- session: sessionStorage(),
1110
- id: id,
1111
- rest: new useRest({
1112
- useHook: false,
1113
- tenant_id: this.#tenant_id,
1114
- useCustomApi: false,
1115
- }),
1116
- });
1117
- return resolve(result!);
1118
- }
1119
-
1120
1110
  if (col?.hooks?.beforeOperation && useHook) {
1121
1111
  await col.hooks.beforeOperation({
1122
1112
  sharedData: sharedData,
@@ -1149,6 +1139,20 @@ class useRest {
1149
1139
  });
1150
1140
  }
1151
1141
 
1142
+ if (col?.customApi?.deleteOne && useCustomApi) {
1143
+ let result = await col?.customApi?.deleteOne({
1144
+ io: Cfg.io,
1145
+ session: sessionStorage(),
1146
+ id: id,
1147
+ rest: new useRest({
1148
+ useHook: false,
1149
+ tenant_id: this.#tenant_id,
1150
+ useCustomApi: false,
1151
+ }),
1152
+ });
1153
+ return resolve(result!);
1154
+ }
1155
+
1152
1156
  let doc = await this.#tenant.database.db
1153
1157
  ?.collection(collection)
1154
1158
  .findOneAndDelete(
@@ -1192,25 +1196,6 @@ class useRest {
1192
1196
 
1193
1197
  let sharedData = {};
1194
1198
  let col = getCollection(collection, this.#tenant_id);
1195
-
1196
- if (col?.customApi?.deleteMany && useCustomApi) {
1197
- let result = await col?.customApi?.deleteMany({
1198
- io: Cfg.io,
1199
- session: sessionStorage(),
1200
- ids: ids,
1201
- rest: new useRest({
1202
- useHook: false,
1203
- tenant_id: this.#tenant_id,
1204
- useCustomApi: false,
1205
- }),
1206
- });
1207
- return resolve(result!);
1208
- }
1209
-
1210
- let deletedIds: any = ids || [];
1211
-
1212
- if (!ids) fn.error("List of id required", 400);
1213
-
1214
1199
  if (col?.hooks?.beforeOperation && useHook) {
1215
1200
  await col.hooks.beforeOperation({
1216
1201
  sharedData: sharedData,
@@ -1243,6 +1228,24 @@ class useRest {
1243
1228
  });
1244
1229
  }
1245
1230
 
1231
+ if (col?.customApi?.deleteMany && useCustomApi) {
1232
+ let result = await col?.customApi?.deleteMany({
1233
+ io: Cfg.io,
1234
+ session: sessionStorage(),
1235
+ ids: ids,
1236
+ rest: new useRest({
1237
+ useHook: false,
1238
+ tenant_id: this.#tenant_id,
1239
+ useCustomApi: false,
1240
+ }),
1241
+ });
1242
+ return resolve(result!);
1243
+ }
1244
+
1245
+ let deletedIds: any = ids || [];
1246
+
1247
+ if (!ids) fn.error("List of id required", 400);
1248
+
1246
1249
  if (Array.isArray(ids)) {
1247
1250
  await this.#tenant.database.db?.collection(collection).deleteMany(
1248
1251
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.8.14",
3
+ "version": "0.8.15",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {