@technicity/data-service-generator 0.6.0 → 0.6.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.
|
@@ -5,6 +5,8 @@ export declare class RuntimeKSQL implements IRuntime {
|
|
|
5
5
|
#private;
|
|
6
6
|
constructor(clientOpts: ConstructorParameters<typeof KSQL>[0], otherOpts: {
|
|
7
7
|
getBaseTableName: TGetTableName;
|
|
8
|
+
getMaterializedViewName: TGetTableName;
|
|
9
|
+
doNotUseMaterializedViews?: boolean;
|
|
8
10
|
_dbCall: TDbCall;
|
|
9
11
|
}, artifacts: IArtifacts);
|
|
10
12
|
resolve(input: TResolveParams): Promise<any>;
|
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _RuntimeKSQL_ksql, _RuntimeKSQL_middlewareHandler, _RuntimeKSQL_dbCall, _RuntimeKSQL_formatQuery, _RuntimeKSQL_getBaseTableName;
|
|
13
|
+
var _RuntimeKSQL_ksql, _RuntimeKSQL_middlewareHandler, _RuntimeKSQL_dbCall, _RuntimeKSQL_formatQuery, _RuntimeKSQL_getBaseTableName, _RuntimeKSQL_getMaterializedViewName, _RuntimeKSQL_doNotUseMaterializedViews;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.RuntimeKSQL = void 0;
|
|
16
16
|
const SqlString = require("sqlstring");
|
|
@@ -30,14 +30,20 @@ class RuntimeKSQL {
|
|
|
30
30
|
_RuntimeKSQL_dbCall.set(this, void 0);
|
|
31
31
|
_RuntimeKSQL_formatQuery.set(this, void 0);
|
|
32
32
|
_RuntimeKSQL_getBaseTableName.set(this, void 0);
|
|
33
|
+
_RuntimeKSQL_getMaterializedViewName.set(this, void 0);
|
|
34
|
+
_RuntimeKSQL_doNotUseMaterializedViews.set(this, void 0);
|
|
33
35
|
__classPrivateFieldSet(this, _RuntimeKSQL_ksql, new ksql_1.KSQL(clientOpts ?? {}), "f");
|
|
34
36
|
__classPrivateFieldSet(this, _RuntimeKSQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
|
|
35
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_dbCall, otherOpts._dbCall
|
|
37
|
+
__classPrivateFieldSet(this, _RuntimeKSQL_dbCall, typeof otherOpts._dbCall === "function"
|
|
38
|
+
? (q) => otherOpts._dbCall(q).then(mapResult)
|
|
39
|
+
: (q) => __classPrivateFieldGet(this, _RuntimeKSQL_ksql, "f").streamQuery(q).then(mapResult), "f");
|
|
36
40
|
__classPrivateFieldSet(this, _RuntimeKSQL_getBaseTableName, otherOpts.getBaseTableName, "f");
|
|
41
|
+
__classPrivateFieldSet(this, _RuntimeKSQL_getMaterializedViewName, otherOpts.getMaterializedViewName, "f");
|
|
42
|
+
__classPrivateFieldSet(this, _RuntimeKSQL_doNotUseMaterializedViews, otherOpts.doNotUseMaterializedViews ?? false, "f");
|
|
37
43
|
__classPrivateFieldSet(this, _RuntimeKSQL_formatQuery, SqlString.format.bind(SqlString), "f");
|
|
38
44
|
}
|
|
39
45
|
async resolve(input) {
|
|
40
|
-
return resolve(input, __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_formatQuery, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_middlewareHandler, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getBaseTableName, "f"));
|
|
46
|
+
return resolve(input, __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_formatQuery, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_middlewareHandler, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getBaseTableName, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getMaterializedViewName, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_doNotUseMaterializedViews, "f"));
|
|
41
47
|
}
|
|
42
48
|
async $queryRaw(sql, values) {
|
|
43
49
|
return __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").call(this, SqlString.format(sql, values ?? []));
|
|
@@ -53,8 +59,8 @@ class RuntimeKSQL {
|
|
|
53
59
|
}
|
|
54
60
|
}
|
|
55
61
|
exports.RuntimeKSQL = RuntimeKSQL;
|
|
56
|
-
_RuntimeKSQL_ksql = new WeakMap(), _RuntimeKSQL_middlewareHandler = new WeakMap(), _RuntimeKSQL_dbCall = new WeakMap(), _RuntimeKSQL_formatQuery = new WeakMap(), _RuntimeKSQL_getBaseTableName = new WeakMap();
|
|
57
|
-
async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTableName) {
|
|
62
|
+
_RuntimeKSQL_ksql = new WeakMap(), _RuntimeKSQL_middlewareHandler = new WeakMap(), _RuntimeKSQL_dbCall = new WeakMap(), _RuntimeKSQL_formatQuery = new WeakMap(), _RuntimeKSQL_getBaseTableName = new WeakMap(), _RuntimeKSQL_getMaterializedViewName = new WeakMap(), _RuntimeKSQL_doNotUseMaterializedViews = new WeakMap();
|
|
63
|
+
async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
58
64
|
// https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
|
|
59
65
|
let index = -1;
|
|
60
66
|
if (middlewareHandler.length() > 0) {
|
|
@@ -66,21 +72,21 @@ async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTab
|
|
|
66
72
|
return nextMiddleware(paramsMaybeMutated, consumer);
|
|
67
73
|
}
|
|
68
74
|
const paramsChanged = { ...input, ...params };
|
|
69
|
-
return _resolve(paramsChanged, dbCall, formatQuery, getBaseTableName);
|
|
75
|
+
return _resolve(paramsChanged, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
70
76
|
};
|
|
71
77
|
return resource.runInAsyncScope(() => consumer(params));
|
|
72
78
|
}
|
|
73
|
-
return _resolve(input, dbCall, formatQuery, getBaseTableName);
|
|
79
|
+
return _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
74
80
|
}
|
|
75
|
-
function _resolve(input, dbCall, formatQuery, getBaseTableName) {
|
|
81
|
+
function _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
76
82
|
if (input.action === "findUnique") {
|
|
77
|
-
return getData(input, dbCall, formatQuery, getBaseTableName);
|
|
83
|
+
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
78
84
|
}
|
|
79
85
|
if (input.action === "findMany") {
|
|
80
|
-
return getData(input, dbCall, formatQuery, getBaseTableName);
|
|
86
|
+
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
81
87
|
}
|
|
82
88
|
if (input.action === "findManyPaginated") {
|
|
83
|
-
return getData(input, dbCall, formatQuery, getBaseTableName);
|
|
89
|
+
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
84
90
|
}
|
|
85
91
|
if (input.action === "create") {
|
|
86
92
|
throw new Error("Not implemented.");
|
|
@@ -99,34 +105,46 @@ function _resolve(input, dbCall, formatQuery, getBaseTableName) {
|
|
|
99
105
|
}
|
|
100
106
|
throw new Error("Invalid action: " + input.action);
|
|
101
107
|
}
|
|
102
|
-
async function getData(input, dbCall, formatQuery, getBaseTableName) {
|
|
108
|
+
async function getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
103
109
|
const { action } = input;
|
|
104
110
|
const grabMany = action === "findMany" ||
|
|
105
111
|
action === "findManyPaginated" ||
|
|
106
112
|
action === "updateMany" ||
|
|
107
113
|
action === "deleteMany";
|
|
108
|
-
const data = await _getData(input, grabMany, dbCall, formatQuery, getBaseTableName);
|
|
114
|
+
const data = await _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
109
115
|
if (data == null && !grabMany) {
|
|
110
116
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
111
117
|
}
|
|
112
|
-
|
|
113
|
-
// specified, since otherwise there wouldn't be extra keys
|
|
114
|
-
const shouldRemoveExtraKeys = input.fields != null;
|
|
118
|
+
const scalarFields = input.artifacts[input.resource].scalarFields;
|
|
115
119
|
if (action === "findManyPaginated" && data?.results != null) {
|
|
116
|
-
(0, shared_1.postProcess)(data.results, input.fields ??
|
|
120
|
+
(0, shared_1.postProcess)(data.results, input.fields ?? scalarFields, true);
|
|
117
121
|
}
|
|
118
122
|
else {
|
|
119
|
-
(0, shared_1.postProcess)(data, input.fields ??
|
|
123
|
+
(0, shared_1.postProcess)(data, input.fields ?? scalarFields, true);
|
|
120
124
|
}
|
|
121
125
|
return data;
|
|
122
126
|
}
|
|
123
|
-
async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName) {
|
|
127
|
+
async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
124
128
|
const { resource: table, artifacts, args, action } = input;
|
|
125
129
|
const tableArtifacts = artifacts[table];
|
|
130
|
+
const isInputFieldAnArray = Array.isArray(input.fields);
|
|
126
131
|
let fields = input.fields;
|
|
127
132
|
if (!Array.isArray(fields)) {
|
|
128
133
|
fields = tableArtifacts.scalarFields;
|
|
129
134
|
}
|
|
135
|
+
let useMaterializedView = !doNotUseMaterializedViews &&
|
|
136
|
+
// If fields isn't an array, we're only getting scalar fields,
|
|
137
|
+
// so no point using the materialized view.
|
|
138
|
+
isInputFieldAnArray &&
|
|
139
|
+
// We're getting relation fields
|
|
140
|
+
fields.some((x) => typeof x === "object") &&
|
|
141
|
+
Object.values(tableArtifacts.relationFields).filter((x) => x.type === "one-to-many__many-to-one" && x.kind === "many-to-one").length > 0 &&
|
|
142
|
+
!Object.values(tableArtifacts.relationFields).some((x) => x.type === "one-to-many__many-to-one" &&
|
|
143
|
+
x.kind === "many-to-one" &&
|
|
144
|
+
x.nullable === true);
|
|
145
|
+
const getTableName = useMaterializedView
|
|
146
|
+
? getMaterializedViewName
|
|
147
|
+
: getBaseTableName;
|
|
130
148
|
let where = undefined;
|
|
131
149
|
if (args?.$where != null) {
|
|
132
150
|
let argsMapped = args;
|
|
@@ -136,7 +154,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
|
|
|
136
154
|
}
|
|
137
155
|
const whereResult = (0, stringifyWhere_1.stringifyWhere)({
|
|
138
156
|
where: argsMapped.$where,
|
|
139
|
-
table: escapeId(
|
|
157
|
+
table: escapeId(getTableName(table)),
|
|
140
158
|
dialect: "mysql",
|
|
141
159
|
args: argsMapped,
|
|
142
160
|
});
|
|
@@ -161,15 +179,19 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
|
|
|
161
179
|
}
|
|
162
180
|
}
|
|
163
181
|
else {
|
|
164
|
-
relationFields.push(x);
|
|
165
182
|
const relationField = tableArtifacts.relationFields?.[x.name];
|
|
166
|
-
if (relationField
|
|
167
|
-
|
|
168
|
-
|
|
183
|
+
if (relationField == null) {
|
|
184
|
+
throw new Error(`Invalid relation field: ${x.name}`);
|
|
185
|
+
}
|
|
186
|
+
if (relationField.type === "one-to-many__many-to-one") {
|
|
187
|
+
relationFields.push(x);
|
|
188
|
+
if (!columns.includes(relationField.relation.foreignKey)) {
|
|
169
189
|
columns.push(relationField.relation.foreignKey);
|
|
170
190
|
}
|
|
171
|
-
|
|
172
|
-
|
|
191
|
+
}
|
|
192
|
+
else if (relationField.type === "many-to-many") {
|
|
193
|
+
relationFields.push(x);
|
|
194
|
+
if (!columns.includes(relationField.relations[0].referencedKey)) {
|
|
173
195
|
columns.push(relationField.relations[0].referencedKey);
|
|
174
196
|
}
|
|
175
197
|
}
|
|
@@ -181,7 +203,25 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
|
|
|
181
203
|
columns.push(tableArtifacts.primaryKey);
|
|
182
204
|
}
|
|
183
205
|
}
|
|
184
|
-
|
|
206
|
+
if (useMaterializedView) {
|
|
207
|
+
relationFields = relationFields.filter((x) => {
|
|
208
|
+
if (typeof x === "string") {
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
const relationField = tableArtifacts.relationFields?.[x.name];
|
|
212
|
+
if (relationField == null) {
|
|
213
|
+
throw new Error(`Invalid relation field: ${x.name}`);
|
|
214
|
+
}
|
|
215
|
+
// Since we're going to use the materialized view,
|
|
216
|
+
// filter out non-nullable many-to-one relations.
|
|
217
|
+
return !(relationField.type === "one-to-many__many-to-one" &&
|
|
218
|
+
relationField.kind === "many-to-one" &&
|
|
219
|
+
relationField.nullable === false);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
let s = useMaterializedView
|
|
223
|
+
? formatQuery(`SELECT * FROM ??`, [getTableName(table)])
|
|
224
|
+
: formatQuery(`SELECT ?? FROM ??`, [columns, getTableName(table)]);
|
|
185
225
|
if (where) {
|
|
186
226
|
s += ` WHERE ${where}`;
|
|
187
227
|
}
|
|
@@ -215,7 +255,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
|
|
|
215
255
|
result = _.orderBy(result, orderBy.map((x) => x.column), orderBy.map((x) => x.direction));
|
|
216
256
|
if (action === "findManyPaginated") {
|
|
217
257
|
result = paginate(result, args, tableArtifacts.primaryKey, action);
|
|
218
|
-
return Promise.all(result.results.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(x, xx)))).then((xx) => {
|
|
258
|
+
return Promise.all(result.results.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(x, xx)))).then((xx) => {
|
|
219
259
|
result.results = xx;
|
|
220
260
|
return result;
|
|
221
261
|
});
|
|
@@ -224,12 +264,12 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
|
|
|
224
264
|
if (typeof limit === "number") {
|
|
225
265
|
result = result.slice(0, limit);
|
|
226
266
|
}
|
|
227
|
-
return Promise.all(result.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(x, xx))));
|
|
267
|
+
return Promise.all(result.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(x, xx))));
|
|
228
268
|
}
|
|
229
|
-
return resolveDependentFields(table, result, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(result, xx));
|
|
269
|
+
return resolveDependentFields(table, result, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(result, xx));
|
|
230
270
|
});
|
|
231
271
|
}
|
|
232
|
-
async function resolveDependentFields(table, parentData, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName) {
|
|
272
|
+
async function resolveDependentFields(table, parentData, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
233
273
|
const tableArtifacts = artifacts[table];
|
|
234
274
|
return Promise.all([]
|
|
235
275
|
.concat(mappedFields.map((x) => {
|
|
@@ -305,7 +345,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
|
|
|
305
345
|
fields: x.fields,
|
|
306
346
|
artifacts,
|
|
307
347
|
action: relationField.grabMany ? "findMany" : "findUnique",
|
|
308
|
-
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
348
|
+
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
309
349
|
});
|
|
310
350
|
}
|
|
311
351
|
if (relationField.type === "one-to-many__many-to-one") {
|
|
@@ -328,7 +368,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
|
|
|
328
368
|
fields: x.fields,
|
|
329
369
|
artifacts,
|
|
330
370
|
action: relationField.grabMany ? "findMany" : "findUnique",
|
|
331
|
-
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
371
|
+
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
332
372
|
}
|
|
333
373
|
throw new Error(`Invalid relationField.type`);
|
|
334
374
|
})));
|
|
@@ -386,3 +426,13 @@ function paginate(data, args, primaryKey, action) {
|
|
|
386
426
|
}
|
|
387
427
|
throw new Error(`Invalid pagination action: ${action}`);
|
|
388
428
|
}
|
|
429
|
+
function mapResult(data) {
|
|
430
|
+
return data.map((x) => {
|
|
431
|
+
for (let k of Object.keys(x)) {
|
|
432
|
+
if (typeof x[k] === "string" && (k === "uuid" || k.endsWith("Uuid"))) {
|
|
433
|
+
x[k] = x[k].toLocaleLowerCase();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return x;
|
|
437
|
+
});
|
|
438
|
+
}
|