@technicity/data-service-generator 0.22.1 → 0.22.2-next.1
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.
|
@@ -111,6 +111,11 @@ async function generate(input) {
|
|
|
111
111
|
devDependencies: {
|
|
112
112
|
"@types/node": require("../../package.json").devDependencies["@types/node"],
|
|
113
113
|
typescript: require("../../package.json").devDependencies.typescript
|
|
114
|
+
},
|
|
115
|
+
// Not `resolutions` because npm used for install
|
|
116
|
+
overrides: {
|
|
117
|
+
// Fix for: `Cannot find type definition file for 'glob'`
|
|
118
|
+
glob: ">9.0.0"
|
|
114
119
|
}
|
|
115
120
|
};
|
|
116
121
|
const tmpDirPath = path.join(os.tmpdir(),
|
|
@@ -159,8 +164,8 @@ async function generate(input) {
|
|
|
159
164
|
// TODO: workaround for IRuntime.d.ts not being included
|
|
160
165
|
// copyFileSync hangs for some reason, so use writeFileSync + readFileSync instead
|
|
161
166
|
fs.writeFileSync(path.join(tmpBuildOutputPath, "IRuntime.d.ts"), fs.existsSync(path.join(__dirname, "../runtime", "IRuntime.d.ts"))
|
|
162
|
-
? fs.readFileSync(path.join(__dirname, "../runtime", "IRuntime.d.ts"))
|
|
163
|
-
: fs.readFileSync(sourceIRuntimeFilePath));
|
|
167
|
+
? fs.readFileSync(path.join(__dirname, "../runtime", "IRuntime.d.ts"), "utf-8")
|
|
168
|
+
: fs.readFileSync(sourceIRuntimeFilePath, "utf-8"));
|
|
164
169
|
if (dialect === "mysql" && input.outputSqliteSchema) {
|
|
165
170
|
// Since mysql2sqlite outputs a malformed string if a column
|
|
166
171
|
// has the name `enum`, temporarily change the name to something else,
|
|
@@ -247,11 +252,13 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
|
|
|
247
252
|
runtime: IRuntime;
|
|
248
253
|
onHandlerMap: Map<string, TOnHandler>;
|
|
249
254
|
eventTarget: EventTarget;
|
|
255
|
+
passBeforeValueToAfterCallback: boolean;
|
|
250
256
|
|
|
251
257
|
constructor(opts: {
|
|
252
258
|
runtime: any;
|
|
253
259
|
clientOpts: { [k: string]: any; },
|
|
254
|
-
otherOpts?: { [k: string]: any; }
|
|
260
|
+
otherOpts?: { [k: string]: any; },
|
|
261
|
+
passBeforeValueToAfterCallback: boolean,
|
|
255
262
|
}) {
|
|
256
263
|
let otherOpts = opts.otherOpts ?? {};
|
|
257
264
|
if (opts.clientOpts.filename === ":memory:") {
|
|
@@ -264,6 +271,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
|
|
|
264
271
|
: "otherOpts"}, artifacts);
|
|
265
272
|
this.onHandlerMap = new Map();
|
|
266
273
|
this.eventTarget = new EventTarget();
|
|
274
|
+
this.passBeforeValueToAfterCallback = opts.passBeforeValueToAfterCallback;
|
|
267
275
|
}
|
|
268
276
|
|
|
269
277
|
$use(middleware: TMiddleware) {
|
|
@@ -371,10 +379,23 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts,
|
|
|
371
379
|
}))).join("\n\n")}
|
|
372
380
|
|
|
373
381
|
${(await Promise.all(input.flatMap(async (x) => {
|
|
382
|
+
if (x.kind === "postOne") {
|
|
383
|
+
return getMethodSourceOnHandlerPostOne(x);
|
|
384
|
+
}
|
|
374
385
|
if (x.kind === "patchOne") {
|
|
375
386
|
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
376
387
|
return getMethodSourceOnHandlerPatchOne(x, findOnes);
|
|
377
388
|
}
|
|
389
|
+
if (x.kind === "patchList") {
|
|
390
|
+
return getMethodSourceOnHandlerPatchList(x);
|
|
391
|
+
}
|
|
392
|
+
if (x.kind === "deleteOne") {
|
|
393
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
394
|
+
return getMethodSourceOnHandlerDeleteOne(x, findOnes);
|
|
395
|
+
}
|
|
396
|
+
if (x.kind === "deleteList") {
|
|
397
|
+
return getMethodSourceOnHandlerDeleteList(x);
|
|
398
|
+
}
|
|
378
399
|
})))
|
|
379
400
|
.filter(Boolean)
|
|
380
401
|
.join("\n\n")}
|
|
@@ -495,7 +516,8 @@ function getMethodSourceGetOne(x, findOnes, isTransaction) {
|
|
|
495
516
|
context: param2?.context,
|
|
496
517
|
skipCache: param2?.skipCache,
|
|
497
518
|
eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
|
|
498
|
-
${isTransaction ? "dbCall" : ""}
|
|
519
|
+
${isTransaction ? "dbCall," : ""}
|
|
520
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
499
521
|
}
|
|
500
522
|
);
|
|
501
523
|
}`;
|
|
@@ -516,7 +538,8 @@ function getMethodSourceGetList(x, isTransaction) {
|
|
|
516
538
|
context: param2?.context,
|
|
517
539
|
skipCache: param2?.skipCache,
|
|
518
540
|
eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
|
|
519
|
-
${isTransaction ? "dbCall" : ""}
|
|
541
|
+
${isTransaction ? "dbCall," : ""}
|
|
542
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
520
543
|
}
|
|
521
544
|
);
|
|
522
545
|
}`;
|
|
@@ -537,7 +560,8 @@ function getMethodSourceGetListPaginated(x, isTransaction) {
|
|
|
537
560
|
context: param2?.context,
|
|
538
561
|
skipCache: param2?.skipCache,
|
|
539
562
|
eventTarget: ${isTransaction ? "eventTarget" : "this.eventTarget"},
|
|
540
|
-
${isTransaction ? "dbCall" : ""}
|
|
563
|
+
${isTransaction ? "dbCall," : ""}
|
|
564
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
541
565
|
}
|
|
542
566
|
);
|
|
543
567
|
}`;
|
|
@@ -559,10 +583,21 @@ function getMethodSourcePostOne(x, specialCaseUuidColumn, isTransaction) {
|
|
|
559
583
|
onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
|
|
560
584
|
asyncLocalStorage,
|
|
561
585
|
sdk: this,
|
|
562
|
-
${isTransaction ? "dbCall" : ""}
|
|
586
|
+
${isTransaction ? "dbCall," : ""}
|
|
587
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
563
588
|
});
|
|
564
589
|
}`;
|
|
565
590
|
}
|
|
591
|
+
function getMethodSourceOnHandlerPostOne(x) {
|
|
592
|
+
return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
|
|
593
|
+
(sdk: InstanceType<typeof SDK>, input: { data: ${x.typeDataName} },
|
|
594
|
+
output: Partial<${getTypeReturnName(x.table)}>,
|
|
595
|
+
context: TContext,
|
|
596
|
+
) => Promise<void>
|
|
597
|
+
): void {
|
|
598
|
+
this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
|
|
599
|
+
}`;
|
|
600
|
+
}
|
|
566
601
|
function getMethodSourcePatchOne(x, findOnes, isTransaction) {
|
|
567
602
|
const param2 = `{ ${keyFields}?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }`;
|
|
568
603
|
return `async ${x.methodName}<T extends ${param2}>(
|
|
@@ -584,7 +619,8 @@ return ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
|
584
619
|
onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
|
|
585
620
|
asyncLocalStorage,
|
|
586
621
|
sdk: this,
|
|
587
|
-
${isTransaction ? "dbCall" : ""}
|
|
622
|
+
${isTransaction ? "dbCall," : ""}
|
|
623
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
588
624
|
});
|
|
589
625
|
}`;
|
|
590
626
|
}
|
|
@@ -593,7 +629,9 @@ function getMethodSourceOnHandlerPatchOne(x, findOnes) {
|
|
|
593
629
|
(sdk: InstanceType<typeof SDK>, input: { $where: ${findOnes
|
|
594
630
|
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
595
631
|
.join(" | ")}, data: ${x.typeDataName} },
|
|
596
|
-
output: Partial<${getTypeReturnName(x.table)}
|
|
632
|
+
output: Partial<${getTypeReturnName(x.table)}>,
|
|
633
|
+
before: ${getTypeReturnName(x.table)} | null,
|
|
634
|
+
context: TContext,
|
|
597
635
|
) => Promise<void>
|
|
598
636
|
): void {
|
|
599
637
|
this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
|
|
@@ -618,10 +656,22 @@ function getMethodSourcePatchList(x, isTransaction) {
|
|
|
618
656
|
onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
|
|
619
657
|
asyncLocalStorage,
|
|
620
658
|
sdk: this,
|
|
621
|
-
${isTransaction ? "dbCall" : ""}
|
|
659
|
+
${isTransaction ? "dbCall," : ""}
|
|
660
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
622
661
|
});
|
|
623
662
|
}`;
|
|
624
663
|
}
|
|
664
|
+
function getMethodSourceOnHandlerPatchList(x) {
|
|
665
|
+
return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
|
|
666
|
+
(sdk: InstanceType<typeof SDK>, input: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, data: ${x.typeDataName} },
|
|
667
|
+
output: Partial<${getTypeReturnName(x.table)}>[],
|
|
668
|
+
before: ${getTypeReturnName(x.table)}[] | null,
|
|
669
|
+
context: TContext,
|
|
670
|
+
) => Promise<void>
|
|
671
|
+
): void {
|
|
672
|
+
this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
|
|
673
|
+
}`;
|
|
674
|
+
}
|
|
625
675
|
function getMethodSourceDeleteOne(x, findOnes, isTransaction) {
|
|
626
676
|
return `async ${x.methodName}(
|
|
627
677
|
param1: ${findOnes
|
|
@@ -639,10 +689,23 @@ function getMethodSourceDeleteOne(x, findOnes, isTransaction) {
|
|
|
639
689
|
onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
|
|
640
690
|
asyncLocalStorage,
|
|
641
691
|
sdk: this,
|
|
642
|
-
${isTransaction ? "dbCall" : ""}
|
|
692
|
+
${isTransaction ? "dbCall," : ""}
|
|
693
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
643
694
|
});
|
|
644
695
|
}`;
|
|
645
696
|
}
|
|
697
|
+
function getMethodSourceOnHandlerDeleteOne(x, findOnes) {
|
|
698
|
+
return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
|
|
699
|
+
(sdk: InstanceType<typeof SDK>, input: { $where: ${findOnes
|
|
700
|
+
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
701
|
+
.join(" | ")}, },
|
|
702
|
+
output: void,
|
|
703
|
+
context: TContext,
|
|
704
|
+
) => Promise<void>
|
|
705
|
+
): void {
|
|
706
|
+
this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
|
|
707
|
+
}`;
|
|
708
|
+
}
|
|
646
709
|
function getMethodSourceDeleteList(x, isTransaction) {
|
|
647
710
|
return `async ${x.methodName}(
|
|
648
711
|
param1: { $where?: ${x.typeWhereName} },
|
|
@@ -658,10 +721,22 @@ function getMethodSourceDeleteList(x, isTransaction) {
|
|
|
658
721
|
onHandler: ${isTransaction ? "onHandlerMap" : "this.onHandlerMap"}.get("${mapKindToAction(x.kind)}-${x.table}"),
|
|
659
722
|
asyncLocalStorage,
|
|
660
723
|
sdk: this,
|
|
661
|
-
${isTransaction ? "dbCall" : ""}
|
|
724
|
+
${isTransaction ? "dbCall," : ""}
|
|
725
|
+
passBeforeValueToAfterCallback: this.passBeforeValueToAfterCallback,
|
|
662
726
|
});
|
|
663
727
|
}`;
|
|
664
728
|
}
|
|
729
|
+
function getMethodSourceOnHandlerDeleteList(x) {
|
|
730
|
+
return `on${(0, capitalizeFirstLetter_1.capitalizeFirstLetter)(x.methodName)}(handler:
|
|
731
|
+
(sdk: InstanceType<typeof SDK>, input: { $where?: ${x.typeWhereName} },
|
|
732
|
+
output: void,
|
|
733
|
+
before: ${getTypeReturnName(x.table)}[] | null,
|
|
734
|
+
context: TContext,
|
|
735
|
+
) => Promise<void>
|
|
736
|
+
): void {
|
|
737
|
+
this.onHandlerMap.set("${mapKindToAction(x.kind)}-${x.table}", handler);
|
|
738
|
+
}`;
|
|
739
|
+
}
|
|
665
740
|
function mapKindToAction(kind) {
|
|
666
741
|
if (kind === "getOne") {
|
|
667
742
|
return "findUnique";
|
|
@@ -27,13 +27,18 @@ 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) => 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;
|
|
36
|
+
resource: string;
|
|
37
|
+
action: TAction;
|
|
35
38
|
constructor(message: string, data: ConstructorParameters<typeof Event>[1] & {
|
|
36
39
|
error: T;
|
|
40
|
+
resource: string;
|
|
41
|
+
action: TAction;
|
|
37
42
|
});
|
|
38
43
|
}
|
|
39
44
|
export type TContext = {
|
package/dist/runtime/IRuntime.js
CHANGED
|
@@ -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
|
-
|
|
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))
|
|
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", {
|
|
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
|
-
|
|
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))
|
|
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", {
|
|
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
|
}
|