@technicity/data-service-generator 0.10.0-next.0 → 0.11.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/generation/generate.js +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -3
- package/dist/runtime/RuntimeKSQL.d.ts +1 -1
- package/dist/runtime/RuntimeKSQL.js +11 -13
- package/dist/runtime/RuntimeMSSQL.d.ts +14 -18
- package/dist/runtime/RuntimeMSSQL.js +43 -48
- package/dist/runtime/RuntimeMySQL.d.ts +12 -10
- package/dist/runtime/RuntimeMySQL.js +84 -46
- package/dist/runtime/lib/MSSQL.d.ts +13 -0
- package/dist/runtime/lib/MSSQL.js +73 -0
- package/dist/runtime/lib/SDKBadWhereError.d.ts +4 -0
- package/dist/runtime/lib/SDKBadWhereError.js +10 -0
- package/dist/runtime/lib/SDKNotFoundError.d.ts +4 -0
- package/dist/runtime/lib/SDKNotFoundError.js +10 -0
- package/dist/runtime/lib/getDateTimeStringMySQL.d.ts +1 -0
- package/dist/runtime/lib/getDateTimeStringMySQL.js +8 -0
- package/dist/runtime/lib/getSqlAst.js +2 -2
- package/dist/runtime/lib/getWhere.d.ts +0 -17
- package/dist/runtime/lib/getWhere.js +4 -228
- package/dist/runtime/lib/shared.d.ts +13 -22
- package/dist/runtime/lib/shared.js +787 -16
- package/dist/runtime/lib/stringifyWhere.d.ts +18 -0
- package/dist/runtime/lib/stringifyWhere.js +228 -0
- package/dist/runtime/lib/typeCastMSSQL.js +1 -1
- package/package.json +4 -1
- package/dist/runtime/Runtime.d.ts +0 -23
- package/dist/runtime/Runtime.js +0 -29
- package/dist/runtime/lib/create.d.ts +0 -12
- package/dist/runtime/lib/create.js +0 -175
- package/dist/runtime/lib/delete.d.ts +0 -5
- package/dist/runtime/lib/delete.js +0 -43
- package/dist/runtime/lib/error.d.ts +0 -9
- package/dist/runtime/lib/error.js +0 -22
- package/dist/runtime/lib/getData.d.ts +0 -4
- package/dist/runtime/lib/getData.js +0 -227
- package/dist/runtime/lib/prepareWhere.d.ts +0 -2
- package/dist/runtime/lib/prepareWhere.js +0 -158
- package/dist/runtime/lib/resolve.d.ts +0 -10
- package/dist/runtime/lib/resolve.js +0 -66
- package/dist/runtime/lib/update.d.ts +0 -4
- package/dist/runtime/lib/update.js +0 -143
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const MySqlString = require("sqlstring");
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
const TSqlString = require("tsqlstring");
|
|
3
|
+
exports.getWhere = void 0;
|
|
4
|
+
const stringifyWhere_1 = require("./stringifyWhere");
|
|
8
5
|
function getWhere(
|
|
9
6
|
// Note: `table` is not escaped in getWhere, so it should be escaped beforehand.
|
|
10
7
|
table, args, dialect, orderBy, rowWithMatchingCursor) {
|
|
11
8
|
if (args?.$where == null && args?.$paginate == null) {
|
|
12
9
|
return null;
|
|
13
10
|
}
|
|
14
|
-
|
|
15
|
-
return (stringifyWhere({
|
|
11
|
+
let where = args?.$where ?? {};
|
|
12
|
+
return ((0, stringifyWhere_1.stringifyWhere)({
|
|
16
13
|
where,
|
|
17
14
|
table,
|
|
18
15
|
dialect,
|
|
@@ -22,224 +19,3 @@ table, args, dialect, orderBy, rowWithMatchingCursor) {
|
|
|
22
19
|
}) || null);
|
|
23
20
|
}
|
|
24
21
|
exports.getWhere = getWhere;
|
|
25
|
-
function stringifyWhere(input) {
|
|
26
|
-
const { where, table, dialect, args, orderBy, rowWithMatchingCursor } = input;
|
|
27
|
-
const escapeId = getEscapeId(dialect);
|
|
28
|
-
const escape = getEscape(dialect);
|
|
29
|
-
let result = _stringifyWhere(where, table, escapeId, escape, "");
|
|
30
|
-
const paginationWhere = stringifyPaginationWhere({
|
|
31
|
-
table,
|
|
32
|
-
args,
|
|
33
|
-
orderBy,
|
|
34
|
-
escapeId,
|
|
35
|
-
escape,
|
|
36
|
-
rowWithMatchingCursor,
|
|
37
|
-
});
|
|
38
|
-
if (paginationWhere) {
|
|
39
|
-
result =
|
|
40
|
-
result.length === 0
|
|
41
|
-
? paginationWhere
|
|
42
|
-
: result + " AND " + paginationWhere;
|
|
43
|
-
}
|
|
44
|
-
return result;
|
|
45
|
-
}
|
|
46
|
-
exports.stringifyWhere = stringifyWhere;
|
|
47
|
-
function _stringifyWhere(where, table, escapeId, escape, result) {
|
|
48
|
-
if (Object.prototype.hasOwnProperty.call(where, "$and")) {
|
|
49
|
-
if (Object.keys(where).length !== 1) {
|
|
50
|
-
throw new Error("Must have 1 key: " + JSON.stringify(where, null, 2));
|
|
51
|
-
}
|
|
52
|
-
const _xs = where.$and.filter((x) => x != null);
|
|
53
|
-
if (_xs.length === 0) {
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
const xs = _xs
|
|
57
|
-
.map((x) => _stringifyWhere(x, table, escapeId, escape, result))
|
|
58
|
-
.filter(Boolean);
|
|
59
|
-
if (xs.length === 0) {
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
return `(${xs.join(" AND ")})`;
|
|
63
|
-
}
|
|
64
|
-
if (Object.prototype.hasOwnProperty.call(where, "$or")) {
|
|
65
|
-
if (Object.keys(where).length !== 1) {
|
|
66
|
-
throw new Error("Must have 1 key: " + JSON.stringify(where, null, 2));
|
|
67
|
-
}
|
|
68
|
-
const _xs = where.$or.filter((x) => x != null);
|
|
69
|
-
if (_xs.length === 0) {
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
const xs = _xs
|
|
73
|
-
.map((x) => _stringifyWhere(x, table, escapeId, escape, result))
|
|
74
|
-
.filter(Boolean);
|
|
75
|
-
if (xs.length === 0) {
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
78
|
-
return `(${xs.join(" OR ")})`;
|
|
79
|
-
}
|
|
80
|
-
function printValue(v) {
|
|
81
|
-
if (v === true) {
|
|
82
|
-
v = 1;
|
|
83
|
-
}
|
|
84
|
-
if (v === false) {
|
|
85
|
-
v = 0;
|
|
86
|
-
}
|
|
87
|
-
return escape(v);
|
|
88
|
-
}
|
|
89
|
-
return Object.keys(where)
|
|
90
|
-
.map((k) => {
|
|
91
|
-
const v = where[k];
|
|
92
|
-
if (v === void 0) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
// Can't do this, since we need this function to be sync
|
|
96
|
-
// if (relationMap[k] !== null) {
|
|
97
|
-
// const relationData = await getRelationData(k, v, relationMap);
|
|
98
|
-
// k = relationData.k;
|
|
99
|
-
// v = relationData.v;
|
|
100
|
-
// }
|
|
101
|
-
if (v === null) {
|
|
102
|
-
return `${table}.${escapeId(k)} IS NULL`;
|
|
103
|
-
}
|
|
104
|
-
if (typeof v === "string" ||
|
|
105
|
-
typeof v === "number" ||
|
|
106
|
-
typeof v === "boolean") {
|
|
107
|
-
return `${table}.${escapeId(k)} = ${printValue(v)}`;
|
|
108
|
-
}
|
|
109
|
-
if (_.isPlainObject(v)) {
|
|
110
|
-
const keys = Object.keys(v);
|
|
111
|
-
if (keys.length !== 1) {
|
|
112
|
-
throw new Error("Must have 1 key: " + JSON.stringify(v, null, 2));
|
|
113
|
-
}
|
|
114
|
-
const operator = keys[0];
|
|
115
|
-
const operand = v[operator];
|
|
116
|
-
if (operator === "$ne") {
|
|
117
|
-
return `${table}.${escapeId(k)} ${getOperatorNeq(v)} ${printValue(operand)}`;
|
|
118
|
-
}
|
|
119
|
-
if (operator === "$gt") {
|
|
120
|
-
return `${table}.${escapeId(k)} > ${printValue(operand)}`;
|
|
121
|
-
}
|
|
122
|
-
if (operator === "$gte") {
|
|
123
|
-
return `${table}.${escapeId(k)} >= ${printValue(operand)}`;
|
|
124
|
-
}
|
|
125
|
-
if (operator === "$lt") {
|
|
126
|
-
return `${table}.${escapeId(k)} < ${printValue(operand)}`;
|
|
127
|
-
}
|
|
128
|
-
if (operator === "$lte") {
|
|
129
|
-
return `${table}.${escapeId(k)} <= ${printValue(operand)}`;
|
|
130
|
-
}
|
|
131
|
-
if (operator === "$like") {
|
|
132
|
-
return `${table}.${escapeId(k)} LIKE ${printValue(operand)}`;
|
|
133
|
-
}
|
|
134
|
-
if (operator === "$nlike") {
|
|
135
|
-
return `${table}.${escapeId(k)} NOT LIKE ${printValue(operand)}`;
|
|
136
|
-
}
|
|
137
|
-
if (operator === "$in") {
|
|
138
|
-
if (operand.length === 0) {
|
|
139
|
-
// Would do "FALSE" instead, as it's more readable, but it
|
|
140
|
-
// causes a RequestError in MSSQL.
|
|
141
|
-
return "(1=0)";
|
|
142
|
-
}
|
|
143
|
-
return `${table}.${escapeId(k)} IN (${operand
|
|
144
|
-
.map((x) => printValue(x))
|
|
145
|
-
.join(",")})`;
|
|
146
|
-
}
|
|
147
|
-
if (operator === "$nin") {
|
|
148
|
-
if (operand.length === 0) {
|
|
149
|
-
// Would do "TRUE" instead, as it's more readable, but it
|
|
150
|
-
// causes a RequestError in MSSQL.
|
|
151
|
-
return "(1=1)";
|
|
152
|
-
}
|
|
153
|
-
return `${table}.${escapeId(k)} NOT IN (${operand
|
|
154
|
-
.map((x) => printValue(x))
|
|
155
|
-
.join(",")})`;
|
|
156
|
-
}
|
|
157
|
-
if (operator === "$btwn") {
|
|
158
|
-
if (operand.length !== 2) {
|
|
159
|
-
throw new Error("Expected length of 2, got: " + operand.length);
|
|
160
|
-
}
|
|
161
|
-
return `${table}.${escapeId(k)} BETWEEN ${printValue(operand[0])} AND ${printValue(operand[1])}`;
|
|
162
|
-
}
|
|
163
|
-
if (operator === "$nbtwn") {
|
|
164
|
-
if (operand.length !== 2) {
|
|
165
|
-
throw new Error("Expected length of 2, got: " + operand.length);
|
|
166
|
-
}
|
|
167
|
-
return `${table}.${escapeId(k)} NOT BETWEEN ${printValue(operand[0])} AND ${printValue(operand[1])}`;
|
|
168
|
-
}
|
|
169
|
-
throw new Error("Invalid operator: " + operator);
|
|
170
|
-
}
|
|
171
|
-
throw new Error("Invalid value: " + v);
|
|
172
|
-
})
|
|
173
|
-
.filter((x) => x != null)
|
|
174
|
-
.join(" AND ");
|
|
175
|
-
}
|
|
176
|
-
const valuesThatUseIsOrIsNot = new Set([null]);
|
|
177
|
-
function getOperatorNeq(value) {
|
|
178
|
-
return valuesThatUseIsOrIsNot.has(value) ? "IS NOT" : "!=";
|
|
179
|
-
}
|
|
180
|
-
// async function getRelationData(k: string, v: any, relationMap: IRelationMap) {
|
|
181
|
-
// // TODO
|
|
182
|
-
// return { k, v };
|
|
183
|
-
// }
|
|
184
|
-
function stringifyPaginationWhere(input) {
|
|
185
|
-
const { table, args, orderBy, escapeId, escape, rowWithMatchingCursor } = input;
|
|
186
|
-
if (args?.$paginate?.after == null && args?.$paginate?.before == null) {
|
|
187
|
-
return "";
|
|
188
|
-
}
|
|
189
|
-
if (rowWithMatchingCursor == null) {
|
|
190
|
-
return "";
|
|
191
|
-
}
|
|
192
|
-
// orderBy should never be null because of getOrderBy, but add a check.
|
|
193
|
-
if (orderBy == null) {
|
|
194
|
-
throw new Error("orderBy cannot be null when paginating");
|
|
195
|
-
}
|
|
196
|
-
function getCompOp(dir) {
|
|
197
|
-
return dir === "asc" ? ">" : "<";
|
|
198
|
-
}
|
|
199
|
-
function printValue(v) {
|
|
200
|
-
if (v === true) {
|
|
201
|
-
v = 1;
|
|
202
|
-
}
|
|
203
|
-
if (v === false) {
|
|
204
|
-
v = 0;
|
|
205
|
-
}
|
|
206
|
-
return escape(v);
|
|
207
|
-
}
|
|
208
|
-
// https://gist.github.com/pcattori/2bb645d587e45c9fdbcabf5cef7a7106
|
|
209
|
-
const cond = orderBy
|
|
210
|
-
.map(({ column, direction }, i) => {
|
|
211
|
-
const a = orderBy.slice(0, i).map(({ column: col2 }) => {
|
|
212
|
-
const field = `${table}.${escapeId(col2)}`;
|
|
213
|
-
const op = "=";
|
|
214
|
-
const v = printValue(rowWithMatchingCursor[col2]);
|
|
215
|
-
return `${field} ${op} ${v}`;
|
|
216
|
-
});
|
|
217
|
-
const field = `${table}.${escapeId(column)}`;
|
|
218
|
-
const op = getCompOp(direction);
|
|
219
|
-
const v = printValue(rowWithMatchingCursor[column]);
|
|
220
|
-
a.push(`${field} ${op} ${v}`);
|
|
221
|
-
return "(" + a.join(" AND ") + ")";
|
|
222
|
-
})
|
|
223
|
-
.join(" OR ");
|
|
224
|
-
return "(" + cond + ")";
|
|
225
|
-
}
|
|
226
|
-
function getEscapeId(dialect) {
|
|
227
|
-
if (dialect === "mysql") {
|
|
228
|
-
return MySqlString.escapeId.bind(MySqlString);
|
|
229
|
-
}
|
|
230
|
-
if (dialect === "mssql") {
|
|
231
|
-
return TSqlString.escapeId.bind(TSqlString);
|
|
232
|
-
}
|
|
233
|
-
throw new Error("Unsupported dialect: " + dialect);
|
|
234
|
-
}
|
|
235
|
-
exports.getEscapeId = getEscapeId;
|
|
236
|
-
function getEscape(dialect) {
|
|
237
|
-
if (dialect === "mysql") {
|
|
238
|
-
return MySqlString.escape.bind(MySqlString);
|
|
239
|
-
}
|
|
240
|
-
if (dialect === "mssql") {
|
|
241
|
-
return TSqlString.escape.bind(TSqlString);
|
|
242
|
-
}
|
|
243
|
-
throw new Error("Unsupported dialect: " + dialect);
|
|
244
|
-
}
|
|
245
|
-
exports.getEscape = getEscape;
|
|
@@ -1,23 +1,14 @@
|
|
|
1
|
-
import { IArtifacts,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
hasNextPage: boolean;
|
|
13
|
-
startCursor: string | null;
|
|
14
|
-
endCursor: string | null;
|
|
15
|
-
totalCount: number;
|
|
16
|
-
};
|
|
17
|
-
results: any[];
|
|
18
|
-
};
|
|
19
|
-
export declare function getTotalCount(sqlASTTotalCount: any, dbCall: TDbCall, context: any, options: any): Promise<any>;
|
|
20
|
-
export declare function getScalarFields(table: string, artifacts: IArtifacts): string[];
|
|
21
|
-
export declare function hasMappedFields(artifacts: IArtifacts, table: string, data: any): boolean;
|
|
22
|
-
export declare function mapMappedFields(artifactsForTable: IArtifacts[string], data: any, dbCall: TDbCall, formatQuery: TFormatQuery): Promise<void>;
|
|
1
|
+
import type { IGetSQLASTInput, IArtifacts, IDialect, TDbCall, TFormatQuery, TBeginTransaction, TContext } from "../IRuntime";
|
|
2
|
+
import type { TMiddleware, TResolveParams } from "../IRuntime";
|
|
3
|
+
import Cache from "../Cache";
|
|
4
|
+
export declare function resolve(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, beginTransaction: TBeginTransaction, dialect: IDialect, middlewareHandler: MiddlewareHandler<TMiddleware>, context: TContext, cache?: Cache): Promise<any>;
|
|
5
|
+
export declare class MiddlewareHandler<M extends Function> {
|
|
6
|
+
private _middlewares;
|
|
7
|
+
register(middleware: M): void;
|
|
8
|
+
get(id: number): M | undefined;
|
|
9
|
+
has(id: number): boolean;
|
|
10
|
+
length(): number;
|
|
11
|
+
}
|
|
23
12
|
export declare function postProcess(data: any, fields: IGetSQLASTInput["fields"], shouldRemoveExtraKeys: boolean): void;
|
|
13
|
+
export declare function whereNeedsProcessing(where: any): boolean;
|
|
14
|
+
export declare function _prepareWhere(artifacts: IArtifacts, table: string, data: any, dbCall: TDbCall, formatQuery: TFormatQuery): Promise<{}>;
|