@technicity/data-service-generator 0.22.2-next.0 → 0.22.2-next.2

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.
@@ -252,11 +252,13 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
252
252
  runtime: IRuntime;
253
253
  onHandlerMap: Map<string, TOnHandler>;
254
254
  eventTarget: EventTarget;
255
+ passBeforeValueToAfterCallback: boolean;
255
256
 
256
257
  constructor(opts: {
257
258
  runtime: any;
258
259
  clientOpts: { [k: string]: any; },
259
- otherOpts?: { [k: string]: any; }
260
+ otherOpts?: { [k: string]: any; },
261
+ passBeforeValueToAfterCallback: boolean,
260
262
  }) {
261
263
  let otherOpts = opts.otherOpts ?? {};
262
264
  if (opts.clientOpts.filename === ":memory:") {
@@ -269,6 +271,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
269
271
  : "otherOpts"}, artifacts);
270
272
  this.onHandlerMap = new Map();
271
273
  this.eventTarget = new EventTarget();
274
+ this.passBeforeValueToAfterCallback = opts.passBeforeValueToAfterCallback;
272
275
  }
273
276
 
274
277
  $use(middleware: TMiddleware) {
@@ -305,6 +308,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
305
308
  const runtime = this.runtime;
306
309
  const eventTarget = this.eventTarget;
307
310
  const onHandlerMap = this.onHandlerMap;
311
+ const passBeforeValueToAfterCallback = this.passBeforeValueToAfterCallback;
308
312
  return {
309
313
  $queryRaw: queryRaw,
310
314
  $commit: commit,
@@ -383,10 +387,16 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
383
387
  const findOnes = await getFindOnes(x, specialCaseUuidColumn);
384
388
  return getMethodSourceOnHandlerPatchOne(x, findOnes);
385
389
  }
390
+ if (x.kind === "patchList") {
391
+ return getMethodSourceOnHandlerPatchList(x);
392
+ }
386
393
  if (x.kind === "deleteOne") {
387
394
  const findOnes = await getFindOnes(x, specialCaseUuidColumn);
388
395
  return getMethodSourceOnHandlerDeleteOne(x, findOnes);
389
396
  }
397
+ if (x.kind === "deleteList") {
398
+ return getMethodSourceOnHandlerDeleteList(x);
399
+ }
390
400
  })))
391
401
  .filter(Boolean)
392
402
  .join("\n\n")}
@@ -507,7 +517,10 @@ function getMethodSourceGetOne(x, findOnes, isTransaction) {
507
517
  context: param2?.context,
508
518
  skipCache: param2?.skipCache,
509
519
  eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
510
- ${isTransaction ? "dbCall" : ""}
520
+ ${isTransaction ? "dbCall," : ""}
521
+ passBeforeValueToAfterCallback: ${isTransaction
522
+ ? "passBeforeValueToAfterCallback"
523
+ : "this.passBeforeValueToAfterCallback"},
511
524
  }
512
525
  );
513
526
  }`;
@@ -528,7 +541,10 @@ function getMethodSourceGetList(x, isTransaction) {
528
541
  context: param2?.context,
529
542
  skipCache: param2?.skipCache,
530
543
  eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
531
- ${isTransaction ? "dbCall" : ""}
544
+ ${isTransaction ? "dbCall," : ""}
545
+ passBeforeValueToAfterCallback: ${isTransaction
546
+ ? "passBeforeValueToAfterCallback"
547
+ : "this.passBeforeValueToAfterCallback"},
532
548
  }
533
549
  );
534
550
  }`;
@@ -549,7 +565,10 @@ function getMethodSourceGetListPaginated(x, isTransaction) {
549
565
  context: param2?.context,
550
566
  skipCache: param2?.skipCache,
551
567
  eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
552
- ${isTransaction ? "dbCall" : ""}
568
+ ${isTransaction ? "dbCall," : ""}
569
+ passBeforeValueToAfterCallback: ${isTransaction
570
+ ? "passBeforeValueToAfterCallback"
571
+ : "this.passBeforeValueToAfterCallback"},
553
572
  }
554
573
  );
555
574
  }`;
@@ -571,7 +590,10 @@ function getMethodSourcePostOne(x, specialCaseUuidColumn, isTransaction) {
571
590
  onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
572
591
  asyncLocalStorage,
573
592
  sdk: this,
574
- ${isTransaction ? "dbCall" : ""}
593
+ ${isTransaction ? "dbCall," : ""}
594
+ passBeforeValueToAfterCallback: ${isTransaction
595
+ ? "passBeforeValueToAfterCallback"
596
+ : "this.passBeforeValueToAfterCallback"},
575
597
  });
576
598
  }`;
577
599
  }
@@ -606,7 +628,10 @@ return ${isTransaction ? "runtime" : "this.runtime"}.resolve({
606
628
  onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
607
629
  asyncLocalStorage,
608
630
  sdk: this,
609
- ${isTransaction ? "dbCall" : ""}
631
+ ${isTransaction ? "dbCall," : ""}
632
+ passBeforeValueToAfterCallback: ${isTransaction
633
+ ? "passBeforeValueToAfterCallback"
634
+ : "this.passBeforeValueToAfterCallback"},
610
635
  });
611
636
  }`;
612
637
  }
@@ -616,6 +641,7 @@ function getMethodSourceOnHandlerPatchOne(x, findOnes) {
616
641
  .map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
617
642
  .join(" | ")}, data: ${x.typeDataName} },
618
643
  output: Partial<${getTypeReturnName(x.table)}>,
644
+ before: ${getTypeReturnName(x.table)} | null,
619
645
  context: TContext,
620
646
  ) => Promise<void>
621
647
  ): void {
@@ -641,10 +667,24 @@ function getMethodSourcePatchList(x, isTransaction) {
641
667
  onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
642
668
  asyncLocalStorage,
643
669
  sdk: this,
644
- ${isTransaction ? "dbCall" : ""}
670
+ ${isTransaction ? "dbCall," : ""}
671
+ passBeforeValueToAfterCallback: ${isTransaction
672
+ ? "passBeforeValueToAfterCallback"
673
+ : "this.passBeforeValueToAfterCallback"},
645
674
  });
646
675
  }`;
647
676
  }
677
+ function getMethodSourceOnHandlerPatchList(x) {
678
+ return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
679
+ (sdk: InstanceType<typeof SDK>, input: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, data: ${x.typeDataName} },
680
+ output: Partial<${getTypeReturnName(x.table)}>[],
681
+ before: ${getTypeReturnName(x.table)}[] | null,
682
+ context: TContext,
683
+ ) => Promise<void>
684
+ ): void {
685
+ this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
686
+ }`;
687
+ }
648
688
  function getMethodSourceDeleteOne(x, findOnes, isTransaction) {
649
689
  return `async ${x.methodName}(
650
690
  param1: ${findOnes
@@ -662,7 +702,10 @@ function getMethodSourceDeleteOne(x, findOnes, isTransaction) {
662
702
  onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
663
703
  asyncLocalStorage,
664
704
  sdk: this,
665
- ${isTransaction ? "dbCall" : ""}
705
+ ${isTransaction ? "dbCall," : ""}
706
+ passBeforeValueToAfterCallback: ${isTransaction
707
+ ? "passBeforeValueToAfterCallback"
708
+ : "this.passBeforeValueToAfterCallback"},
666
709
  });
667
710
  }`;
668
711
  }
@@ -693,10 +736,24 @@ function getMethodSourceDeleteList(x, isTransaction) {
693
736
  onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
694
737
  asyncLocalStorage,
695
738
  sdk: this,
696
- ${isTransaction ? "dbCall" : ""}
739
+ ${isTransaction ? "dbCall," : ""}
740
+ passBeforeValueToAfterCallback: ${isTransaction
741
+ ? "passBeforeValueToAfterCallback"
742
+ : "this.passBeforeValueToAfterCallback"},
697
743
  });
698
744
  }`;
699
745
  }
746
+ function getMethodSourceOnHandlerDeleteList(x) {
747
+ return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
748
+ (sdk: InstanceType<typeof SDK>, input: { $where?: ${x.typeWhereName} },
749
+ output: void,
750
+ before: ${getTypeReturnName(x.table)}[] | null,
751
+ context: TContext,
752
+ ) => Promise<void>
753
+ ): void {
754
+ this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
755
+ }`;
756
+ }
700
757
  function mapKindToAction(kind) {
701
758
  if (kind === "getOne") {
702
759
  return "findUnique";
@@ -27,9 +27,10 @@ export type TResolveParams = {
27
27
  isInOnHandler?: boolean;
28
28
  isTransaction?: boolean;
29
29
  }>;
30
+ passBeforeValueToAfterCallback: boolean;
30
31
  sdk?: unknown;
31
32
  };
32
- export type TOnHandler = (sdk: unknown, input: unknown, output: unknown, TContext: TContext) => Promise<void>;
33
+ export type TOnHandler = (sdk: unknown, input: unknown, output: unknown, before: unknown, TContext: TContext) => Promise<void>;
33
34
  export declare class EventOnHandlerError<T> extends Event {
34
35
  error: T;
35
36
  resource: string;
@@ -59,23 +59,38 @@ async function resolve(input, dbCall, formatQuery, beginTransaction, dialect, mi
59
59
  if (middlewareHandler.length() > 0) {
60
60
  const resource = new async_hooks_1.AsyncResource("sdk-request");
61
61
  const params = input;
62
- const consumer = (paramsMaybeMutated) => {
62
+ const consumer = async (paramsMaybeMutated) => {
63
63
  const nextMiddleware = middlewareHandler.get(++index);
64
64
  if (nextMiddleware != null) {
65
65
  return nextMiddleware(paramsMaybeMutated, consumer);
66
66
  }
67
67
  const paramsChanged = { ...input, ...params };
68
- const p = _resolve(paramsChanged, dbCall, formatQuery, beginTransaction, dialect, context, cache);
69
68
  const onHandler = input.onHandler;
70
69
  const asyncLocalStorage = input.asyncLocalStorage;
71
- if (typeof onHandler === "function" &&
70
+ const shouldRunOnHandler = typeof onHandler === "function" &&
72
71
  asyncLocalStorage != null &&
73
- asyncLocalStorage?.getStore()?.isInOnHandler !== true) {
72
+ asyncLocalStorage?.getStore()?.isInOnHandler !== true;
73
+ const beforeValue = shouldRunOnHandler && input.passBeforeValueToAfterCallback
74
+ ? await _resolve({
75
+ ...paramsChanged,
76
+ action: paramsChanged.action === "updateMany" ||
77
+ paramsChanged.action === "deleteMany"
78
+ ? "findMany"
79
+ : "findUnique",
80
+ fields: undefined
81
+ }, dbCall, formatQuery, beginTransaction, dialect, context, cache)
82
+ : null;
83
+ const p = _resolve(paramsChanged, dbCall, formatQuery, beginTransaction, dialect, context, cache);
84
+ if (shouldRunOnHandler) {
74
85
  return p.then((output) => asyncLocalStorage
75
- .run({ isInOnHandler: true }, async () => onHandler(input.sdk, { ...input.args, data: input.data }, output, context))
86
+ .run({ isInOnHandler: true }, async () => onHandler(input.sdk, { ...input.args, data: input.data }, output, beforeValue, context))
76
87
  .then(() => output)
77
88
  .catch((error) => {
78
- input.eventTarget.dispatchEvent(new IRuntime_1.EventOnHandlerError("error", { error, resource: input.resource, action: input.action }));
89
+ input.eventTarget.dispatchEvent(new IRuntime_1.EventOnHandlerError("error", {
90
+ error,
91
+ resource: input.resource,
92
+ action: input.action
93
+ }));
79
94
  return output;
80
95
  }));
81
96
  }
@@ -83,17 +98,31 @@ async function resolve(input, dbCall, formatQuery, beginTransaction, dialect, mi
83
98
  };
84
99
  return resource.runInAsyncScope(() => consumer(params));
85
100
  }
86
- const p = _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache);
87
101
  const onHandler = input.onHandler;
88
102
  const asyncLocalStorage = input.asyncLocalStorage;
89
- if (typeof onHandler === "function" &&
103
+ const shouldRunOnHandler = typeof onHandler === "function" &&
90
104
  asyncLocalStorage != null &&
91
- asyncLocalStorage?.getStore()?.isInOnHandler !== true) {
105
+ asyncLocalStorage?.getStore()?.isInOnHandler !== true;
106
+ const beforeValue = shouldRunOnHandler && input.passBeforeValueToAfterCallback
107
+ ? await _resolve({
108
+ ...input,
109
+ action: input.action === "updateMany" || input.action === "deleteMany"
110
+ ? "findMany"
111
+ : "findUnique",
112
+ fields: undefined
113
+ }, dbCall, formatQuery, beginTransaction, dialect, context, cache)
114
+ : null;
115
+ const p = _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache);
116
+ if (shouldRunOnHandler) {
92
117
  return p.then((output) => asyncLocalStorage
93
- .run({ isInOnHandler: true }, async () => onHandler(input.sdk, { ...input.args, data: input.data }, output, context))
118
+ .run({ isInOnHandler: true }, async () => onHandler(input.sdk, { ...input.args, data: input.data }, output, beforeValue, context))
94
119
  .then(() => output)
95
120
  .catch((error) => {
96
- input.eventTarget.dispatchEvent(new IRuntime_1.EventOnHandlerError("error", { error, resource: input.resource, action: input.action }));
121
+ input.eventTarget.dispatchEvent(new IRuntime_1.EventOnHandlerError("error", {
122
+ error,
123
+ resource: input.resource,
124
+ action: input.action
125
+ }));
97
126
  return output;
98
127
  }));
99
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.22.2-next.0",
3
+ "version": "0.22.2-next.2",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"