@dnax/core 0.76.10 → 0.76.11

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 +139 -154
  2. package/package.json +1 -1
@@ -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
- validator(
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 } = {
@@ -578,6 +623,13 @@ class useRest {
578
623
  });
579
624
  }
580
625
 
626
+ this.disptachEvent("change", {
627
+ action: "insertOne",
628
+ collection: collection,
629
+ data: data,
630
+ result: toJson(data),
631
+ });
632
+
581
633
  // if action done
582
634
  restActivity.save(this.#tenant, {
583
635
  operation: {
@@ -601,23 +653,6 @@ class useRest {
601
653
  ip: sessionStorage()?.get()?._v?.ip,
602
654
  state: sessionStorage()?.get()?.state,
603
655
  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
656
  } as activityInput);
622
657
  if (col?.cache?.invalidate) {
623
658
  col.cache.invalidate({
@@ -633,6 +668,7 @@ class useRest {
633
668
  tags: [String(data?._id)],
634
669
  });
635
670
  }
671
+
636
672
  return resolve(toJson(data));
637
673
  } catch (err) {
638
674
  return reject(err);
@@ -779,6 +815,14 @@ class useRest {
779
815
  });
780
816
  }
781
817
 
818
+ // dispatchEvent
819
+ this.disptachEvent("change", {
820
+ action: "insertMany",
821
+ collection: collection,
822
+ data: data,
823
+ result: toJson(data),
824
+ });
825
+
782
826
  // if insertMany done
783
827
  restActivity.save(this.#tenant, {
784
828
  uuid: sessionStorage()?.get()?.uuid,
@@ -841,44 +885,6 @@ class useRest {
841
885
  });
842
886
  }
843
887
 
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
888
  /**
883
889
  *
884
890
  * @param collection - Collection name
@@ -1605,7 +1611,7 @@ class useRest {
1605
1611
  );
1606
1612
  }
1607
1613
 
1608
- if (col?.hooks?.afterUpdate && useHook) {
1614
+ if (col?.hooks?.afterUpdate && useHook && result?.doc) {
1609
1615
  await col.hooks.afterUpdate({
1610
1616
  sharedData: sharedData,
1611
1617
  c: this.#c,
@@ -1615,7 +1621,7 @@ class useRest {
1615
1621
  action: "updateOne",
1616
1622
  update: update,
1617
1623
  session: sessionStorage(),
1618
- result: toJson(result.doc),
1624
+ result: toJson(result?.doc),
1619
1625
  rest: new useRest({
1620
1626
  useCustomApi: false,
1621
1627
  session: this.#session,
@@ -1624,7 +1630,8 @@ class useRest {
1624
1630
  }),
1625
1631
  });
1626
1632
  }
1627
- if (col?.hooks?.afterOperation && useHook) {
1633
+
1634
+ if (col?.hooks?.afterOperation && useHook && result?.doc) {
1628
1635
  await col.hooks.afterOperation({
1629
1636
  sharedData: sharedData,
1630
1637
  c: this.#c,
@@ -1644,15 +1651,15 @@ class useRest {
1644
1651
  });
1645
1652
  }
1646
1653
 
1647
- if (
1648
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
1649
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
1650
- ) {
1651
- this.meilisearch.client
1652
- .index(col?.searchEngine?.meilisearch?.index)
1653
- .updateDocuments([{ ...toJson(result?.doc || {}) }])
1654
- .catch();
1654
+ // dispatchEvent
1655
+ if (result?.doc) {
1656
+ this.disptachEvent("change", {
1657
+ action: "updateOne",
1658
+ collection: collection,
1659
+ result: toJson(result?.doc),
1660
+ });
1655
1661
  }
1662
+
1656
1663
  // if updateOne done
1657
1664
  restActivity.save(this.#tenant, {
1658
1665
  operation: {
@@ -1814,16 +1821,14 @@ class useRest {
1814
1821
  })
1815
1822
  .toArray();
1816
1823
 
1817
- if (
1818
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
1819
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
1820
- ) {
1821
- this.meilisearch.client
1822
- .index(col?.searchEngine?.meilisearch?.index)
1823
- .updateDocuments(result.docs)
1824
- .catch();
1824
+ // dispatchEvent
1825
+ if (result?.docs) {
1826
+ this.disptachEvent("change", {
1827
+ action: "findAndUpdate",
1828
+ collection: collection,
1829
+ result: toJson(result?.docs),
1830
+ });
1825
1831
  }
1826
- ``;
1827
1832
 
1828
1833
  restActivity.save(this.#tenant, {
1829
1834
  ip: sessionStorage()?.get()?._v?.ip,
@@ -1849,21 +1854,9 @@ class useRest {
1849
1854
  filter: filter,
1850
1855
  },
1851
1856
  },
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
1857
  });
1858
+
1859
+ // Cache
1867
1860
  if (col?.cache?.invalidate) {
1868
1861
  col.cache.invalidate({
1869
1862
  operationType: "update",
@@ -1929,7 +1922,7 @@ class useRest {
1929
1922
  )
1930
1923
  )
1931
1924
  );
1932
- update.$set = transformAllDate(update.$set);
1925
+ update.$set = transformAllDate(update?.$set || {});
1933
1926
  update.$set = await hashPasswordAuto(update.$set, col);
1934
1927
  var { valid, output, error } = this.validator(
1935
1928
  collection,
@@ -1982,19 +1975,16 @@ class useRest {
1982
1975
  session: this.#session ? this.#session : undefined,
1983
1976
  }
1984
1977
  );
1985
- if (
1986
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
1987
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
1988
- ) {
1989
- this.meilisearch.client
1990
- .index(col?.searchEngine?.meilisearch?.index)
1991
- .updateDocuments([
1992
- {
1993
- ...toJson(result.doc || {}),
1994
- },
1995
- ])
1996
- .catch();
1978
+
1979
+ // dispatchEvent
1980
+ if (result?.doc) {
1981
+ this.disptachEvent("change", {
1982
+ action: "findOneAndUpdate",
1983
+ collection: collection,
1984
+ result: toJson(result?.doc),
1985
+ });
1997
1986
  }
1987
+
1998
1988
  // if findOneAndUpdate
1999
1989
  restActivity.save(this.#tenant, {
2000
1990
  ip: sessionStorage()?.get()?._v?.ip,
@@ -2020,21 +2010,9 @@ class useRest {
2020
2010
  filter: filter,
2021
2011
  },
2022
2012
  },
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
2013
  });
2014
+
2015
+ //
2038
2016
  if (col?.cache?.invalidate) {
2039
2017
  col.cache.invalidate({
2040
2018
  operationType: "update",
@@ -2045,8 +2023,8 @@ class useRest {
2045
2023
  tenant_id: this.#tenant_id,
2046
2024
  }),
2047
2025
  action: "findOneAndUpdate",
2048
- ids: [doc?._id],
2049
- tags: [doc?._id],
2026
+ ids: [result?.doc?._id],
2027
+ tags: [result?.doc?._id],
2050
2028
  });
2051
2029
  }
2052
2030
  return resolve(toJson(result?.doc || {}));
@@ -2155,11 +2133,11 @@ class useRest {
2155
2133
  )
2156
2134
  )
2157
2135
  );
2158
- update.$set = transformAllDate(update.$set);
2136
+ update.$set = transformAllDate(update?.$set || {});
2159
2137
  update.$set = await hashPasswordAuto(update.$set, col);
2160
2138
  var { valid, output, error } = this.validator(
2161
2139
  collection,
2162
- dotJson.object(update?.$set),
2140
+ dotJson.object(update?.$set || {}),
2163
2141
  {
2164
2142
  partial: true,
2165
2143
  }
@@ -2247,15 +2225,15 @@ class useRest {
2247
2225
  });
2248
2226
  }
2249
2227
 
2250
- if (
2251
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
2252
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
2253
- ) {
2254
- this.meilisearch.client
2255
- .index(col?.searchEngine?.meilisearch?.index)
2256
- .updateDocuments(result.docs)
2257
- .catch();
2228
+ // dispatchEvent
2229
+ if (result?.docs) {
2230
+ this.disptachEvent("change", {
2231
+ action: "updateMany",
2232
+ collection: collection,
2233
+ result: toJson(result?.docs),
2234
+ });
2258
2235
  }
2236
+
2259
2237
  // if updateMany done
2260
2238
  restActivity.save(this.#tenant, {
2261
2239
  ip: sessionStorage()?.get()?._v?.ip,
@@ -2312,7 +2290,7 @@ class useRest {
2312
2290
  }
2313
2291
  return resolve(result.docs);
2314
2292
  } else {
2315
- throw new contextError("List of id required", 400);
2293
+ throw new contextError("List of ids required", 400);
2316
2294
  }
2317
2295
  } catch (err) {
2318
2296
  return reject(err);
@@ -2402,7 +2380,7 @@ class useRest {
2402
2380
  }
2403
2381
  );
2404
2382
 
2405
- if (col?.hooks?.afterDelete && useHook) {
2383
+ if (col?.hooks?.afterDelete && useHook && doc) {
2406
2384
  await col.hooks.afterDelete({
2407
2385
  sharedData: sharedData,
2408
2386
  id: id,
@@ -2420,7 +2398,7 @@ class useRest {
2420
2398
  }),
2421
2399
  });
2422
2400
  }
2423
- if (col?.hooks?.afterOperation && useHook) {
2401
+ if (col?.hooks?.afterOperation && useHook && doc) {
2424
2402
  await col.hooks.afterOperation({
2425
2403
  sharedData: sharedData,
2426
2404
  id: id,
@@ -2439,14 +2417,13 @@ class useRest {
2439
2417
  });
2440
2418
  }
2441
2419
 
2442
- if (
2443
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
2444
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
2445
- ) {
2446
- this.meilisearch.client
2447
- .index(col?.searchEngine?.meilisearch?.index)
2448
- .deleteDocument(id)
2449
- .catch();
2420
+ // Dispatch events
2421
+ if (doc) {
2422
+ this.disptachEvent("change", {
2423
+ action: "deleteOne",
2424
+ collection: collection,
2425
+ result: toJson(doc),
2426
+ });
2450
2427
  }
2451
2428
 
2452
2429
  // if deleteOne done
@@ -2619,6 +2596,7 @@ class useRest {
2619
2596
  }
2620
2597
 
2621
2598
  let deletedIds: any = ids || [];
2599
+ let contentIds: any = [];
2622
2600
 
2623
2601
  if (!ids)
2624
2602
  return reject({
@@ -2626,7 +2604,14 @@ class useRest {
2626
2604
  message: "Ids required",
2627
2605
  });
2628
2606
 
2629
- if (Array.isArray(ids)) {
2607
+ if (Array.isArray(ids) && ids?.length) {
2608
+ contentIds = await this.#tenant.database.db
2609
+ ?.collection(collection)
2610
+ .find({
2611
+ _id: { $in: formatData(ids.map((d) => d?._id || d)) },
2612
+ })
2613
+ .toArray();
2614
+
2630
2615
  await this.#tenant.database.db?.collection(collection).deleteMany(
2631
2616
  {
2632
2617
  _id: { $in: formatData(ids.map((d) => d?._id || d)) },
@@ -2637,7 +2622,7 @@ class useRest {
2637
2622
  );
2638
2623
  }
2639
2624
 
2640
- if (col?.hooks?.afterDelete && useHook) {
2625
+ if (col?.hooks?.afterDelete && useHook && ids?.length) {
2641
2626
  await col.hooks.afterDelete({
2642
2627
  sharedData: sharedData,
2643
2628
  ids: ids,
@@ -2645,7 +2630,7 @@ class useRest {
2645
2630
  driver: "mongodb",
2646
2631
  io: Cfg.io,
2647
2632
  action: "deleteMany",
2648
- result: deletedIds,
2633
+ result: contentIds,
2649
2634
  session: sessionStorage(),
2650
2635
  rest: new useRest({
2651
2636
  session: this.#session,
@@ -2656,7 +2641,7 @@ class useRest {
2656
2641
  });
2657
2642
  }
2658
2643
 
2659
- if (col?.hooks?.afterOperation && useHook) {
2644
+ if (col?.hooks?.afterOperation && useHook && deletedIds) {
2660
2645
  await col.hooks.afterOperation({
2661
2646
  sharedData: sharedData,
2662
2647
  ids: ids,
@@ -2664,7 +2649,7 @@ class useRest {
2664
2649
  driver: "mongodb",
2665
2650
  io: Cfg.io,
2666
2651
  action: "deleteMany",
2667
- result: deletedIds,
2652
+ result: contentIds,
2668
2653
  session: sessionStorage(),
2669
2654
  rest: new useRest({
2670
2655
  session: this.#session,
@@ -2675,15 +2660,15 @@ class useRest {
2675
2660
  });
2676
2661
  }
2677
2662
 
2678
- if (
2679
- this.#tenant?.searchEngine?.meilisearch?.enabled &&
2680
- col?.searchEngine?.meilisearch?.dispatchAction == "auto"
2681
- ) {
2682
- this.meilisearch.client
2683
- .index(col?.searchEngine?.meilisearch?.index)
2684
- .deleteDocuments(deletedIds)
2685
- .catch();
2663
+ // Dispatch events
2664
+ if (deletedIds) {
2665
+ this.disptachEvent("change", {
2666
+ action: "deleteMany",
2667
+ collection: collection,
2668
+ result: contentIds,
2669
+ });
2686
2670
  }
2671
+
2687
2672
  // if deleteMany done
2688
2673
  restActivity.save(this.#tenant, {
2689
2674
  ip: sessionStorage()?.get()?._v?.ip,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.76.10",
3
+ "version": "0.76.11",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},