@technicity/data-service-generator 0.7.1 → 0.8.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.
|
@@ -15,7 +15,7 @@ const TSqlString = require("tsqlstring");
|
|
|
15
15
|
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
16
16
|
const getDuplicates_1 = require("../lib/getDuplicates");
|
|
17
17
|
const isNotNullOrUndefined_1 = require("../lib/isNotNullOrUndefined");
|
|
18
|
-
const
|
|
18
|
+
const MySQL_1 = require("../runtime/lib/MySQL");
|
|
19
19
|
// json-schema-to-typescript inlines everything. We don't want that,
|
|
20
20
|
// so use `tsType`, and add imports manually.
|
|
21
21
|
// https://github.com/bcherny/json-schema-to-typescript#custom-schema-properties
|
|
@@ -108,6 +108,7 @@ async function generate(input) {
|
|
|
108
108
|
fs.writeFileSync(path.join(typesDirPath, x.typeDataName + ".ts"), x.typeData);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
+
fs.writeFileSync(path.join(typesDirPath, "index.ts"), getTypeTypesIndex(data));
|
|
111
112
|
fs.writeFileSync(path.join(tmpDirPath, "package.json"), JSON.stringify(packageJSON, null, 2));
|
|
112
113
|
fs.writeFileSync(path.join(tmpDirPath, "tsconfig.json"), JSON.stringify(tsConfigJSON, null, 2));
|
|
113
114
|
fse.copySync(__dirname, path.join(tmpDirPath, "src"));
|
|
@@ -116,7 +117,7 @@ async function generate(input) {
|
|
|
116
117
|
const sdkOutputPath = path.join(outdir, "sdk-ts");
|
|
117
118
|
const nccVersion = "^0.33.0";
|
|
118
119
|
child_process.execSync("npm i", { cwd: tmpDirPath, stdio: "inherit" });
|
|
119
|
-
child_process.execSync(`npx -p @vercel/ncc@${nccVersion} ncc build ./${sdkFilename} -o ${tmpBuildOutputPath} -e ./artifacts`, { cwd: tmpDirPath, stdio: "inherit" });
|
|
120
|
+
child_process.execSync(`npx --yes -p @vercel/ncc@${nccVersion} ncc build ./${sdkFilename} -o ${tmpBuildOutputPath} -e ./artifacts`, { cwd: tmpDirPath, stdio: "inherit" });
|
|
120
121
|
// TODO: workaround for artifacts.js not being output by ncc
|
|
121
122
|
fs.writeFileSync(path.join(tmpBuildOutputPath, "artifacts.js"), artifactsSource
|
|
122
123
|
.replace("export const artifacts: IArtifacts = ", "module.exports.artifacts = ")
|
|
@@ -138,7 +139,7 @@ exports.generate = generate;
|
|
|
138
139
|
function init(input) {
|
|
139
140
|
const { database, user, password, host, port, server } = input;
|
|
140
141
|
if (dialect === "mysql") {
|
|
141
|
-
let mysql = new
|
|
142
|
+
let mysql = new MySQL_1.MySQL({
|
|
142
143
|
user,
|
|
143
144
|
password,
|
|
144
145
|
host,
|
|
@@ -169,26 +170,30 @@ function init(input) {
|
|
|
169
170
|
// It's a bit awkward to put __whereNeedsProcessing, __prepareWhere on the class,
|
|
170
171
|
// but it allows us to share the same database pool, clientOpts, etc.
|
|
171
172
|
async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts) {
|
|
173
|
+
function getTypeImports() {
|
|
174
|
+
let set = new Set();
|
|
175
|
+
for (let d of input) {
|
|
176
|
+
for (let k of [
|
|
177
|
+
"typeFieldsName",
|
|
178
|
+
"typeReturnBaseName",
|
|
179
|
+
"typeWhereName",
|
|
180
|
+
"typeOrderByName",
|
|
181
|
+
"typeDataName",
|
|
182
|
+
]) {
|
|
183
|
+
const str = d[k];
|
|
184
|
+
if (str) {
|
|
185
|
+
set.add(str);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return `import type { ${["Paginate", "ListPaginated"]
|
|
190
|
+
.concat(Array.from(set).sort())
|
|
191
|
+
.join(",\n")} } from "./types";`;
|
|
192
|
+
}
|
|
172
193
|
const src = `import type { IRuntime, TMiddleware, TContext } from "./IRuntime"
|
|
173
194
|
import { artifacts } from "./artifacts";
|
|
174
|
-
|
|
175
|
-
${
|
|
176
|
-
if (x.kind === "getOne") {
|
|
177
|
-
acc += `import type { ${x.typeReturnBaseName} } from "./types/${x.typeReturnBaseName}";`;
|
|
178
|
-
acc += `import type { ${x.typeFieldsName} } from "./types/${x.typeFieldsName}";`;
|
|
179
|
-
}
|
|
180
|
-
if (x.kind === "getList") {
|
|
181
|
-
acc += `import type { ${x.typeWhereName} } from "./types/${x.typeWhereName}";`;
|
|
182
|
-
acc += `import type { ${x.typeOrderByName} } from "./types/${x.typeOrderByName}";`;
|
|
183
|
-
}
|
|
184
|
-
if (x.kind === "postOne") {
|
|
185
|
-
acc += `import type { ${x.typeDataName} } from "./types/${x.typeDataName}";`;
|
|
186
|
-
}
|
|
187
|
-
if (x.kind === "patchOne") {
|
|
188
|
-
acc += `import type { ${x.typeDataName} } from "./types/${x.typeDataName}";`;
|
|
189
|
-
}
|
|
190
|
-
return acc;
|
|
191
|
-
}, "")}
|
|
195
|
+
|
|
196
|
+
${getTypeImports()}
|
|
192
197
|
|
|
193
198
|
export class SDK {
|
|
194
199
|
runtime: IRuntime;
|
|
@@ -834,6 +839,37 @@ async function getJSONSchemaOrderBy(table, name) {
|
|
|
834
839
|
oneOf: [_schema, { type: "array", items: _schema }],
|
|
835
840
|
};
|
|
836
841
|
}
|
|
842
|
+
function getTypeTypesIndex(data) {
|
|
843
|
+
function getExport(name) {
|
|
844
|
+
return `export type { ${name} } from "./${name}";`;
|
|
845
|
+
}
|
|
846
|
+
let set = new Set();
|
|
847
|
+
for (let d of data) {
|
|
848
|
+
for (let k of [
|
|
849
|
+
"typeFieldsName",
|
|
850
|
+
"typeReturnBaseName",
|
|
851
|
+
"typeWhereName",
|
|
852
|
+
"typeOrderByName",
|
|
853
|
+
"typeDataName",
|
|
854
|
+
]) {
|
|
855
|
+
const str = d[k];
|
|
856
|
+
if (str) {
|
|
857
|
+
set.add(str);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
let src = `export type { ${[
|
|
862
|
+
"Paginate",
|
|
863
|
+
"ListPaginated",
|
|
864
|
+
"TUpdateOperationsString",
|
|
865
|
+
"TUpdateOperationsNumber",
|
|
866
|
+
].join(",")} } from "./_shared";\n\n`;
|
|
867
|
+
let arr = Array.from(set).sort();
|
|
868
|
+
for (let x of arr) {
|
|
869
|
+
src += getExport(x);
|
|
870
|
+
}
|
|
871
|
+
return prettier.format(src, { parser: "typescript" });
|
|
872
|
+
}
|
|
837
873
|
function getTypeShared() {
|
|
838
874
|
const src = `export type Paginate = {
|
|
839
875
|
first: number;
|
|
@@ -882,19 +918,14 @@ async function getTypeFields(table, name, includeMappedFields) {
|
|
|
882
918
|
const argsProperties = x.type === "many-to-many"
|
|
883
919
|
? {
|
|
884
920
|
[keyWhere]: {
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
{
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
{ tsType: getTypeWhereName(x.table) },
|
|
891
|
-
{ tsType: getTypeWhereName(x.junctionTable) },
|
|
892
|
-
],
|
|
893
|
-
additionalItems: false,
|
|
894
|
-
minItems: 2,
|
|
895
|
-
maxItems: 2,
|
|
921
|
+
type: "object",
|
|
922
|
+
properties: {
|
|
923
|
+
[x.table]: { tsType: getTypeWhereName(x.table) },
|
|
924
|
+
[x.junctionTable]: {
|
|
925
|
+
tsType: getTypeWhereName(x.junctionTable),
|
|
896
926
|
},
|
|
897
|
-
|
|
927
|
+
},
|
|
928
|
+
additionalProperties: false,
|
|
898
929
|
},
|
|
899
930
|
}
|
|
900
931
|
: { [keyWhere]: { tsType: getTypeWhereName(x.table) } };
|
|
@@ -148,9 +148,10 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName,
|
|
|
148
148
|
let where = undefined;
|
|
149
149
|
if (args?.$where != null) {
|
|
150
150
|
let argsMapped = args;
|
|
151
|
-
if (
|
|
151
|
+
if (typeof argsMapped.$where === "object" &&
|
|
152
|
+
argsMapped.$where[table] != null) {
|
|
152
153
|
argsMapped = _.cloneDeep(argsMapped);
|
|
153
|
-
argsMapped.$where = argsMapped.$where[
|
|
154
|
+
argsMapped.$where = argsMapped.$where[table];
|
|
154
155
|
}
|
|
155
156
|
const whereResult = (0, stringifyWhere_1.stringifyWhere)({
|
|
156
157
|
where: argsMapped.$where,
|
|
@@ -304,8 +305,10 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
|
|
|
304
305
|
if (junctionKeyValue == null) {
|
|
305
306
|
return { [x.as ?? x.name]: [] };
|
|
306
307
|
}
|
|
307
|
-
let whereJunction = x
|
|
308
|
-
|
|
308
|
+
let whereJunction = typeof x?.args?.$where === "object" &&
|
|
309
|
+
x.args.$where != null &&
|
|
310
|
+
x.args.$where[relationField.junctionTable] != null
|
|
311
|
+
? _.cloneDeep(x.args.$where[relationField.junctionTable])
|
|
309
312
|
: {};
|
|
310
313
|
whereJunction = {
|
|
311
314
|
$and: [
|
|
@@ -328,9 +331,11 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
|
|
|
328
331
|
[relationField.relations[1].referencedKey]: { $in: keys },
|
|
329
332
|
};
|
|
330
333
|
const argsMapped = x.args == null ? {} : _.cloneDeep(x.args);
|
|
331
|
-
if (
|
|
334
|
+
if (typeof argsMapped?.$where === "object" &&
|
|
335
|
+
argsMapped.$where != null &&
|
|
336
|
+
argsMapped.$where[relationField.table] != null) {
|
|
332
337
|
argsMapped.$where = {
|
|
333
|
-
$and: [whereDest, argsMapped.$where[
|
|
338
|
+
$and: [whereDest, argsMapped.$where[relationField.table]],
|
|
334
339
|
};
|
|
335
340
|
}
|
|
336
341
|
else if (argsMapped.$where != null) {
|
|
File without changes
|
|
File without changes
|
|
@@ -21,9 +21,12 @@ function getSqlAst(input) {
|
|
|
21
21
|
if (input.where == null) {
|
|
22
22
|
where = (table, args) => {
|
|
23
23
|
let argsMapped = args;
|
|
24
|
-
if (junction != null &&
|
|
24
|
+
if (junction != null &&
|
|
25
|
+
typeof argsMapped?.$where === "object" &&
|
|
26
|
+
argsMapped.$where != null &&
|
|
27
|
+
argsMapped.$where[input.table] != null) {
|
|
25
28
|
argsMapped = _.cloneDeep(argsMapped);
|
|
26
|
-
argsMapped.$where = argsMapped.$where[
|
|
29
|
+
argsMapped.$where = argsMapped.$where[input.table];
|
|
27
30
|
}
|
|
28
31
|
const whereResult = getWhere(
|
|
29
32
|
// table is escaped already
|
|
@@ -109,11 +112,14 @@ function getSqlAst(input) {
|
|
|
109
112
|
as: asJunction,
|
|
110
113
|
uniqueKey,
|
|
111
114
|
where: (table, args) => {
|
|
112
|
-
if (
|
|
115
|
+
if (typeof args?.$where !== "object" ||
|
|
116
|
+
typeof args?.$where == null ||
|
|
117
|
+
args.$where[relationField.junctionTable] == null) {
|
|
113
118
|
return undefined;
|
|
114
119
|
}
|
|
115
120
|
const argsMapped = _.cloneDeep(args);
|
|
116
|
-
argsMapped.$where =
|
|
121
|
+
argsMapped.$where =
|
|
122
|
+
argsMapped.$where[relationField.junctionTable];
|
|
117
123
|
const whereResult = getWhere(
|
|
118
124
|
// table is escaped already
|
|
119
125
|
table, argsMapped, dialect, orderBy, rowWithMatchingCursor);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@technicity/data-service-generator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"fs-extra": "10.0.0",
|
|
18
18
|
"graphql": "15.8.0",
|
|
19
19
|
"graphql-relay": "^0.9.0",
|
|
20
|
-
"join-monster": "git+https://github.com/apalm/join-monster.git#
|
|
20
|
+
"join-monster": "git+https://github.com/apalm/join-monster.git#3e93d7028ccbf50f728577b2290b5f2638b639cb",
|
|
21
21
|
"json-schema-to-typescript": "10.1.5",
|
|
22
22
|
"lodash": "^4.17.20",
|
|
23
23
|
"mssql": "^6.3.1",
|