@technicity/data-service-generator 0.13.26 → 0.14.0
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/dist/generation/generate.js +172 -82
- package/dist/runtime/IRuntime.d.ts +14 -14
- package/dist/runtime/lib/getSqlAst.js +120 -108
- package/dist/runtime/lib/shared.d.ts +1 -2
- package/dist/runtime/lib/shared.js +38 -41
- package/dist/traverseFieldArgs.d.ts +2 -2
- package/dist/traverseFieldArgs.js +8 -3
- package/dist/traverseFieldArgs.test.js +37 -53
- package/package.json +1 -1
- package/dist/ksql.d.ts +0 -15
- package/dist/ksql.js +0 -78
- package/dist/runtime/RuntimeKSQL.d.ts +0 -26
- package/dist/runtime/RuntimeKSQL.js +0 -476
- package/dist/runtime/lib/runTransforms.d.ts +0 -2
- package/dist/runtime/lib/runTransforms.js +0 -36
- package/dist/runtime/lib/runTransforms.test.d.ts +0 -1
- package/dist/runtime/lib/runTransforms.test.js +0 -112
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { IRuntime, TMiddleware, TResolveParams, IArtifacts, TDbCall } from "./IRuntime";
|
|
2
|
-
import { KSQL } from "../ksql";
|
|
3
|
-
declare type TGetTableName = (table: string) => string;
|
|
4
|
-
export declare class RuntimeKSQL implements IRuntime {
|
|
5
|
-
#private;
|
|
6
|
-
constructor(clientOpts: ConstructorParameters<typeof KSQL>[0], otherOpts: {
|
|
7
|
-
getBaseTableName: TGetTableName;
|
|
8
|
-
getMaterializedViewName: TGetTableName;
|
|
9
|
-
doNotUseMaterializedViews?: boolean;
|
|
10
|
-
_dbCall: TDbCall;
|
|
11
|
-
}, artifacts: IArtifacts);
|
|
12
|
-
resolve(input: TResolveParams): Promise<any>;
|
|
13
|
-
$queryRaw(sql: string, values?: any[]): Promise<any>;
|
|
14
|
-
$use(middleware: TMiddleware): Promise<void>;
|
|
15
|
-
$whereNeedsProcessing(where: any): boolean;
|
|
16
|
-
$prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
|
|
17
|
-
$shutdown(): Promise<void>;
|
|
18
|
-
$startTransaction(input?: {
|
|
19
|
-
isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
|
|
20
|
-
}): Promise<{
|
|
21
|
-
dbCall: (q: string) => Promise<string>;
|
|
22
|
-
commit: () => Promise<void>;
|
|
23
|
-
rollback: () => Promise<void>;
|
|
24
|
-
}>;
|
|
25
|
-
}
|
|
26
|
-
export {};
|
|
@@ -1,476 +0,0 @@
|
|
|
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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
26
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
28
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
29
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
30
|
-
};
|
|
31
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
32
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
33
|
-
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");
|
|
34
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
|
-
};
|
|
36
|
-
var _RuntimeKSQL_ksql, _RuntimeKSQL_middlewareHandler, _RuntimeKSQL_dbCall, _RuntimeKSQL_formatQuery, _RuntimeKSQL_getBaseTableName, _RuntimeKSQL_getMaterializedViewName, _RuntimeKSQL_doNotUseMaterializedViews;
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.RuntimeKSQL = void 0;
|
|
39
|
-
const SqlString = __importStar(require("sqlstring"));
|
|
40
|
-
const async_hooks_1 = require("async_hooks");
|
|
41
|
-
const _ = __importStar(require("lodash"));
|
|
42
|
-
const graphql_relay_1 = require("graphql-relay");
|
|
43
|
-
const shared_1 = require("./lib/shared");
|
|
44
|
-
const ksql_1 = require("../ksql");
|
|
45
|
-
const SDKNotFoundError_1 = require("./lib/SDKNotFoundError");
|
|
46
|
-
const stringifyWhere_1 = require("./lib/stringifyWhere");
|
|
47
|
-
const cursor_1 = require("./lib/cursor");
|
|
48
|
-
const escapeId = SqlString.escapeId.bind(SqlString);
|
|
49
|
-
class RuntimeKSQL {
|
|
50
|
-
constructor(clientOpts, otherOpts, artifacts) {
|
|
51
|
-
_RuntimeKSQL_ksql.set(this, void 0);
|
|
52
|
-
_RuntimeKSQL_middlewareHandler.set(this, void 0);
|
|
53
|
-
_RuntimeKSQL_dbCall.set(this, void 0);
|
|
54
|
-
_RuntimeKSQL_formatQuery.set(this, void 0);
|
|
55
|
-
_RuntimeKSQL_getBaseTableName.set(this, void 0);
|
|
56
|
-
_RuntimeKSQL_getMaterializedViewName.set(this, void 0);
|
|
57
|
-
_RuntimeKSQL_doNotUseMaterializedViews.set(this, void 0);
|
|
58
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_ksql, new ksql_1.KSQL(clientOpts ?? {}), "f");
|
|
59
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
|
|
60
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_dbCall, typeof otherOpts._dbCall === "function"
|
|
61
|
-
? (q) => otherOpts._dbCall(q).then(mapResult)
|
|
62
|
-
: (q) => __classPrivateFieldGet(this, _RuntimeKSQL_ksql, "f").streamQuery(q).then(mapResult), "f");
|
|
63
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_getBaseTableName, otherOpts.getBaseTableName, "f");
|
|
64
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_getMaterializedViewName, otherOpts.getMaterializedViewName, "f");
|
|
65
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_doNotUseMaterializedViews, otherOpts.doNotUseMaterializedViews ?? false, "f");
|
|
66
|
-
__classPrivateFieldSet(this, _RuntimeKSQL_formatQuery, SqlString.format.bind(SqlString), "f");
|
|
67
|
-
}
|
|
68
|
-
async resolve(input) {
|
|
69
|
-
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"));
|
|
70
|
-
}
|
|
71
|
-
async $queryRaw(sql, values) {
|
|
72
|
-
return __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").call(this, SqlString.format(sql, values ?? []));
|
|
73
|
-
}
|
|
74
|
-
async $use(middleware) {
|
|
75
|
-
__classPrivateFieldGet(this, _RuntimeKSQL_middlewareHandler, "f").register(middleware);
|
|
76
|
-
}
|
|
77
|
-
$whereNeedsProcessing(where) {
|
|
78
|
-
return (0, shared_1.whereNeedsProcessing)(where);
|
|
79
|
-
}
|
|
80
|
-
async $prepareWhere(artifacts, table, data) {
|
|
81
|
-
return (0, shared_1._prepareWhere)(artifacts, table, data, __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_formatQuery, "f").bind(this));
|
|
82
|
-
}
|
|
83
|
-
async $shutdown() {
|
|
84
|
-
// Nothing to do here, I think
|
|
85
|
-
}
|
|
86
|
-
async $startTransaction(input) {
|
|
87
|
-
throw new Error("Not implemented.");
|
|
88
|
-
return {
|
|
89
|
-
dbCall: async (q) => "",
|
|
90
|
-
commit: async () => { },
|
|
91
|
-
rollback: async () => { }
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
exports.RuntimeKSQL = RuntimeKSQL;
|
|
96
|
-
_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();
|
|
97
|
-
async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
98
|
-
// https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
|
|
99
|
-
let index = -1;
|
|
100
|
-
if (middlewareHandler.length() > 0) {
|
|
101
|
-
const resource = new async_hooks_1.AsyncResource("sdk-request");
|
|
102
|
-
const params = input;
|
|
103
|
-
const consumer = (paramsMaybeMutated) => {
|
|
104
|
-
const nextMiddleware = middlewareHandler.get(++index);
|
|
105
|
-
if (nextMiddleware != null) {
|
|
106
|
-
return nextMiddleware(paramsMaybeMutated, consumer);
|
|
107
|
-
}
|
|
108
|
-
const paramsChanged = { ...input, ...params };
|
|
109
|
-
return _resolve(paramsChanged, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
110
|
-
};
|
|
111
|
-
return resource.runInAsyncScope(() => consumer(params));
|
|
112
|
-
}
|
|
113
|
-
return _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
114
|
-
}
|
|
115
|
-
function _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
116
|
-
if (input.action === "findUnique") {
|
|
117
|
-
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
118
|
-
}
|
|
119
|
-
if (input.action === "findMany") {
|
|
120
|
-
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
121
|
-
}
|
|
122
|
-
if (input.action === "findManyPaginated") {
|
|
123
|
-
return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
124
|
-
}
|
|
125
|
-
if (input.action === "create") {
|
|
126
|
-
throw new Error("Not implemented.");
|
|
127
|
-
}
|
|
128
|
-
if (input.action === "update") {
|
|
129
|
-
throw new Error("Not implemented.");
|
|
130
|
-
}
|
|
131
|
-
if (input.action === "updateMany") {
|
|
132
|
-
throw new Error("Not implemented.");
|
|
133
|
-
}
|
|
134
|
-
if (input.action === "delete") {
|
|
135
|
-
throw new Error("Not implemented.");
|
|
136
|
-
}
|
|
137
|
-
if (input.action === "deleteMany") {
|
|
138
|
-
throw new Error("Not implemented.");
|
|
139
|
-
}
|
|
140
|
-
throw new Error("Invalid action: " + input.action);
|
|
141
|
-
}
|
|
142
|
-
async function getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
143
|
-
const { action } = input;
|
|
144
|
-
const grabMany = action === "findMany" ||
|
|
145
|
-
action === "findManyPaginated" ||
|
|
146
|
-
action === "updateMany" ||
|
|
147
|
-
action === "deleteMany";
|
|
148
|
-
const data = await _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
|
|
149
|
-
if (data == null && !grabMany) {
|
|
150
|
-
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
151
|
-
}
|
|
152
|
-
const scalarFields = input.artifacts[input.resource].scalarFields;
|
|
153
|
-
if (action === "findManyPaginated" && data?.results != null) {
|
|
154
|
-
(0, shared_1.postProcess)(data.results, input.fields ?? scalarFields, true);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
(0, shared_1.postProcess)(data, input.fields ?? scalarFields, true);
|
|
158
|
-
}
|
|
159
|
-
return data;
|
|
160
|
-
}
|
|
161
|
-
async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
162
|
-
const { resource: table, artifacts, args, action } = input;
|
|
163
|
-
const tableArtifacts = artifacts[table];
|
|
164
|
-
const isInputFieldAnArray = Array.isArray(input.fields);
|
|
165
|
-
let fields = input.fields;
|
|
166
|
-
if (!Array.isArray(fields)) {
|
|
167
|
-
fields = tableArtifacts.scalarFields;
|
|
168
|
-
}
|
|
169
|
-
let useMaterializedView = !doNotUseMaterializedViews &&
|
|
170
|
-
// If fields isn't an array, we're only getting scalar fields,
|
|
171
|
-
// so no point using the materialized view.
|
|
172
|
-
isInputFieldAnArray &&
|
|
173
|
-
// We're getting relation fields
|
|
174
|
-
fields.some((x) => typeof x === "object") &&
|
|
175
|
-
Object.values(tableArtifacts.relationFields).filter((x) => x.type === "one-to-many__many-to-one" && x.kind === "many-to-one").length > 0 &&
|
|
176
|
-
!Object.values(tableArtifacts.relationFields).some((x) => x.type === "one-to-many__many-to-one" &&
|
|
177
|
-
x.kind === "many-to-one" &&
|
|
178
|
-
x.nullable === true);
|
|
179
|
-
const getTableName = useMaterializedView
|
|
180
|
-
? getMaterializedViewName
|
|
181
|
-
: getBaseTableName;
|
|
182
|
-
let where = undefined;
|
|
183
|
-
if (args?.$where != null) {
|
|
184
|
-
let argsMapped = args;
|
|
185
|
-
if (typeof argsMapped.$where === "object" && argsMapped.$where[table] != null) {
|
|
186
|
-
argsMapped = _.cloneDeep(argsMapped);
|
|
187
|
-
argsMapped.$where = argsMapped.$where[table];
|
|
188
|
-
}
|
|
189
|
-
const whereResult = (0, stringifyWhere_1.stringifyWhere)({
|
|
190
|
-
where: argsMapped.$where,
|
|
191
|
-
table: escapeId(getTableName(table)),
|
|
192
|
-
dialect: "mysql",
|
|
193
|
-
args: argsMapped
|
|
194
|
-
});
|
|
195
|
-
if (whereResult) {
|
|
196
|
-
where = whereResult;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
let columns = [];
|
|
200
|
-
let mappedFields = [];
|
|
201
|
-
let relationFields = [];
|
|
202
|
-
for (let x of fields) {
|
|
203
|
-
if (typeof x === "string") {
|
|
204
|
-
const mappedField = tableArtifacts.mappedFields?.[x];
|
|
205
|
-
if (mappedField != null) {
|
|
206
|
-
mappedFields.push(x);
|
|
207
|
-
if (!columns.includes(mappedField.foreignKey)) {
|
|
208
|
-
columns.push(mappedField.foreignKey);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
columns.push(x);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
const relationField = tableArtifacts.relationFields?.[x.name];
|
|
217
|
-
if (relationField == null) {
|
|
218
|
-
throw new Error(`Invalid relation field: ${x.name}`);
|
|
219
|
-
}
|
|
220
|
-
if (relationField.type === "one-to-many__many-to-one") {
|
|
221
|
-
relationFields.push(x);
|
|
222
|
-
if (!columns.includes(relationField.relation.foreignKey)) {
|
|
223
|
-
columns.push(relationField.relation.foreignKey);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else if (relationField.type === "many-to-many") {
|
|
227
|
-
relationFields.push(x);
|
|
228
|
-
if (!columns.includes(relationField.relations[0].referencedKey)) {
|
|
229
|
-
columns.push(relationField.relations[0].referencedKey);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (action === "findManyPaginated") {
|
|
235
|
-
// For cursor
|
|
236
|
-
if (!columns.includes(tableArtifacts.primaryKey)) {
|
|
237
|
-
columns.push(tableArtifacts.primaryKey);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
if (useMaterializedView) {
|
|
241
|
-
relationFields = relationFields.filter((x) => {
|
|
242
|
-
if (typeof x === "string") {
|
|
243
|
-
return true;
|
|
244
|
-
}
|
|
245
|
-
const relationField = tableArtifacts.relationFields?.[x.name];
|
|
246
|
-
if (relationField == null) {
|
|
247
|
-
throw new Error(`Invalid relation field: ${x.name}`);
|
|
248
|
-
}
|
|
249
|
-
// Since we're going to use the materialized view,
|
|
250
|
-
// filter out non-nullable many-to-one relations.
|
|
251
|
-
return !(relationField.type === "one-to-many__many-to-one" &&
|
|
252
|
-
relationField.kind === "many-to-one" &&
|
|
253
|
-
relationField.nullable === false);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
let s = useMaterializedView
|
|
257
|
-
? formatQuery(`SELECT * FROM ??`, [getTableName(table)])
|
|
258
|
-
: formatQuery(`SELECT ?? FROM ??`, [columns, getTableName(table)]);
|
|
259
|
-
if (where) {
|
|
260
|
-
s += ` WHERE ${where}`;
|
|
261
|
-
}
|
|
262
|
-
s += ";";
|
|
263
|
-
return dbCall(s).then((_result) => {
|
|
264
|
-
let result = grabMany ? _result : _result[0];
|
|
265
|
-
if (result == null) {
|
|
266
|
-
return null;
|
|
267
|
-
}
|
|
268
|
-
if (grabMany && Array.isArray(result)) {
|
|
269
|
-
// Important: ordering must be before pagination, since pagination is done in code
|
|
270
|
-
let orderBy;
|
|
271
|
-
if (args == null || args.$orderBy == null) {
|
|
272
|
-
orderBy = [{ column: tableArtifacts.primaryKey, direction: "asc" }];
|
|
273
|
-
}
|
|
274
|
-
else if (Array.isArray(args.$orderBy)) {
|
|
275
|
-
orderBy = args.$orderBy.map((x) => {
|
|
276
|
-
const [column, direction] = Object.entries(x)[0];
|
|
277
|
-
return { column, direction };
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
const [column, direction] = Object.entries(args.$orderBy)[0];
|
|
282
|
-
orderBy = [{ column, direction }];
|
|
283
|
-
}
|
|
284
|
-
for (let x of orderBy) {
|
|
285
|
-
if (!["asc", "desc"].includes(x.direction)) {
|
|
286
|
-
throw new Error(`Expected \`asc\` or \`desc\` for \`direction\` but got: ${x.direction}`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
result = _.orderBy(result, orderBy.map((x) => x.column), orderBy.map((x) => x.direction));
|
|
290
|
-
if (action === "findManyPaginated") {
|
|
291
|
-
result = paginate(result, args, tableArtifacts.primaryKey, action);
|
|
292
|
-
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) => {
|
|
293
|
-
result.results = xx;
|
|
294
|
-
return result;
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
const limit = args?.$limit;
|
|
298
|
-
if (typeof limit === "number") {
|
|
299
|
-
result = result.slice(0, limit);
|
|
300
|
-
}
|
|
301
|
-
return Promise.all(result.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(x, xx))));
|
|
302
|
-
}
|
|
303
|
-
return resolveDependentFields(table, result, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(result, xx));
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
async function resolveDependentFields(table, parentData, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
|
|
307
|
-
const tableArtifacts = artifacts[table];
|
|
308
|
-
return Promise.all([]
|
|
309
|
-
.concat(mappedFields.map((x) => {
|
|
310
|
-
const mappedField = tableArtifacts.mappedFields?.[x];
|
|
311
|
-
if (mappedField == null) {
|
|
312
|
-
throw new Error(`Invalid mappedField: ${x}`);
|
|
313
|
-
}
|
|
314
|
-
if (parentData?.[mappedField.foreignKey] == null) {
|
|
315
|
-
return { [x]: null };
|
|
316
|
-
}
|
|
317
|
-
const s = formatQuery(`SELECT ?? FROM ?? WHERE ?? = ?;`, [
|
|
318
|
-
mappedField.name,
|
|
319
|
-
getBaseTableName(mappedField.referencedTable),
|
|
320
|
-
mappedField.referencedKey,
|
|
321
|
-
parentData?.[mappedField.foreignKey]
|
|
322
|
-
]);
|
|
323
|
-
return dbCall(s).then((xs) => ({
|
|
324
|
-
[x]: xs?.[0]?.[mappedField.name] ?? null
|
|
325
|
-
}));
|
|
326
|
-
}))
|
|
327
|
-
.concat(relationFields.map((x) => {
|
|
328
|
-
if (typeof x === "string") {
|
|
329
|
-
throw new Error(`Invalid field: ${x}`);
|
|
330
|
-
}
|
|
331
|
-
const relationField = tableArtifacts.relationFields?.[x.name];
|
|
332
|
-
if (relationField == null) {
|
|
333
|
-
throw new Error("Invalid field: " + table + "." + x.name);
|
|
334
|
-
}
|
|
335
|
-
if (relationField.type === "many-to-many") {
|
|
336
|
-
const junctionReferencedKey = relationField.relations[0].referencedKey;
|
|
337
|
-
const junctionKeyValue = parentData?.[junctionReferencedKey];
|
|
338
|
-
if (junctionKeyValue == null) {
|
|
339
|
-
return { [x.as ?? x.name]: [] };
|
|
340
|
-
}
|
|
341
|
-
let whereJunction = typeof x?.args?.$where === "object" &&
|
|
342
|
-
x.args.$where != null &&
|
|
343
|
-
x.args.$where[relationField.junctionTable] != null
|
|
344
|
-
? _.cloneDeep(x.args.$where[relationField.junctionTable])
|
|
345
|
-
: {};
|
|
346
|
-
whereJunction = {
|
|
347
|
-
$and: [
|
|
348
|
-
{ [relationField.relations[0].foreignKey]: junctionKeyValue },
|
|
349
|
-
whereJunction
|
|
350
|
-
]
|
|
351
|
-
};
|
|
352
|
-
const key = relationField.relations[1].foreignKey;
|
|
353
|
-
const s = formatQuery(`SELECT ?? FROM ?? WHERE ${(0, stringifyWhere_1.stringifyWhere)({
|
|
354
|
-
where: whereJunction,
|
|
355
|
-
table: escapeId(getBaseTableName(relationField.junctionTable)),
|
|
356
|
-
dialect: "mysql",
|
|
357
|
-
// Since we're paginating, empty is fine
|
|
358
|
-
args: {}
|
|
359
|
-
})};`, [key, getBaseTableName(relationField.junctionTable)]);
|
|
360
|
-
return dbCall(s)
|
|
361
|
-
.then((xs) => xs.map((x) => x?.[key]))
|
|
362
|
-
.then((keys) => {
|
|
363
|
-
const whereDest = {
|
|
364
|
-
[relationField.relations[1].referencedKey]: { $in: keys }
|
|
365
|
-
};
|
|
366
|
-
const argsMapped = x.args == null ? {} : _.cloneDeep(x.args);
|
|
367
|
-
if (typeof argsMapped?.$where === "object" &&
|
|
368
|
-
argsMapped.$where != null &&
|
|
369
|
-
argsMapped.$where[relationField.table] != null) {
|
|
370
|
-
argsMapped.$where = {
|
|
371
|
-
$and: [whereDest, argsMapped.$where[relationField.table]]
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
else if (argsMapped.$where != null) {
|
|
375
|
-
argsMapped.$where = { $and: [whereDest, argsMapped.$where] };
|
|
376
|
-
}
|
|
377
|
-
else {
|
|
378
|
-
argsMapped.$where = whereDest;
|
|
379
|
-
}
|
|
380
|
-
return _getData({
|
|
381
|
-
resource: relationField.table,
|
|
382
|
-
args: argsMapped,
|
|
383
|
-
fields: x.fields,
|
|
384
|
-
artifacts,
|
|
385
|
-
action: relationField.grabMany ? "findMany" : "findUnique"
|
|
386
|
-
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
if (relationField.type === "one-to-many__many-to-one") {
|
|
390
|
-
const referencedKey = relationField.relation.referencedKey;
|
|
391
|
-
const foreignKeyValue = parentData?.[relationField.relation.foreignKey];
|
|
392
|
-
if (foreignKeyValue == null) {
|
|
393
|
-
return { [x.as ?? x.name]: relationField.grabMany ? [] : null };
|
|
394
|
-
}
|
|
395
|
-
const whereRef = { [referencedKey]: foreignKeyValue };
|
|
396
|
-
const argsMapped = x.args == null ? {} : _.cloneDeep(x.args);
|
|
397
|
-
if (argsMapped.$where == null) {
|
|
398
|
-
argsMapped.$where = whereRef;
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
argsMapped.$where = { $and: [whereRef, argsMapped.$where] };
|
|
402
|
-
}
|
|
403
|
-
return _getData({
|
|
404
|
-
resource: relationField.table,
|
|
405
|
-
args: argsMapped,
|
|
406
|
-
fields: x.fields,
|
|
407
|
-
artifacts,
|
|
408
|
-
action: relationField.grabMany ? "findMany" : "findUnique"
|
|
409
|
-
}, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
|
|
410
|
-
}
|
|
411
|
-
throw new Error(`Invalid relationField.type`);
|
|
412
|
-
})));
|
|
413
|
-
}
|
|
414
|
-
function mergeSubResults(result, subResults) {
|
|
415
|
-
for (let x of subResults) {
|
|
416
|
-
result = { ...result, ...x };
|
|
417
|
-
}
|
|
418
|
-
return result;
|
|
419
|
-
}
|
|
420
|
-
function paginate(data, args, primaryKey, action) {
|
|
421
|
-
if (action === "findManyPaginated") {
|
|
422
|
-
if (typeof args?.$paginate?.limit === "number") {
|
|
423
|
-
const totalCount = data.length;
|
|
424
|
-
const limit = args.$paginate.limit;
|
|
425
|
-
const offset = args.$paginate.offset ?? 0;
|
|
426
|
-
data = data.slice(offset, offset + limit);
|
|
427
|
-
return {
|
|
428
|
-
results: data,
|
|
429
|
-
paginationInfo: { totalCount }
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
if (typeof args?.$paginate?.first === "number" ||
|
|
433
|
-
typeof args?.$paginate?.last === "number") {
|
|
434
|
-
const totalCount = data.length;
|
|
435
|
-
let argsMapped = args;
|
|
436
|
-
// Convert cursor(s) for graphql-relay
|
|
437
|
-
for (let k of ["after", "before"]) {
|
|
438
|
-
if (args?.$paginate?.[k] != null) {
|
|
439
|
-
argsMapped = _.cloneDeep(argsMapped);
|
|
440
|
-
const offset = data.findIndex((x) => x[primaryKey] === (0, cursor_1.decodeCursor)(args.$paginate[k]));
|
|
441
|
-
if (offset === -1) {
|
|
442
|
-
throw new Error(`Invalid cursor: ${args.$paginate[k]}`);
|
|
443
|
-
}
|
|
444
|
-
argsMapped.$paginate[k] = (0, graphql_relay_1.offsetToCursor)(offset);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
const connection = (0, graphql_relay_1.connectionFromArray)(data, argsMapped.$paginate);
|
|
448
|
-
const results = connection.edges.map((x) => x.node);
|
|
449
|
-
// Convert cursors back
|
|
450
|
-
const startKey = results[0]?.[primaryKey];
|
|
451
|
-
const endKey = results[results.length - 1]?.[primaryKey];
|
|
452
|
-
const startCursor = startKey == null ? null : (0, cursor_1.encodeCursor)(startKey);
|
|
453
|
-
const endCursor = endKey == null ? null : (0, cursor_1.encodeCursor)(endKey);
|
|
454
|
-
return {
|
|
455
|
-
results,
|
|
456
|
-
paginationInfo: {
|
|
457
|
-
...connection.pageInfo,
|
|
458
|
-
startCursor,
|
|
459
|
-
endCursor,
|
|
460
|
-
totalCount
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
throw new Error(`Invalid pagination action: ${action}`);
|
|
466
|
-
}
|
|
467
|
-
function mapResult(data) {
|
|
468
|
-
return data.map((x) => {
|
|
469
|
-
for (let k of Object.keys(x)) {
|
|
470
|
-
if (typeof x[k] === "string" && (k === "uuid" || k.endsWith("Uuid"))) {
|
|
471
|
-
x[k] = x[k].toLocaleLowerCase();
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
return x;
|
|
475
|
-
});
|
|
476
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runTransforms = void 0;
|
|
4
|
-
function runTransforms(data, fields) {
|
|
5
|
-
if (data == null) {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
for (let x of fields) {
|
|
9
|
-
if (typeof x === "object") {
|
|
10
|
-
const k = x.as ?? x.name;
|
|
11
|
-
if (Array.isArray(data)) {
|
|
12
|
-
for (let d of data) {
|
|
13
|
-
if (d != null) {
|
|
14
|
-
runTransforms(d[k], x.fields);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
runTransforms(data[k], x.fields);
|
|
20
|
-
}
|
|
21
|
-
if (typeof x.transform === "function") {
|
|
22
|
-
if (Array.isArray(data)) {
|
|
23
|
-
for (let d of data) {
|
|
24
|
-
if (d != null) {
|
|
25
|
-
d[k] = x.transform(d[k]);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
data[k] = x.transform(data[k]);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.runTransforms = runTransforms;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,112 +0,0 @@
|
|
|
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
|
-
});
|