@dnax/core 0.76.10 → 0.76.13
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.
- package/driver/mongo/rest.ts +171 -154
- package/package.json +1 -1
package/driver/mongo/rest.ts
CHANGED
|
@@ -2,7 +2,7 @@ import moment from "moment";
|
|
|
2
2
|
import { sessionStorage } from "../../lib/asyncLocalStorage";
|
|
3
3
|
import { getCollection, getKeyFields } from "../../lib/collection";
|
|
4
4
|
import { getTenant } from "../../lib/tenant";
|
|
5
|
-
import type { Actions, Collection, Tenant } from "./../../types/index";
|
|
5
|
+
import type { Actions, Collection, Q, Tenant } from "./../../types/index";
|
|
6
6
|
import { isArray } from "radash";
|
|
7
7
|
import { omit } from "../../utils";
|
|
8
8
|
import {
|
|
@@ -124,6 +124,7 @@ class useRest {
|
|
|
124
124
|
#tenant_id: string;
|
|
125
125
|
#session: ClientSession | null | undefined;
|
|
126
126
|
#useCustomApi: boolean = true;
|
|
127
|
+
#events: Array<any> = [];
|
|
127
128
|
/**
|
|
128
129
|
Direct Api Mongo , use it with caution
|
|
129
130
|
*/
|
|
@@ -218,7 +219,51 @@ class useRest {
|
|
|
218
219
|
} */
|
|
219
220
|
}
|
|
220
221
|
|
|
221
|
-
|
|
222
|
+
// onEvents
|
|
223
|
+
on(
|
|
224
|
+
event: "change",
|
|
225
|
+
filter: {},
|
|
226
|
+
clb: (ctx: {
|
|
227
|
+
action:
|
|
228
|
+
| "insertOne"
|
|
229
|
+
| "insertMany"
|
|
230
|
+
| "updateOne"
|
|
231
|
+
| "updateMany"
|
|
232
|
+
| "deleteOne"
|
|
233
|
+
| "deleteMany"
|
|
234
|
+
| "findOneAndDelete"
|
|
235
|
+
| "findAndUpdate"
|
|
236
|
+
| "findAndDelete"
|
|
237
|
+
| "findOneAndUpdate";
|
|
238
|
+
collection: string;
|
|
239
|
+
data?: any;
|
|
240
|
+
result?: any;
|
|
241
|
+
}) => void
|
|
242
|
+
) {
|
|
243
|
+
this.#events.push({
|
|
244
|
+
event: "change",
|
|
245
|
+
filter: filter || {},
|
|
246
|
+
clb: clb,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private disptachEvent(
|
|
251
|
+
event: "change",
|
|
252
|
+
opts: { action: Actions; collection: string; data?: any; result?: any }
|
|
253
|
+
) {
|
|
254
|
+
this.#events?.forEach((e) => {
|
|
255
|
+
if (e.event == event) {
|
|
256
|
+
e.clb({
|
|
257
|
+
action: opts.action,
|
|
258
|
+
collection: opts.collection,
|
|
259
|
+
data: opts?.data,
|
|
260
|
+
result: opts?.result,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private validator(
|
|
222
267
|
collection: string,
|
|
223
268
|
data: any,
|
|
224
269
|
options: { partial: boolean } = {
|
|
@@ -330,6 +375,7 @@ class useRest {
|
|
|
330
375
|
sharedData: sharedData,
|
|
331
376
|
pipeline: pipeline,
|
|
332
377
|
c: this.#c,
|
|
378
|
+
error: fn.error,
|
|
333
379
|
driver: "mongodb",
|
|
334
380
|
action: "aggregate",
|
|
335
381
|
session: sessionStorage(),
|
|
@@ -367,6 +413,7 @@ class useRest {
|
|
|
367
413
|
io: Cfg.io,
|
|
368
414
|
sharedData: sharedData,
|
|
369
415
|
docs: result.docs,
|
|
416
|
+
error: fn.error,
|
|
370
417
|
c: this.#c,
|
|
371
418
|
driver: "mongodb",
|
|
372
419
|
action: "aggregate",
|
|
@@ -384,6 +431,7 @@ class useRest {
|
|
|
384
431
|
sharedData: sharedData,
|
|
385
432
|
docs: result.docs,
|
|
386
433
|
c: this.#c,
|
|
434
|
+
error: fn.error,
|
|
387
435
|
driver: "mongodb",
|
|
388
436
|
action: "aggregate",
|
|
389
437
|
session: sessionStorage(),
|
|
@@ -480,6 +528,7 @@ class useRest {
|
|
|
480
528
|
data: data,
|
|
481
529
|
io: Cfg.io,
|
|
482
530
|
c: this.#c,
|
|
531
|
+
error: fn.error,
|
|
483
532
|
driver: "mongodb",
|
|
484
533
|
action: "insertOne",
|
|
485
534
|
session: sessionStorage(),
|
|
@@ -545,6 +594,7 @@ class useRest {
|
|
|
545
594
|
sharedData: sharedData,
|
|
546
595
|
data: toJson(data),
|
|
547
596
|
c: this.#c,
|
|
597
|
+
error: fn.error,
|
|
548
598
|
io: Cfg.io,
|
|
549
599
|
driver: "mongodb",
|
|
550
600
|
result: toJson(data),
|
|
@@ -566,6 +616,7 @@ class useRest {
|
|
|
566
616
|
c: this.#c,
|
|
567
617
|
io: Cfg.io,
|
|
568
618
|
driver: "mongodb",
|
|
619
|
+
error: fn.error,
|
|
569
620
|
result: toJson(data),
|
|
570
621
|
action: "insertOne",
|
|
571
622
|
session: sessionStorage(),
|
|
@@ -578,6 +629,13 @@ class useRest {
|
|
|
578
629
|
});
|
|
579
630
|
}
|
|
580
631
|
|
|
632
|
+
this.disptachEvent("change", {
|
|
633
|
+
action: "insertOne",
|
|
634
|
+
collection: collection,
|
|
635
|
+
data: data,
|
|
636
|
+
result: toJson(data),
|
|
637
|
+
});
|
|
638
|
+
|
|
581
639
|
// if action done
|
|
582
640
|
restActivity.save(this.#tenant, {
|
|
583
641
|
operation: {
|
|
@@ -601,23 +659,6 @@ class useRest {
|
|
|
601
659
|
ip: sessionStorage()?.get()?._v?.ip,
|
|
602
660
|
state: sessionStorage()?.get()?.state,
|
|
603
661
|
uuid: sessionStorage()?.get()?.uuid,
|
|
604
|
-
/* collection: collection,
|
|
605
|
-
data: {
|
|
606
|
-
data: data,
|
|
607
|
-
_id: data?._id,
|
|
608
|
-
action: "insertOne",
|
|
609
|
-
collection: collection,
|
|
610
|
-
payload: data,
|
|
611
|
-
},
|
|
612
|
-
transaction: this.#session ? true : false,
|
|
613
|
-
action: "insertOne",
|
|
614
|
-
internal: sessionStorage()?.get() ? false : true,
|
|
615
|
-
ip: sessionStorage()?.get()?._v?.ip,
|
|
616
|
-
auth: sessionStorage()?.get()?._v?.isAuth ?? false,
|
|
617
|
-
role: sessionStorage()?.get()?.role!,
|
|
618
|
-
user: sessionStorage()?.get()?.state?.user,
|
|
619
|
-
state: sessionStorage()?.get()?.state,
|
|
620
|
-
uuid: sessionStorage()?.get()?.uuid, */
|
|
621
662
|
} as activityInput);
|
|
622
663
|
if (col?.cache?.invalidate) {
|
|
623
664
|
col.cache.invalidate({
|
|
@@ -633,6 +674,7 @@ class useRest {
|
|
|
633
674
|
tags: [String(data?._id)],
|
|
634
675
|
});
|
|
635
676
|
}
|
|
677
|
+
|
|
636
678
|
return resolve(toJson(data));
|
|
637
679
|
} catch (err) {
|
|
638
680
|
return reject(err);
|
|
@@ -682,6 +724,7 @@ class useRest {
|
|
|
682
724
|
data: data,
|
|
683
725
|
io: Cfg.io,
|
|
684
726
|
c: this.#c,
|
|
727
|
+
error: fn.error,
|
|
685
728
|
driver: "mongodb",
|
|
686
729
|
action: "insertMany",
|
|
687
730
|
session: sessionStorage(),
|
|
@@ -746,6 +789,7 @@ class useRest {
|
|
|
746
789
|
sharedData: sharedData,
|
|
747
790
|
data: toJson(data),
|
|
748
791
|
c: this.#c,
|
|
792
|
+
error: fn.error,
|
|
749
793
|
io: Cfg.io,
|
|
750
794
|
driver: "mongodb",
|
|
751
795
|
action: "insertMany",
|
|
@@ -768,6 +812,7 @@ class useRest {
|
|
|
768
812
|
io: Cfg.io,
|
|
769
813
|
driver: "mongodb",
|
|
770
814
|
action: "insertMany",
|
|
815
|
+
error: fn.error,
|
|
771
816
|
result: toJson(data),
|
|
772
817
|
session: sessionStorage(),
|
|
773
818
|
rest: new useRest({
|
|
@@ -779,6 +824,14 @@ class useRest {
|
|
|
779
824
|
});
|
|
780
825
|
}
|
|
781
826
|
|
|
827
|
+
// dispatchEvent
|
|
828
|
+
this.disptachEvent("change", {
|
|
829
|
+
action: "insertMany",
|
|
830
|
+
collection: collection,
|
|
831
|
+
data: data,
|
|
832
|
+
result: toJson(data),
|
|
833
|
+
});
|
|
834
|
+
|
|
782
835
|
// if insertMany done
|
|
783
836
|
restActivity.save(this.#tenant, {
|
|
784
837
|
uuid: sessionStorage()?.get()?.uuid,
|
|
@@ -841,44 +894,6 @@ class useRest {
|
|
|
841
894
|
});
|
|
842
895
|
}
|
|
843
896
|
|
|
844
|
-
//on(event: restEventType, clb: Function) {}
|
|
845
|
-
/**
|
|
846
|
-
*
|
|
847
|
-
* @param queries
|
|
848
|
-
* @returns [{ [key: string]: any }]
|
|
849
|
-
*/
|
|
850
|
-
async batch<T extends "findOne" | "find">(queries: QueryBatchType<T>[]) {
|
|
851
|
-
return new Promise(async (resolve, reject) => {
|
|
852
|
-
try {
|
|
853
|
-
let result = {} as any;
|
|
854
|
-
for await (let query of queries) {
|
|
855
|
-
let resultName = query.as || query.collection;
|
|
856
|
-
if (
|
|
857
|
-
query.action == "findOne" &&
|
|
858
|
-
(query?.filter?.id || query?.filter?._id)
|
|
859
|
-
) {
|
|
860
|
-
result[resultName] = await this.findOne(
|
|
861
|
-
query.collection,
|
|
862
|
-
query?.filter?.id || query?.filter?._id,
|
|
863
|
-
query?.filter?.params || {},
|
|
864
|
-
query.options
|
|
865
|
-
);
|
|
866
|
-
}
|
|
867
|
-
if (query.action == "find") {
|
|
868
|
-
result[resultName] = await this.find(
|
|
869
|
-
query.collection,
|
|
870
|
-
query?.filter?.params || {},
|
|
871
|
-
query?.options || {}
|
|
872
|
-
);
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
return resolve(result);
|
|
876
|
-
} catch (err) {
|
|
877
|
-
return reject(err);
|
|
878
|
-
}
|
|
879
|
-
});
|
|
880
|
-
}
|
|
881
|
-
|
|
882
897
|
/**
|
|
883
898
|
*
|
|
884
899
|
* @param collection - Collection name
|
|
@@ -942,6 +957,7 @@ class useRest {
|
|
|
942
957
|
await col.hooks.beforeFind({
|
|
943
958
|
sharedData: sharedData,
|
|
944
959
|
c: this.#c,
|
|
960
|
+
error: fn.error,
|
|
945
961
|
driver: "mongodb",
|
|
946
962
|
action: "find",
|
|
947
963
|
io: Cfg.io,
|
|
@@ -984,6 +1000,7 @@ class useRest {
|
|
|
984
1000
|
c: this.#c,
|
|
985
1001
|
io: Cfg.io,
|
|
986
1002
|
driver: "mongodb",
|
|
1003
|
+
error: fn.error,
|
|
987
1004
|
action: "find",
|
|
988
1005
|
meta: {},
|
|
989
1006
|
count: result?.docs?.length || 0,
|
|
@@ -1003,6 +1020,7 @@ class useRest {
|
|
|
1003
1020
|
c: this.#c,
|
|
1004
1021
|
io: Cfg.io,
|
|
1005
1022
|
driver: "mongodb",
|
|
1023
|
+
error: fn.error,
|
|
1006
1024
|
action: "find",
|
|
1007
1025
|
meta: {},
|
|
1008
1026
|
count: result?.docs?.length || 0,
|
|
@@ -1077,6 +1095,7 @@ class useRest {
|
|
|
1077
1095
|
c: this.#c,
|
|
1078
1096
|
driver: "mongodb",
|
|
1079
1097
|
action: "find",
|
|
1098
|
+
error: fn.error,
|
|
1080
1099
|
io: Cfg.io,
|
|
1081
1100
|
params: params,
|
|
1082
1101
|
session: sessionStorage(),
|
|
@@ -1123,6 +1142,7 @@ class useRest {
|
|
|
1123
1142
|
c: this.#c,
|
|
1124
1143
|
io: Cfg.io,
|
|
1125
1144
|
driver: "mongodb",
|
|
1145
|
+
error: fn.error,
|
|
1126
1146
|
action: "find",
|
|
1127
1147
|
meta: meta,
|
|
1128
1148
|
count: result?.docs?.length || 0,
|
|
@@ -1218,6 +1238,7 @@ class useRest {
|
|
|
1218
1238
|
if (col?.hooks?.beforeCount && useHook) {
|
|
1219
1239
|
await col.hooks.beforeCount({
|
|
1220
1240
|
sharedData: sharedData,
|
|
1241
|
+
error: fn.error,
|
|
1221
1242
|
c: this.#c,
|
|
1222
1243
|
driver: "mongodb",
|
|
1223
1244
|
action: "find",
|
|
@@ -1265,6 +1286,7 @@ class useRest {
|
|
|
1265
1286
|
sharedData: sharedData,
|
|
1266
1287
|
c: this.#c,
|
|
1267
1288
|
io: Cfg.io,
|
|
1289
|
+
error: fn.error,
|
|
1268
1290
|
driver: "mongodb",
|
|
1269
1291
|
action: "find",
|
|
1270
1292
|
params: params,
|
|
@@ -1283,6 +1305,7 @@ class useRest {
|
|
|
1283
1305
|
sharedData: sharedData,
|
|
1284
1306
|
c: this.#c,
|
|
1285
1307
|
io: Cfg.io,
|
|
1308
|
+
error: fn.error,
|
|
1286
1309
|
driver: "mongodb",
|
|
1287
1310
|
action: "find",
|
|
1288
1311
|
params: params,
|
|
@@ -1360,6 +1383,7 @@ class useRest {
|
|
|
1360
1383
|
c: this.#c,
|
|
1361
1384
|
driver: "mongodb",
|
|
1362
1385
|
io: Cfg.io,
|
|
1386
|
+
error: fn.error,
|
|
1363
1387
|
action: "find",
|
|
1364
1388
|
params: params,
|
|
1365
1389
|
session: sessionStorage(),
|
|
@@ -1414,6 +1438,7 @@ class useRest {
|
|
|
1414
1438
|
sharedData: sharedData,
|
|
1415
1439
|
c: this.#c,
|
|
1416
1440
|
io: Cfg.io,
|
|
1441
|
+
error: fn.error,
|
|
1417
1442
|
driver: "mongodb",
|
|
1418
1443
|
action: "findOne",
|
|
1419
1444
|
params: params,
|
|
@@ -1433,6 +1458,7 @@ class useRest {
|
|
|
1433
1458
|
io: Cfg.io,
|
|
1434
1459
|
driver: "mongodb",
|
|
1435
1460
|
action: "findOne",
|
|
1461
|
+
error: fn.error,
|
|
1436
1462
|
params: params,
|
|
1437
1463
|
session: sessionStorage(),
|
|
1438
1464
|
result: docs,
|
|
@@ -1506,6 +1532,7 @@ class useRest {
|
|
|
1506
1532
|
sharedData: sharedData,
|
|
1507
1533
|
c: this.#c,
|
|
1508
1534
|
id: id,
|
|
1535
|
+
error: fn.error,
|
|
1509
1536
|
driver: "mongodb",
|
|
1510
1537
|
io: Cfg.io,
|
|
1511
1538
|
action: "updateOne",
|
|
@@ -1605,7 +1632,7 @@ class useRest {
|
|
|
1605
1632
|
);
|
|
1606
1633
|
}
|
|
1607
1634
|
|
|
1608
|
-
if (col?.hooks?.afterUpdate && useHook) {
|
|
1635
|
+
if (col?.hooks?.afterUpdate && useHook && result?.doc) {
|
|
1609
1636
|
await col.hooks.afterUpdate({
|
|
1610
1637
|
sharedData: sharedData,
|
|
1611
1638
|
c: this.#c,
|
|
@@ -1613,9 +1640,10 @@ class useRest {
|
|
|
1613
1640
|
id: id,
|
|
1614
1641
|
io: Cfg.io,
|
|
1615
1642
|
action: "updateOne",
|
|
1643
|
+
error: fn.error,
|
|
1616
1644
|
update: update,
|
|
1617
1645
|
session: sessionStorage(),
|
|
1618
|
-
result: toJson(result
|
|
1646
|
+
result: toJson(result?.doc),
|
|
1619
1647
|
rest: new useRest({
|
|
1620
1648
|
useCustomApi: false,
|
|
1621
1649
|
session: this.#session,
|
|
@@ -1624,12 +1652,14 @@ class useRest {
|
|
|
1624
1652
|
}),
|
|
1625
1653
|
});
|
|
1626
1654
|
}
|
|
1627
|
-
|
|
1655
|
+
|
|
1656
|
+
if (col?.hooks?.afterOperation && useHook && result?.doc) {
|
|
1628
1657
|
await col.hooks.afterOperation({
|
|
1629
1658
|
sharedData: sharedData,
|
|
1630
1659
|
c: this.#c,
|
|
1631
1660
|
driver: "mongodb",
|
|
1632
1661
|
id: id,
|
|
1662
|
+
error: fn.error,
|
|
1633
1663
|
io: Cfg.io,
|
|
1634
1664
|
action: "updateOne",
|
|
1635
1665
|
update: update,
|
|
@@ -1644,15 +1674,15 @@ class useRest {
|
|
|
1644
1674
|
});
|
|
1645
1675
|
}
|
|
1646
1676
|
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
.catch();
|
|
1677
|
+
// dispatchEvent
|
|
1678
|
+
if (result?.doc) {
|
|
1679
|
+
this.disptachEvent("change", {
|
|
1680
|
+
action: "updateOne",
|
|
1681
|
+
collection: collection,
|
|
1682
|
+
result: toJson(result?.doc),
|
|
1683
|
+
});
|
|
1655
1684
|
}
|
|
1685
|
+
|
|
1656
1686
|
// if updateOne done
|
|
1657
1687
|
restActivity.save(this.#tenant, {
|
|
1658
1688
|
operation: {
|
|
@@ -1814,16 +1844,14 @@ class useRest {
|
|
|
1814
1844
|
})
|
|
1815
1845
|
.toArray();
|
|
1816
1846
|
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
.catch();
|
|
1847
|
+
// dispatchEvent
|
|
1848
|
+
if (result?.docs) {
|
|
1849
|
+
this.disptachEvent("change", {
|
|
1850
|
+
action: "findAndUpdate",
|
|
1851
|
+
collection: collection,
|
|
1852
|
+
result: toJson(result?.docs),
|
|
1853
|
+
});
|
|
1825
1854
|
}
|
|
1826
|
-
``;
|
|
1827
1855
|
|
|
1828
1856
|
restActivity.save(this.#tenant, {
|
|
1829
1857
|
ip: sessionStorage()?.get()?._v?.ip,
|
|
@@ -1849,21 +1877,9 @@ class useRest {
|
|
|
1849
1877
|
filter: filter,
|
|
1850
1878
|
},
|
|
1851
1879
|
},
|
|
1852
|
-
/* collection: collection,
|
|
1853
|
-
data: {
|
|
1854
|
-
update: update,
|
|
1855
|
-
filter: filter,
|
|
1856
|
-
},
|
|
1857
|
-
transaction: this.#session ? true : false,
|
|
1858
|
-
action: "findAndUpdate",
|
|
1859
|
-
internal: sessionStorage()?.get() ? false : true,
|
|
1860
|
-
ip: sessionStorage()?.get()?._v?.ip,
|
|
1861
|
-
auth: sessionStorage()?.get()?._v?.isAuth ?? false,
|
|
1862
|
-
role: sessionStorage()?.get()?.role!,
|
|
1863
|
-
user: sessionStorage()?.get()?.state?.user,
|
|
1864
|
-
state: sessionStorage()?.get()?.state,
|
|
1865
|
-
uuid: sessionStorage()?.get()?.uuid, */
|
|
1866
1880
|
});
|
|
1881
|
+
|
|
1882
|
+
// Cache
|
|
1867
1883
|
if (col?.cache?.invalidate) {
|
|
1868
1884
|
col.cache.invalidate({
|
|
1869
1885
|
operationType: "update",
|
|
@@ -1929,7 +1945,7 @@ class useRest {
|
|
|
1929
1945
|
)
|
|
1930
1946
|
)
|
|
1931
1947
|
);
|
|
1932
|
-
update.$set = transformAllDate(update
|
|
1948
|
+
update.$set = transformAllDate(update?.$set || {});
|
|
1933
1949
|
update.$set = await hashPasswordAuto(update.$set, col);
|
|
1934
1950
|
var { valid, output, error } = this.validator(
|
|
1935
1951
|
collection,
|
|
@@ -1982,19 +1998,16 @@ class useRest {
|
|
|
1982
1998
|
session: this.#session ? this.#session : undefined,
|
|
1983
1999
|
}
|
|
1984
2000
|
);
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
...toJson(result.doc || {}),
|
|
1994
|
-
},
|
|
1995
|
-
])
|
|
1996
|
-
.catch();
|
|
2001
|
+
|
|
2002
|
+
// dispatchEvent
|
|
2003
|
+
if (result?.doc) {
|
|
2004
|
+
this.disptachEvent("change", {
|
|
2005
|
+
action: "findOneAndUpdate",
|
|
2006
|
+
collection: collection,
|
|
2007
|
+
result: toJson(result?.doc),
|
|
2008
|
+
});
|
|
1997
2009
|
}
|
|
2010
|
+
|
|
1998
2011
|
// if findOneAndUpdate
|
|
1999
2012
|
restActivity.save(this.#tenant, {
|
|
2000
2013
|
ip: sessionStorage()?.get()?._v?.ip,
|
|
@@ -2020,21 +2033,9 @@ class useRest {
|
|
|
2020
2033
|
filter: filter,
|
|
2021
2034
|
},
|
|
2022
2035
|
},
|
|
2023
|
-
/* collection: collection,
|
|
2024
|
-
data: {
|
|
2025
|
-
update: update,
|
|
2026
|
-
filter: filter,
|
|
2027
|
-
},
|
|
2028
|
-
transaction: this.#session ? true : false,
|
|
2029
|
-
action: "findOneAndUpdate",
|
|
2030
|
-
internal: sessionStorage()?.get() ? false : true,
|
|
2031
|
-
ip: sessionStorage()?.get()?._v?.ip,
|
|
2032
|
-
auth: sessionStorage()?.get()?._v?.isAuth ?? false,
|
|
2033
|
-
role: sessionStorage()?.get()?.role!,
|
|
2034
|
-
user: sessionStorage()?.get()?.state?.user,
|
|
2035
|
-
state: sessionStorage()?.get()?.state,
|
|
2036
|
-
uuid: sessionStorage()?.get()?.uuid, */
|
|
2037
2036
|
});
|
|
2037
|
+
|
|
2038
|
+
//
|
|
2038
2039
|
if (col?.cache?.invalidate) {
|
|
2039
2040
|
col.cache.invalidate({
|
|
2040
2041
|
operationType: "update",
|
|
@@ -2045,8 +2046,8 @@ class useRest {
|
|
|
2045
2046
|
tenant_id: this.#tenant_id,
|
|
2046
2047
|
}),
|
|
2047
2048
|
action: "findOneAndUpdate",
|
|
2048
|
-
ids: [doc?._id],
|
|
2049
|
-
tags: [doc?._id],
|
|
2049
|
+
ids: [result?.doc?._id],
|
|
2050
|
+
tags: [result?.doc?._id],
|
|
2050
2051
|
});
|
|
2051
2052
|
}
|
|
2052
2053
|
return resolve(toJson(result?.doc || {}));
|
|
@@ -2107,6 +2108,7 @@ class useRest {
|
|
|
2107
2108
|
c: this.#c,
|
|
2108
2109
|
ids: ids,
|
|
2109
2110
|
driver: "mongodb",
|
|
2111
|
+
error: fn.error,
|
|
2110
2112
|
io: Cfg.io,
|
|
2111
2113
|
action: "updateMany",
|
|
2112
2114
|
update: update,
|
|
@@ -2155,11 +2157,11 @@ class useRest {
|
|
|
2155
2157
|
)
|
|
2156
2158
|
)
|
|
2157
2159
|
);
|
|
2158
|
-
update.$set = transformAllDate(update
|
|
2160
|
+
update.$set = transformAllDate(update?.$set || {});
|
|
2159
2161
|
update.$set = await hashPasswordAuto(update.$set, col);
|
|
2160
2162
|
var { valid, output, error } = this.validator(
|
|
2161
2163
|
collection,
|
|
2162
|
-
dotJson.object(update?.$set),
|
|
2164
|
+
dotJson.object(update?.$set || {}),
|
|
2163
2165
|
{
|
|
2164
2166
|
partial: true,
|
|
2165
2167
|
}
|
|
@@ -2213,6 +2215,7 @@ class useRest {
|
|
|
2213
2215
|
c: this.#c,
|
|
2214
2216
|
driver: "mongodb",
|
|
2215
2217
|
ids: ids,
|
|
2218
|
+
error: fn.error,
|
|
2216
2219
|
action: "updateMany",
|
|
2217
2220
|
update: update,
|
|
2218
2221
|
session: sessionStorage(),
|
|
@@ -2231,6 +2234,7 @@ class useRest {
|
|
|
2231
2234
|
await col.hooks.afterOperation({
|
|
2232
2235
|
sharedData: sharedData,
|
|
2233
2236
|
c: this.#c,
|
|
2237
|
+
error: fn.error,
|
|
2234
2238
|
driver: "mongodb",
|
|
2235
2239
|
ids: ids,
|
|
2236
2240
|
action: "updateMany",
|
|
@@ -2247,15 +2251,15 @@ class useRest {
|
|
|
2247
2251
|
});
|
|
2248
2252
|
}
|
|
2249
2253
|
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
.catch();
|
|
2254
|
+
// dispatchEvent
|
|
2255
|
+
if (result?.docs) {
|
|
2256
|
+
this.disptachEvent("change", {
|
|
2257
|
+
action: "updateMany",
|
|
2258
|
+
collection: collection,
|
|
2259
|
+
result: toJson(result?.docs),
|
|
2260
|
+
});
|
|
2258
2261
|
}
|
|
2262
|
+
|
|
2259
2263
|
// if updateMany done
|
|
2260
2264
|
restActivity.save(this.#tenant, {
|
|
2261
2265
|
ip: sessionStorage()?.get()?._v?.ip,
|
|
@@ -2312,7 +2316,7 @@ class useRest {
|
|
|
2312
2316
|
}
|
|
2313
2317
|
return resolve(result.docs);
|
|
2314
2318
|
} else {
|
|
2315
|
-
throw new contextError("List of
|
|
2319
|
+
throw new contextError("List of ids required", 400);
|
|
2316
2320
|
}
|
|
2317
2321
|
} catch (err) {
|
|
2318
2322
|
return reject(err);
|
|
@@ -2356,6 +2360,7 @@ class useRest {
|
|
|
2356
2360
|
await col.hooks.beforeDelete({
|
|
2357
2361
|
sharedData: sharedData,
|
|
2358
2362
|
id: id,
|
|
2363
|
+
error: fn.error,
|
|
2359
2364
|
c: this.#c,
|
|
2360
2365
|
driver: "mongodb",
|
|
2361
2366
|
io: Cfg.io,
|
|
@@ -2402,9 +2407,10 @@ class useRest {
|
|
|
2402
2407
|
}
|
|
2403
2408
|
);
|
|
2404
2409
|
|
|
2405
|
-
if (col?.hooks?.afterDelete && useHook) {
|
|
2410
|
+
if (col?.hooks?.afterDelete && useHook && doc) {
|
|
2406
2411
|
await col.hooks.afterDelete({
|
|
2407
2412
|
sharedData: sharedData,
|
|
2413
|
+
error: fn.error,
|
|
2408
2414
|
id: id,
|
|
2409
2415
|
c: this.#c,
|
|
2410
2416
|
driver: "mongodb",
|
|
@@ -2420,11 +2426,12 @@ class useRest {
|
|
|
2420
2426
|
}),
|
|
2421
2427
|
});
|
|
2422
2428
|
}
|
|
2423
|
-
if (col?.hooks?.afterOperation && useHook) {
|
|
2429
|
+
if (col?.hooks?.afterOperation && useHook && doc) {
|
|
2424
2430
|
await col.hooks.afterOperation({
|
|
2425
2431
|
sharedData: sharedData,
|
|
2426
2432
|
id: id,
|
|
2427
2433
|
c: this.#c,
|
|
2434
|
+
error: fn.error,
|
|
2428
2435
|
driver: "mongodb",
|
|
2429
2436
|
action: "deleteOne",
|
|
2430
2437
|
result: toJson(doc),
|
|
@@ -2439,14 +2446,13 @@ class useRest {
|
|
|
2439
2446
|
});
|
|
2440
2447
|
}
|
|
2441
2448
|
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
.catch();
|
|
2449
|
+
// Dispatch events
|
|
2450
|
+
if (doc) {
|
|
2451
|
+
this.disptachEvent("change", {
|
|
2452
|
+
action: "deleteOne",
|
|
2453
|
+
collection: collection,
|
|
2454
|
+
result: toJson(doc),
|
|
2455
|
+
});
|
|
2450
2456
|
}
|
|
2451
2457
|
|
|
2452
2458
|
// if deleteOne done
|
|
@@ -2589,6 +2595,7 @@ class useRest {
|
|
|
2589
2595
|
sharedData: sharedData,
|
|
2590
2596
|
ids: ids,
|
|
2591
2597
|
c: this.#c,
|
|
2598
|
+
error: fn.error,
|
|
2592
2599
|
driver: "mongodb",
|
|
2593
2600
|
io: Cfg.io,
|
|
2594
2601
|
action: "deleteMany",
|
|
@@ -2619,6 +2626,7 @@ class useRest {
|
|
|
2619
2626
|
}
|
|
2620
2627
|
|
|
2621
2628
|
let deletedIds: any = ids || [];
|
|
2629
|
+
let contentIds: any = [];
|
|
2622
2630
|
|
|
2623
2631
|
if (!ids)
|
|
2624
2632
|
return reject({
|
|
@@ -2626,7 +2634,14 @@ class useRest {
|
|
|
2626
2634
|
message: "Ids required",
|
|
2627
2635
|
});
|
|
2628
2636
|
|
|
2629
|
-
if (Array.isArray(ids)) {
|
|
2637
|
+
if (Array.isArray(ids) && ids?.length) {
|
|
2638
|
+
contentIds = await this.#tenant.database.db
|
|
2639
|
+
?.collection(collection)
|
|
2640
|
+
.find({
|
|
2641
|
+
_id: { $in: formatData(ids.map((d) => d?._id || d)) },
|
|
2642
|
+
})
|
|
2643
|
+
.toArray();
|
|
2644
|
+
|
|
2630
2645
|
await this.#tenant.database.db?.collection(collection).deleteMany(
|
|
2631
2646
|
{
|
|
2632
2647
|
_id: { $in: formatData(ids.map((d) => d?._id || d)) },
|
|
@@ -2637,15 +2652,16 @@ class useRest {
|
|
|
2637
2652
|
);
|
|
2638
2653
|
}
|
|
2639
2654
|
|
|
2640
|
-
if (col?.hooks?.afterDelete && useHook) {
|
|
2655
|
+
if (col?.hooks?.afterDelete && useHook && ids?.length) {
|
|
2641
2656
|
await col.hooks.afterDelete({
|
|
2642
2657
|
sharedData: sharedData,
|
|
2643
2658
|
ids: ids,
|
|
2644
2659
|
c: this.#c,
|
|
2645
2660
|
driver: "mongodb",
|
|
2661
|
+
error: fn.error,
|
|
2646
2662
|
io: Cfg.io,
|
|
2647
2663
|
action: "deleteMany",
|
|
2648
|
-
result:
|
|
2664
|
+
result: contentIds,
|
|
2649
2665
|
session: sessionStorage(),
|
|
2650
2666
|
rest: new useRest({
|
|
2651
2667
|
session: this.#session,
|
|
@@ -2656,15 +2672,16 @@ class useRest {
|
|
|
2656
2672
|
});
|
|
2657
2673
|
}
|
|
2658
2674
|
|
|
2659
|
-
if (col?.hooks?.afterOperation && useHook) {
|
|
2675
|
+
if (col?.hooks?.afterOperation && useHook && deletedIds) {
|
|
2660
2676
|
await col.hooks.afterOperation({
|
|
2661
2677
|
sharedData: sharedData,
|
|
2662
2678
|
ids: ids,
|
|
2663
2679
|
c: this.#c,
|
|
2664
2680
|
driver: "mongodb",
|
|
2681
|
+
error: fn.error,
|
|
2665
2682
|
io: Cfg.io,
|
|
2666
2683
|
action: "deleteMany",
|
|
2667
|
-
result:
|
|
2684
|
+
result: contentIds,
|
|
2668
2685
|
session: sessionStorage(),
|
|
2669
2686
|
rest: new useRest({
|
|
2670
2687
|
session: this.#session,
|
|
@@ -2675,15 +2692,15 @@ class useRest {
|
|
|
2675
2692
|
});
|
|
2676
2693
|
}
|
|
2677
2694
|
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
.catch();
|
|
2695
|
+
// Dispatch events
|
|
2696
|
+
if (deletedIds) {
|
|
2697
|
+
this.disptachEvent("change", {
|
|
2698
|
+
action: "deleteMany",
|
|
2699
|
+
collection: collection,
|
|
2700
|
+
result: contentIds,
|
|
2701
|
+
});
|
|
2686
2702
|
}
|
|
2703
|
+
|
|
2687
2704
|
// if deleteMany done
|
|
2688
2705
|
restActivity.save(this.#tenant, {
|
|
2689
2706
|
ip: sessionStorage()?.get()?._v?.ip,
|