@technicity/data-service-generator 0.8.4 → 0.10.0-next.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/index.d.ts +1 -1
- package/dist/index.js +4 -3
- package/dist/runtime/Cache.d.ts +18 -0
- package/dist/runtime/Cache.js +90 -0
- package/dist/runtime/Runtime.d.ts +23 -0
- package/dist/runtime/Runtime.js +29 -0
- package/dist/runtime/RuntimeKSQL.d.ts +1 -1
- package/dist/runtime/RuntimeKSQL.js +13 -11
- package/dist/runtime/RuntimeMSSQL.d.ts +18 -14
- package/dist/runtime/RuntimeMSSQL.js +48 -43
- package/dist/runtime/RuntimeMySQL.d.ts +11 -12
- package/dist/runtime/RuntimeMySQL.js +46 -79
- package/dist/runtime/lib/create.d.ts +12 -0
- package/dist/runtime/lib/create.js +175 -0
- package/dist/runtime/lib/delete.d.ts +5 -0
- package/dist/runtime/lib/delete.js +43 -0
- package/dist/runtime/lib/error.d.ts +9 -0
- package/dist/runtime/lib/error.js +22 -0
- package/dist/runtime/lib/getData.d.ts +4 -0
- package/dist/runtime/lib/getData.js +227 -0
- package/dist/runtime/lib/getSqlAst.js +2 -2
- package/dist/runtime/lib/getWhere.d.ts +17 -0
- package/dist/runtime/lib/getWhere.js +228 -4
- package/dist/runtime/lib/prepareWhere.d.ts +2 -0
- package/dist/runtime/lib/prepareWhere.js +158 -0
- package/dist/runtime/lib/resolve.d.ts +10 -0
- package/dist/runtime/lib/resolve.js +66 -0
- package/dist/runtime/lib/shared.d.ts +22 -12
- package/dist/runtime/lib/shared.js +16 -728
- package/dist/runtime/lib/typeCastMSSQL.js +1 -1
- package/dist/runtime/lib/update.d.ts +4 -0
- package/dist/runtime/lib/update.js +143 -0
- package/dist/runtime/lib/utility.d.ts +5 -0
- package/dist/runtime/lib/utility.js +15 -0
- package/package.json +8 -7
- package/dist/runtime/lib/MSSQL.d.ts +0 -13
- package/dist/runtime/lib/MSSQL.js +0 -73
- package/dist/runtime/lib/SDKNotFoundError.d.ts +0 -4
- package/dist/runtime/lib/SDKNotFoundError.js +0 -10
- package/dist/runtime/lib/getDateTimeStringMySQL.d.ts +0 -1
- package/dist/runtime/lib/getDateTimeStringMySQL.js +0 -8
- package/dist/runtime/lib/stringifyWhere.d.ts +0 -18
- package/dist/runtime/lib/stringifyWhere.js +0 -228
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runCreateTree = exports.create = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const getData_1 = require("./getData");
|
|
6
|
+
const shared_1 = require("./shared");
|
|
7
|
+
async function create(input, dbCall, formatQuery, beginTransaction, dialect, context) {
|
|
8
|
+
async function _create() {
|
|
9
|
+
// Shallow clone, as we're going to mutate later
|
|
10
|
+
let data = { ...input.data };
|
|
11
|
+
const tableArtifacts = input.artifacts[input.resource];
|
|
12
|
+
// Top-level only
|
|
13
|
+
if ((0, shared_1.hasMappedFields)(input.artifacts, input.resource, data)) {
|
|
14
|
+
await (0, shared_1.mapMappedFields)(tableArtifacts, data, dbCall, formatQuery);
|
|
15
|
+
}
|
|
16
|
+
const hasChildren = Object.keys(data).some((k) => {
|
|
17
|
+
const oneToManyRelation = tableArtifacts.relationFields[k];
|
|
18
|
+
if (oneToManyRelation == null) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (oneToManyRelation.type === "one-to-many__many-to-one" &&
|
|
22
|
+
oneToManyRelation.kind === "one-to-many") {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
if (hasChildren) {
|
|
27
|
+
const { dbCall: dbCallTransaction, commit } = await beginTransaction();
|
|
28
|
+
const id = await runCreateTree(getCreateTree([data], input.resource, null, context?.specialCaseUuidColumn, dialect, input.artifacts), null, dialect === "mssql" ? runCreateTreeMSSQL : runCreateTreeMySQL, dbCallTransaction, formatQuery, dialect).then((xs) => xs[0]);
|
|
29
|
+
await commit();
|
|
30
|
+
return id;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
data = processCreateData(data, tableArtifacts, dialect, context?.specialCaseUuidColumn);
|
|
34
|
+
if (dialect === "mysql") {
|
|
35
|
+
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
|
|
36
|
+
return inserted.insertId;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const columns = Object.keys(data);
|
|
40
|
+
const values = Object.values(data);
|
|
41
|
+
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
|
|
42
|
+
return inserted[0]["SCOPE_IDENTITY"];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const id = await _create();
|
|
47
|
+
return (0, getData_1.getData)({ ...input, args: { $where: { id } } }, dbCall, formatQuery, dialect);
|
|
48
|
+
}
|
|
49
|
+
exports.create = create;
|
|
50
|
+
function processCreateData(data, tableArtifacts, dialect, specialCaseUuidColumn) {
|
|
51
|
+
const out = { ...data };
|
|
52
|
+
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
53
|
+
for (let k in tableArtifacts.dateTimeFields) {
|
|
54
|
+
if (out[k] != null) {
|
|
55
|
+
out[k] = (0, shared_1.getDateTimeStringMySQL)(out[k]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Delete keys with value of undefined
|
|
60
|
+
for (let k in out) {
|
|
61
|
+
if (out[k] === void 0) {
|
|
62
|
+
delete out[k];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const scalarFieldSet = new Set(tableArtifacts.scalarFields);
|
|
66
|
+
if (specialCaseUuidColumn &&
|
|
67
|
+
scalarFieldSet.has("uuid") &&
|
|
68
|
+
!Object.prototype.hasOwnProperty.call(out, "uuid")) {
|
|
69
|
+
out["uuid"] = (0, uuid_1.v4)();
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
async function runCreateTree(tree, referencedKeyValue, runCreateTreeSQL, dbCall, formatQuery, dialect) {
|
|
74
|
+
const ids = await runCreateTreeSQL(tree.table, tree.referencedKey, referencedKeyValue, tree.columns, tree.values, dbCall, formatQuery);
|
|
75
|
+
if (tree.children?.length > 0) {
|
|
76
|
+
// https://github.com/tediousjs/node-mssql/issues/302
|
|
77
|
+
if (dialect === "mssql") {
|
|
78
|
+
const idIndexes = Array(ids.length)
|
|
79
|
+
.fill(null)
|
|
80
|
+
.map((_, i) => i);
|
|
81
|
+
for (let i of idIndexes) {
|
|
82
|
+
for (let c of tree.children[i]) {
|
|
83
|
+
await runCreateTree(c, ids[i], runCreateTreeSQL, dbCall, formatQuery, dialect);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
await Promise.all(ids.flatMap((id, i) => tree.children[i].map((c) => runCreateTree(c, id, runCreateTreeSQL, dbCall, formatQuery, dialect))));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return ids;
|
|
92
|
+
}
|
|
93
|
+
exports.runCreateTree = runCreateTree;
|
|
94
|
+
function getCreateTree(data, table, referencedKey, specialCaseUuidColumn, dialect, artifacts) {
|
|
95
|
+
const tableArtifacts = artifacts[table];
|
|
96
|
+
const scalarFieldSet = new Set(tableArtifacts.scalarFields);
|
|
97
|
+
const out = {
|
|
98
|
+
table,
|
|
99
|
+
referencedKey,
|
|
100
|
+
columns: [],
|
|
101
|
+
values: [],
|
|
102
|
+
children: [],
|
|
103
|
+
};
|
|
104
|
+
for (let i = 0; i < data.length; i++) {
|
|
105
|
+
let d = data[i];
|
|
106
|
+
if (Object.keys(d).length === 0) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
d = processCreateData(d, tableArtifacts, dialect, specialCaseUuidColumn);
|
|
110
|
+
out.columns[i] = [];
|
|
111
|
+
out.values[i] = [];
|
|
112
|
+
out.children[i] = [];
|
|
113
|
+
for (let k in d) {
|
|
114
|
+
if (scalarFieldSet.has(k)) {
|
|
115
|
+
out.columns[i].push(k);
|
|
116
|
+
const v = d[k];
|
|
117
|
+
out.values[i].push(v);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const oneToManyRelation = tableArtifacts.relationFields[k];
|
|
121
|
+
if (oneToManyRelation == null ||
|
|
122
|
+
oneToManyRelation.type !== "one-to-many__many-to-one" ||
|
|
123
|
+
oneToManyRelation.kind !== "one-to-many") {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!Array.isArray(d[k]?.$create)) {
|
|
127
|
+
throw new Error("Invalid data: " + k);
|
|
128
|
+
}
|
|
129
|
+
out.children[i].push(getCreateTree(d[k].$create, oneToManyRelation.table, oneToManyRelation.relation.referencedKey, specialCaseUuidColumn, dialect, artifacts));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
const runCreateTreeMySQL = async (table, referencedKey, referencedKeyValue, columns, values, dbCall, formatQuery) => {
|
|
135
|
+
let allColumns = columns;
|
|
136
|
+
if (typeof referencedKey === "string") {
|
|
137
|
+
allColumns = allColumns.slice().map((xs) => xs.concat(referencedKey));
|
|
138
|
+
}
|
|
139
|
+
let allValues = values;
|
|
140
|
+
if (referencedKeyValue != null) {
|
|
141
|
+
allValues = allValues.slice().map((xs) => xs.concat(referencedKeyValue));
|
|
142
|
+
}
|
|
143
|
+
return Promise.all(allColumns.map((cs, i) => dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?)`, [table, cs, allValues[i]])).then((x) => x.insertId)));
|
|
144
|
+
};
|
|
145
|
+
// This doesn't use bulk inserts because:
|
|
146
|
+
// 1. columns aren't necessarily uniform
|
|
147
|
+
// 2. We don't want to do backflips to get all the inserted IDs
|
|
148
|
+
const runCreateTreeMSSQL = async (table, referencedKey, referencedKeyValue, columns, values, dbCall, formatQuery) => {
|
|
149
|
+
let allColumns = columns;
|
|
150
|
+
if (typeof referencedKey === "string") {
|
|
151
|
+
allColumns = allColumns.slice().map((xs) => xs.concat(referencedKey));
|
|
152
|
+
}
|
|
153
|
+
let allValues = values;
|
|
154
|
+
if (referencedKeyValue != null) {
|
|
155
|
+
allValues = allValues.slice().map((xs) => xs.concat(referencedKeyValue));
|
|
156
|
+
}
|
|
157
|
+
// https://github.com/tediousjs/node-mssql/issues/302
|
|
158
|
+
// return Promise.all(
|
|
159
|
+
// allColumns.map((cs, i) =>
|
|
160
|
+
// dbCall(
|
|
161
|
+
// formatQuery(
|
|
162
|
+
// `INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`,
|
|
163
|
+
// [table, cs, allValues[i]]
|
|
164
|
+
// )
|
|
165
|
+
// ).then((xs) => xs[0]["SCOPE_IDENTITY"])
|
|
166
|
+
// )
|
|
167
|
+
// );
|
|
168
|
+
const out = [];
|
|
169
|
+
let i = 0;
|
|
170
|
+
for (let cs of allColumns) {
|
|
171
|
+
out.push(await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [table, cs, allValues[i]])).then((xs) => xs[0]["SCOPE_IDENTITY"]));
|
|
172
|
+
i += 1;
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IDialect, TDbCall, TFormatQuery } from '../IRuntime';
|
|
2
|
+
import type { TResolveParams } from "../IRuntime";
|
|
3
|
+
import Cache from '../Cache';
|
|
4
|
+
export declare function deleteOne(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, dialect: IDialect, cache?: Cache): Promise<boolean>;
|
|
5
|
+
export declare function deleteMany(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, dialect: IDialect, cache?: Cache): Promise<boolean>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteMany = exports.deleteOne = void 0;
|
|
4
|
+
const error_1 = require("./error");
|
|
5
|
+
const getWhere_1 = require("./getWhere");
|
|
6
|
+
async function deleteOne(input, dbCall, formatQuery, dialect, cache) {
|
|
7
|
+
const _findOne = Object.entries(input.args.$where)[0];
|
|
8
|
+
const findOne = { key: _findOne[0], value: _findOne[1] };
|
|
9
|
+
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
10
|
+
input.resource,
|
|
11
|
+
findOne.key,
|
|
12
|
+
findOne.value,
|
|
13
|
+
])).then((xs) => xs[0]);
|
|
14
|
+
if (current == null) {
|
|
15
|
+
throw new error_1.SDKNotFoundError();
|
|
16
|
+
}
|
|
17
|
+
if (cache && current.uuid)
|
|
18
|
+
cache.purge(current.uuid);
|
|
19
|
+
await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
|
|
20
|
+
input.resource,
|
|
21
|
+
findOne.key,
|
|
22
|
+
findOne.value,
|
|
23
|
+
]));
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
exports.deleteOne = deleteOne;
|
|
27
|
+
async function deleteMany(input, dbCall, formatQuery, dialect, cache) {
|
|
28
|
+
const escapeId = (0, getWhere_1.getEscapeId)(dialect);
|
|
29
|
+
const where = (0, getWhere_1.getWhere)(escapeId(input.resource), input.args, dialect);
|
|
30
|
+
if (where == null)
|
|
31
|
+
throw new Error("Null where");
|
|
32
|
+
if (cache)
|
|
33
|
+
try {
|
|
34
|
+
const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [input.resource]);
|
|
35
|
+
const matches = await dbCall(query);
|
|
36
|
+
const uuids = matches.map((x) => x.uuid);
|
|
37
|
+
cache.purge(...uuids);
|
|
38
|
+
}
|
|
39
|
+
catch (err) { }
|
|
40
|
+
await dbCall(formatQuery(`DELETE FROM ?? WHERE ${where}`, [input.resource]));
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
exports.deleteMany = deleteMany;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class CustomError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class SDKBadWhereError extends CustomError {
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class SDKNotFoundError extends CustomError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SDKNotFoundError = exports.SDKBadWhereError = exports.CustomError = void 0;
|
|
4
|
+
class CustomError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CustomError = CustomError;
|
|
11
|
+
class SDKBadWhereError extends CustomError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message || "Invalid $where");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.SDKBadWhereError = SDKBadWhereError;
|
|
17
|
+
class SDKNotFoundError extends CustomError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Not found");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SDKNotFoundError = SDKNotFoundError;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import Cache from '../Cache';
|
|
2
|
+
import { IDialect, TDbCall, TFormatQuery, TResolveParams } from '../IRuntime';
|
|
3
|
+
export declare function getCacheableData(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, dialect: IDialect, cache: Cache): Promise<any>;
|
|
4
|
+
export declare function getData(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, dialect: IDialect): Promise<any>;
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getData = exports.getCacheableData = void 0;
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
const batch_planner_1 = require("join-monster/dist/batch-planner");
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const util_1 = require("join-monster/dist/util");
|
|
8
|
+
const _ = require("lodash/fp");
|
|
9
|
+
const error_1 = require("./error");
|
|
10
|
+
const addNullFallbacks_1 = require("./addNullFallbacks");
|
|
11
|
+
const cursor_1 = require("./cursor");
|
|
12
|
+
const getOrderBy_1 = require("./getOrderBy");
|
|
13
|
+
const getSqlAst_1 = require("./getSqlAst");
|
|
14
|
+
const getWhere_1 = require("./getWhere");
|
|
15
|
+
const shared_1 = require("./shared");
|
|
16
|
+
async function getCacheableData(input, dbCall, formatQuery, dialect, cache) {
|
|
17
|
+
const { request, cached } = await cache.from(input);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
ensureUuidSelect(input);
|
|
21
|
+
const results = await getData(input, dbCall, formatQuery, dialect);
|
|
22
|
+
cache.insert(request, results);
|
|
23
|
+
return results;
|
|
24
|
+
}
|
|
25
|
+
exports.getCacheableData = getCacheableData;
|
|
26
|
+
async function getData(input, dbCall, formatQuery, dialect) {
|
|
27
|
+
const context = {};
|
|
28
|
+
const { action } = input;
|
|
29
|
+
const { primaryKey } = input.artifacts[input.resource];
|
|
30
|
+
let paginationType;
|
|
31
|
+
let limit = undefined;
|
|
32
|
+
let offset = undefined;
|
|
33
|
+
let rowWithMatchingCursor = undefined;
|
|
34
|
+
if (action === "findManyPaginated") {
|
|
35
|
+
if (input.args?.$paginate == null) {
|
|
36
|
+
throw new Error("$paginate required but not supplied");
|
|
37
|
+
}
|
|
38
|
+
if (typeof input?.args?.$paginate?.limit === "number") {
|
|
39
|
+
paginationType = "limit-offset";
|
|
40
|
+
limit = input.args.$paginate.limit;
|
|
41
|
+
offset = input.args.$paginate.offset;
|
|
42
|
+
}
|
|
43
|
+
else if (typeof input?.args?.$paginate?.first === "number" ||
|
|
44
|
+
typeof input?.args?.$paginate?.last === "number") {
|
|
45
|
+
paginationType = "cursor";
|
|
46
|
+
limit = (typeof input?.args?.$paginate?.first === "number"
|
|
47
|
+
? input?.args?.$paginate?.first
|
|
48
|
+
: input?.args?.$paginate?.last);
|
|
49
|
+
// + 1 to peek if there is more data
|
|
50
|
+
limit += 1;
|
|
51
|
+
if (input.args.$paginate.after != null ||
|
|
52
|
+
input.args.$paginate.before != null) {
|
|
53
|
+
const cursor = input.args.$paginate.after != null
|
|
54
|
+
? input.args.$paginate.after
|
|
55
|
+
: input.args.$paginate.before;
|
|
56
|
+
rowWithMatchingCursor = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
57
|
+
input.resource,
|
|
58
|
+
primaryKey,
|
|
59
|
+
(0, cursor_1.decodeCursor)(cursor),
|
|
60
|
+
])).then((xs) => xs[0]);
|
|
61
|
+
if (rowWithMatchingCursor == null) {
|
|
62
|
+
throw new Error(`Invalid cursor: ${cursor}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
throw new Error(`Invalid $paginate: ${input?.args?.$paginate}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (action === "findMany") {
|
|
71
|
+
if (typeof input?.args?.$limit === "number") {
|
|
72
|
+
limit = input?.args?.$limit;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// we need to read the query AST and build a new "SQL AST" from which the SQL and
|
|
76
|
+
// const sqlAST = queryAST.queryASTToSqlAST(resolveInfo, options, context);
|
|
77
|
+
const fields = input.fields ?? (0, shared_1.getScalarFields)(input.resource, input.artifacts);
|
|
78
|
+
const orderByListPaginatedRootResult =
|
|
79
|
+
// MSSQL's OFFSET and FETCH requires ORDER BY, so we need to provide a fallback
|
|
80
|
+
dialect === "mssql" && paginationType === "limit-offset"
|
|
81
|
+
? {
|
|
82
|
+
orderBy: (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)?.orderBy ?? [
|
|
83
|
+
{ column: primaryKey, direction: "asc" },
|
|
84
|
+
],
|
|
85
|
+
flip: false,
|
|
86
|
+
}
|
|
87
|
+
: action === "findManyPaginated"
|
|
88
|
+
? (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)
|
|
89
|
+
: undefined;
|
|
90
|
+
const grabMany = action === "findMany" ||
|
|
91
|
+
action === "findManyPaginated" ||
|
|
92
|
+
action === "updateMany" ||
|
|
93
|
+
action === "deleteMany";
|
|
94
|
+
const sqlAST = (0, getSqlAst_1.getSqlAst)({
|
|
95
|
+
...input,
|
|
96
|
+
table: input.resource,
|
|
97
|
+
fieldName: "data",
|
|
98
|
+
fields,
|
|
99
|
+
getWhere: getWhere_1.getWhere,
|
|
100
|
+
orderBy: orderByListPaginatedRootResult?.orderBy,
|
|
101
|
+
rowWithMatchingCursor,
|
|
102
|
+
dialect,
|
|
103
|
+
grabMany,
|
|
104
|
+
});
|
|
105
|
+
const options = { dialect };
|
|
106
|
+
let { sql, shapeDefinition } = await (0, util_1.compileSqlAST)(sqlAST, context, options);
|
|
107
|
+
if (!sql) {
|
|
108
|
+
// return {};
|
|
109
|
+
throw new Error("No SQL");
|
|
110
|
+
}
|
|
111
|
+
// TODO - remove if limit support added for mysql dialect
|
|
112
|
+
if (action === "findMany" || action === "findManyPaginated") {
|
|
113
|
+
if (typeof limit === "number") {
|
|
114
|
+
const escape = (0, getWhere_1.getEscape)(dialect);
|
|
115
|
+
if (dialect === "mssql") {
|
|
116
|
+
if (typeof offset === "number") {
|
|
117
|
+
sql += ` OFFSET ${escape(offset)} ROWS FETCH NEXT ${escape(limit)} ROWS ONLY`;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
sql = sql.replace("SELECT", `SELECT TOP ${escape(limit)}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
sql += ` LIMIT ${escape(limit)}`;
|
|
125
|
+
if (typeof offset === "number") {
|
|
126
|
+
sql += ` OFFSET ${escape(offset)}`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// call their function for querying the DB, handle the different cases, do some validation, return a promise of the object
|
|
132
|
+
let data = await (0, util_1.handleUserDbCall)(dbCall, sql, sqlAST, shapeDefinition);
|
|
133
|
+
// if they are paginating, we'll get back an array which is essentially a "slice" of the whole data.
|
|
134
|
+
// this function goes through the data tree and converts the arrays to Connection Objects
|
|
135
|
+
// data = arrToConnection(data, sqlAST);
|
|
136
|
+
// so far we handled the first "batch". up until now, additional batches were ignored
|
|
137
|
+
// this function recursively scanss the sqlAST and runs remaining batches
|
|
138
|
+
await (0, batch_planner_1.default)(sqlAST, data, dbCall, context, options);
|
|
139
|
+
(0, addNullFallbacks_1.addNullFallbacks)(sqlAST, data);
|
|
140
|
+
// We only need to remove extra keys if input.fields isn't
|
|
141
|
+
// specified, since otherwise there wouldn't be extra keys
|
|
142
|
+
const shouldRemoveExtraKeys = input.fields != null;
|
|
143
|
+
if (action !== "findManyPaginated") {
|
|
144
|
+
// Remove additional keys that are added for batches and joins
|
|
145
|
+
// Do later for `listPaginated`, since the `id` is needed for
|
|
146
|
+
// creating the cursor
|
|
147
|
+
(0, shared_1.postProcess)(data, fields, shouldRemoveExtraKeys);
|
|
148
|
+
}
|
|
149
|
+
// check for batch data
|
|
150
|
+
if (Array.isArray(data)) {
|
|
151
|
+
// TODO - not sure why this code exists in original
|
|
152
|
+
// source code of join-monster; doesn't make sense to me.
|
|
153
|
+
// const childrenToCheck = sqlAST.children.filter(
|
|
154
|
+
// (child: any) => child.sqlBatch
|
|
155
|
+
// );
|
|
156
|
+
// data = data.filter((d) => {
|
|
157
|
+
// for (const child of childrenToCheck) {
|
|
158
|
+
// if (d[child.fieldName] == null) {
|
|
159
|
+
// return false;
|
|
160
|
+
// }
|
|
161
|
+
// }
|
|
162
|
+
// return true;
|
|
163
|
+
// });
|
|
164
|
+
if (action === "findManyPaginated") {
|
|
165
|
+
const argsTotalCount = input.args && _.cloneDeep(input.args);
|
|
166
|
+
if (argsTotalCount != null) {
|
|
167
|
+
if (argsTotalCount.$paginate != null) {
|
|
168
|
+
// We don't want the where clause to include cursor-related stuff
|
|
169
|
+
delete argsTotalCount.$paginate.after;
|
|
170
|
+
delete argsTotalCount.$paginate.before;
|
|
171
|
+
// We don't need offset
|
|
172
|
+
delete argsTotalCount.$paginate.offset;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const sqlASTTotalCount = (0, getSqlAst_1.getSqlAst)({
|
|
176
|
+
...input,
|
|
177
|
+
table: input.resource,
|
|
178
|
+
fieldName: "data",
|
|
179
|
+
args: argsTotalCount,
|
|
180
|
+
// Because we're going to manually set children anyway
|
|
181
|
+
fields: [],
|
|
182
|
+
getWhere: getWhere_1.getWhere,
|
|
183
|
+
// We don't want the where clause to include cursor-related stuff
|
|
184
|
+
rowWithMatchingCursor: null,
|
|
185
|
+
dialect,
|
|
186
|
+
grabMany: true,
|
|
187
|
+
});
|
|
188
|
+
// Because orderBy doesn't matter for total count.
|
|
189
|
+
// getOrderBy adds an element if paginating, so deleting args.$orderBy
|
|
190
|
+
// isn't sufficient.
|
|
191
|
+
delete sqlASTTotalCount.orderBy;
|
|
192
|
+
const totalCount = await (0, shared_1.getTotalCount)(sqlASTTotalCount, dbCall, context, options);
|
|
193
|
+
if (paginationType === "cursor") {
|
|
194
|
+
data = (0, shared_1.wrapListPaginationCursor)(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
195
|
+
(0, shared_1.postProcess)(xs, fields, shouldRemoveExtraKeys);
|
|
196
|
+
}, input.artifacts[input.resource].primaryKey, totalCount);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
(0, shared_1.postProcess)(data, fields, shouldRemoveExtraKeys);
|
|
200
|
+
data = (0, shared_1.wrapListPaginationLimitOffset)(data, totalCount);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
if (data == null && !grabMany) {
|
|
206
|
+
throw new error_1.SDKNotFoundError();
|
|
207
|
+
}
|
|
208
|
+
return data;
|
|
209
|
+
}
|
|
210
|
+
exports.getData = getData;
|
|
211
|
+
function ensureUuidSelect(input) {
|
|
212
|
+
const { resource, artifacts } = input;
|
|
213
|
+
ensure(resource, input);
|
|
214
|
+
function ensure(type, input) {
|
|
215
|
+
const { scalarFields, relationFields } = artifacts[type];
|
|
216
|
+
if (!scalarFields.includes("uuid"))
|
|
217
|
+
return;
|
|
218
|
+
const fields = input.fields;
|
|
219
|
+
if (!fields.includes("uuid"))
|
|
220
|
+
fields.unshift("uuid");
|
|
221
|
+
for (const field of fields)
|
|
222
|
+
if (typeof field == "object") {
|
|
223
|
+
const { table } = relationFields[field.name];
|
|
224
|
+
ensure(table, field);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -12,12 +12,12 @@ const namespace = new alias_namespace_1.default(true);
|
|
|
12
12
|
function getSqlAst(input) {
|
|
13
13
|
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect, } = input;
|
|
14
14
|
const tableArtifacts = artifacts[table];
|
|
15
|
-
const primaryKey = tableArtifacts
|
|
15
|
+
const { primaryKey } = tableArtifacts;
|
|
16
16
|
const format = dialect === "mysql"
|
|
17
17
|
? SqlString.format.bind(SqlString)
|
|
18
18
|
: TSqlString.format.bind(TSqlString);
|
|
19
19
|
const orderBy = input.orderBy ?? (0, getOrderBy_1.getOrderBy)(args, primaryKey)?.orderBy;
|
|
20
|
-
let where = input
|
|
20
|
+
let { where } = input;
|
|
21
21
|
if (input.where == null) {
|
|
22
22
|
where = (table, args) => {
|
|
23
23
|
let argsMapped = args;
|
|
@@ -1,2 +1,19 @@
|
|
|
1
1
|
import type { IOrderBy, IDialect } from "../IRuntime";
|
|
2
2
|
export declare function getWhere(table: string, args: any, dialect: IDialect, orderBy?: IOrderBy | undefined, rowWithMatchingCursor?: any): string | null;
|
|
3
|
+
declare type IWhere = {
|
|
4
|
+
[k: string]: any;
|
|
5
|
+
};
|
|
6
|
+
declare type IArgs = {
|
|
7
|
+
[k: string]: any;
|
|
8
|
+
};
|
|
9
|
+
export declare function stringifyWhere(input: {
|
|
10
|
+
where: IWhere;
|
|
11
|
+
table: string;
|
|
12
|
+
dialect: IDialect;
|
|
13
|
+
args: IArgs;
|
|
14
|
+
orderBy?: IOrderBy | undefined;
|
|
15
|
+
rowWithMatchingCursor?: any;
|
|
16
|
+
}): string;
|
|
17
|
+
export declare function getEscapeId(dialect: IDialect): any;
|
|
18
|
+
export declare function getEscape(dialect: IDialect): any;
|
|
19
|
+
export {};
|