@dnax/core 0.76.7 → 0.76.9

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.
@@ -378,6 +378,22 @@ class useRest {
378
378
  }),
379
379
  });
380
380
  }
381
+ if (col?.hooks?.afterOperation && useHook) {
382
+ await col.hooks.afterOperation({
383
+ io: Cfg.io,
384
+ sharedData: sharedData,
385
+ docs: result.docs,
386
+ c: this.#c,
387
+ driver: "mongodb",
388
+ action: "aggregate",
389
+ session: sessionStorage(),
390
+ result: result.docs,
391
+ rest: new useRest({
392
+ useHook: false,
393
+ tenant_id: this.#tenant_id,
394
+ }),
395
+ });
396
+ }
381
397
 
382
398
  return resolve(toJson(result.docs));
383
399
  } catch (err) {
@@ -543,6 +559,25 @@ class useRest {
543
559
  });
544
560
  }
545
561
 
562
+ if (col?.hooks?.afterOperation && useHook) {
563
+ await col.hooks.afterOperation({
564
+ sharedData: sharedData,
565
+ data: toJson(data),
566
+ c: this.#c,
567
+ io: Cfg.io,
568
+ driver: "mongodb",
569
+ result: toJson(data),
570
+ action: "insertOne",
571
+ session: sessionStorage(),
572
+ rest: new useRest({
573
+ useCustomApi: false,
574
+ session: this.#session,
575
+ useHook: false,
576
+ tenant_id: this.#tenant_id,
577
+ }),
578
+ });
579
+ }
580
+
546
581
  // if action done
547
582
  restActivity.save(this.#tenant, {
548
583
  operation: {
@@ -725,6 +760,25 @@ class useRest {
725
760
  });
726
761
  }
727
762
 
763
+ if (col?.hooks?.afterOperation && useHook) {
764
+ await col.hooks.afterOperation({
765
+ sharedData: sharedData,
766
+ data: toJson(data),
767
+ c: this.#c,
768
+ io: Cfg.io,
769
+ driver: "mongodb",
770
+ action: "insertMany",
771
+ result: toJson(data),
772
+ session: sessionStorage(),
773
+ rest: new useRest({
774
+ session: this.#session,
775
+ useCustomApi: false,
776
+ useHook: false,
777
+ tenant_id: this.#tenant_id,
778
+ }),
779
+ });
780
+ }
781
+
728
782
  // if insertMany done
729
783
  restActivity.save(this.#tenant, {
730
784
  uuid: sessionStorage()?.get()?.uuid,
@@ -943,6 +997,25 @@ class useRest {
943
997
  });
944
998
  }
945
999
 
1000
+ if (col?.hooks?.afterOperation && useHook) {
1001
+ await col.hooks.afterOperation({
1002
+ sharedData: sharedData,
1003
+ c: this.#c,
1004
+ io: Cfg.io,
1005
+ driver: "mongodb",
1006
+ action: "find",
1007
+ meta: {},
1008
+ count: result?.docs?.length || 0,
1009
+ params: toJson(params),
1010
+ session: sessionStorage(),
1011
+ result: result.docs,
1012
+ rest: new useRest({
1013
+ useHook: false,
1014
+ tenant_id: this.#tenant_id,
1015
+ }),
1016
+ });
1017
+ }
1018
+
946
1019
  let resultDocs = toJson([...(result?.docs || [])]);
947
1020
 
948
1021
  if (typeof options?.elementAt == "number" && options?.elementAt) {
@@ -1205,6 +1278,24 @@ class useRest {
1205
1278
  });
1206
1279
  }
1207
1280
 
1281
+ if (col?.hooks?.afterOperation && useHook) {
1282
+ await col.hooks.afterOperation({
1283
+ sharedData: sharedData,
1284
+ c: this.#c,
1285
+ io: Cfg.io,
1286
+ driver: "mongodb",
1287
+ action: "find",
1288
+ params: params,
1289
+ session: sessionStorage(),
1290
+ result: result.docs,
1291
+ count: result?.docs?.length || 0,
1292
+ rest: new useRest({
1293
+ useHook: false,
1294
+ tenant_id: this.#tenant_id,
1295
+ }),
1296
+ });
1297
+ }
1298
+
1208
1299
  let resultDocs = toJson([...(result?.docs || [])]);
1209
1300
  return resolve(resultDocs?.length);
1210
1301
  } catch (err) {
@@ -1335,6 +1426,23 @@ class useRest {
1335
1426
  });
1336
1427
  }
1337
1428
 
1429
+ if (col?.hooks?.afterOperation && useHook) {
1430
+ await col.hooks.afterOperation({
1431
+ sharedData: sharedData,
1432
+ c: this.#c,
1433
+ io: Cfg.io,
1434
+ driver: "mongodb",
1435
+ action: "findOne",
1436
+ params: params,
1437
+ session: sessionStorage(),
1438
+ result: docs,
1439
+ rest: new useRest({
1440
+ useHook: false,
1441
+ tenant_id: this.#tenant_id,
1442
+ }),
1443
+ });
1444
+ }
1445
+
1338
1446
  return resolve(docs?.length ? toJson(docs[0]) : null);
1339
1447
  } catch (err) {
1340
1448
  return reject(err);
@@ -1516,6 +1624,25 @@ class useRest {
1516
1624
  }),
1517
1625
  });
1518
1626
  }
1627
+ if (col?.hooks?.afterOperation && useHook) {
1628
+ await col.hooks.afterOperation({
1629
+ sharedData: sharedData,
1630
+ c: this.#c,
1631
+ driver: "mongodb",
1632
+ id: id,
1633
+ io: Cfg.io,
1634
+ action: "updateOne",
1635
+ update: update,
1636
+ session: sessionStorage(),
1637
+ result: toJson(result.doc),
1638
+ rest: new useRest({
1639
+ useCustomApi: false,
1640
+ session: this.#session,
1641
+ useHook: false,
1642
+ tenant_id: this.#tenant_id,
1643
+ }),
1644
+ });
1645
+ }
1519
1646
 
1520
1647
  if (
1521
1648
  this.#tenant?.searchEngine?.meilisearch?.enabled &&
@@ -2100,6 +2227,26 @@ class useRest {
2100
2227
  });
2101
2228
  }
2102
2229
 
2230
+ if (col?.hooks?.afterOperation && useHook) {
2231
+ await col.hooks.afterOperation({
2232
+ sharedData: sharedData,
2233
+ c: this.#c,
2234
+ driver: "mongodb",
2235
+ ids: ids,
2236
+ action: "updateMany",
2237
+ update: update,
2238
+ session: sessionStorage(),
2239
+ result: toJson(result.docs),
2240
+ io: Cfg.io,
2241
+ rest: new useRest({
2242
+ session: this.#session,
2243
+ useCustomApi: false,
2244
+ useHook: false,
2245
+ tenant_id: this.#tenant_id,
2246
+ }),
2247
+ });
2248
+ }
2249
+
2103
2250
  if (
2104
2251
  this.#tenant?.searchEngine?.meilisearch?.enabled &&
2105
2252
  col?.searchEngine?.meilisearch?.dispatchAction == "auto"
@@ -2273,6 +2420,24 @@ class useRest {
2273
2420
  }),
2274
2421
  });
2275
2422
  }
2423
+ if (col?.hooks?.afterOperation && useHook) {
2424
+ await col.hooks.afterOperation({
2425
+ sharedData: sharedData,
2426
+ id: id,
2427
+ c: this.#c,
2428
+ driver: "mongodb",
2429
+ action: "deleteOne",
2430
+ result: toJson(doc),
2431
+ io: Cfg.io,
2432
+ session: sessionStorage(),
2433
+ rest: new useRest({
2434
+ session: this.#session,
2435
+ useCustomApi: false,
2436
+ useHook: false,
2437
+ tenant_id: this.#tenant_id,
2438
+ }),
2439
+ });
2440
+ }
2276
2441
 
2277
2442
  if (
2278
2443
  this.#tenant?.searchEngine?.meilisearch?.enabled &&
@@ -2491,6 +2656,25 @@ class useRest {
2491
2656
  });
2492
2657
  }
2493
2658
 
2659
+ if (col?.hooks?.afterOperation && useHook) {
2660
+ await col.hooks.afterOperation({
2661
+ sharedData: sharedData,
2662
+ ids: ids,
2663
+ c: this.#c,
2664
+ driver: "mongodb",
2665
+ io: Cfg.io,
2666
+ action: "deleteMany",
2667
+ result: deletedIds,
2668
+ session: sessionStorage(),
2669
+ rest: new useRest({
2670
+ session: this.#session,
2671
+ useCustomApi: false,
2672
+ useHook: false,
2673
+ tenant_id: this.#tenant_id,
2674
+ }),
2675
+ });
2676
+ }
2677
+
2494
2678
  if (
2495
2679
  this.#tenant?.searchEngine?.meilisearch?.enabled &&
2496
2680
  col?.searchEngine?.meilisearch?.dispatchAction == "auto"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.76.7",
3
+ "version": "0.76.9",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},
@@ -37,6 +37,7 @@
37
37
  "joi": "18.0.2",
38
38
  "json-joy": "16.8.0",
39
39
  "jsonwebtoken": "^9.0.3",
40
+ "liquidjs": "^10.24.0",
40
41
  "mime-types": "^2.1.35",
41
42
  "moment": "^2.30.1",
42
43
  "mongodb": "7.0.0",
package/types/index.ts CHANGED
@@ -377,6 +377,7 @@ export type Collection = {
377
377
  afterSearch?: hooksCtx;
378
378
  beforeListActivity?: hooksCtx;
379
379
  afterListActivity?: hooksCtx;
380
+ afterOperation?: hooksCtx;
380
381
  };
381
382
  access?:{
382
383
  [key:string]:accessCtx;
@@ -499,6 +500,7 @@ export type TimeSeriesCollection = Collection &{
499
500
  afterSearch?: hooksCtx;
500
501
  beforeListActivity?: hooksCtx;
501
502
  afterListActivity?: hooksCtx;
503
+ afterOperation?: hooksCtx;
502
504
  };
503
505
  access?: {
504
506
  "*"?: accessCtx;
package/utils/index.ts CHANGED
@@ -26,6 +26,9 @@ import type { SupportedCryptoAlgorithms } from "bun";
26
26
  //import {applyPatch} from "json-joy/lib/json-patch"
27
27
  import cleanDeep from "clean-deep";
28
28
 
29
+ import { Liquid } from "liquidjs";
30
+ const engineTemplate = new Liquid();
31
+
29
32
  function copy(data: object): object | any {
30
33
  return deepCopy(data);
31
34
  }
@@ -595,11 +598,70 @@ function getRecentDate<T extends Record<string, any>>(
595
598
  return { data: latestItem, date: latest };
596
599
  }
597
600
 
601
+ /**
602
+ * Compile et exécute un template Liquid avec les paramètres fournis
603
+ * @param {string} templateContent - Le contenu du template avec les variables {{variable}}
604
+ * @param {Object} params - Les paramètres à remplacer dans le template
605
+ * @returns {Promise<string>} - Le template compilé avec les valeurs remplacées
606
+ */
607
+ async function renderTemplate(templateContent: string, params: object = {}) {
608
+ try {
609
+ // Rendre le template avec les paramètres
610
+ const result = await engineTemplate.parseAndRender(templateContent, params);
611
+ return result;
612
+ } catch (error) {
613
+ console.error("Erreur lors du rendu du template:", error);
614
+ throw error;
615
+ }
616
+ }
617
+
618
+ // Version synchrone (si besoin)
619
+ function renderTemplateSync(templateContent: string, params: object = {}) {
620
+ try {
621
+ const result = engineTemplate.parseAndRenderSync(templateContent, params);
622
+ return result;
623
+ } catch (error) {
624
+ console.error(error);
625
+ throw error;
626
+ }
627
+ }
628
+
629
+ // eventBus.ts
630
+ type Handler<Payload = any> = (payload: Payload) => void | Promise<void>;
631
+
632
+ class EventBus<Events extends Record<string, any> = Record<string, any>> {
633
+ private listeners = new Map<keyof Events, Handler[]>();
634
+
635
+ on<K extends keyof Events>(event: K, handler: Handler<Events[K]>) {
636
+ const list = this.listeners.get(event) ?? [];
637
+ list.push(handler);
638
+ this.listeners.set(event, list);
639
+ }
640
+
641
+ off<K extends keyof Events>(event: K, handler: Handler<Events[K]>) {
642
+ const list = this.listeners.get(event);
643
+ if (!list) return;
644
+ this.listeners.set(
645
+ event,
646
+ list.filter((h) => h !== handler)
647
+ );
648
+ }
649
+
650
+ async emit<K extends keyof Events>(event: K, payload: Events[K]) {
651
+ const list = this.listeners.get(event);
652
+ if (!list) return;
653
+ for (const handler of list) {
654
+ await handler(payload);
655
+ }
656
+ }
657
+ }
658
+
598
659
  const contextError = ContextError;
599
660
  export {
600
661
  file,
601
662
  mime,
602
663
  dotJson,
664
+ EventBus,
603
665
  uuid,
604
666
  pick,
605
667
  password, // Hash and verify Password utils
@@ -635,4 +697,6 @@ export {
635
697
  getRecentDate,
636
698
  getPidUsage,
637
699
  cleanDeep,
700
+ renderTemplate,
701
+ renderTemplateSync,
638
702
  };