@technicity/data-service-generator 0.2.8 → 0.4.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.
- package/dist/CustomError.d.ts +3 -0
- package/dist/SDKNotFoundError.d.ts +4 -0
- package/dist/addNullFallbacks.d.ts +1 -0
- package/dist/addNullFallbacks.js +3 -0
- package/dist/cursor.d.ts +2 -0
- package/dist/generate.d.ts +20 -0
- package/dist/generate.js +100 -111
- package/dist/getDateTimeStringMySQL.d.ts +1 -0
- package/dist/getDuplicates.d.ts +1 -0
- package/dist/getOrderBy.d.ts +5 -0
- package/dist/getOrderBy.js +54 -0
- package/dist/getSqlAst.d.ts +2 -0
- package/dist/getSqlAst.js +4 -34
- package/dist/getWhere.d.ts +2 -0
- package/dist/getWhere.js +1 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/isNotNullOrUndefined.d.ts +1 -0
- package/dist/ksql.d.ts +9 -0
- package/dist/ksql.js +53 -0
- package/dist/mysql.d.ts +2 -0
- package/dist/mysql.js +0 -3
- package/dist/runTransforms.d.ts +2 -0
- package/dist/runtime/IRuntime.d.ts +47 -0
- package/dist/{IDialect.js → runtime/IRuntime.js} +0 -0
- package/dist/runtime/RuntimeKSQL.d.ts +19 -0
- package/dist/runtime/RuntimeKSQL.js +128 -0
- package/dist/runtime/RuntimeMSSQL.d.ts +21 -0
- package/dist/runtime/RuntimeMSSQL.js +88 -0
- package/dist/runtime/RuntimeMySQL.d.ts +20 -0
- package/dist/runtime/RuntimeMySQL.js +117 -0
- package/dist/runtime/__MSSQL.d.ts +7 -0
- package/dist/runtime/__MSSQL.js +46 -0
- package/dist/runtime/__shared.d.ts +32 -0
- package/dist/{runtime.js → runtime/__shared.js} +213 -230
- package/dist/runtime/__typeCastMSSQL.d.ts +3 -0
- package/dist/runtime/__typeCastMSSQL.js +40 -0
- package/dist/stringifyWhere.d.ts +18 -0
- package/dist/stringifyWhere.js +41 -50
- package/dist/types.d.ts +142 -0
- package/dist/types.js +2 -0
- package/package.json +1 -1
- package/dist/driverOpts.js +0 -54
package/dist/stringifyWhere.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringifyWhere = void 0;
|
|
3
|
+
exports.getEscape = exports.getEscapeId = exports.stringifyWhere = void 0;
|
|
4
4
|
const _ = require("lodash/fp");
|
|
5
5
|
const MySqlString = require("sqlstring");
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
const TSqlString = require("tsqlstring");
|
|
8
|
-
const cursor_1 = require("./cursor");
|
|
9
8
|
function stringifyWhere(input) {
|
|
10
|
-
const { where, table, dialect, args,
|
|
9
|
+
const { where, table, dialect, args, orderBy, rowWithCursorId } = input;
|
|
11
10
|
const escapeId = getEscapeId(dialect);
|
|
12
11
|
const escape = getEscape(dialect);
|
|
13
12
|
let result = _stringifyWhere(where, table, escapeId, escape, "");
|
|
@@ -15,8 +14,6 @@ function stringifyWhere(input) {
|
|
|
15
14
|
table,
|
|
16
15
|
args,
|
|
17
16
|
orderBy,
|
|
18
|
-
primaryKey,
|
|
19
|
-
dialect,
|
|
20
17
|
escapeId,
|
|
21
18
|
escape,
|
|
22
19
|
rowWithCursorId,
|
|
@@ -162,54 +159,46 @@ function getOperatorNeq(value) {
|
|
|
162
159
|
// return { k, v };
|
|
163
160
|
// }
|
|
164
161
|
function stringifyPaginationWhere(input) {
|
|
165
|
-
const { table, args, orderBy,
|
|
166
|
-
if (args?.$paginate?.after
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
162
|
+
const { table, args, orderBy, escapeId, escape, rowWithCursorId } = input;
|
|
163
|
+
if (args?.$paginate?.after == null && args?.$paginate?.before == null) {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
if (rowWithCursorId == null) {
|
|
167
|
+
return "";
|
|
168
|
+
}
|
|
169
|
+
// orderBy should never be null because of getOrderBy, but add a check.
|
|
170
|
+
if (orderBy == null) {
|
|
171
|
+
throw new Error("orderBy cannot be null when paginating");
|
|
172
|
+
}
|
|
173
|
+
function getCompOp(dir) {
|
|
174
|
+
return dir === "asc" ? ">" : "<";
|
|
175
|
+
}
|
|
176
|
+
function printValue(v) {
|
|
177
|
+
if (v === true) {
|
|
178
|
+
v = 1;
|
|
174
179
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
.filter((x) => x.column !== primaryKey)
|
|
178
|
-
.concat({ column: primaryKey, direction: "asc" });
|
|
179
|
-
function printValue(v) {
|
|
180
|
-
if (v === true) {
|
|
181
|
-
v = 1;
|
|
182
|
-
}
|
|
183
|
-
if (v === false) {
|
|
184
|
-
v = 0;
|
|
185
|
-
}
|
|
186
|
-
return escape(v);
|
|
187
|
-
}
|
|
188
|
-
const cond = orders
|
|
189
|
-
.map(({ direction }, i) => {
|
|
190
|
-
const a = orders
|
|
191
|
-
.slice(0, i + 1)
|
|
192
|
-
.map(({ column: col2 }, j, arr) => {
|
|
193
|
-
const field = `${table}.${escapeId(col2)}`;
|
|
194
|
-
const op = j === arr.length - 1 ? getCompOp(direction) : "=";
|
|
195
|
-
let v = rowWithCursorId[col2];
|
|
196
|
-
// TODO?
|
|
197
|
-
if (dialect === "mssql" && v instanceof Date) {
|
|
198
|
-
// Clone, since setMinutes mutates
|
|
199
|
-
v = new Date(v.valueOf());
|
|
200
|
-
v.setMinutes(v.getMinutes() + new Date().getTimezoneOffset());
|
|
201
|
-
}
|
|
202
|
-
v = printValue(v);
|
|
203
|
-
return `${field} ${op} ${v}`;
|
|
204
|
-
})
|
|
205
|
-
.join(" AND ");
|
|
206
|
-
return "(" + a + ")";
|
|
207
|
-
})
|
|
208
|
-
.join(" OR ");
|
|
209
|
-
return "(" + cond + ")";
|
|
180
|
+
if (v === false) {
|
|
181
|
+
v = 0;
|
|
210
182
|
}
|
|
183
|
+
return escape(v);
|
|
211
184
|
}
|
|
212
|
-
|
|
185
|
+
// https://gist.github.com/pcattori/2bb645d587e45c9fdbcabf5cef7a7106
|
|
186
|
+
const cond = orderBy
|
|
187
|
+
.map(({ column, direction }, i) => {
|
|
188
|
+
let a = orderBy.slice(0, i).map(({ column: col2 }) => {
|
|
189
|
+
const field = `${table}.${escapeId(col2)}`;
|
|
190
|
+
const op = "=";
|
|
191
|
+
const v = printValue(rowWithCursorId[col2]);
|
|
192
|
+
return `${field} ${op} ${v}`;
|
|
193
|
+
});
|
|
194
|
+
const field = `${table}.${escapeId(column)}`;
|
|
195
|
+
const op = getCompOp(direction);
|
|
196
|
+
const v = printValue(rowWithCursorId[column]);
|
|
197
|
+
a.push(`${field} ${op} ${v}`);
|
|
198
|
+
return "(" + a.join(" AND ") + ")";
|
|
199
|
+
})
|
|
200
|
+
.join(" OR ");
|
|
201
|
+
return "(" + cond + ")";
|
|
213
202
|
}
|
|
214
203
|
function getEscapeId(dialect) {
|
|
215
204
|
if (dialect === "mysql") {
|
|
@@ -220,6 +209,7 @@ function getEscapeId(dialect) {
|
|
|
220
209
|
}
|
|
221
210
|
throw new Error("Unsupported dialect: " + dialect);
|
|
222
211
|
}
|
|
212
|
+
exports.getEscapeId = getEscapeId;
|
|
223
213
|
function getEscape(dialect) {
|
|
224
214
|
if (dialect === "mysql") {
|
|
225
215
|
return MySqlString.escape.bind(MySqlString);
|
|
@@ -229,3 +219,4 @@ function getEscape(dialect) {
|
|
|
229
219
|
}
|
|
230
220
|
throw new Error("Unsupported dialect: " + dialect);
|
|
231
221
|
}
|
|
222
|
+
exports.getEscape = getEscape;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { getWhere } from "./getWhere";
|
|
2
|
+
export declare type IDialect = "mysql" | "mssql" | "ksql";
|
|
3
|
+
export declare type TDbCall = (q: string) => Promise<any>;
|
|
4
|
+
export declare type TFormatQuery = (q: string, values: any[]) => string;
|
|
5
|
+
export declare type ISupplementClientOpts = boolean;
|
|
6
|
+
export declare type IOrderBy = Array<{
|
|
7
|
+
column: string;
|
|
8
|
+
direction: any;
|
|
9
|
+
}>;
|
|
10
|
+
export declare type IArgs = {
|
|
11
|
+
[k: string]: any;
|
|
12
|
+
};
|
|
13
|
+
declare type IField = string | {
|
|
14
|
+
name: string;
|
|
15
|
+
as?: string;
|
|
16
|
+
fields: Array<IField>;
|
|
17
|
+
args?: IArgs;
|
|
18
|
+
transform?: (record: any) => any;
|
|
19
|
+
};
|
|
20
|
+
declare type IWhere = (table: string, args: IArgs) => string | undefined;
|
|
21
|
+
declare type IASTChildColumn = {
|
|
22
|
+
type: "column";
|
|
23
|
+
name: string;
|
|
24
|
+
fieldName: string;
|
|
25
|
+
as: string;
|
|
26
|
+
fromOtherTable?: string | undefined;
|
|
27
|
+
};
|
|
28
|
+
declare type IASTChildComposite = {
|
|
29
|
+
type: "composite";
|
|
30
|
+
name: string[];
|
|
31
|
+
fieldName: string;
|
|
32
|
+
as: string;
|
|
33
|
+
fromOtherTable?: string | undefined;
|
|
34
|
+
};
|
|
35
|
+
declare type ISqlJoin = (t1: string, t2: string, args: IArgs) => string;
|
|
36
|
+
declare type ISqlBatch = {
|
|
37
|
+
thisKey: IASTChildColumn;
|
|
38
|
+
parentKey: IASTChildColumn;
|
|
39
|
+
};
|
|
40
|
+
declare type IJunction = {
|
|
41
|
+
sqlTable: string;
|
|
42
|
+
as: string;
|
|
43
|
+
uniqueKey: string | string[];
|
|
44
|
+
where?: IWhere;
|
|
45
|
+
sqlBatch: {
|
|
46
|
+
thisKey: IASTChildColumn;
|
|
47
|
+
parentKey: IASTChildColumn;
|
|
48
|
+
sqlJoin: ISqlJoin;
|
|
49
|
+
};
|
|
50
|
+
} | {
|
|
51
|
+
sqlTable: string;
|
|
52
|
+
as: string;
|
|
53
|
+
where?: IWhere;
|
|
54
|
+
sqlJoins: Array<ISqlJoin>;
|
|
55
|
+
};
|
|
56
|
+
export declare type IGetSQLASTInput = {
|
|
57
|
+
table: string;
|
|
58
|
+
fieldName: string;
|
|
59
|
+
fields: Array<IField>;
|
|
60
|
+
args?: IArgs;
|
|
61
|
+
where?: IWhere;
|
|
62
|
+
sqlJoin?: ISqlJoin;
|
|
63
|
+
sqlBatch?: ISqlBatch;
|
|
64
|
+
junction?: IJunction;
|
|
65
|
+
grabMany: boolean;
|
|
66
|
+
getWhere: typeof getWhere;
|
|
67
|
+
orderBy?: IOrderBy | undefined;
|
|
68
|
+
rowWithCursorId?: any;
|
|
69
|
+
artifacts: IArtifacts;
|
|
70
|
+
dialect: IDialect;
|
|
71
|
+
firstChild?: IASTChildColumn | IASTChildComposite;
|
|
72
|
+
};
|
|
73
|
+
export declare type IRelation = {
|
|
74
|
+
table: string;
|
|
75
|
+
foreignKey: string;
|
|
76
|
+
referencedTable: string;
|
|
77
|
+
referencedKey: string;
|
|
78
|
+
nullable: boolean;
|
|
79
|
+
};
|
|
80
|
+
declare type IOneToManyOrManyToOne = "one-to-many__many-to-one";
|
|
81
|
+
declare type IManyToMany = "many-to-many";
|
|
82
|
+
declare type IRelationKind = "one-to-many" | "many-to-one";
|
|
83
|
+
export declare type IRelationOneToManyOrManyToOne = {
|
|
84
|
+
type: IOneToManyOrManyToOne;
|
|
85
|
+
kind: IRelationKind;
|
|
86
|
+
grabMany: boolean;
|
|
87
|
+
table: string;
|
|
88
|
+
name: string;
|
|
89
|
+
relation: IRelation;
|
|
90
|
+
nullable: boolean;
|
|
91
|
+
};
|
|
92
|
+
export declare type IRelationManyToMany = {
|
|
93
|
+
type: IManyToMany;
|
|
94
|
+
grabMany: boolean;
|
|
95
|
+
table: string;
|
|
96
|
+
junctionTable: string;
|
|
97
|
+
name: string;
|
|
98
|
+
relations: Array<IRelation>;
|
|
99
|
+
};
|
|
100
|
+
export declare type IRelationField = {
|
|
101
|
+
name: string;
|
|
102
|
+
type: IOneToManyOrManyToOne;
|
|
103
|
+
kind: IRelationKind;
|
|
104
|
+
table: string;
|
|
105
|
+
grabMany: boolean;
|
|
106
|
+
relation: IRelation;
|
|
107
|
+
} | {
|
|
108
|
+
name: string;
|
|
109
|
+
type: IManyToMany;
|
|
110
|
+
table: string;
|
|
111
|
+
junctionTable: string;
|
|
112
|
+
grabMany: boolean;
|
|
113
|
+
relations: Array<IRelation>;
|
|
114
|
+
};
|
|
115
|
+
export declare type IMappedField = {
|
|
116
|
+
name: string;
|
|
117
|
+
as: string;
|
|
118
|
+
type: string;
|
|
119
|
+
nullable: boolean;
|
|
120
|
+
table: string;
|
|
121
|
+
foreignKey: string;
|
|
122
|
+
referencedTable: string;
|
|
123
|
+
referencedKey: string;
|
|
124
|
+
};
|
|
125
|
+
export declare type IArtifacts = {
|
|
126
|
+
[k: string]: {
|
|
127
|
+
table: string;
|
|
128
|
+
primaryKey: string;
|
|
129
|
+
scalarFields: Array<string>;
|
|
130
|
+
relationFields: {
|
|
131
|
+
[k: string]: IRelationField;
|
|
132
|
+
};
|
|
133
|
+
mappedFields: {
|
|
134
|
+
[k: string]: IMappedField;
|
|
135
|
+
} | null;
|
|
136
|
+
dateTimeFields: {
|
|
137
|
+
[k: string]: boolean;
|
|
138
|
+
};
|
|
139
|
+
dateTimeFieldsCount: number;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
export {};
|
package/dist/types.js
ADDED
package/package.json
CHANGED
package/dist/driverOpts.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.driverOptsMSSQL = exports.driverOptsMySQL = void 0;
|
|
4
|
-
const mssql = require("mssql");
|
|
5
|
-
exports.driverOptsMySQL = {
|
|
6
|
-
supportBigNumbers: true,
|
|
7
|
-
typeCast: (field, next) => {
|
|
8
|
-
if (field.type == "TINY" && field.length == 1) {
|
|
9
|
-
return field.string() == "1";
|
|
10
|
-
}
|
|
11
|
-
// if (field.type === "DATE") {
|
|
12
|
-
// return field.string();
|
|
13
|
-
// }
|
|
14
|
-
// if (
|
|
15
|
-
// field.type === "TIMESTAMP" ||
|
|
16
|
-
// field.type === "DATE" ||
|
|
17
|
-
// field.type === "DATETIME"
|
|
18
|
-
// ) {
|
|
19
|
-
// return new Date(field.string()).toISOString();
|
|
20
|
-
// }
|
|
21
|
-
if (field.type === "VAR_STRING" || field.type === "STRING") {
|
|
22
|
-
return field.string();
|
|
23
|
-
}
|
|
24
|
-
return next();
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
exports.driverOptsMSSQL = {
|
|
28
|
-
typeCast: (result, meta) => {
|
|
29
|
-
let typeCasts = {};
|
|
30
|
-
for (let k in meta) {
|
|
31
|
-
const v = meta[k];
|
|
32
|
-
// ids
|
|
33
|
-
if (v.identity === true || v.type === mssql.BigInt) {
|
|
34
|
-
typeCasts[v.name] = (x) => typeof x === "string" ? parseInt(x) : x;
|
|
35
|
-
}
|
|
36
|
-
// uuids
|
|
37
|
-
if (v.type === mssql.UniqueIdentifier) {
|
|
38
|
-
typeCasts[v.name] = (x) => typeof x === "string" ? x.toLocaleLowerCase() : x;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const data = result.recordset;
|
|
42
|
-
if (Object.keys(typeCasts).length === 0) {
|
|
43
|
-
return data;
|
|
44
|
-
}
|
|
45
|
-
return data.map((x) => {
|
|
46
|
-
for (let k of Object.keys(x)) {
|
|
47
|
-
if (typeCasts[k]) {
|
|
48
|
-
x[k] = typeCasts[k](x[k]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return x;
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
};
|