@technicity/data-service-generator 0.12.0-next.0 → 0.12.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.
- package/README.md +7 -0
- package/dist/generation/generate.js +427 -244
- package/dist/getFakeData.d.ts +12 -0
- package/dist/getFakeData.js +90 -0
- package/dist/getIsList.d.ts +1 -0
- package/dist/getIsList.js +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/runtime/Cache.d.ts +16 -6
- package/dist/runtime/Cache.js +78 -29
- package/dist/runtime/IRuntime.d.ts +30 -3
- package/dist/runtime/RuntimeKSQL.d.ts +7 -0
- package/dist/runtime/RuntimeKSQL.js +22 -15
- package/dist/runtime/RuntimeMSSQL.d.ts +7 -1
- package/dist/runtime/RuntimeMSSQL.js +4 -4
- package/dist/runtime/RuntimeMySQL.d.ts +5 -2
- package/dist/runtime/RuntimeMySQL.js +13 -9
- package/dist/runtime/Stats.d.ts +12 -0
- package/dist/runtime/Stats.js +32 -0
- package/dist/runtime/lib/MSSQL.d.ts +2 -1
- package/dist/runtime/lib/MSSQL.js +11 -6
- package/dist/runtime/lib/MySQL.d.ts +1 -1
- package/dist/runtime/lib/MySQL.js +15 -2
- package/dist/runtime/lib/shared.js +99 -37
- package/dist/runtime/lib/stringifyWhere.js +1 -1
- package/package.json +7 -5
|
@@ -25,7 +25,7 @@ const MySQL_1 = require("../runtime/lib/MySQL");
|
|
|
25
25
|
let query = undefined;
|
|
26
26
|
let dialect = "mysql";
|
|
27
27
|
const json2TsOpts = {
|
|
28
|
-
bannerComment: ""
|
|
28
|
+
bannerComment: ""
|
|
29
29
|
};
|
|
30
30
|
async function generate(input) {
|
|
31
31
|
if (input.tables != null && input.excludeTables != null) {
|
|
@@ -57,7 +57,7 @@ async function generate(input) {
|
|
|
57
57
|
getPatchOneData(x, specialCaseUuidColumn, includeMappedFields),
|
|
58
58
|
getPatchListData(x),
|
|
59
59
|
getDeleteOneData(x),
|
|
60
|
-
getDeleteListData(x)
|
|
60
|
+
getDeleteListData(x)
|
|
61
61
|
]));
|
|
62
62
|
const artifactsSource = await getArtifactsSource(tables, includeMappedFields, specialCaseUuidColumn);
|
|
63
63
|
const sdkSource = await getSDKSource(data, specialCaseUuidColumn, supplementClientOpts);
|
|
@@ -70,9 +70,9 @@ async function generate(input) {
|
|
|
70
70
|
moduleResolution: "node",
|
|
71
71
|
target: "es2020",
|
|
72
72
|
declaration: true,
|
|
73
|
-
outDir: "./sdk-ts"
|
|
73
|
+
outDir: "./sdk-ts"
|
|
74
74
|
},
|
|
75
|
-
include: [sdkFilename, artifactsFilename, IRuntimeFilename]
|
|
75
|
+
include: [sdkFilename, artifactsFilename, IRuntimeFilename]
|
|
76
76
|
};
|
|
77
77
|
const packageJSON = {
|
|
78
78
|
name: "temp",
|
|
@@ -81,8 +81,8 @@ async function generate(input) {
|
|
|
81
81
|
dependencies: require("../../package.json").dependencies,
|
|
82
82
|
devDependencies: {
|
|
83
83
|
"@types/node": require("../../package.json").devDependencies["@types/node"],
|
|
84
|
-
typescript: require("../../package.json").devDependencies.typescript
|
|
85
|
-
}
|
|
84
|
+
typescript: require("../../package.json").devDependencies.typescript
|
|
85
|
+
}
|
|
86
86
|
};
|
|
87
87
|
const tmpDirPath = path.join(os.tmpdir(), `dsg-${(0, uuid_1.v4)()}`);
|
|
88
88
|
fse.mkdirpSync(tmpDirPath);
|
|
@@ -145,7 +145,7 @@ function init(input) {
|
|
|
145
145
|
password,
|
|
146
146
|
host,
|
|
147
147
|
port,
|
|
148
|
-
database
|
|
148
|
+
database
|
|
149
149
|
});
|
|
150
150
|
query = mysql.query.bind(mysql);
|
|
151
151
|
}
|
|
@@ -155,7 +155,7 @@ function init(input) {
|
|
|
155
155
|
user,
|
|
156
156
|
password,
|
|
157
157
|
port,
|
|
158
|
-
database
|
|
158
|
+
database
|
|
159
159
|
});
|
|
160
160
|
const poolConnect = pool.connect();
|
|
161
161
|
async function runMSSQLQuery(...input) {
|
|
@@ -179,7 +179,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts)
|
|
|
179
179
|
"typeReturnBaseName",
|
|
180
180
|
"typeWhereName",
|
|
181
181
|
"typeOrderByName",
|
|
182
|
-
"typeDataName"
|
|
182
|
+
"typeDataName"
|
|
183
183
|
]) {
|
|
184
184
|
const str = d[k];
|
|
185
185
|
if (str) {
|
|
@@ -191,6 +191,21 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts)
|
|
|
191
191
|
.concat(Array.from(set).sort())
|
|
192
192
|
.join(",\n")} } from "./types";`;
|
|
193
193
|
}
|
|
194
|
+
function getTypeNullResponses() {
|
|
195
|
+
return `type TNullResponses = { ${Object.entries(_.groupBy((x) => x.table, input))
|
|
196
|
+
.flatMap(([table, xx]) => {
|
|
197
|
+
const name = table;
|
|
198
|
+
return (`${name}: {` +
|
|
199
|
+
xx
|
|
200
|
+
.map(({ kind }) => {
|
|
201
|
+
const operation = mapKindToAction(kind);
|
|
202
|
+
return `${operation}: (input: { args: { select: Fields${table} | undefined, where: Where${table} | undefined }, model: "${table}", operation: "${operation}" }) => Promise<ReturnBase${table}>`;
|
|
203
|
+
})
|
|
204
|
+
.join(",") +
|
|
205
|
+
" } ");
|
|
206
|
+
})
|
|
207
|
+
.join(",")} }`;
|
|
208
|
+
}
|
|
194
209
|
const src = `import type { IRuntime, TMiddleware, TContext } from "./IRuntime"
|
|
195
210
|
import { artifacts } from "./artifacts";
|
|
196
211
|
|
|
@@ -198,16 +213,33 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts)
|
|
|
198
213
|
|
|
199
214
|
export class SDK {
|
|
200
215
|
runtime: IRuntime;
|
|
216
|
+
_nullability: TNullResponses | undefined;
|
|
201
217
|
|
|
218
|
+
static create(opts: {
|
|
219
|
+
runtime: any;
|
|
220
|
+
clientOpts: { [k: string]: any; },
|
|
221
|
+
otherOpts?: { [k: string]: any; }
|
|
222
|
+
}) {
|
|
223
|
+
return new this(opts);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static createNull(opts: {
|
|
227
|
+
runtime: any;
|
|
228
|
+
query?: TNullResponses
|
|
229
|
+
}) {
|
|
230
|
+
return new this({ runtime: opts.runtime, clientOpts: {}, otherOpts: { nullability: opts.query } });
|
|
231
|
+
}
|
|
232
|
+
|
|
202
233
|
constructor(opts: {
|
|
203
234
|
runtime: any;
|
|
204
235
|
clientOpts: { [k: string]: any; },
|
|
205
236
|
otherOpts?: { [k: string]: any; }
|
|
206
237
|
}) {
|
|
207
|
-
|
|
238
|
+
const { nullability, ...otherOpts } = opts.otherOpts ?? {};
|
|
208
239
|
this.runtime = new opts.runtime(opts.clientOpts, ${supplementClientOpts === true
|
|
209
|
-
? "{ supplementClientOpts: true, ...
|
|
210
|
-
: "
|
|
240
|
+
? "{ supplementClientOpts: true, ...otherOpts }"
|
|
241
|
+
: "otherOpts"}, artifacts);
|
|
242
|
+
this._nullability = nullability;
|
|
211
243
|
}
|
|
212
244
|
|
|
213
245
|
$use(middleware: TMiddleware) {
|
|
@@ -233,159 +265,278 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts)
|
|
|
233
265
|
return this.runtime.$shutdown();
|
|
234
266
|
}
|
|
235
267
|
|
|
268
|
+
async $startTransaction(input?: {
|
|
269
|
+
isolationLevel?:
|
|
270
|
+
| "READ UNCOMMITTED"
|
|
271
|
+
| "READ COMMITTED"
|
|
272
|
+
| "REPEATABLE READ"
|
|
273
|
+
| "SERIALIZABLE"
|
|
274
|
+
}) {
|
|
275
|
+
const { dbCall, commit, rollback } = await this.runtime.$startTransaction(input);
|
|
276
|
+
const runtime = this.runtime;
|
|
277
|
+
return {
|
|
278
|
+
$commit: commit,
|
|
279
|
+
$rollback: rollback,
|
|
280
|
+
${(await Promise.all(input.flatMap(async (x) => {
|
|
281
|
+
if (x.kind === "getOne") {
|
|
282
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
283
|
+
return getMethodSourceGetOne(x, findOnes, true);
|
|
284
|
+
}
|
|
285
|
+
if (x.kind === "getList") {
|
|
286
|
+
return getMethodSourceGetList(x, true);
|
|
287
|
+
}
|
|
288
|
+
if (x.kind === "getListPaginated") {
|
|
289
|
+
return getMethodSourceGetListPaginated(x, true);
|
|
290
|
+
}
|
|
291
|
+
if (x.kind === "postOne") {
|
|
292
|
+
return getMethodSourcePostOne(x, specialCaseUuidColumn, true);
|
|
293
|
+
}
|
|
294
|
+
if (x.kind === "patchOne") {
|
|
295
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
296
|
+
return getMethodSourcePatchOne(x, findOnes, true);
|
|
297
|
+
}
|
|
298
|
+
if (x.kind === "patchList") {
|
|
299
|
+
return getMethodSourcePatchList(x, true);
|
|
300
|
+
}
|
|
301
|
+
if (x.kind === "deleteOne") {
|
|
302
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
303
|
+
return getMethodSourceDeleteOne(x, findOnes, true);
|
|
304
|
+
}
|
|
305
|
+
if (x.kind === "deleteList") {
|
|
306
|
+
return getMethodSourceDeleteList(x, true);
|
|
307
|
+
}
|
|
308
|
+
}))).join(",\n")}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
236
312
|
${(await Promise.all(input.flatMap(async (x) => {
|
|
237
|
-
let findOnes = [];
|
|
238
|
-
const primaryColumn = await getPrimaryColumn(x.table);
|
|
239
|
-
const uniqueColumns = await getUniqueColumns(x.table, specialCaseUuidColumn);
|
|
240
|
-
findOnes = findOnes
|
|
241
|
-
.concat([primaryColumn])
|
|
242
|
-
.concat(uniqueColumns)
|
|
243
|
-
.map((x) => ({
|
|
244
|
-
...x,
|
|
245
|
-
type: x.type === "integer" ? "number" : x.type,
|
|
246
|
-
}));
|
|
247
313
|
if (x.kind === "getOne") {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
251
|
-
.join(" | ")},
|
|
252
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
253
|
-
): Promise<${x.typeReturnBaseName}> {
|
|
254
|
-
return this.runtime.resolve(
|
|
255
|
-
{
|
|
256
|
-
resource: "${x.table}",
|
|
257
|
-
action: "findUnique",
|
|
258
|
-
args: { $where: param1 },
|
|
259
|
-
fields: param2?.fields,
|
|
260
|
-
artifacts,
|
|
261
|
-
context: param2?.context,
|
|
262
|
-
skipCache: param2?.skipCache
|
|
263
|
-
}
|
|
264
|
-
);
|
|
265
|
-
}`;
|
|
314
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
315
|
+
return getMethodSourceGetOne(x, findOnes, false);
|
|
266
316
|
}
|
|
267
317
|
if (x.kind === "getList") {
|
|
268
|
-
return
|
|
269
|
-
param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $limit?: number },
|
|
270
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
271
|
-
): Promise<Array<${x.typeReturnBaseName}>> {
|
|
272
|
-
return this.runtime.resolve(
|
|
273
|
-
{
|
|
274
|
-
resource: "${x.table}",
|
|
275
|
-
action: "findMany",
|
|
276
|
-
args: param1,
|
|
277
|
-
fields: param2?.fields,
|
|
278
|
-
artifacts,
|
|
279
|
-
context: param2?.context,
|
|
280
|
-
skipCache: param2?.skipCache
|
|
281
|
-
}
|
|
282
|
-
);
|
|
283
|
-
}`;
|
|
318
|
+
return getMethodSourceGetList(x, false);
|
|
284
319
|
}
|
|
285
320
|
if (x.kind === "getListPaginated") {
|
|
286
|
-
return
|
|
287
|
-
param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $paginate: Paginate },
|
|
288
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
289
|
-
): Promise<ListPaginated<${x.typeReturnBaseName}>> {
|
|
290
|
-
return this.runtime.resolve(
|
|
291
|
-
{
|
|
292
|
-
resource: "${x.table}",
|
|
293
|
-
action: "findManyPaginated",
|
|
294
|
-
args: param1,
|
|
295
|
-
fields: param2?.fields,
|
|
296
|
-
artifacts,
|
|
297
|
-
context: param2?.context,
|
|
298
|
-
skipCache: param2?.skipCache
|
|
299
|
-
}
|
|
300
|
-
);
|
|
301
|
-
}`;
|
|
321
|
+
return getMethodSourceGetListPaginated(x, false);
|
|
302
322
|
}
|
|
303
323
|
if (x.kind === "postOne") {
|
|
304
|
-
return
|
|
305
|
-
data: ${x.typeDataName},
|
|
306
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
307
|
-
): Promise<${x.typeReturnBaseName}> {
|
|
308
|
-
return this.runtime.resolve({
|
|
309
|
-
resource: "${x.table}",
|
|
310
|
-
action: "create",
|
|
311
|
-
data,
|
|
312
|
-
artifacts,
|
|
313
|
-
fields: param2?.fields,
|
|
314
|
-
context: {...param2?.context, specialCaseUuidColumn: ${JSON.stringify(specialCaseUuidColumn)}}
|
|
315
|
-
});
|
|
316
|
-
}`;
|
|
324
|
+
return getMethodSourcePostOne(x, specialCaseUuidColumn, false);
|
|
317
325
|
}
|
|
318
326
|
if (x.kind === "patchOne") {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
322
|
-
.join(" | ")},
|
|
323
|
-
data: ${x.typeDataName},
|
|
324
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
325
|
-
): Promise<${x.typeReturnBaseName}> {
|
|
326
|
-
return this.runtime.resolve({
|
|
327
|
-
resource: "${x.table}",
|
|
328
|
-
action: "update",
|
|
329
|
-
args: { $where: param1 },
|
|
330
|
-
data,
|
|
331
|
-
artifacts,
|
|
332
|
-
fields: param2?.fields,
|
|
333
|
-
context: param2?.context
|
|
334
|
-
});
|
|
335
|
-
}`;
|
|
327
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
328
|
+
return getMethodSourcePatchOne(x, findOnes, false);
|
|
336
329
|
}
|
|
337
330
|
if (x.kind === "patchList") {
|
|
338
|
-
return
|
|
339
|
-
param1: { $where?: ${x.typeWhereName} },
|
|
340
|
-
data: ${x.typeDataName},
|
|
341
|
-
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
342
|
-
): Promise<Array<${x.typeReturnBaseName}>> {
|
|
343
|
-
return this.runtime.resolve({
|
|
344
|
-
resource: "${x.table}",
|
|
345
|
-
action: "updateMany",
|
|
346
|
-
args: param1,
|
|
347
|
-
data,
|
|
348
|
-
artifacts,
|
|
349
|
-
fields: param2?.fields,
|
|
350
|
-
context: param2?.context
|
|
351
|
-
});
|
|
352
|
-
}`;
|
|
331
|
+
return getMethodSourcePatchList(x, false);
|
|
353
332
|
}
|
|
354
333
|
if (x.kind === "deleteOne") {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
358
|
-
.join(" | ")},
|
|
359
|
-
param2?: { correlationId?: string, context?: TContext }
|
|
360
|
-
): Promise<void> {
|
|
361
|
-
await this.runtime.resolve({
|
|
362
|
-
resource: "${x.table}",
|
|
363
|
-
action: "delete",
|
|
364
|
-
args: { $where: param1 },
|
|
365
|
-
artifacts,
|
|
366
|
-
context: param2?.context
|
|
367
|
-
});
|
|
368
|
-
}`;
|
|
334
|
+
const findOnes = await getFindOnes(x, specialCaseUuidColumn);
|
|
335
|
+
return getMethodSourceDeleteOne(x, findOnes, false);
|
|
369
336
|
}
|
|
370
337
|
if (x.kind === "deleteList") {
|
|
371
|
-
return
|
|
372
|
-
param1: { $where?: ${x.typeWhereName} },
|
|
373
|
-
param2?: { correlationId?: string, context?: TContext }
|
|
374
|
-
): Promise<void> {
|
|
375
|
-
await this.runtime.resolve({
|
|
376
|
-
resource: "${x.table}",
|
|
377
|
-
action: "deleteMany",
|
|
378
|
-
args: param1,
|
|
379
|
-
artifacts,
|
|
380
|
-
context: param2?.context
|
|
381
|
-
});
|
|
382
|
-
}`;
|
|
338
|
+
return getMethodSourceDeleteList(x, false);
|
|
383
339
|
}
|
|
384
340
|
}))).join("\n\n")}
|
|
385
341
|
}
|
|
342
|
+
|
|
343
|
+
${getTypeNullResponses()}
|
|
386
344
|
`;
|
|
387
345
|
return prettier.format(src, { parser: "typescript" });
|
|
388
346
|
}
|
|
347
|
+
async function getFindOnes(x, specialCaseUuidColumn) {
|
|
348
|
+
let findOnes = [];
|
|
349
|
+
const primaryColumn = await getPrimaryColumn(x.table);
|
|
350
|
+
const uniqueColumns = await getUniqueColumns(x.table, specialCaseUuidColumn);
|
|
351
|
+
findOnes = findOnes
|
|
352
|
+
.concat([primaryColumn])
|
|
353
|
+
.concat(uniqueColumns)
|
|
354
|
+
.map((x) => ({
|
|
355
|
+
...x,
|
|
356
|
+
type: x.type === "integer" ? "number" : x.type
|
|
357
|
+
}));
|
|
358
|
+
return findOnes;
|
|
359
|
+
}
|
|
360
|
+
function getMethodSourceGetOne(x, findOnes, isTransaction) {
|
|
361
|
+
return `async ${x.methodName}(
|
|
362
|
+
param1: ${findOnes
|
|
363
|
+
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
364
|
+
.join(" | ")},
|
|
365
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
366
|
+
): Promise<${x.typeReturnBaseName}> {
|
|
367
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve(
|
|
368
|
+
{
|
|
369
|
+
resource: "${x.table}",
|
|
370
|
+
action: "${mapKindToAction(x.kind)}",
|
|
371
|
+
args: { $where: param1 },
|
|
372
|
+
fields: param2?.fields,
|
|
373
|
+
artifacts,
|
|
374
|
+
context: param2?.context,
|
|
375
|
+
skipCache: param2?.skipCache,
|
|
376
|
+
nullability: this._nullability,
|
|
377
|
+
${isTransaction ? "dbCall" : ""}
|
|
378
|
+
}
|
|
379
|
+
);
|
|
380
|
+
}`;
|
|
381
|
+
}
|
|
382
|
+
function getMethodSourceGetList(x, isTransaction) {
|
|
383
|
+
return `async ${x.methodName}(
|
|
384
|
+
param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $limit?: number },
|
|
385
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
386
|
+
): Promise<Array<${x.typeReturnBaseName}>> {
|
|
387
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve(
|
|
388
|
+
{
|
|
389
|
+
resource: "${x.table}",
|
|
390
|
+
action: "${mapKindToAction(x.kind)}",
|
|
391
|
+
args: param1,
|
|
392
|
+
fields: param2?.fields,
|
|
393
|
+
artifacts,
|
|
394
|
+
context: param2?.context,
|
|
395
|
+
skipCache: param2?.skipCache,
|
|
396
|
+
nullability: this._nullability,
|
|
397
|
+
${isTransaction ? "dbCall" : ""}
|
|
398
|
+
}
|
|
399
|
+
);
|
|
400
|
+
}`;
|
|
401
|
+
}
|
|
402
|
+
function getMethodSourceGetListPaginated(x, isTransaction) {
|
|
403
|
+
return `async ${x.methodName}(
|
|
404
|
+
param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $paginate: Paginate },
|
|
405
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, skipCache?: boolean, context?: TContext }
|
|
406
|
+
): Promise<ListPaginated<${x.typeReturnBaseName}>> {
|
|
407
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve(
|
|
408
|
+
{
|
|
409
|
+
resource: "${x.table}",
|
|
410
|
+
action: "${mapKindToAction(x.kind)}",
|
|
411
|
+
args: param1,
|
|
412
|
+
fields: param2?.fields,
|
|
413
|
+
artifacts,
|
|
414
|
+
context: param2?.context,
|
|
415
|
+
skipCache: param2?.skipCache,
|
|
416
|
+
nullability: this._nullability,
|
|
417
|
+
${isTransaction ? "dbCall" : ""}
|
|
418
|
+
}
|
|
419
|
+
);
|
|
420
|
+
}`;
|
|
421
|
+
}
|
|
422
|
+
function getMethodSourcePostOne(x, specialCaseUuidColumn, isTransaction) {
|
|
423
|
+
return `async ${x.methodName}(
|
|
424
|
+
data: ${x.typeDataName},
|
|
425
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
426
|
+
): Promise<${x.typeReturnBaseName}> {
|
|
427
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
428
|
+
resource: "${x.table}",
|
|
429
|
+
action: "${mapKindToAction(x.kind)}",
|
|
430
|
+
data,
|
|
431
|
+
artifacts,
|
|
432
|
+
fields: param2?.fields,
|
|
433
|
+
context: {...param2?.context, specialCaseUuidColumn: ${JSON.stringify(specialCaseUuidColumn)}},
|
|
434
|
+
nullability: this._nullability,
|
|
435
|
+
${isTransaction ? "dbCall" : ""}
|
|
436
|
+
});
|
|
437
|
+
}`;
|
|
438
|
+
}
|
|
439
|
+
function getMethodSourcePatchOne(x, findOnes, isTransaction) {
|
|
440
|
+
return `async ${x.methodName}(
|
|
441
|
+
param1: ${findOnes
|
|
442
|
+
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
443
|
+
.join(" | ")},
|
|
444
|
+
data: ${x.typeDataName},
|
|
445
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
446
|
+
): Promise<${x.typeReturnBaseName}> {
|
|
447
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
448
|
+
resource: "${x.table}",
|
|
449
|
+
action: "${mapKindToAction(x.kind)}",
|
|
450
|
+
args: { $where: param1 },
|
|
451
|
+
data,
|
|
452
|
+
artifacts,
|
|
453
|
+
fields: param2?.fields,
|
|
454
|
+
context: param2?.context,
|
|
455
|
+
nullability: this._nullability,
|
|
456
|
+
${isTransaction ? "dbCall" : ""}
|
|
457
|
+
});
|
|
458
|
+
}`;
|
|
459
|
+
}
|
|
460
|
+
function getMethodSourcePatchList(x, isTransaction) {
|
|
461
|
+
return `async ${x.methodName}(
|
|
462
|
+
param1: { $where?: ${x.typeWhereName} },
|
|
463
|
+
data: ${x.typeDataName},
|
|
464
|
+
param2?: { fields?: ${x.typeFieldsName}, correlationId?: string, context?: TContext }
|
|
465
|
+
): Promise<Array<${x.typeReturnBaseName}>> {
|
|
466
|
+
return ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
467
|
+
resource: "${x.table}",
|
|
468
|
+
action: "${mapKindToAction(x.kind)}",
|
|
469
|
+
args: param1,
|
|
470
|
+
data,
|
|
471
|
+
artifacts,
|
|
472
|
+
fields: param2?.fields,
|
|
473
|
+
context: param2?.context,
|
|
474
|
+
nullability: this._nullability,
|
|
475
|
+
${isTransaction ? "dbCall" : ""}
|
|
476
|
+
});
|
|
477
|
+
}`;
|
|
478
|
+
}
|
|
479
|
+
function getMethodSourceDeleteOne(x, findOnes, isTransaction) {
|
|
480
|
+
return `async ${x.methodName}(
|
|
481
|
+
param1: ${findOnes
|
|
482
|
+
.map((findOne) => `{ ${findOne.name}: ${findOne.type}${findOne.nullable ? " | null" : ""} }`)
|
|
483
|
+
.join(" | ")},
|
|
484
|
+
param2?: { correlationId?: string, context?: TContext }
|
|
485
|
+
): Promise<void> {
|
|
486
|
+
await ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
487
|
+
resource: "${x.table}",
|
|
488
|
+
action: "${mapKindToAction(x.kind)}",
|
|
489
|
+
args: { $where: param1 },
|
|
490
|
+
artifacts,
|
|
491
|
+
context: param2?.context,
|
|
492
|
+
nullability: this._nullability,
|
|
493
|
+
${isTransaction ? "dbCall" : ""}
|
|
494
|
+
});
|
|
495
|
+
}`;
|
|
496
|
+
}
|
|
497
|
+
function getMethodSourceDeleteList(x, isTransaction) {
|
|
498
|
+
return `async ${x.methodName}(
|
|
499
|
+
param1: { $where?: ${x.typeWhereName} },
|
|
500
|
+
param2?: { correlationId?: string, context?: TContext }
|
|
501
|
+
): Promise<void> {
|
|
502
|
+
await ${isTransaction ? "runtime" : "this.runtime"}.resolve({
|
|
503
|
+
resource: "${x.table}",
|
|
504
|
+
action: "${mapKindToAction(x.kind)}",
|
|
505
|
+
args: param1,
|
|
506
|
+
artifacts,
|
|
507
|
+
context: param2?.context,
|
|
508
|
+
nullability: this._nullability,
|
|
509
|
+
${isTransaction ? "dbCall" : ""}
|
|
510
|
+
});
|
|
511
|
+
}`;
|
|
512
|
+
}
|
|
513
|
+
function mapKindToAction(kind) {
|
|
514
|
+
if (kind === "getOne") {
|
|
515
|
+
return "findUnique";
|
|
516
|
+
}
|
|
517
|
+
if (kind === "getList") {
|
|
518
|
+
return "findMany";
|
|
519
|
+
}
|
|
520
|
+
if (kind === "getListPaginated") {
|
|
521
|
+
return "findManyPaginated";
|
|
522
|
+
}
|
|
523
|
+
if (kind === "postOne") {
|
|
524
|
+
return "create";
|
|
525
|
+
}
|
|
526
|
+
if (kind === "patchOne") {
|
|
527
|
+
return "update";
|
|
528
|
+
}
|
|
529
|
+
if (kind === "patchList") {
|
|
530
|
+
return "updateMany";
|
|
531
|
+
}
|
|
532
|
+
if (kind === "deleteOne") {
|
|
533
|
+
return "delete";
|
|
534
|
+
}
|
|
535
|
+
if (kind === "deleteList") {
|
|
536
|
+
return "deleteMany";
|
|
537
|
+
}
|
|
538
|
+
throw new Error(`Unhandled kind: ${kind}`);
|
|
539
|
+
}
|
|
389
540
|
function getTypeReturnBaseName(table) {
|
|
390
541
|
return "ReturnBase" + changeCase.pascalCase(table);
|
|
391
542
|
}
|
|
@@ -411,7 +562,7 @@ async function getGetOneData(table, includeMappedFields) {
|
|
|
411
562
|
typeFields: await getTypeFields(table, typeFieldsName, includeMappedFields),
|
|
412
563
|
typeFieldsName,
|
|
413
564
|
typeReturnBase: await getTypeReturnBase(table, typeReturnBaseName, includeMappedFields),
|
|
414
|
-
typeReturnBaseName
|
|
565
|
+
typeReturnBaseName
|
|
415
566
|
};
|
|
416
567
|
}
|
|
417
568
|
async function getGetListData(table) {
|
|
@@ -430,7 +581,7 @@ async function getGetListData(table) {
|
|
|
430
581
|
typeWhere,
|
|
431
582
|
typeWhereName,
|
|
432
583
|
typeOrderBy,
|
|
433
|
-
typeOrderByName
|
|
584
|
+
typeOrderByName
|
|
434
585
|
};
|
|
435
586
|
}
|
|
436
587
|
async function getGetListPaginatedData(table) {
|
|
@@ -445,7 +596,7 @@ async function getGetListPaginatedData(table) {
|
|
|
445
596
|
typeFieldsName,
|
|
446
597
|
typeReturnBaseName,
|
|
447
598
|
typeWhereName,
|
|
448
|
-
typeOrderByName
|
|
599
|
+
typeOrderByName
|
|
449
600
|
};
|
|
450
601
|
}
|
|
451
602
|
async function getPostOneData(table, specialCaseUuidColumn, includeMappedFields) {
|
|
@@ -459,7 +610,7 @@ async function getPostOneData(table, specialCaseUuidColumn, includeMappedFields)
|
|
|
459
610
|
typeFieldsName,
|
|
460
611
|
typeReturnBaseName,
|
|
461
612
|
typeData: await getTypeDataPost(table, typeDataName, specialCaseUuidColumn, includeMappedFields),
|
|
462
|
-
typeDataName
|
|
613
|
+
typeDataName
|
|
463
614
|
};
|
|
464
615
|
}
|
|
465
616
|
async function getPatchOneData(table, specialCaseUuidColumn, includeMappedFields) {
|
|
@@ -473,7 +624,7 @@ async function getPatchOneData(table, specialCaseUuidColumn, includeMappedFields
|
|
|
473
624
|
typeFieldsName,
|
|
474
625
|
typeReturnBaseName,
|
|
475
626
|
typeData: await getTypeDataPatch(table, typeDataName, specialCaseUuidColumn, includeMappedFields),
|
|
476
|
-
typeDataName
|
|
627
|
+
typeDataName
|
|
477
628
|
};
|
|
478
629
|
}
|
|
479
630
|
async function getPatchListData(table) {
|
|
@@ -488,14 +639,14 @@ async function getPatchListData(table) {
|
|
|
488
639
|
typeFieldsName,
|
|
489
640
|
typeReturnBaseName,
|
|
490
641
|
typeWhereName,
|
|
491
|
-
typeDataName
|
|
642
|
+
typeDataName
|
|
492
643
|
};
|
|
493
644
|
}
|
|
494
645
|
function getDeleteOneData(table) {
|
|
495
646
|
return {
|
|
496
647
|
kind: "deleteOne",
|
|
497
648
|
table,
|
|
498
|
-
methodName: "delete" + changeCase.pascalCase(table)
|
|
649
|
+
methodName: "delete" + changeCase.pascalCase(table)
|
|
499
650
|
};
|
|
500
651
|
}
|
|
501
652
|
function getDeleteListData(table) {
|
|
@@ -504,7 +655,7 @@ function getDeleteListData(table) {
|
|
|
504
655
|
kind: "deleteList",
|
|
505
656
|
table,
|
|
506
657
|
methodName: "delete" + changeCase.pascalCase(table) + "List",
|
|
507
|
-
typeWhereName
|
|
658
|
+
typeWhereName
|
|
508
659
|
};
|
|
509
660
|
}
|
|
510
661
|
async function getTypeWhere(table, name) {
|
|
@@ -516,11 +667,11 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
516
667
|
const tableMeta = (await getTableMeta(table)).filter((x) => x.Field !== primaryColumn.name);
|
|
517
668
|
const nullable = tableMeta.reduce((acc, m) => ({
|
|
518
669
|
...acc,
|
|
519
|
-
[m.Field]: m.Null === "YES" ? true : false
|
|
670
|
+
[m.Field]: m.Null === "YES" ? true : false
|
|
520
671
|
}), {});
|
|
521
672
|
const hasDefault = tableMeta.reduce((acc, m) => ({
|
|
522
673
|
...acc,
|
|
523
|
-
[m.Field]: m.Default == null ? false : true
|
|
674
|
+
[m.Field]: m.Default == null ? false : true
|
|
524
675
|
}), {});
|
|
525
676
|
let properties = getJSONSchemaObjProperties(tableMeta);
|
|
526
677
|
let notRequiredList = [];
|
|
@@ -533,7 +684,7 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
533
684
|
...mappedFields.reduce((acc, v) => {
|
|
534
685
|
acc[v.as] = { type: getJSONTypes(v.type, v.nullable) };
|
|
535
686
|
return acc;
|
|
536
|
-
}, {})
|
|
687
|
+
}, {})
|
|
537
688
|
};
|
|
538
689
|
notRequiredList = mappedFields.flatMap((x) => [x.as, x.foreignKey]);
|
|
539
690
|
for (let r of oneToManyRelations) {
|
|
@@ -548,9 +699,7 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
548
699
|
...oneToManyRelations.reduce((acc, v) => {
|
|
549
700
|
let tsType = getTypeDataPostName(v.table);
|
|
550
701
|
const mappedFields = mappedFieldsMap.get(v.table);
|
|
551
|
-
if (includeMappedFields &&
|
|
552
|
-
mappedFields != null &&
|
|
553
|
-
mappedFields.length > 0) {
|
|
702
|
+
if (includeMappedFields && mappedFields != null && mappedFields.length > 0) {
|
|
554
703
|
tsType = `Omit<${tsType}, ${mappedFields
|
|
555
704
|
.map((x) => JSON.stringify(x.as))
|
|
556
705
|
.join(" | ")}>`;
|
|
@@ -558,21 +707,19 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
558
707
|
tsType = `{$create: ${tsType}[]}`;
|
|
559
708
|
acc[v.name] = { tsType };
|
|
560
709
|
return acc;
|
|
561
|
-
}, {})
|
|
710
|
+
}, {})
|
|
562
711
|
},
|
|
563
712
|
additionalProperties: false,
|
|
564
713
|
required: Object.keys(properties)
|
|
565
714
|
.filter(
|
|
566
715
|
// `uuid` should not be required
|
|
567
|
-
(x) => !specialCaseUuidColumn || uuidColumn == null
|
|
568
|
-
? true
|
|
569
|
-
: x !== uuidColumn.name)
|
|
716
|
+
(x) => !specialCaseUuidColumn || uuidColumn == null ? true : x !== uuidColumn.name)
|
|
570
717
|
.filter(
|
|
571
718
|
// Required if column is non-nullable and has no default.
|
|
572
719
|
(x) => !nullable[x] && !hasDefault[x])
|
|
573
720
|
// Instead of doing a union with all possible permutations of UUID and IDs,
|
|
574
721
|
// for simplicity, just make both not required for now.
|
|
575
|
-
.filter((x) => !notRequiredList.includes(x))
|
|
722
|
+
.filter((x) => !notRequiredList.includes(x))
|
|
576
723
|
};
|
|
577
724
|
let type = await (0, json_schema_to_typescript_1.compile)(jsonSchema, name, json2TsOpts);
|
|
578
725
|
const imports = _.uniq(oneToManyRelations
|
|
@@ -601,13 +748,13 @@ async function getTypeDataPatch(table, name, specialCaseUuidColumn, includeMappe
|
|
|
601
748
|
const type = unwrapJSONType(properties[key].type);
|
|
602
749
|
if (type === "string") {
|
|
603
750
|
properties[key] = {
|
|
604
|
-
oneOf: [properties[key], { tsType: "TUpdateOperationsString" }]
|
|
751
|
+
oneOf: [properties[key], { tsType: "TUpdateOperationsString" }]
|
|
605
752
|
};
|
|
606
753
|
mustImportTUpdateOperationsString = true;
|
|
607
754
|
}
|
|
608
755
|
else if (type === "number" || type === "integer") {
|
|
609
756
|
properties[key] = {
|
|
610
|
-
oneOf: [properties[key], { tsType: "TUpdateOperationsNumber" }]
|
|
757
|
+
oneOf: [properties[key], { tsType: "TUpdateOperationsNumber" }]
|
|
611
758
|
};
|
|
612
759
|
mustImportTUpdateOperationsNumber = true;
|
|
613
760
|
}
|
|
@@ -619,14 +766,14 @@ async function getTypeDataPatch(table, name, specialCaseUuidColumn, includeMappe
|
|
|
619
766
|
...mappedFields.reduce((acc, v) => {
|
|
620
767
|
acc[v.as] = { type: getJSONTypes(v.type, v.nullable) };
|
|
621
768
|
return acc;
|
|
622
|
-
}, {})
|
|
769
|
+
}, {})
|
|
623
770
|
};
|
|
624
771
|
}
|
|
625
772
|
const jsonSchema = {
|
|
626
773
|
type: "object",
|
|
627
774
|
properties,
|
|
628
775
|
additionalProperties: false,
|
|
629
|
-
required: []
|
|
776
|
+
required: []
|
|
630
777
|
};
|
|
631
778
|
let type = await (0, json_schema_to_typescript_1.compile)(jsonSchema, name, json2TsOpts);
|
|
632
779
|
if (mustImportTUpdateOperationsString || mustImportTUpdateOperationsNumber) {
|
|
@@ -669,7 +816,7 @@ async function getMappedFields(table) {
|
|
|
669
816
|
name: "uuid",
|
|
670
817
|
// Replace `Id` with `Uuid`
|
|
671
818
|
as: x.foreignKey.slice(0, -2) + "Uuid",
|
|
672
|
-
type: getBaseJSONType(uuidColumn.Type)
|
|
819
|
+
type: getBaseJSONType(uuidColumn.Type)
|
|
673
820
|
});
|
|
674
821
|
}
|
|
675
822
|
return out;
|
|
@@ -688,52 +835,52 @@ async function getJSONSchemaWhere(table) {
|
|
|
688
835
|
{
|
|
689
836
|
type: "object",
|
|
690
837
|
properties: { $eq: v },
|
|
691
|
-
additionalProperties: false
|
|
838
|
+
additionalProperties: false
|
|
692
839
|
},
|
|
693
840
|
{
|
|
694
841
|
type: "object",
|
|
695
842
|
properties: { $neq: v },
|
|
696
|
-
additionalProperties: false
|
|
843
|
+
additionalProperties: false
|
|
697
844
|
},
|
|
698
845
|
{
|
|
699
846
|
type: "object",
|
|
700
847
|
properties: { $gt: v },
|
|
701
|
-
additionalProperties: false
|
|
848
|
+
additionalProperties: false
|
|
702
849
|
},
|
|
703
850
|
{
|
|
704
851
|
type: "object",
|
|
705
852
|
properties: { $gte: v },
|
|
706
|
-
additionalProperties: false
|
|
853
|
+
additionalProperties: false
|
|
707
854
|
},
|
|
708
855
|
{
|
|
709
856
|
type: "object",
|
|
710
857
|
properties: { $lt: v },
|
|
711
|
-
additionalProperties: false
|
|
858
|
+
additionalProperties: false
|
|
712
859
|
},
|
|
713
860
|
{
|
|
714
861
|
type: "object",
|
|
715
862
|
properties: { $lte: v },
|
|
716
|
-
additionalProperties: false
|
|
863
|
+
additionalProperties: false
|
|
717
864
|
},
|
|
718
865
|
{
|
|
719
866
|
type: "object",
|
|
720
867
|
properties: { $like: { type: "string", minLength: 1 } },
|
|
721
|
-
additionalProperties: false
|
|
868
|
+
additionalProperties: false
|
|
722
869
|
},
|
|
723
870
|
{
|
|
724
871
|
type: "object",
|
|
725
872
|
properties: { $nlike: { type: "string", minLength: 1 } },
|
|
726
|
-
additionalProperties: false
|
|
873
|
+
additionalProperties: false
|
|
727
874
|
},
|
|
728
875
|
{
|
|
729
876
|
type: "object",
|
|
730
877
|
properties: { $in: { type: "array", items: v } },
|
|
731
|
-
additionalProperties: false
|
|
878
|
+
additionalProperties: false
|
|
732
879
|
},
|
|
733
880
|
{
|
|
734
881
|
type: "object",
|
|
735
882
|
properties: { $nin: { type: "array", items: v } },
|
|
736
|
-
additionalProperties: false
|
|
883
|
+
additionalProperties: false
|
|
737
884
|
},
|
|
738
885
|
{
|
|
739
886
|
type: "object",
|
|
@@ -743,10 +890,10 @@ async function getJSONSchemaWhere(table) {
|
|
|
743
890
|
items: v,
|
|
744
891
|
minItems: 2,
|
|
745
892
|
maxItems: 2,
|
|
746
|
-
uniqueItems: true
|
|
747
|
-
}
|
|
893
|
+
uniqueItems: true
|
|
894
|
+
}
|
|
748
895
|
},
|
|
749
|
-
additionalProperties: false
|
|
896
|
+
additionalProperties: false
|
|
750
897
|
},
|
|
751
898
|
{
|
|
752
899
|
type: "object",
|
|
@@ -756,15 +903,15 @@ async function getJSONSchemaWhere(table) {
|
|
|
756
903
|
items: v,
|
|
757
904
|
minItems: 2,
|
|
758
905
|
maxItems: 2,
|
|
759
|
-
uniqueItems: true
|
|
760
|
-
}
|
|
906
|
+
uniqueItems: true
|
|
907
|
+
}
|
|
761
908
|
},
|
|
762
|
-
additionalProperties: false
|
|
763
|
-
}
|
|
764
|
-
]
|
|
765
|
-
}
|
|
909
|
+
additionalProperties: false
|
|
910
|
+
}
|
|
911
|
+
]
|
|
912
|
+
}
|
|
766
913
|
}), {}),
|
|
767
|
-
additionalProperties: false
|
|
914
|
+
additionalProperties: false
|
|
768
915
|
},
|
|
769
916
|
{
|
|
770
917
|
type: "object",
|
|
@@ -772,17 +919,17 @@ async function getJSONSchemaWhere(table) {
|
|
|
772
919
|
$and: {
|
|
773
920
|
type: "array",
|
|
774
921
|
items: {
|
|
775
|
-
$ref: `#/definitions/${whereSchemaName}
|
|
922
|
+
$ref: `#/definitions/${whereSchemaName}`
|
|
776
923
|
},
|
|
777
924
|
// While it makes sense conceptually for $and to have
|
|
778
925
|
// at least 2 items, in practice, $and could be
|
|
779
926
|
// generated dynamically and could end up having
|
|
780
927
|
// less than 2 items, so don't enforce minItems.
|
|
781
928
|
// minItems: 2,
|
|
782
|
-
additionalProperties: false
|
|
783
|
-
}
|
|
929
|
+
additionalProperties: false
|
|
930
|
+
}
|
|
784
931
|
},
|
|
785
|
-
additionalProperties: false
|
|
932
|
+
additionalProperties: false
|
|
786
933
|
},
|
|
787
934
|
{
|
|
788
935
|
type: "object",
|
|
@@ -790,19 +937,19 @@ async function getJSONSchemaWhere(table) {
|
|
|
790
937
|
$or: {
|
|
791
938
|
type: "array",
|
|
792
939
|
items: {
|
|
793
|
-
$ref: `#/definitions/${whereSchemaName}
|
|
940
|
+
$ref: `#/definitions/${whereSchemaName}`
|
|
794
941
|
},
|
|
795
942
|
// While it makes sense conceptually for $and to have
|
|
796
943
|
// at least 2 items, in practice, $and could be
|
|
797
944
|
// generated dynamically and could end up having
|
|
798
945
|
// less than 2 items, so don't enforce minItems.
|
|
799
946
|
// minItems: 2,
|
|
800
|
-
additionalProperties: false
|
|
801
|
-
}
|
|
947
|
+
additionalProperties: false
|
|
948
|
+
}
|
|
802
949
|
},
|
|
803
|
-
additionalProperties: false
|
|
804
|
-
}
|
|
805
|
-
]
|
|
950
|
+
additionalProperties: false
|
|
951
|
+
}
|
|
952
|
+
]
|
|
806
953
|
};
|
|
807
954
|
return {
|
|
808
955
|
definitions: { [whereSchemaName]: defWhere },
|
|
@@ -819,12 +966,12 @@ async function getJSONSchemaWhere(table) {
|
|
|
819
966
|
// generated dynamically and could end up having
|
|
820
967
|
// less than 2 items, so don't enforce minItems.
|
|
821
968
|
// minItems: 2,
|
|
822
|
-
additionalProperties: false
|
|
823
|
-
}
|
|
969
|
+
additionalProperties: false
|
|
970
|
+
}
|
|
824
971
|
},
|
|
825
|
-
additionalProperties: false
|
|
826
|
-
}))
|
|
827
|
-
]
|
|
972
|
+
additionalProperties: false
|
|
973
|
+
}))
|
|
974
|
+
]
|
|
828
975
|
};
|
|
829
976
|
}
|
|
830
977
|
async function getTypeOrderBy(table, name) {
|
|
@@ -837,14 +984,14 @@ async function getJSONSchemaOrderBy(table, name) {
|
|
|
837
984
|
type: "object",
|
|
838
985
|
properties: { [k]: { enum: ["asc", "desc"] } },
|
|
839
986
|
required: [k],
|
|
840
|
-
additionalProperties: false
|
|
841
|
-
}))
|
|
987
|
+
additionalProperties: false
|
|
988
|
+
}))
|
|
842
989
|
};
|
|
843
990
|
const defName = `_${name}`;
|
|
844
991
|
const _schema = { $ref: `#/definitions/${defName}` };
|
|
845
992
|
return {
|
|
846
993
|
definitions: { [defName]: def },
|
|
847
|
-
oneOf: [_schema, { type: "array", items: _schema }]
|
|
994
|
+
oneOf: [_schema, { type: "array", items: _schema }]
|
|
848
995
|
};
|
|
849
996
|
}
|
|
850
997
|
function getTypeTypesIndex(data) {
|
|
@@ -858,7 +1005,7 @@ function getTypeTypesIndex(data) {
|
|
|
858
1005
|
"typeReturnBaseName",
|
|
859
1006
|
"typeWhereName",
|
|
860
1007
|
"typeOrderByName",
|
|
861
|
-
"typeDataName"
|
|
1008
|
+
"typeDataName"
|
|
862
1009
|
]) {
|
|
863
1010
|
const str = d[k];
|
|
864
1011
|
if (str) {
|
|
@@ -870,7 +1017,7 @@ function getTypeTypesIndex(data) {
|
|
|
870
1017
|
"Paginate",
|
|
871
1018
|
"ListPaginated",
|
|
872
1019
|
"TUpdateOperationsString",
|
|
873
|
-
"TUpdateOperationsNumber"
|
|
1020
|
+
"TUpdateOperationsNumber"
|
|
874
1021
|
].join(",")} } from "./_shared";\n\n`;
|
|
875
1022
|
let arr = Array.from(set).sort();
|
|
876
1023
|
for (let x of arr) {
|
|
@@ -920,7 +1067,7 @@ async function getTypeFields(table, name, includeMappedFields) {
|
|
|
920
1067
|
items: {
|
|
921
1068
|
anyOf: [
|
|
922
1069
|
{
|
|
923
|
-
enum: scalarKeys.concat(mappedFields.map((x) => x.as))
|
|
1070
|
+
enum: scalarKeys.concat(mappedFields.map((x) => x.as))
|
|
924
1071
|
},
|
|
925
1072
|
...relations.map((x) => {
|
|
926
1073
|
const argsProperties = x.type === "many-to-many"
|
|
@@ -930,17 +1077,17 @@ async function getTypeFields(table, name, includeMappedFields) {
|
|
|
930
1077
|
properties: {
|
|
931
1078
|
[x.table]: { tsType: getTypeWhereName(x.table) },
|
|
932
1079
|
[x.junctionTable]: {
|
|
933
|
-
tsType: getTypeWhereName(x.junctionTable)
|
|
934
|
-
}
|
|
1080
|
+
tsType: getTypeWhereName(x.junctionTable)
|
|
1081
|
+
}
|
|
935
1082
|
},
|
|
936
|
-
additionalProperties: false
|
|
937
|
-
}
|
|
1083
|
+
additionalProperties: false
|
|
1084
|
+
}
|
|
938
1085
|
}
|
|
939
1086
|
: { [keyWhere]: { tsType: getTypeWhereName(x.table) } };
|
|
940
1087
|
// $orderBy only makes sense for a list
|
|
941
1088
|
if (x.grabMany) {
|
|
942
1089
|
argsProperties[keyOrderBy] = {
|
|
943
|
-
tsType: getTypeOrderByName(x.table)
|
|
1090
|
+
tsType: getTypeOrderByName(x.table)
|
|
944
1091
|
};
|
|
945
1092
|
}
|
|
946
1093
|
return {
|
|
@@ -952,21 +1099,19 @@ async function getTypeFields(table, name, includeMappedFields) {
|
|
|
952
1099
|
args: {
|
|
953
1100
|
type: "object",
|
|
954
1101
|
properties: argsProperties,
|
|
955
|
-
additionalProperties: false
|
|
1102
|
+
additionalProperties: false
|
|
956
1103
|
},
|
|
957
|
-
transform: { tsType: `(x: any) => any` }
|
|
1104
|
+
transform: { tsType: `(x: any) => any` }
|
|
958
1105
|
},
|
|
959
1106
|
additionalProperties: false,
|
|
960
|
-
required: ["name", "fields"]
|
|
1107
|
+
required: ["name", "fields"]
|
|
961
1108
|
};
|
|
962
|
-
})
|
|
963
|
-
]
|
|
964
|
-
}
|
|
1109
|
+
})
|
|
1110
|
+
]
|
|
1111
|
+
}
|
|
965
1112
|
};
|
|
966
1113
|
let type = await (0, json_schema_to_typescript_1.compile)(jsonSchemaFields, name, json2TsOpts);
|
|
967
|
-
const fieldImports = _.uniq(relations
|
|
968
|
-
.filter((x) => x.table !== table)
|
|
969
|
-
.map((x) => getTypeFieldsName(x.table)));
|
|
1114
|
+
const fieldImports = _.uniq(relations.filter((x) => x.table !== table).map((x) => getTypeFieldsName(x.table)));
|
|
970
1115
|
const whereImports = _.uniq(relations.flatMap((x) => x.type === "many-to-many"
|
|
971
1116
|
? [getTypeWhereName(x.table), getTypeWhereName(x.junctionTable)]
|
|
972
1117
|
: getTypeWhereName(x.table)));
|
|
@@ -1006,10 +1151,10 @@ async function getTypeReturnBase(table, name, includeMappedFields) {
|
|
|
1006
1151
|
acc[key] = { tsType };
|
|
1007
1152
|
}
|
|
1008
1153
|
return acc;
|
|
1009
|
-
}, {})
|
|
1154
|
+
}, {})
|
|
1010
1155
|
},
|
|
1011
1156
|
// Because of aliases
|
|
1012
|
-
additionalProperties: true
|
|
1157
|
+
additionalProperties: true
|
|
1013
1158
|
};
|
|
1014
1159
|
let type = await (0, json_schema_to_typescript_1.compile)(jsonSchemaReturn, name, json2TsOpts);
|
|
1015
1160
|
const imports = _.uniq(relations.map((x) => getTypeReturnBaseName(x.table)))
|
|
@@ -1040,7 +1185,7 @@ async function getArtifactsSource(tables, includeMappedFields, specialCaseUuidCo
|
|
|
1040
1185
|
table: x.table,
|
|
1041
1186
|
grabMany: x.grabMany,
|
|
1042
1187
|
nullable: x.nullable,
|
|
1043
|
-
relation: x.relation
|
|
1188
|
+
relation: x.relation
|
|
1044
1189
|
};
|
|
1045
1190
|
}
|
|
1046
1191
|
else {
|
|
@@ -1050,14 +1195,12 @@ async function getArtifactsSource(tables, includeMappedFields, specialCaseUuidCo
|
|
|
1050
1195
|
table: x.table,
|
|
1051
1196
|
junctionTable: x.junctionTable,
|
|
1052
1197
|
grabMany: x.grabMany,
|
|
1053
|
-
relations: x.relations
|
|
1198
|
+
relations: x.relations
|
|
1054
1199
|
};
|
|
1055
1200
|
}
|
|
1056
1201
|
return acc;
|
|
1057
1202
|
}, {});
|
|
1058
|
-
const _mappedFields = includeMappedFields
|
|
1059
|
-
? await getMappedFields(table)
|
|
1060
|
-
: [];
|
|
1203
|
+
const _mappedFields = includeMappedFields ? await getMappedFields(table) : [];
|
|
1061
1204
|
const mappedFields = _mappedFields.length === 0
|
|
1062
1205
|
? null
|
|
1063
1206
|
: _mappedFields.reduce((acc, v) => {
|
|
@@ -1073,6 +1216,47 @@ async function getArtifactsSource(tables, includeMappedFields, specialCaseUuidCo
|
|
|
1073
1216
|
return acc;
|
|
1074
1217
|
}, {});
|
|
1075
1218
|
const uniqueFields = await getUniqueColumns(table, specialCaseUuidColumn);
|
|
1219
|
+
let fields = tableMeta.map((t) => {
|
|
1220
|
+
const nullable = t.Null === "YES";
|
|
1221
|
+
const isEnum = t.Type.startsWith("enum");
|
|
1222
|
+
if (isEnum) {
|
|
1223
|
+
const values = getPropertyEnum(t.Type);
|
|
1224
|
+
if (values && nullable) {
|
|
1225
|
+
values.push(null);
|
|
1226
|
+
}
|
|
1227
|
+
return {
|
|
1228
|
+
kind: "enum",
|
|
1229
|
+
values,
|
|
1230
|
+
name: t.Field,
|
|
1231
|
+
nullable
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
return {
|
|
1235
|
+
kind: "scalar",
|
|
1236
|
+
type: getBaseJSONType(t.Type),
|
|
1237
|
+
name: t.Field,
|
|
1238
|
+
nullable,
|
|
1239
|
+
hasDefaultValue: !!t.Default
|
|
1240
|
+
};
|
|
1241
|
+
});
|
|
1242
|
+
for (let x of _mappedFields) {
|
|
1243
|
+
fields.push({
|
|
1244
|
+
kind: "scalar",
|
|
1245
|
+
type: x.type,
|
|
1246
|
+
name: x.as,
|
|
1247
|
+
nullable: x.nullable,
|
|
1248
|
+
// TODO
|
|
1249
|
+
hasDefaultValue: false
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
for (let x of relationInfo) {
|
|
1253
|
+
fields.push({
|
|
1254
|
+
kind: "object",
|
|
1255
|
+
type: x.table,
|
|
1256
|
+
name: x.name,
|
|
1257
|
+
isList: x.grabMany
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1076
1260
|
return {
|
|
1077
1261
|
table,
|
|
1078
1262
|
primaryKey,
|
|
@@ -1082,6 +1266,7 @@ async function getArtifactsSource(tables, includeMappedFields, specialCaseUuidCo
|
|
|
1082
1266
|
uniqueFields,
|
|
1083
1267
|
dateTimeFields,
|
|
1084
1268
|
dateTimeFieldsCount: Object.keys(dateTimeFields).length,
|
|
1269
|
+
fields
|
|
1085
1270
|
};
|
|
1086
1271
|
}));
|
|
1087
1272
|
const artifacts = tableMetaList.reduce((acc, x) => {
|
|
@@ -1108,7 +1293,7 @@ const getRelationInfo = _.memoize(async function getRelationInfo(table) {
|
|
|
1108
1293
|
table: x.referencedTable,
|
|
1109
1294
|
name,
|
|
1110
1295
|
relation: x,
|
|
1111
|
-
nullable: x.nullable
|
|
1296
|
+
nullable: x.nullable
|
|
1112
1297
|
};
|
|
1113
1298
|
}));
|
|
1114
1299
|
const relationsOneToManyDuplicates = (0, getDuplicates_1.getDuplicates)(relationsOneToMany.map((x) => x.referencedTable));
|
|
@@ -1131,7 +1316,7 @@ const getRelationInfo = _.memoize(async function getRelationInfo(table) {
|
|
|
1131
1316
|
table: x.referencedTable,
|
|
1132
1317
|
name,
|
|
1133
1318
|
relation: x,
|
|
1134
|
-
nullable: x.nullable
|
|
1319
|
+
nullable: x.nullable
|
|
1135
1320
|
};
|
|
1136
1321
|
}));
|
|
1137
1322
|
const relationsManyToMany = (await getJunctionTables()).reduce((acc, x) => {
|
|
@@ -1150,7 +1335,7 @@ const getRelationInfo = _.memoize(async function getRelationInfo(table) {
|
|
|
1150
1335
|
name: changeCase.camelCase(dataForChildTable.referencedTable) + "List",
|
|
1151
1336
|
// Ensure parent comes before child
|
|
1152
1337
|
relations: [dataForParentTable, dataForChildTable],
|
|
1153
|
-
grabMany: true
|
|
1338
|
+
grabMany: true
|
|
1154
1339
|
});
|
|
1155
1340
|
return acc;
|
|
1156
1341
|
}, []);
|
|
@@ -1171,7 +1356,7 @@ async function getJunctionTables() {
|
|
|
1171
1356
|
// e.g. junction of Foo, Bar must be FooBar or BarFoo
|
|
1172
1357
|
[
|
|
1173
1358
|
relations[0].referencedTable + relations[1].referencedTable,
|
|
1174
|
-
relations[1].referencedTable + relations[0].referencedTable
|
|
1359
|
+
relations[1].referencedTable + relations[0].referencedTable
|
|
1175
1360
|
].includes(table)) {
|
|
1176
1361
|
return { table, relations };
|
|
1177
1362
|
}
|
|
@@ -1227,7 +1412,7 @@ const getRelationsManyToOne = _.memoize(async function getRelationsManyToOne(tab
|
|
|
1227
1412
|
foreignKey: v.t1Field,
|
|
1228
1413
|
referencedTable: v.t2,
|
|
1229
1414
|
referencedKey: v.t2Field,
|
|
1230
|
-
nullable: tableMeta.find((m) => m.Field === v.t1Field)?.Null === "YES"
|
|
1415
|
+
nullable: tableMeta.find((m) => m.Field === v.t1Field)?.Null === "YES"
|
|
1231
1416
|
};
|
|
1232
1417
|
})));
|
|
1233
1418
|
return _.sortBy((x) => x.referencedTable, xs);
|
|
@@ -1276,7 +1461,7 @@ const getRelationsOneToMany = _.memoize(async function getRelationsOneToMany(tab
|
|
|
1276
1461
|
referencedTable: v.t1,
|
|
1277
1462
|
referencedKey: v.t1Field,
|
|
1278
1463
|
// TODO? I think this is right, since it's one-to-many, so a list
|
|
1279
|
-
nullable: false
|
|
1464
|
+
nullable: false
|
|
1280
1465
|
};
|
|
1281
1466
|
})));
|
|
1282
1467
|
return _.sortBy((x) => x.referencedKey, _.sortBy((x) => x.referencedTable, xs));
|
|
@@ -1291,7 +1476,7 @@ async function getPrimaryColumn(table) {
|
|
|
1291
1476
|
return {
|
|
1292
1477
|
name: column.Field,
|
|
1293
1478
|
type: getBaseJSONType(column.Type),
|
|
1294
|
-
nullable: column.Null === "YES"
|
|
1479
|
+
nullable: column.Null === "YES"
|
|
1295
1480
|
};
|
|
1296
1481
|
}
|
|
1297
1482
|
async function getUniqueColumns(table, specialCaseUuidColumn) {
|
|
@@ -1303,7 +1488,7 @@ async function getUniqueColumns(table, specialCaseUuidColumn) {
|
|
|
1303
1488
|
.map((x) => ({
|
|
1304
1489
|
name: x.Field,
|
|
1305
1490
|
type: getBaseJSONType(x.Type),
|
|
1306
|
-
nullable: x.Null === "YES"
|
|
1491
|
+
nullable: x.Null === "YES"
|
|
1307
1492
|
}));
|
|
1308
1493
|
}
|
|
1309
1494
|
async function getUuidColumn(table) {
|
|
@@ -1315,7 +1500,7 @@ async function getUuidColumn(table) {
|
|
|
1315
1500
|
return {
|
|
1316
1501
|
name: column.Field,
|
|
1317
1502
|
type: column.Type,
|
|
1318
|
-
nullable: column.Null === "YES"
|
|
1503
|
+
nullable: column.Null === "YES"
|
|
1319
1504
|
};
|
|
1320
1505
|
}
|
|
1321
1506
|
const getTableMeta = _.memoize(async function getTableMeta(table) {
|
|
@@ -1359,7 +1544,7 @@ const getTableMeta = _.memoize(async function getTableMeta(table) {
|
|
|
1359
1544
|
? "UNI"
|
|
1360
1545
|
: "",
|
|
1361
1546
|
Null: x["IS_NULLABLE"],
|
|
1362
|
-
Default: x["COLUMN_DEFAULT"]
|
|
1547
|
+
Default: x["COLUMN_DEFAULT"]
|
|
1363
1548
|
};
|
|
1364
1549
|
})));
|
|
1365
1550
|
}
|
|
@@ -1378,7 +1563,7 @@ function getJSONSchemaObjProperties(tableMeta) {
|
|
|
1378
1563
|
_enum.push(null);
|
|
1379
1564
|
}
|
|
1380
1565
|
acc[m.Field] = {
|
|
1381
|
-
type: getJSONTypes(baseType, nullable)
|
|
1566
|
+
type: getJSONTypes(baseType, nullable)
|
|
1382
1567
|
// maxLength:
|
|
1383
1568
|
// baseType === "string" && format == null && isEnum == null
|
|
1384
1569
|
// ? getPropertyMaxLength(m.Type)
|
|
@@ -1488,9 +1673,7 @@ function getPropertyEnum(sqlType) {
|
|
|
1488
1673
|
return c;
|
|
1489
1674
|
}
|
|
1490
1675
|
function getPropertyFormat(sqlType) {
|
|
1491
|
-
if (sqlType === "datetime" ||
|
|
1492
|
-
sqlType === "datetime2" ||
|
|
1493
|
-
sqlType === "timestamp") {
|
|
1676
|
+
if (sqlType === "datetime" || sqlType === "datetime2" || sqlType === "timestamp") {
|
|
1494
1677
|
// TODO: not sure this is correct for `timestamp`
|
|
1495
1678
|
return "date-time";
|
|
1496
1679
|
}
|
|
@@ -1514,7 +1697,7 @@ const mssqlTableExcludes = new Set([
|
|
|
1514
1697
|
"ddl_history",
|
|
1515
1698
|
"index_columns",
|
|
1516
1699
|
"lsn_time_mapping",
|
|
1517
|
-
"systranschemas"
|
|
1700
|
+
"systranschemas"
|
|
1518
1701
|
]);
|
|
1519
1702
|
async function getTableNames() {
|
|
1520
1703
|
if (dialect === "mysql") {
|