@technicity/data-service-generator 0.12.4 → 0.13.25
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 +9 -4
- package/dist/generation/generate.d.ts +1 -0
- package/dist/generation/generate.js +403 -68
- package/dist/index.d.ts +0 -2
- package/dist/index.js +1 -5
- package/dist/ksql.js +24 -1
- package/dist/runtime/Cache.js +6 -3
- package/dist/runtime/IRuntime.d.ts +4 -2
- package/dist/runtime/RuntimeKSQL.js +25 -2
- package/dist/runtime/RuntimeMySQL.js +29 -3
- package/dist/runtime/RuntimeSQLite.d.ts +38 -0
- package/dist/runtime/RuntimeSQLite.js +135 -0
- package/dist/runtime/lib/MSSQL.js +25 -2
- package/dist/runtime/lib/addNullFallbacks.test.d.ts +1 -0
- package/dist/runtime/lib/addNullFallbacks.test.js +206 -0
- package/dist/runtime/lib/getSqlAst.js +46 -21
- package/dist/runtime/lib/runTransforms.test.d.ts +1 -0
- package/dist/runtime/lib/runTransforms.test.js +112 -0
- package/dist/runtime/lib/shared.js +127 -47
- package/dist/runtime/lib/stringifyWhere.js +38 -11
- package/dist/runtime/lib/stringifyWhere.test.d.ts +1 -0
- package/dist/runtime/lib/stringifyWhere.test.js +236 -0
- package/dist/runtime/lib/typeCastMSSQL.js +24 -1
- package/dist/traverseFieldArgs.test.d.ts +1 -0
- package/dist/traverseFieldArgs.test.js +72 -0
- package/package.json +13 -12
- package/dist/getFakeData.d.ts +0 -12
- package/dist/getFakeData.js +0 -90
- package/dist/getIsList.d.ts +0 -1
- package/dist/getIsList.js +0 -7
|
@@ -1,21 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
2
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
29
|
exports.getSqlAst = void 0;
|
|
4
|
-
const SqlString = require("sqlstring");
|
|
30
|
+
const SqlString = __importStar(require("sqlstring"));
|
|
5
31
|
// @ts-ignore
|
|
6
|
-
const TSqlString = require("tsqlstring");
|
|
32
|
+
const TSqlString = __importStar(require("tsqlstring"));
|
|
7
33
|
// @ts-ignore
|
|
8
|
-
const alias_namespace_1 = require("join-monster/dist/alias-namespace");
|
|
9
|
-
const _ = require("lodash/fp");
|
|
34
|
+
const alias_namespace_1 = __importDefault(require("join-monster/dist/alias-namespace"));
|
|
35
|
+
const _ = __importStar(require("lodash/fp"));
|
|
10
36
|
const getOrderBy_1 = require("./getOrderBy");
|
|
11
37
|
const namespace = new alias_namespace_1.default(true);
|
|
12
38
|
function getSqlAst(input) {
|
|
13
|
-
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect
|
|
39
|
+
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect } = input;
|
|
14
40
|
const tableArtifacts = artifacts[table];
|
|
15
41
|
const primaryKey = tableArtifacts.primaryKey;
|
|
16
|
-
const format = dialect === "
|
|
17
|
-
?
|
|
18
|
-
:
|
|
42
|
+
const format = dialect === "mssql"
|
|
43
|
+
? TSqlString.format.bind(TSqlString)
|
|
44
|
+
: SqlString.format.bind(SqlString);
|
|
19
45
|
const orderBy = input.orderBy ?? (0, getOrderBy_1.getOrderBy)(args, primaryKey)?.orderBy;
|
|
20
46
|
let where = input.where;
|
|
21
47
|
if (input.where == null) {
|
|
@@ -74,12 +100,12 @@ function getSqlAst(input) {
|
|
|
74
100
|
referencedTableAlias,
|
|
75
101
|
referencedTableAlias,
|
|
76
102
|
mappedField.referencedKey,
|
|
77
|
-
mappedField.foreignKey
|
|
103
|
+
mappedField.foreignKey
|
|
78
104
|
]);
|
|
79
105
|
},
|
|
80
106
|
name: x,
|
|
81
107
|
fieldName: x,
|
|
82
|
-
as: namespace.generate("column", x)
|
|
108
|
+
as: namespace.generate("column", x)
|
|
83
109
|
};
|
|
84
110
|
}
|
|
85
111
|
// TODO - validate in dev?
|
|
@@ -105,7 +131,7 @@ function getSqlAst(input) {
|
|
|
105
131
|
// Notice the duplicate names in the select. This results in missing rows because the
|
|
106
132
|
// shape definition passed into NestHydrationJS will only have 1 `id`.
|
|
107
133
|
fieldName: String(Date.now()),
|
|
108
|
-
as: String(Date.now())
|
|
134
|
+
as: String(Date.now())
|
|
109
135
|
},
|
|
110
136
|
junction: {
|
|
111
137
|
sqlTable: relationField.junctionTable,
|
|
@@ -118,8 +144,7 @@ function getSqlAst(input) {
|
|
|
118
144
|
return undefined;
|
|
119
145
|
}
|
|
120
146
|
const argsMapped = _.cloneDeep(args);
|
|
121
|
-
argsMapped.$where =
|
|
122
|
-
argsMapped.$where[relationField.junctionTable];
|
|
147
|
+
argsMapped.$where = argsMapped.$where[relationField.junctionTable];
|
|
123
148
|
const whereResult = getWhere(
|
|
124
149
|
// table is escaped already
|
|
125
150
|
table, argsMapped, dialect, orderBy, rowWithMatchingCursor);
|
|
@@ -143,12 +168,12 @@ function getSqlAst(input) {
|
|
|
143
168
|
sqlBatch: {
|
|
144
169
|
thisKey: columnToASTChild(relationField.relations[0].foreignKey, namespace, asJunction),
|
|
145
170
|
parentKey: columnToASTChild(relationField.relations[0].referencedKey, namespace),
|
|
146
|
-
sqlJoin: (junctionTable, t, args) => `${junctionTable}.${relationField.relations[1].foreignKey} = ${t}.${relationField.relations[1].referencedKey}
|
|
147
|
-
}
|
|
171
|
+
sqlJoin: (junctionTable, t, args) => `${junctionTable}.${relationField.relations[1].foreignKey} = ${t}.${relationField.relations[1].referencedKey}`
|
|
172
|
+
}
|
|
148
173
|
},
|
|
149
174
|
getWhere,
|
|
150
175
|
artifacts,
|
|
151
|
-
dialect
|
|
176
|
+
dialect
|
|
152
177
|
});
|
|
153
178
|
}
|
|
154
179
|
return getSqlAst({
|
|
@@ -174,13 +199,13 @@ function getSqlAst(input) {
|
|
|
174
199
|
// sub lists are limited, too.
|
|
175
200
|
sqlBatch: {
|
|
176
201
|
thisKey: columnToASTChild(relationField.relation.referencedKey, namespace),
|
|
177
|
-
parentKey: columnToASTChild(relationField.relation.foreignKey, namespace)
|
|
202
|
+
parentKey: columnToASTChild(relationField.relation.foreignKey, namespace)
|
|
178
203
|
},
|
|
179
204
|
getWhere,
|
|
180
205
|
artifacts,
|
|
181
|
-
dialect
|
|
206
|
+
dialect
|
|
182
207
|
});
|
|
183
|
-
}))
|
|
208
|
+
}))
|
|
184
209
|
};
|
|
185
210
|
}
|
|
186
211
|
exports.getSqlAst = getSqlAst;
|
|
@@ -192,7 +217,7 @@ function keyToASTChild(key, namespace, fromOtherTable) {
|
|
|
192
217
|
name: key,
|
|
193
218
|
fieldName: clumsyName,
|
|
194
219
|
as: namespace.generate("column", clumsyName),
|
|
195
|
-
fromOtherTable
|
|
220
|
+
fromOtherTable
|
|
196
221
|
};
|
|
197
222
|
}
|
|
198
223
|
return columnToASTChild(key, namespace, fromOtherTable);
|
|
@@ -203,7 +228,7 @@ function columnToASTChild(columnName, namespace, fromOtherTable) {
|
|
|
203
228
|
name: columnName,
|
|
204
229
|
fieldName: columnName,
|
|
205
230
|
as: namespace.generate("column", columnName),
|
|
206
|
-
fromOtherTable
|
|
231
|
+
fromOtherTable
|
|
207
232
|
};
|
|
208
233
|
}
|
|
209
234
|
function toClumsyName(keyArr) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const globals_1 = require("@jest/globals");
|
|
7
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
8
|
+
const runTransforms_1 = require("./runTransforms");
|
|
9
|
+
(0, globals_1.describe)("runTransforms", () => {
|
|
10
|
+
(0, globals_1.test)("should work", () => {
|
|
11
|
+
const fields = [
|
|
12
|
+
"id",
|
|
13
|
+
{
|
|
14
|
+
name: "business",
|
|
15
|
+
as: "mainBusiness",
|
|
16
|
+
fields: ["id", "name"],
|
|
17
|
+
transform: (x) => {
|
|
18
|
+
x.name = `TRANSFORMED_BUSINESS_NAME ${x.id}`;
|
|
19
|
+
return x;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "sessionList",
|
|
24
|
+
as: "mainSession",
|
|
25
|
+
fields: [
|
|
26
|
+
"id",
|
|
27
|
+
{
|
|
28
|
+
name: "deepNestedList",
|
|
29
|
+
fields: ["id"],
|
|
30
|
+
transform: (xs) => {
|
|
31
|
+
return xs.map((x) => (x == null ? x : { ...x, id: x.id + 100 }));
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "deepNestedList2",
|
|
36
|
+
fields: ["id"],
|
|
37
|
+
transform: (xs) => {
|
|
38
|
+
return xs[0] ?? null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
args: {
|
|
43
|
+
$where: { valid: true, archived: false },
|
|
44
|
+
$orderBy: { id: "desc" }
|
|
45
|
+
},
|
|
46
|
+
transform: (x) => {
|
|
47
|
+
return x[0] ?? null;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"name"
|
|
51
|
+
];
|
|
52
|
+
const data = [
|
|
53
|
+
{
|
|
54
|
+
id: 1,
|
|
55
|
+
mainBusiness: { id: 1, name: "My Business Name 1" },
|
|
56
|
+
mainSession: [
|
|
57
|
+
{
|
|
58
|
+
id: 1,
|
|
59
|
+
deepNestedList: [{ id: 1 }, null, { id: 2 }],
|
|
60
|
+
deepNestedList2: [{ id: 1 }, null, { id: 2 }]
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 2,
|
|
64
|
+
deepNestedList: [{ id: 1 }, null, { id: 2 }],
|
|
65
|
+
deepNestedList2: [{ id: 1 }, null, { id: 2 }]
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
name: "FOO"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: 2,
|
|
72
|
+
mainBusiness: { id: 2, name: "My Business Name 2" },
|
|
73
|
+
mainSession: [
|
|
74
|
+
{
|
|
75
|
+
id: 1,
|
|
76
|
+
deepNestedList: [{ id: 1 }, null, { id: 2 }],
|
|
77
|
+
deepNestedList2: [{ id: 1 }, null, { id: 2 }]
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: 2,
|
|
81
|
+
deepNestedList: [{ id: 1 }, null, { id: 2 }],
|
|
82
|
+
deepNestedList2: [{ id: 1 }, null, { id: 2 }]
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
name: "BAR"
|
|
86
|
+
}
|
|
87
|
+
];
|
|
88
|
+
(0, runTransforms_1.runTransforms)(data, fields);
|
|
89
|
+
strict_1.default.deepEqual(data, [
|
|
90
|
+
{
|
|
91
|
+
id: 1,
|
|
92
|
+
mainBusiness: { id: 1, name: "TRANSFORMED_BUSINESS_NAME 1" },
|
|
93
|
+
mainSession: {
|
|
94
|
+
id: 1,
|
|
95
|
+
deepNestedList: [{ id: 101 }, null, { id: 102 }],
|
|
96
|
+
deepNestedList2: { id: 1 }
|
|
97
|
+
},
|
|
98
|
+
name: "FOO"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: 2,
|
|
102
|
+
mainBusiness: { id: 2, name: "TRANSFORMED_BUSINESS_NAME 2" },
|
|
103
|
+
mainSession: {
|
|
104
|
+
id: 1,
|
|
105
|
+
deepNestedList: [{ id: 101 }, null, { id: 102 }],
|
|
106
|
+
deepNestedList2: { id: 1 }
|
|
107
|
+
},
|
|
108
|
+
name: "BAR"
|
|
109
|
+
}
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -1,4 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
2
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
29
|
exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exports.MiddlewareHandler = exports.resolve = void 0;
|
|
4
30
|
// @ts-ignore
|
|
@@ -6,11 +32,11 @@ exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exp
|
|
|
6
32
|
// @ts-ignore
|
|
7
33
|
// import arrToConnection from "join-monster/dist/array-to-connection";
|
|
8
34
|
// @ts-ignore
|
|
9
|
-
const batch_planner_1 = require("join-monster/dist/batch-planner");
|
|
35
|
+
const batch_planner_1 = __importDefault(require("join-monster/dist/batch-planner"));
|
|
10
36
|
// @ts-ignore
|
|
11
37
|
const util_1 = require("join-monster/dist/util");
|
|
12
38
|
const async_hooks_1 = require("async_hooks");
|
|
13
|
-
const _ = require("lodash/fp");
|
|
39
|
+
const _ = __importStar(require("lodash/fp"));
|
|
14
40
|
const uuid_1 = require("uuid");
|
|
15
41
|
const getSqlAst_1 = require("./getSqlAst");
|
|
16
42
|
const getWhere_1 = require("./getWhere");
|
|
@@ -35,43 +61,13 @@ async function resolve(input, dbCall, formatQuery, beginTransaction, dialect, mi
|
|
|
35
61
|
return nextMiddleware(paramsMaybeMutated, consumer);
|
|
36
62
|
}
|
|
37
63
|
const paramsChanged = { ...input, ...params };
|
|
38
|
-
if (paramsChanged?.nullability) {
|
|
39
|
-
return _nullResolve(paramsChanged);
|
|
40
|
-
}
|
|
41
64
|
return _resolve(paramsChanged, dbCall, formatQuery, beginTransaction, dialect, context, cache);
|
|
42
65
|
};
|
|
43
66
|
return resource.runInAsyncScope(() => consumer(params));
|
|
44
67
|
}
|
|
45
|
-
if (input?.nullability) {
|
|
46
|
-
return _nullResolve(input);
|
|
47
|
-
}
|
|
48
68
|
return _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache);
|
|
49
69
|
}
|
|
50
70
|
exports.resolve = resolve;
|
|
51
|
-
async function _nullResolve(input) {
|
|
52
|
-
const { nullability } = input;
|
|
53
|
-
if (typeof nullability !== "object" || nullability == null) {
|
|
54
|
-
throw new Error("Invalid nullability");
|
|
55
|
-
}
|
|
56
|
-
const table = input.resource;
|
|
57
|
-
const tableConfig = nullability[table];
|
|
58
|
-
if (tableConfig == null) {
|
|
59
|
-
throw new Error(`Response not configured for \`${table}\`.`);
|
|
60
|
-
}
|
|
61
|
-
const { action } = input;
|
|
62
|
-
const responseFn = tableConfig[action];
|
|
63
|
-
if (responseFn == null) {
|
|
64
|
-
throw new Error(`Response not configured for \`${action}\`, \`${table}\`.`);
|
|
65
|
-
}
|
|
66
|
-
if (typeof responseFn !== "function") {
|
|
67
|
-
throw new Error(`Invalid configured response for \`${action}\`, \`${table}\`: not a function.`);
|
|
68
|
-
}
|
|
69
|
-
return responseFn({
|
|
70
|
-
operation: action,
|
|
71
|
-
model: table,
|
|
72
|
-
args: { select: input.fields, where: input.args?.$where }
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
71
|
function _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache) {
|
|
76
72
|
switch (input.action) {
|
|
77
73
|
case "findMany":
|
|
@@ -192,7 +188,7 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
192
188
|
dialect,
|
|
193
189
|
grabMany
|
|
194
190
|
});
|
|
195
|
-
const options = { dialect };
|
|
191
|
+
const options = { dialect: dialect === "sqlite" ? "mysql" : dialect };
|
|
196
192
|
let { sql, shapeDefinition } = await (0, util_1.compileSqlAST)(sqlAST, context, options);
|
|
197
193
|
if (!sql) {
|
|
198
194
|
// return {};
|
|
@@ -235,6 +231,9 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
235
231
|
// Do later for `listPaginated`, since the `id` is needed for
|
|
236
232
|
// creating the cursor
|
|
237
233
|
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
234
|
+
if (dialect === "sqlite") {
|
|
235
|
+
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
236
|
+
}
|
|
238
237
|
}
|
|
239
238
|
// check for batch data
|
|
240
239
|
if (Array.isArray(data)) {
|
|
@@ -283,10 +282,16 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
283
282
|
if (paginationType === "cursor") {
|
|
284
283
|
data = wrapListPaginationCursor(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
285
284
|
postProcess(xs, fields, shouldRemoveExtraKeys);
|
|
285
|
+
if (dialect === "sqlite") {
|
|
286
|
+
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
287
|
+
}
|
|
286
288
|
}, input.artifacts[input.resource].primaryKey, totalCount);
|
|
287
289
|
}
|
|
288
290
|
else {
|
|
289
291
|
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
292
|
+
if (dialect === "sqlite") {
|
|
293
|
+
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
294
|
+
}
|
|
290
295
|
data = wrapListPaginationLimitOffset(data, totalCount);
|
|
291
296
|
}
|
|
292
297
|
}
|
|
@@ -472,16 +477,22 @@ async function create(input, dbCall, formatQuery, beginTransaction, dialect, con
|
|
|
472
477
|
}
|
|
473
478
|
else {
|
|
474
479
|
data = processCreateData(data, tableArtifacts, dialect, context?.specialCaseUuidColumn);
|
|
475
|
-
if (dialect === "
|
|
476
|
-
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
|
|
477
|
-
return inserted.insertId;
|
|
478
|
-
}
|
|
479
|
-
else {
|
|
480
|
+
if (dialect === "mssql") {
|
|
480
481
|
const columns = Object.keys(data);
|
|
481
482
|
const values = Object.values(data);
|
|
482
483
|
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
|
|
483
484
|
return inserted[0]["SCOPE_IDENTITY"];
|
|
484
485
|
}
|
|
486
|
+
else {
|
|
487
|
+
const columns = Object.keys(data);
|
|
488
|
+
const values = Object.values(data);
|
|
489
|
+
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?)`, [
|
|
490
|
+
input.resource,
|
|
491
|
+
columns,
|
|
492
|
+
values
|
|
493
|
+
]));
|
|
494
|
+
return inserted.insertId;
|
|
495
|
+
}
|
|
485
496
|
}
|
|
486
497
|
}
|
|
487
498
|
const id = await _create();
|
|
@@ -627,19 +638,19 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
|
627
638
|
const op = _entries?.[0];
|
|
628
639
|
const vv = _entries?.[1];
|
|
629
640
|
if (op === "$prepend") {
|
|
630
|
-
opsStrs.push(`${
|
|
641
|
+
opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${stringifyConcat(escape(vv), escapeId(k), dialect)} END`);
|
|
631
642
|
continue;
|
|
632
643
|
}
|
|
633
644
|
if (op === "$append") {
|
|
634
|
-
opsStrs.push(`${
|
|
645
|
+
opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${stringifyConcat(escapeId(k), escape(vv), dialect)} END`);
|
|
635
646
|
continue;
|
|
636
647
|
}
|
|
637
648
|
if (op === "$increment") {
|
|
638
|
-
opsStrs.push(`${
|
|
649
|
+
opsStrs.push(`${escapeId(k)} = ${escapeId(k)} + ${escape(vv)}`);
|
|
639
650
|
continue;
|
|
640
651
|
}
|
|
641
652
|
if (op === "$decrement") {
|
|
642
|
-
opsStrs.push(`${
|
|
653
|
+
opsStrs.push(`${escapeId(k)} = ${escapeId(k)} - ${escape(vv)}`);
|
|
643
654
|
continue;
|
|
644
655
|
}
|
|
645
656
|
dataRegular[k] = v;
|
|
@@ -649,18 +660,28 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
|
649
660
|
if (opsStrs.length > 0) {
|
|
650
661
|
q += `SET ${opsStrs.join(", ")}`;
|
|
651
662
|
}
|
|
652
|
-
|
|
663
|
+
const columns = Object.keys(dataRegular);
|
|
664
|
+
if (columns.length > 0) {
|
|
653
665
|
if (opsStrs.length > 0) {
|
|
654
|
-
q += ",
|
|
666
|
+
q += ", ";
|
|
655
667
|
}
|
|
656
668
|
else {
|
|
657
|
-
q += "SET
|
|
669
|
+
q += "SET ";
|
|
670
|
+
}
|
|
671
|
+
q += columns.map((x) => `${escapeId(x)} = ?`).join(", ");
|
|
672
|
+
for (let k in dataRegular) {
|
|
673
|
+
values.push(dataRegular[k]);
|
|
658
674
|
}
|
|
659
|
-
values.push(dataRegular);
|
|
660
675
|
}
|
|
661
676
|
q += ` WHERE ${where}`;
|
|
662
677
|
return formatQuery(q, values);
|
|
663
678
|
}
|
|
679
|
+
function stringifyConcat(operand1, operand2, dialect) {
|
|
680
|
+
if (dialect !== "sqlite") {
|
|
681
|
+
return `CONCAT(${operand1}, ${operand2})`;
|
|
682
|
+
}
|
|
683
|
+
return `(${operand1} || ${operand2})`;
|
|
684
|
+
}
|
|
664
685
|
async function updateMany(input, dbCall, formatQuery, dialect, cache) {
|
|
665
686
|
async function _updateMany() {
|
|
666
687
|
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
@@ -846,7 +867,7 @@ function removeExtraKeys(data, fields) {
|
|
|
846
867
|
}
|
|
847
868
|
for (let x of fields) {
|
|
848
869
|
if (typeof x === "object") {
|
|
849
|
-
const k =
|
|
870
|
+
const k = x.as ?? x.name;
|
|
850
871
|
if (Array.isArray(data)) {
|
|
851
872
|
for (let d of data) {
|
|
852
873
|
removeExtraKeys(d[k], x.fields);
|
|
@@ -858,6 +879,65 @@ function removeExtraKeys(data, fields) {
|
|
|
858
879
|
}
|
|
859
880
|
}
|
|
860
881
|
}
|
|
882
|
+
const getTypeCastMap = _.memoize(function getTypeCastMap(artifacts) {
|
|
883
|
+
const typeCastMap = new Map();
|
|
884
|
+
for (let table in artifacts) {
|
|
885
|
+
let booleanColumns = new Set();
|
|
886
|
+
for (let field of artifacts[table].fields) {
|
|
887
|
+
if (field.kind === "scalar" && field.type === "boolean") {
|
|
888
|
+
booleanColumns.add(field.name);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
typeCastMap.set(table, { boolean: booleanColumns });
|
|
892
|
+
}
|
|
893
|
+
return typeCastMap;
|
|
894
|
+
});
|
|
895
|
+
function typeCastSqlite(data, fields, table, artifacts) {
|
|
896
|
+
if (data == null || (Array.isArray(data) && data[0] == null)) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
const typeCastMap = getTypeCastMap(artifacts);
|
|
900
|
+
const booleanColumns = typeCastMap.get(table)?.boolean;
|
|
901
|
+
if (booleanColumns == null) {
|
|
902
|
+
throw new Error(`Failed to resolve typeCastMap for table \`${table}\``);
|
|
903
|
+
}
|
|
904
|
+
if (booleanColumns.size > 0) {
|
|
905
|
+
for (let field of fields) {
|
|
906
|
+
const name = typeof field === "string" ? field : field.name;
|
|
907
|
+
const as = typeof field === "string" ? undefined : field.as;
|
|
908
|
+
if (booleanColumns.has(name)) {
|
|
909
|
+
const k = as ?? name;
|
|
910
|
+
if (Array.isArray(data)) {
|
|
911
|
+
for (let d of data) {
|
|
912
|
+
d[k] = !!d[k];
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
else {
|
|
916
|
+
data[k] = !!data[k];
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const tableArtifacts = artifacts[table];
|
|
922
|
+
for (let x of fields) {
|
|
923
|
+
if (typeof x === "object") {
|
|
924
|
+
const name = x.name;
|
|
925
|
+
const relationFields = tableArtifacts.relationFields[name];
|
|
926
|
+
if (relationFields == null) {
|
|
927
|
+
throw new Error(`Failed to resolve relationFields for field \`${name}\``);
|
|
928
|
+
}
|
|
929
|
+
const k = x.as ?? name;
|
|
930
|
+
if (Array.isArray(data)) {
|
|
931
|
+
for (let d of data) {
|
|
932
|
+
typeCastSqlite(d[k], x.fields, relationFields.table, artifacts);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
else {
|
|
936
|
+
typeCastSqlite(data[k], x.fields, relationFields.table, artifacts);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
861
941
|
function whereNeedsProcessing(where) {
|
|
862
942
|
return JSON.stringify(where).includes("Uuid");
|
|
863
943
|
}
|
|
@@ -1,10 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.getEscape = exports.getEscapeId = exports.stringifyWhere = void 0;
|
|
4
|
-
const _ = require("lodash/fp");
|
|
5
|
-
const MySqlString = require("sqlstring");
|
|
6
|
-
// @ts-
|
|
7
|
-
const TSqlString = require("tsqlstring");
|
|
27
|
+
const _ = __importStar(require("lodash/fp"));
|
|
28
|
+
const MySqlString = __importStar(require("sqlstring"));
|
|
29
|
+
// @ts-expect-error
|
|
30
|
+
const TSqlString = __importStar(require("tsqlstring"));
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
const SqliteString = __importStar(require("sqlstring-sqlite"));
|
|
8
33
|
function stringifyWhere(input) {
|
|
9
34
|
const { where, table, dialect, args, orderBy, rowWithMatchingCursor } = input;
|
|
10
35
|
const escapeId = getEscapeId(dialect);
|
|
@@ -16,13 +41,11 @@ function stringifyWhere(input) {
|
|
|
16
41
|
orderBy,
|
|
17
42
|
escapeId,
|
|
18
43
|
escape,
|
|
19
|
-
rowWithMatchingCursor
|
|
44
|
+
rowWithMatchingCursor
|
|
20
45
|
});
|
|
21
46
|
if (paginationWhere) {
|
|
22
47
|
result =
|
|
23
|
-
result.length === 0
|
|
24
|
-
? paginationWhere
|
|
25
|
-
: result + " AND " + paginationWhere;
|
|
48
|
+
result.length === 0 ? paginationWhere : result + " AND " + paginationWhere;
|
|
26
49
|
}
|
|
27
50
|
return result;
|
|
28
51
|
}
|
|
@@ -84,9 +107,7 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
|
|
|
84
107
|
if (v === null) {
|
|
85
108
|
return `${table}.${escapeId(k)} IS NULL`;
|
|
86
109
|
}
|
|
87
|
-
if (typeof v === "string" ||
|
|
88
|
-
typeof v === "number" ||
|
|
89
|
-
typeof v === "boolean") {
|
|
110
|
+
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
|
|
90
111
|
return `${table}.${escapeId(k)} = ${printValue(v)}`;
|
|
91
112
|
}
|
|
92
113
|
if (_.isPlainObject(v)) {
|
|
@@ -213,6 +234,9 @@ function getEscapeId(dialect) {
|
|
|
213
234
|
if (dialect === "mssql") {
|
|
214
235
|
return TSqlString.escapeId.bind(TSqlString);
|
|
215
236
|
}
|
|
237
|
+
if (dialect === "sqlite") {
|
|
238
|
+
return SqliteString.escapeId.bind(SqliteString);
|
|
239
|
+
}
|
|
216
240
|
throw new Error("Unsupported dialect: " + dialect);
|
|
217
241
|
}
|
|
218
242
|
exports.getEscapeId = getEscapeId;
|
|
@@ -223,6 +247,9 @@ function getEscape(dialect) {
|
|
|
223
247
|
if (dialect === "mssql") {
|
|
224
248
|
return TSqlString.escape.bind(TSqlString);
|
|
225
249
|
}
|
|
250
|
+
if (dialect === "sqlite") {
|
|
251
|
+
return SqliteString.escape.bind(SqliteString);
|
|
252
|
+
}
|
|
226
253
|
throw new Error("Unsupported dialect: " + dialect);
|
|
227
254
|
}
|
|
228
255
|
exports.getEscape = getEscape;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|