sasat 0.21.21 → 0.22.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/cli/index.cjs +604 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +604 -0
- package/dist/index.cjs +773 -943
- package/dist/index.d.cts +1047 -987
- package/dist/index.d.mts +1047 -987
- package/dist/index.mjs +765 -924
- package/dist/migrate-Cc47Mufl.cjs +4165 -0
- package/dist/migrate-DfAkhVPb.mjs +3947 -0
- package/package.json +24 -27
- package/dist/cli/cli.cjs +0 -5626
- package/dist/cli/cli.d.cts +0 -1
- package/dist/cli/cli.d.mts +0 -1
- package/dist/cli/cli.d.ts +0 -1
- package/dist/cli/cli.mjs +0 -5601
- package/dist/index.d.ts +0 -1167
- package/dist/shared/sasat.CFfsuShk.mjs +0 -398
- package/dist/shared/sasat.DHiyRw3a.cjs +0 -436
package/dist/index.cjs
CHANGED
|
@@ -1,1065 +1,895 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
QueryNodeKind2[QueryNodeKind2["Parenthesis"] = 7] = "Parenthesis";
|
|
34
|
-
QueryNodeKind2[QueryNodeKind2["InExpr"] = 8] = "InExpr";
|
|
35
|
-
QueryNodeKind2[QueryNodeKind2["BetweenExpr"] = 9] = "BetweenExpr";
|
|
36
|
-
QueryNodeKind2[QueryNodeKind2["ContainsExpr"] = 10] = "ContainsExpr";
|
|
37
|
-
QueryNodeKind2[QueryNodeKind2["Literal"] = 11] = "Literal";
|
|
38
|
-
QueryNodeKind2[QueryNodeKind2["Sort"] = 12] = "Sort";
|
|
39
|
-
QueryNodeKind2[QueryNodeKind2["Identifier"] = 13] = "Identifier";
|
|
40
|
-
QueryNodeKind2[QueryNodeKind2["Exists"] = 14] = "Exists";
|
|
41
|
-
QueryNodeKind2[QueryNodeKind2["Raw"] = 15] = "Raw";
|
|
42
|
-
QueryNodeKind2[QueryNodeKind2["GroupBy"] = 16] = "GroupBy";
|
|
43
|
-
QueryNodeKind2[QueryNodeKind2["Over"] = 17] = "Over";
|
|
44
|
-
QueryNodeKind2[QueryNodeKind2["Window"] = 18] = "Window";
|
|
45
|
-
return QueryNodeKind2;
|
|
46
|
-
})(QueryNodeKind || {});
|
|
47
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_migrate = require("./migrate-Cc47Mufl.cjs");
|
|
3
|
+
let sqlstring = require("sqlstring");
|
|
4
|
+
sqlstring = require_migrate.__toESM(sqlstring, 1);
|
|
5
|
+
let mysql2_promise = require("mysql2/promise");
|
|
6
|
+
//#region src/db/connectors/mysql/client.ts
|
|
7
|
+
var MysqlClient = class extends require_migrate.DBClient {
|
|
8
|
+
async release() {}
|
|
9
|
+
constructor(connectionOption, logger) {
|
|
10
|
+
super(logger);
|
|
11
|
+
this.connectionOption = connectionOption;
|
|
12
|
+
}
|
|
13
|
+
getConnection() {
|
|
14
|
+
return (0, mysql2_promise.createConnection)({
|
|
15
|
+
dateStrings: true,
|
|
16
|
+
...this.connectionOption
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async transaction() {
|
|
20
|
+
const connection = await this.getConnection();
|
|
21
|
+
await connection.beginTransaction();
|
|
22
|
+
return new require_migrate.MySqlTransaction(connection);
|
|
23
|
+
}
|
|
24
|
+
async execSql(sql) {
|
|
25
|
+
const connection = await this.getConnection();
|
|
26
|
+
const r = await connection.query(sql);
|
|
27
|
+
await connection.end();
|
|
28
|
+
return r[0];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/runtime/dsl/query/sql/nodeToSql.ts
|
|
48
33
|
function partitionBy(ids) {
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
if (!ids || ids.length === 0) return "";
|
|
35
|
+
return `PARTITION BY ${ids.map(Sql.identifier).join(",")} `;
|
|
51
36
|
}
|
|
52
37
|
function orderBy(sorts) {
|
|
53
|
-
|
|
54
|
-
|
|
38
|
+
if (!sorts || sorts.length === 0) return "";
|
|
39
|
+
return `ORDER BY ${sorts.map(Sql.sort).join(",")} `;
|
|
55
40
|
}
|
|
56
41
|
function windowValue(value) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
return value.type;
|
|
42
|
+
if (value.type === "FOLLOWING" || value.type === "PRECEDING") return `${value.value} ${value.type}`;
|
|
43
|
+
return value.type;
|
|
61
44
|
}
|
|
62
|
-
function window$1(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
return `${window2.type} ${windowValue(window2.value)}`;
|
|
45
|
+
function window$1(window) {
|
|
46
|
+
if (!window) return "";
|
|
47
|
+
if (window.between) return `${window.type} BETWEEN ${windowValue(window.start)} AND ${window.end}`;
|
|
48
|
+
return `${window.type} ${windowValue(window.value)}`;
|
|
68
49
|
}
|
|
69
50
|
function over(v) {
|
|
70
|
-
|
|
71
|
-
|
|
51
|
+
if (!v) return "";
|
|
52
|
+
return `OVER (${partitionBy(v.partitionBy)}${orderBy(v.orderBy)}${window$1(v.window)})`;
|
|
72
53
|
}
|
|
73
|
-
const SELECT_ALIAS_SEPARATOR = "__";
|
|
74
54
|
const Sql = {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return `EXISTS (${Sql.queryOrRaw(expr.query)})`;
|
|
160
|
-
},
|
|
161
|
-
sort: (expr) => {
|
|
162
|
-
const field = () => {
|
|
163
|
-
switch (expr.field.kind) {
|
|
164
|
-
case QueryNodeKind.Field:
|
|
165
|
-
return Sql.fieldInCondition(expr.field);
|
|
166
|
-
case QueryNodeKind.Identifier:
|
|
167
|
-
return Sql.identifier(expr.field);
|
|
168
|
-
default:
|
|
169
|
-
return Sql.fn(expr.field);
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
if (expr.direction)
|
|
173
|
-
return `${field()} ${expr.direction === "DESC" ? "DESC" : "ASC"}`;
|
|
174
|
-
return field();
|
|
175
|
-
},
|
|
176
|
-
sorts: (sorts) => sorts.map(Sql.sort).join(", "),
|
|
177
|
-
queryOrRaw: (expr) => {
|
|
178
|
-
if ("kind" in expr) {
|
|
179
|
-
return expr.expr;
|
|
180
|
-
}
|
|
181
|
-
return queryToSql(expr);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
55
|
+
select: (expr) => {
|
|
56
|
+
switch (expr.kind) {
|
|
57
|
+
case 15: return expr.expr;
|
|
58
|
+
case 0: return Sql.fieldInSelect(expr);
|
|
59
|
+
case 13: return Sql.identifier(expr);
|
|
60
|
+
case 1: return Sql.fn(expr);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
literal: (literal) => require_migrate.SqlString.escape(literal.value),
|
|
64
|
+
fieldInCondition: (identifier) => require_migrate.SqlString.escapeId(identifier.table) + "." + require_migrate.SqlString.escapeId(identifier.name),
|
|
65
|
+
fieldInSelect: (identifier) => {
|
|
66
|
+
const alias = identifier.alias && identifier.name !== identifier.alias ? " AS " + require_migrate.SqlString.escapeId(identifier.alias) : "";
|
|
67
|
+
return require_migrate.SqlString.escapeId(identifier.table) + "." + require_migrate.SqlString.escapeId(identifier.name) + alias;
|
|
68
|
+
},
|
|
69
|
+
identifier: (ident) => {
|
|
70
|
+
return require_migrate.SqlString.escapeId(ident.identifier);
|
|
71
|
+
},
|
|
72
|
+
fn: (fn) => `${fn.fnName}(${fn.args.map(Sql.value).join(",")})${over(fn.over)}${fn.alias ? ` AS ${fn.alias}` : ""}`,
|
|
73
|
+
value: (v) => {
|
|
74
|
+
if (v.kind === 1) return Sql.fn(v);
|
|
75
|
+
if (v.kind === 0) return Sql.fieldInCondition(v);
|
|
76
|
+
if (v.kind === 13) return Sql.identifier(v);
|
|
77
|
+
return Sql.literal(v);
|
|
78
|
+
},
|
|
79
|
+
between: (expr) => `${Sql.value(expr.left)} BETWEEN ${Sql.value(expr.begin)} AND ${Sql.value(expr.end)}`,
|
|
80
|
+
contains: (expr) => {
|
|
81
|
+
const operator = expr.isNot ? "NOT LIKE" : "LIKE";
|
|
82
|
+
const val = (value, type) => {
|
|
83
|
+
if (type === "contains") return "%" + value + "%";
|
|
84
|
+
if (type === "start") return "%" + value;
|
|
85
|
+
return value + "%";
|
|
86
|
+
};
|
|
87
|
+
return `${Sql.value(expr.left)} ${operator} ${require_migrate.SqlString.escape(val(expr.right, expr.type))}`;
|
|
88
|
+
},
|
|
89
|
+
in: (expr) => {
|
|
90
|
+
if ("right" in expr) return `${Sql.value(expr.left)} ${expr.operator} (${expr.right.map(Sql.value).join(", ")})`;
|
|
91
|
+
return `${Sql.value(expr.left)} ${expr.operator} (${Sql.queryOrRaw(expr.query)})`;
|
|
92
|
+
},
|
|
93
|
+
comparison: (expr) => `${Sql.value(expr.left)} ${expr.operator} ${Sql.value(expr.right)}`,
|
|
94
|
+
compound: (expr) => `${Sql.booleanValue(expr.left)} ${expr.operator} ${Sql.booleanValue(expr.right)}`,
|
|
95
|
+
isNull: (expr) => `${Sql.value(expr.expr)} ${expr.isNot ? "IS NOT NULL" : "IS NULL"}`,
|
|
96
|
+
paren: (expr) => "(" + Sql.booleanValue(expr.expression) + ")",
|
|
97
|
+
table: (table) => {
|
|
98
|
+
if (!table.subquery) {
|
|
99
|
+
if (table.alias === table.name) return require_migrate.SqlString.escapeId(table.name);
|
|
100
|
+
return require_migrate.SqlString.escapeId(table.name) + " AS " + require_migrate.SqlString.escapeId(table.alias);
|
|
101
|
+
}
|
|
102
|
+
return `(${queryToSql(table.query)}) AS ${require_migrate.SqlString.escapeId(table.alias)}`;
|
|
103
|
+
},
|
|
104
|
+
join: (join) => `${join.type ? join.type + " " : ""}JOIN ${Sql.table(join.table)} ON ` + Sql.booleanValue(join.conditions),
|
|
105
|
+
booleanValue: (expr) => {
|
|
106
|
+
switch (expr.kind) {
|
|
107
|
+
case 9: return Sql.between(expr);
|
|
108
|
+
case 4: return Sql.compound(expr);
|
|
109
|
+
case 5: return Sql.comparison(expr);
|
|
110
|
+
case 10: return Sql.contains(expr);
|
|
111
|
+
case 7: return Sql.paren(expr);
|
|
112
|
+
case 8: return Sql.in(expr);
|
|
113
|
+
case 6: return Sql.isNull(expr);
|
|
114
|
+
case 14: return Sql.exists(expr);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
exists: (expr) => {
|
|
118
|
+
return `EXISTS (${Sql.queryOrRaw(expr.query)})`;
|
|
119
|
+
},
|
|
120
|
+
sort: (expr) => {
|
|
121
|
+
const field = () => {
|
|
122
|
+
switch (expr.field.kind) {
|
|
123
|
+
case 0: return Sql.fieldInCondition(expr.field);
|
|
124
|
+
case 13: return Sql.identifier(expr.field);
|
|
125
|
+
default: return Sql.fn(expr.field);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
if (expr.direction) return `${field()} ${expr.direction === "DESC" ? "DESC" : "ASC"}`;
|
|
129
|
+
return field();
|
|
130
|
+
},
|
|
131
|
+
sorts: (sorts) => sorts.map(Sql.sort).join(", "),
|
|
132
|
+
queryOrRaw: (expr) => {
|
|
133
|
+
if ("kind" in expr) return expr.expr;
|
|
134
|
+
return queryToSql(expr);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/runtime/dsl/query/sql/queryToSql.ts
|
|
185
139
|
const getJoin = (from) => {
|
|
186
|
-
|
|
140
|
+
return from.joins.flatMap((join) => [join, ...getJoin(join.table)]);
|
|
187
141
|
};
|
|
188
142
|
const getLock = (lock) => {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
143
|
+
if (!lock) return "";
|
|
144
|
+
if (lock === "FOR UPDATE") return " FOR UPDATE";
|
|
145
|
+
return " FOR SHARE";
|
|
192
146
|
};
|
|
193
147
|
const queryToSql = (query) => {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
148
|
+
const select = query.select.map(Sql.select).join(", ");
|
|
149
|
+
const join = getJoin(query.from).map(Sql.join).join(" ");
|
|
150
|
+
const where = query.where ? " WHERE " + Sql.booleanValue(query.where) : "";
|
|
151
|
+
const groupBy = query.groupBy ? " GROUP BY" + query.groupBy.cols.map(Sql.value).join(",") : "";
|
|
152
|
+
const having = query.having ? "HAVING " + Sql.booleanValue(query.having) : "";
|
|
153
|
+
const sort = query.sort && query.sort.length !== 0 ? " ORDER BY " + Sql.sorts(query.sort) : "";
|
|
154
|
+
const limit = query.limit ? " LIMIT " + query.limit : "";
|
|
155
|
+
const offset = query.offset ? " OFFSET " + query.offset : "";
|
|
156
|
+
if (offset && !limit) throw new Error("LIMIT is required to use OFFSET.");
|
|
157
|
+
return `SELECT ${select} FROM ${Sql.table(query.from)}` + join + where + groupBy + having + sort + limit + offset + getLock(query.lock);
|
|
158
|
+
};
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/db/sql/expression/comparison.ts
|
|
161
|
+
let Comparison = /* @__PURE__ */ function(Comparison) {
|
|
162
|
+
Comparison["eq"] = "=";
|
|
163
|
+
Comparison["gt"] = ">";
|
|
164
|
+
Comparison["lt"] = "<";
|
|
165
|
+
Comparison["gte"] = ">=";
|
|
166
|
+
Comparison["lte"] = "<=";
|
|
167
|
+
Comparison["neq"] = "<>";
|
|
168
|
+
Comparison["like"] = "LIKE";
|
|
169
|
+
Comparison["notLike"] = "NOT LIKE";
|
|
170
|
+
return Comparison;
|
|
171
|
+
}({});
|
|
172
|
+
const comparisonExpressionToSql = (exp) => {
|
|
173
|
+
const type = Object.hasOwn(exp, "__type") ? exp.__type || "AND" : "AND";
|
|
174
|
+
return Object.entries(exp).map(([key, value]) => {
|
|
175
|
+
const column = sqlstring.escapeId(key);
|
|
176
|
+
if (!Array.isArray(value)) return `${column} = ${sqlstring.escape(value)}`;
|
|
177
|
+
if (value[0] === "IS NULL") return `${column} IS NULL`;
|
|
178
|
+
if (value[0] === "IS NOT NULL") return `${column} IS NOT NULL`;
|
|
179
|
+
if (value[0] === "IN") {
|
|
180
|
+
const [, ...columns] = value;
|
|
181
|
+
return `${column} IN (${[columns.map((column) => sqlstring.escape(column)).join(", ")]})`;
|
|
182
|
+
}
|
|
183
|
+
if (value[0] === "BETWEEN") return `${column} BETWEEN ${sqlstring.escape(value[1])} AND ${sqlstring.escape(value[2])}`;
|
|
184
|
+
if (Object.keys(Comparison).includes(value[0])) return `${column} ${value[0]} ${sqlstring.escape(value[1])}`;
|
|
185
|
+
throw new require_migrate.SasatError("SQL PARSE ERROR");
|
|
186
|
+
}).join(` ${type} `);
|
|
187
|
+
};
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region src/db/sql/expression/conditionExpression.ts
|
|
190
|
+
const conditionExpressionToSql = (exp) => {
|
|
191
|
+
if (Array.isArray(exp)) return CompositeCondition.and(exp).toSQL();
|
|
192
|
+
if (exp instanceof CompositeCondition) return exp.toSQL();
|
|
193
|
+
return comparisonExpressionToSql(exp);
|
|
194
|
+
};
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/db/sql/expression/compositeCondition.ts
|
|
197
|
+
var CompositeCondition = class CompositeCondition {
|
|
198
|
+
constructor(type, conditions) {
|
|
199
|
+
this.type = type;
|
|
200
|
+
this.conditions = conditions;
|
|
201
|
+
}
|
|
202
|
+
static or(conditions) {
|
|
203
|
+
return new CompositeCondition("OR", conditions);
|
|
204
|
+
}
|
|
205
|
+
static and(conditions) {
|
|
206
|
+
return new CompositeCondition("AND", conditions);
|
|
207
|
+
}
|
|
208
|
+
toSQL() {
|
|
209
|
+
return "(" + this.conditions.map(conditionExpressionToSql).join(this.type) + ")";
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/migration/makeMutaion.ts
|
|
214
|
+
const formatSubscription = (subscription) => {
|
|
215
|
+
if (subscription === void 0 || subscription === false) return {
|
|
216
|
+
enabled: false,
|
|
217
|
+
subscriptionFilter: []
|
|
218
|
+
};
|
|
219
|
+
if (subscription === true) return {
|
|
220
|
+
enabled: true,
|
|
221
|
+
subscriptionFilter: []
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
enabled: subscription.enabled,
|
|
225
|
+
subscriptionFilter: subscription.subscriptionFilter || []
|
|
226
|
+
};
|
|
211
227
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
228
|
+
const formatOptions = (type, option) => ({
|
|
229
|
+
type,
|
|
230
|
+
noReFetch: option?.noRefetch || false,
|
|
231
|
+
middlewares: option?.middlewares || [],
|
|
232
|
+
contextFields: option?.contextFields || [],
|
|
233
|
+
subscription: formatSubscription(option?.subscription)
|
|
234
|
+
});
|
|
235
|
+
const Mutations = {
|
|
236
|
+
create: (options) => formatOptions("create", options),
|
|
237
|
+
update: (options) => formatOptions("update", options),
|
|
238
|
+
delete: (options) => formatOptions("delete", options)
|
|
218
239
|
};
|
|
219
|
-
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/migration/makeQuery.ts
|
|
220
242
|
const single = (name, options) => ({
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
243
|
+
type: "single",
|
|
244
|
+
name,
|
|
245
|
+
conditions: options?.conditions || [],
|
|
246
|
+
middlewares: options?.middlewares || []
|
|
225
247
|
});
|
|
226
248
|
const listAll = (name, options) => ({
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
249
|
+
type: "list-all",
|
|
250
|
+
name,
|
|
251
|
+
conditions: options?.conditions || [],
|
|
252
|
+
middlewares: options?.middlewares || []
|
|
231
253
|
});
|
|
232
254
|
const paging = (name, options) => ({
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
255
|
+
type: "list-paging",
|
|
256
|
+
name,
|
|
257
|
+
conditions: options?.conditions || [],
|
|
258
|
+
middlewares: options?.middlewares || []
|
|
237
259
|
});
|
|
238
260
|
const primary = (middlewares = []) => ({
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
261
|
+
type: "primary",
|
|
262
|
+
conditions: [],
|
|
263
|
+
middlewares
|
|
242
264
|
});
|
|
243
265
|
const Queries = {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
266
|
+
single,
|
|
267
|
+
listAll,
|
|
268
|
+
paging,
|
|
269
|
+
primary
|
|
248
270
|
};
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
subscriptionFilter: []
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
if (subscription === true)
|
|
258
|
-
return {
|
|
259
|
-
enabled: true,
|
|
260
|
-
subscriptionFilter: []
|
|
261
|
-
};
|
|
262
|
-
return {
|
|
263
|
-
enabled: subscription.enabled,
|
|
264
|
-
subscriptionFilter: subscription.subscriptionFilter || []
|
|
265
|
-
};
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/runtime/createTypeDef.ts
|
|
273
|
+
const makeArgs = (args) => {
|
|
274
|
+
if (!args || args.length === 0) return "";
|
|
275
|
+
return `(${args.map((arg) => `${arg.name}: ${arg.type}`).join(", ")})`;
|
|
266
276
|
};
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
277
|
+
const makeTypedefString = (typeName, typedef, type) => {
|
|
278
|
+
const entries = Object.entries(typedef);
|
|
279
|
+
if (entries.length === 0) return "";
|
|
280
|
+
return `\
|
|
281
|
+
${type} ${typeName} {
|
|
282
|
+
${entries.map(([field, value]) => {
|
|
283
|
+
if (!value.return) throw new Error(`Return type required: ${typeName}.${field}`);
|
|
284
|
+
return ` ${field}${makeArgs(value.args)}: ${value.return}`;
|
|
285
|
+
}).join("\n")}
|
|
286
|
+
}
|
|
287
|
+
`;
|
|
278
288
|
};
|
|
279
|
-
|
|
289
|
+
const createTypeDef = (typeDefs, inputs) => {
|
|
290
|
+
const types = Object.entries(typeDefs).map(([type, fields]) => makeTypedefString(type, fields, "type"));
|
|
291
|
+
const input = Object.entries(inputs).map(([type, fields]) => makeTypedefString(type, fields, "input"));
|
|
292
|
+
return types.join("\n") + input.join("\n");
|
|
293
|
+
};
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/runtime/dsl/factory.ts
|
|
280
296
|
const compound = (expr, operator) => {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
297
|
+
const active = expr.filter(require_migrate.nonNullable);
|
|
298
|
+
if (active.length === 0) return conditions.eq(literal(1), literal(1));
|
|
299
|
+
return active.reduce((acc, current) => ({
|
|
300
|
+
kind: 4,
|
|
301
|
+
left: acc,
|
|
302
|
+
operator,
|
|
303
|
+
right: current
|
|
304
|
+
}));
|
|
289
305
|
};
|
|
290
306
|
const containsExpr = (isNot, type) => (left, right) => ({
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
307
|
+
kind: 10,
|
|
308
|
+
type,
|
|
309
|
+
left,
|
|
310
|
+
isNot,
|
|
311
|
+
right
|
|
296
312
|
});
|
|
297
313
|
const comparison = (operator) => (left, right) => ({
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
314
|
+
kind: 5,
|
|
315
|
+
left,
|
|
316
|
+
operator,
|
|
317
|
+
right
|
|
302
318
|
});
|
|
303
319
|
const and = (...expr) => compound(expr, "AND");
|
|
304
320
|
const or = (...expr) => compound(expr, "OR");
|
|
305
|
-
const field = (
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
321
|
+
const field = (table, name, alias) => ({
|
|
322
|
+
kind: 0,
|
|
323
|
+
table,
|
|
324
|
+
name,
|
|
325
|
+
alias
|
|
310
326
|
});
|
|
311
327
|
const fn = (fnName, args, alias) => ({
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
328
|
+
kind: 1,
|
|
329
|
+
fnName,
|
|
330
|
+
args,
|
|
331
|
+
alias
|
|
316
332
|
});
|
|
317
333
|
const window = (type, value) => ({
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
334
|
+
kind: 18,
|
|
335
|
+
type,
|
|
336
|
+
between: false,
|
|
337
|
+
value
|
|
322
338
|
});
|
|
323
339
|
const windowBetween = (type, start, end) => ({
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
340
|
+
kind: 18,
|
|
341
|
+
type,
|
|
342
|
+
between: true,
|
|
343
|
+
start,
|
|
344
|
+
end
|
|
329
345
|
});
|
|
330
346
|
const paren = (expression) => ({
|
|
331
|
-
|
|
332
|
-
|
|
347
|
+
kind: 7,
|
|
348
|
+
expression
|
|
333
349
|
});
|
|
334
350
|
const In = (left, right) => {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
351
|
+
if (Array.isArray(right)) {
|
|
352
|
+
if (right.length === 0) return conditions.eq(literal(0), literal(1));
|
|
353
|
+
return {
|
|
354
|
+
kind: 8,
|
|
355
|
+
left,
|
|
356
|
+
operator: "IN",
|
|
357
|
+
right: right.map(literal)
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
kind: 8,
|
|
362
|
+
left,
|
|
363
|
+
operator: "IN",
|
|
364
|
+
query: right
|
|
365
|
+
};
|
|
350
366
|
};
|
|
351
367
|
const notIn = (left, values) => ({
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
368
|
+
kind: 8,
|
|
369
|
+
left,
|
|
370
|
+
operator: "NOT IN",
|
|
371
|
+
right: values.map(literal)
|
|
356
372
|
});
|
|
357
373
|
const between = (left, begin, end) => ({
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
374
|
+
kind: 9,
|
|
375
|
+
left,
|
|
376
|
+
begin,
|
|
377
|
+
end
|
|
362
378
|
});
|
|
363
379
|
const isNull = (isNot) => (expr) => ({
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
380
|
+
kind: 6,
|
|
381
|
+
expr,
|
|
382
|
+
isNot
|
|
367
383
|
});
|
|
368
384
|
const simpleWhere = (tableNameOrAlias, where, isOr = false) => {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
return comparison(value[0])(fe, literal(value[1]));
|
|
375
|
-
return conditions.eq(fe, literal(value));
|
|
376
|
-
})
|
|
377
|
-
);
|
|
385
|
+
return (isOr ? or : and)(...Object.entries(where).map(([f, value]) => {
|
|
386
|
+
const fe = field(tableNameOrAlias, f);
|
|
387
|
+
if (Array.isArray(value)) return comparison(value[0])(fe, literal(value[1]));
|
|
388
|
+
return conditions.eq(fe, literal(value));
|
|
389
|
+
}));
|
|
378
390
|
};
|
|
379
391
|
const exists = (query) => ({
|
|
380
|
-
|
|
381
|
-
|
|
392
|
+
kind: 14,
|
|
393
|
+
query
|
|
382
394
|
});
|
|
383
395
|
const raw = (sql) => ({
|
|
384
|
-
|
|
385
|
-
|
|
396
|
+
kind: 15,
|
|
397
|
+
expr: sql
|
|
386
398
|
});
|
|
387
399
|
const conditions = {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
400
|
+
simpleWhere,
|
|
401
|
+
and,
|
|
402
|
+
or,
|
|
403
|
+
eq: comparison("="),
|
|
404
|
+
neq: comparison("<>"),
|
|
405
|
+
gt: comparison(">"),
|
|
406
|
+
gte: comparison(">="),
|
|
407
|
+
lt: comparison("<"),
|
|
408
|
+
lte: comparison("<="),
|
|
409
|
+
comparison: (left, operator, right) => comparison(operator)(left, right),
|
|
410
|
+
contains: containsExpr(false, "contains"),
|
|
411
|
+
notContains: containsExpr(true, "contains"),
|
|
412
|
+
startsWith: containsExpr(false, "start"),
|
|
413
|
+
notStartsWith: containsExpr(true, "start"),
|
|
414
|
+
endsWith: containsExpr(false, "end"),
|
|
415
|
+
notEndsWith: containsExpr(true, "end"),
|
|
416
|
+
in: In,
|
|
417
|
+
notIn,
|
|
418
|
+
between,
|
|
419
|
+
isNull: isNull(false),
|
|
420
|
+
isNotNull: isNull(true),
|
|
421
|
+
exists
|
|
410
422
|
};
|
|
411
423
|
const table = (name, joins, alias) => ({
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
424
|
+
kind: 2,
|
|
425
|
+
subquery: false,
|
|
426
|
+
name,
|
|
427
|
+
alias,
|
|
428
|
+
joins
|
|
417
429
|
});
|
|
418
430
|
const subQueryTable = (query, joins, alias) => ({
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
431
|
+
kind: 2,
|
|
432
|
+
subquery: true,
|
|
433
|
+
query,
|
|
434
|
+
alias,
|
|
435
|
+
joins
|
|
424
436
|
});
|
|
425
|
-
const join = (
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
437
|
+
const join = (table, conditions, type) => ({
|
|
438
|
+
kind: 3,
|
|
439
|
+
type,
|
|
440
|
+
table,
|
|
441
|
+
conditions
|
|
430
442
|
});
|
|
431
443
|
const literal = (value) => ({
|
|
432
|
-
|
|
433
|
-
|
|
444
|
+
kind: 11,
|
|
445
|
+
value
|
|
434
446
|
});
|
|
435
|
-
const sort = (
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
447
|
+
const sort = (field, direction) => ({
|
|
448
|
+
kind: 12,
|
|
449
|
+
field,
|
|
450
|
+
direction
|
|
439
451
|
});
|
|
440
452
|
const ident = (identifier) => ({
|
|
441
|
-
|
|
442
|
-
|
|
453
|
+
kind: 13,
|
|
454
|
+
identifier
|
|
443
455
|
});
|
|
444
456
|
const QExpr = {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
457
|
+
conditions,
|
|
458
|
+
...conditions,
|
|
459
|
+
field,
|
|
460
|
+
fn,
|
|
461
|
+
window,
|
|
462
|
+
windowBetween,
|
|
463
|
+
paren,
|
|
464
|
+
table,
|
|
465
|
+
subQueryTable,
|
|
466
|
+
join,
|
|
467
|
+
value: literal,
|
|
468
|
+
sort,
|
|
469
|
+
order: sort,
|
|
470
|
+
ident,
|
|
471
|
+
id: ident,
|
|
472
|
+
raw
|
|
473
|
+
};
|
|
474
|
+
//#endregion
|
|
475
|
+
//#region src/runtime/date.ts
|
|
476
|
+
const dateOffset = (date, timeZoneHour) => {
|
|
477
|
+
const offset = timeZoneHour ? timeZoneHour * 60 * 60 * 1e3 : date.getTimezoneOffset() * 6e4;
|
|
478
|
+
return new Date(date.getTime() + offset);
|
|
479
|
+
};
|
|
480
|
+
const zeroPad = (v, len) => v.toString().padStart(len, "0");
|
|
481
|
+
const dateToDatetimeString = (d) => {
|
|
482
|
+
const year = d.getUTCFullYear();
|
|
483
|
+
const month = d.getUTCMonth() + 1;
|
|
484
|
+
const day = d.getUTCDate();
|
|
485
|
+
const hour = d.getUTCHours();
|
|
486
|
+
const minute = d.getUTCMinutes();
|
|
487
|
+
const second = d.getUTCSeconds();
|
|
488
|
+
const millisecond = d.getUTCMilliseconds();
|
|
489
|
+
return zeroPad(year, 4) + "-" + zeroPad(month, 2) + "-" + zeroPad(day, 2) + " " + zeroPad(hour, 2) + ":" + zeroPad(minute, 2) + ":" + zeroPad(second, 2) + "." + zeroPad(millisecond, 3);
|
|
490
|
+
};
|
|
491
|
+
const dateToDateString = (d) => {
|
|
492
|
+
const year = d.getUTCFullYear();
|
|
493
|
+
const month = d.getUTCMonth() + 1;
|
|
494
|
+
const day = d.getUTCDate();
|
|
495
|
+
return zeroPad(year, 4) + "-" + zeroPad(month, 2) + "-" + zeroPad(day, 2);
|
|
496
|
+
};
|
|
497
|
+
const getTodayDateString = (timeZoneHour) => {
|
|
498
|
+
const date = /* @__PURE__ */ new Date();
|
|
499
|
+
date.setHours(0, 0, 0, 0);
|
|
500
|
+
return dateToDateString(dateOffset(date, timeZoneHour));
|
|
461
501
|
};
|
|
462
|
-
|
|
502
|
+
const getTodayDateTimeString = (timeZoneHour) => {
|
|
503
|
+
const date = /* @__PURE__ */ new Date();
|
|
504
|
+
date.setHours(0, 0, 0, 0);
|
|
505
|
+
return dateToDatetimeString(dateOffset(date, timeZoneHour));
|
|
506
|
+
};
|
|
507
|
+
const getDayRange = (date, timeZoneHour) => {
|
|
508
|
+
date.setHours(0, 0, 0, 0);
|
|
509
|
+
const d = dateOffset(date, timeZoneHour || 0);
|
|
510
|
+
const begin = dateToDatetimeString(d);
|
|
511
|
+
d.setDate(d.getDate() + 1);
|
|
512
|
+
return [begin, dateToDatetimeString(d)];
|
|
513
|
+
};
|
|
514
|
+
const getDayRangeQExpr = (date, timeZoneHour) => {
|
|
515
|
+
return getDayRange(date, timeZoneHour).map(QExpr.value);
|
|
516
|
+
};
|
|
517
|
+
//#endregion
|
|
518
|
+
//#region src/runtime/gqlResolveInfoToField.ts
|
|
463
519
|
const selectionSetToField = (selections, number) => {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
}
|
|
481
|
-
return [result, num];
|
|
520
|
+
const result = {
|
|
521
|
+
fields: [],
|
|
522
|
+
relations: {},
|
|
523
|
+
tableAlias: "t" + number
|
|
524
|
+
};
|
|
525
|
+
let num = number;
|
|
526
|
+
for (const it of selections) {
|
|
527
|
+
if (it.kind !== "Field") continue;
|
|
528
|
+
if (it.selectionSet) {
|
|
529
|
+
num += 1;
|
|
530
|
+
const field = selectionSetToField(it.selectionSet.selections, num);
|
|
531
|
+
result.relations[it.name.value] = field[0];
|
|
532
|
+
num = field[1];
|
|
533
|
+
} else if (it.name.value !== "__typename") result.fields.push(it.name.value);
|
|
534
|
+
}
|
|
535
|
+
return [result, num];
|
|
482
536
|
};
|
|
483
537
|
const gqlResolveInfoToField = (info) => {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
538
|
+
return selectionSetToField(info.fieldNodes[0].selectionSet.selections, 0)[0];
|
|
539
|
+
};
|
|
540
|
+
//#endregion
|
|
541
|
+
//#region src/runtime/id.ts
|
|
542
|
+
const makeNumberIdEncoder = (hashId) => {
|
|
543
|
+
return {
|
|
544
|
+
encode: (id) => hashId.encode(id),
|
|
545
|
+
decode: (id) => hashId.decode(id)[0]
|
|
546
|
+
};
|
|
488
547
|
};
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
const
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/runtime/makeResolver.ts
|
|
550
|
+
const makeResolver = (resolver, middlewares = []) => {
|
|
551
|
+
return (...args) => {
|
|
552
|
+
return resolver(...middlewares.reduce((args, middleware) => {
|
|
553
|
+
return middleware(args);
|
|
554
|
+
}, args));
|
|
555
|
+
};
|
|
556
|
+
};
|
|
557
|
+
//#endregion
|
|
558
|
+
//#region src/runtime/resolverMiddleware.ts
|
|
559
|
+
const makeParamsMiddleware = (update) => {
|
|
560
|
+
return (args) => {
|
|
561
|
+
args[1] = update(args[1]);
|
|
562
|
+
return args;
|
|
563
|
+
};
|
|
564
|
+
};
|
|
565
|
+
//#endregion
|
|
566
|
+
//#region src/runtime/dsl/mutation/mutation.ts
|
|
567
|
+
const escapeId = require_migrate.SqlString.escapeId;
|
|
492
568
|
const onDuplicateKeyUpdate = (columns) => {
|
|
493
|
-
|
|
494
|
-
|
|
569
|
+
if (!columns || columns.length === 0) return "";
|
|
570
|
+
return " ON DUPLICATE KEY UPDATE " + columns.map(escapeId).map((it) => `${it} = VALUES(${it})`).join(",");
|
|
495
571
|
};
|
|
496
572
|
const createToSql = (dsl, tableInfo) => {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
dsl.table
|
|
501
|
-
)}(${dsl.fields.map((it) => escapeId(map[it]))}) VALUES ${values} ${onDuplicateKeyUpdate(dsl.upsert)}`;
|
|
573
|
+
const map = tableInfo[dsl.table].columnMap;
|
|
574
|
+
const values = dsl.entities.map((it) => `(${it.map((it) => require_migrate.SqlString.escape(it)).join(",")})`).join(",");
|
|
575
|
+
return `INSERT ${dsl.ignore ? "IGNORE " : ""}INTO ${escapeId(dsl.table)}(${dsl.fields.map((it) => escapeId(map[it]))}) VALUES ${values} ${onDuplicateKeyUpdate(dsl.upsert)}`;
|
|
502
576
|
};
|
|
503
577
|
const updateToSql = (dsl, tableInfo) => {
|
|
504
|
-
|
|
505
|
-
|
|
578
|
+
const map = tableInfo[dsl.table].columnMap;
|
|
579
|
+
return `UPDATE ${escapeId(dsl.table)} SET ${dsl.values.map((it) => escapeId(map[it.field]) + " = " + require_migrate.SqlString.escape(it.value)).join(", ")} WHERE ${Sql.booleanValue(dsl.where)}`;
|
|
506
580
|
};
|
|
507
581
|
const deleteToSql = (dsl) => {
|
|
508
|
-
|
|
509
|
-
dsl.where
|
|
510
|
-
)}`;
|
|
582
|
+
return `DELETE FROM ${escapeId(dsl.table)} WHERE ${Sql.booleanValue(dsl.where)}`;
|
|
511
583
|
};
|
|
512
|
-
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region src/runtime/dsl/query/createQueryResolveInfo.ts
|
|
513
586
|
const joinToQueryResolveInfo = (parentTableAlias, property, fields, map, tableInfo) => {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
info.table,
|
|
524
|
-
key,
|
|
525
|
-
value,
|
|
526
|
-
map,
|
|
527
|
-
tableInfo
|
|
528
|
-
)
|
|
529
|
-
).filter(error.nonNullable),
|
|
530
|
-
property
|
|
531
|
-
};
|
|
587
|
+
const info = map[parentTableAlias][property];
|
|
588
|
+
if (!info) return void 0;
|
|
589
|
+
return {
|
|
590
|
+
tableAlias: fields.tableAlias || info.table,
|
|
591
|
+
isArray: info.array,
|
|
592
|
+
keyAliases: tableInfo[info.table].identifiableFields,
|
|
593
|
+
joins: Object.entries(fields.relations || {}).filter(([, value]) => value).map(([key, value]) => joinToQueryResolveInfo(info.table, key, value, map, tableInfo)).filter(require_migrate.nonNullable),
|
|
594
|
+
property
|
|
595
|
+
};
|
|
532
596
|
};
|
|
533
597
|
const createQueryResolveInfo = (tableName, fields, map, tableInfo) => {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
map,
|
|
545
|
-
tableInfo
|
|
546
|
-
)
|
|
547
|
-
).filter(error.nonNullable),
|
|
548
|
-
property: ""
|
|
549
|
-
};
|
|
550
|
-
};
|
|
551
|
-
|
|
598
|
+
return {
|
|
599
|
+
tableAlias: fields.tableAlias || tableName,
|
|
600
|
+
isArray: true,
|
|
601
|
+
keyAliases: tableInfo[tableName].identifiableFields,
|
|
602
|
+
joins: Object.entries(fields.relations || {}).filter(([, value]) => value).map(([key, value]) => joinToQueryResolveInfo(tableName, key, value, map, tableInfo)).filter(require_migrate.nonNullable),
|
|
603
|
+
property: ""
|
|
604
|
+
};
|
|
605
|
+
};
|
|
606
|
+
//#endregion
|
|
607
|
+
//#region src/runtime/dsl/query/sql/hydrate.ts
|
|
552
608
|
const rowToObjs = (row) => {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
return objs;
|
|
609
|
+
const objs = {};
|
|
610
|
+
for (const [key, value] of Object.entries(row)) {
|
|
611
|
+
const [table, column] = key.split("__");
|
|
612
|
+
if (!objs[table]) objs[table] = {};
|
|
613
|
+
objs[table][column] = value;
|
|
614
|
+
}
|
|
615
|
+
return objs;
|
|
562
616
|
};
|
|
563
617
|
const getUnique = (obj, info) => info.keyAliases.map((it) => obj[it]).join("_~_");
|
|
564
618
|
const execTable = (info, objs, current) => {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
currentTarget[it.property] = execTable(
|
|
592
|
-
it,
|
|
593
|
-
objs,
|
|
594
|
-
currentTarget[it.property]
|
|
595
|
-
);
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return result;
|
|
599
|
-
};
|
|
619
|
+
let entity = objs[info.tableAlias];
|
|
620
|
+
if (entity[info.keyAliases[0]] == null) entity = null;
|
|
621
|
+
let result;
|
|
622
|
+
let currentTarget;
|
|
623
|
+
if (info.isArray) if (!current) {
|
|
624
|
+
result = entity == null ? [] : [entity];
|
|
625
|
+
currentTarget = entity;
|
|
626
|
+
} else {
|
|
627
|
+
currentTarget = entity == null ? null : current.find((item) => item && info.keyAliases.every((key) => item[key] === entity[key]));
|
|
628
|
+
if (currentTarget) result = current;
|
|
629
|
+
else {
|
|
630
|
+
currentTarget = entity;
|
|
631
|
+
result = current;
|
|
632
|
+
if (currentTarget) result.push(currentTarget);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
currentTarget = current || entity;
|
|
637
|
+
result = currentTarget;
|
|
638
|
+
}
|
|
639
|
+
if (currentTarget !== null) for (const it of info.joins) currentTarget[it.property] = execTable(it, objs, currentTarget[it.property]);
|
|
640
|
+
return result;
|
|
641
|
+
};
|
|
642
|
+
/**
|
|
643
|
+
* to use this function require to select primary keys for every table
|
|
644
|
+
*/
|
|
600
645
|
const hydrate = (data, info) => {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
};
|
|
618
|
-
|
|
646
|
+
const result = [];
|
|
647
|
+
const t0mapper = {};
|
|
648
|
+
info.isArray = false;
|
|
649
|
+
for (const row of data) {
|
|
650
|
+
const objs = rowToObjs(row);
|
|
651
|
+
const currentObj = objs[info.tableAlias];
|
|
652
|
+
const unique = getUnique(currentObj, info);
|
|
653
|
+
if (t0mapper[unique] === void 0) {
|
|
654
|
+
t0mapper[unique] = result.length;
|
|
655
|
+
result.push(execTable(info, objs, currentObj));
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
const base = result[t0mapper[unique]];
|
|
659
|
+
execTable(info, objs, base);
|
|
660
|
+
}
|
|
661
|
+
return result;
|
|
662
|
+
};
|
|
663
|
+
//#endregion
|
|
664
|
+
//#region src/runtime/sql/runQuery.ts
|
|
619
665
|
const notTypeName = (fieldName) => fieldName !== "__typename";
|
|
620
666
|
const createQuery = (baseTableName, fields, options, tableInfo, relationMap, context) => {
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
resolveFields(rel.table, table2),
|
|
651
|
-
QExpr.and(
|
|
652
|
-
rel.condition({
|
|
653
|
-
parentTableAlias: tableAlias,
|
|
654
|
-
childTableAlias: table2.tableAlias || "t" + current,
|
|
655
|
-
context
|
|
656
|
-
}),
|
|
657
|
-
table2.joinOn
|
|
658
|
-
),
|
|
659
|
-
"LEFT"
|
|
660
|
-
);
|
|
661
|
-
}).filter(error.nonNullable),
|
|
662
|
-
tableAlias
|
|
663
|
-
);
|
|
664
|
-
};
|
|
665
|
-
const from = resolveFields(baseTableName, fields);
|
|
666
|
-
return {
|
|
667
|
-
select,
|
|
668
|
-
from,
|
|
669
|
-
...options
|
|
670
|
-
};
|
|
667
|
+
let tableCount = 0;
|
|
668
|
+
const select = [];
|
|
669
|
+
const resolveFields = (tableName, table) => {
|
|
670
|
+
const tableAlias = table.tableAlias || "t" + tableCount;
|
|
671
|
+
table.tableAlias = tableAlias;
|
|
672
|
+
tableCount++;
|
|
673
|
+
const info = tableInfo[tableName];
|
|
674
|
+
select.push(...require_migrate.unique([...table.fields.filter((it) => {
|
|
675
|
+
return notTypeName(it) && info.columnMap[it];
|
|
676
|
+
}), ...info.identifiableFields]).map((it) => {
|
|
677
|
+
const realName = info.columnMap[it] || it;
|
|
678
|
+
return QExpr.field(tableAlias, realName, tableAlias + "__" + it);
|
|
679
|
+
}));
|
|
680
|
+
return QExpr.table(tableName, Object.entries(table.relations || {}).map(([relationName, table]) => {
|
|
681
|
+
const current = tableCount;
|
|
682
|
+
const rel = relationMap[tableName][relationName];
|
|
683
|
+
if (!rel) return void 0;
|
|
684
|
+
return QExpr.join(resolveFields(rel.table, table), QExpr.and(rel.condition({
|
|
685
|
+
parentTableAlias: tableAlias,
|
|
686
|
+
childTableAlias: table.tableAlias || "t" + current,
|
|
687
|
+
context
|
|
688
|
+
}), table.joinOn), "LEFT");
|
|
689
|
+
}).filter(require_migrate.nonNullable), tableAlias);
|
|
690
|
+
};
|
|
691
|
+
return {
|
|
692
|
+
select,
|
|
693
|
+
from: resolveFields(baseTableName, fields),
|
|
694
|
+
...options
|
|
695
|
+
};
|
|
671
696
|
};
|
|
672
697
|
const createPagingInnerQuery = (tableName, tableAlias, fields, option, tableInfo, relationMap) => {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
};
|
|
689
|
-
const createPagingFieldQuery = ({
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
async find(fields = { fields: this.fields }, options, context) {
|
|
822
|
-
const query = createQuery(
|
|
823
|
-
this.tableName,
|
|
824
|
-
fields,
|
|
825
|
-
options,
|
|
826
|
-
this.tableInfo,
|
|
827
|
-
this.relationMap,
|
|
828
|
-
context
|
|
829
|
-
);
|
|
830
|
-
return this.executeQuery(query, fields);
|
|
831
|
-
}
|
|
832
|
-
async findPageable(paging, fields = { fields: this.fields }, options, context) {
|
|
833
|
-
const query = createPagingFieldQuery({
|
|
834
|
-
baseTableName: this.tableName,
|
|
835
|
-
fields,
|
|
836
|
-
tableInfo: this.tableInfo,
|
|
837
|
-
relationMap: this.relationMap,
|
|
838
|
-
pagingOption: paging,
|
|
839
|
-
queryOption: options,
|
|
840
|
-
context
|
|
841
|
-
});
|
|
842
|
-
return this.executeQuery(query, fields);
|
|
843
|
-
}
|
|
844
|
-
async executeQuery(query, fields) {
|
|
845
|
-
const info = createQueryResolveInfo(
|
|
846
|
-
this.tableName,
|
|
847
|
-
fields,
|
|
848
|
-
this.relationMap,
|
|
849
|
-
this.tableInfo
|
|
850
|
-
);
|
|
851
|
-
const sql = queryToSql(query);
|
|
852
|
-
this.queryLogger(sql);
|
|
853
|
-
const resultRows = await this.client.rawQuery(sql);
|
|
854
|
-
return hydrate(resultRows, info);
|
|
855
|
-
}
|
|
856
|
-
createIdentifiableExpression(entity) {
|
|
857
|
-
const expr = this.identifyFields.map((it) => {
|
|
858
|
-
const value = entity[it];
|
|
859
|
-
if (!value) throw new Error(`field ${it} is required`);
|
|
860
|
-
return QExpr.eq(
|
|
861
|
-
QExpr.field(this.tableName, this.tableInfo[this.tableName].columnMap[it]),
|
|
862
|
-
QExpr.value(value)
|
|
863
|
-
);
|
|
864
|
-
});
|
|
865
|
-
return QExpr.and(...expr);
|
|
866
|
-
}
|
|
867
|
-
getRelationMap() {
|
|
868
|
-
return this.relationMap[this.tableName];
|
|
869
|
-
}
|
|
870
|
-
fieldToColumn(fields) {
|
|
871
|
-
return fields.map((it) => this.tableInfo[this.tableName].columnMap[it] || it);
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
const noop = () => {
|
|
875
|
-
};
|
|
876
|
-
|
|
698
|
+
const map = tableInfo[tableName].columnMap;
|
|
699
|
+
return {
|
|
700
|
+
select: require_migrate.unique([
|
|
701
|
+
...tableInfo[tableName].identifiableKeys,
|
|
702
|
+
...Object.keys(fields.relations || {}).flatMap((key) => {
|
|
703
|
+
return relationMap[tableName][key]?.requiredColumns || [];
|
|
704
|
+
}),
|
|
705
|
+
...fields.fields.filter((it) => notTypeName(it) && map[it]).map((it) => map[it] || it)
|
|
706
|
+
]).map((it) => QExpr.field(tableAlias, it)),
|
|
707
|
+
from: QExpr.table(tableName, option.join || [], tableAlias),
|
|
708
|
+
limit: option.numberOfItem,
|
|
709
|
+
offset: option.offset,
|
|
710
|
+
where: option.where,
|
|
711
|
+
sort: option.sort
|
|
712
|
+
};
|
|
713
|
+
};
|
|
714
|
+
const createPagingFieldQuery = ({ baseTableName, fields, queryOption, pagingOption, tableInfo, relationMap, context }) => {
|
|
715
|
+
const innerQuery = createPagingInnerQuery(baseTableName, fields.tableAlias || "t0", fields, pagingOption, tableInfo, relationMap);
|
|
716
|
+
const main = createQuery(baseTableName, fields, queryOption, tableInfo, relationMap, context);
|
|
717
|
+
return {
|
|
718
|
+
select: main.select,
|
|
719
|
+
from: {
|
|
720
|
+
...main.from,
|
|
721
|
+
subquery: true,
|
|
722
|
+
query: innerQuery
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
};
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region src/runtime/sasatDBDatasource.ts
|
|
728
|
+
var SasatDBDatasource = class {
|
|
729
|
+
constructor(client = require_migrate.getDbClient()) {
|
|
730
|
+
this.client = client;
|
|
731
|
+
}
|
|
732
|
+
async create(entity, option) {
|
|
733
|
+
const obj = {
|
|
734
|
+
...this.getDefaultValueString(),
|
|
735
|
+
...entity
|
|
736
|
+
};
|
|
737
|
+
const fields = Object.keys(obj);
|
|
738
|
+
const sql = createToSql({
|
|
739
|
+
table: this.tableName,
|
|
740
|
+
fields,
|
|
741
|
+
entities: [fields.map((key) => obj[key])],
|
|
742
|
+
upsert: option?.upsert?.updateColumns,
|
|
743
|
+
ignore: option?.ignore
|
|
744
|
+
}, this.tableInfo);
|
|
745
|
+
const response = await this.client.rawCommand(sql);
|
|
746
|
+
if (!this.autoIncrementColumn) return obj;
|
|
747
|
+
return {
|
|
748
|
+
...obj,
|
|
749
|
+
[this.autoIncrementColumn]: response.insertId
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
async createBulk(entities, option) {
|
|
753
|
+
const objects = entities.map((it) => ({
|
|
754
|
+
...this.getDefaultValueString(),
|
|
755
|
+
...it
|
|
756
|
+
}));
|
|
757
|
+
const keys = Object.keys(objects[0]);
|
|
758
|
+
const values = objects.map((it) => keys.map((key) => it[key]));
|
|
759
|
+
const sql = createToSql({
|
|
760
|
+
table: this.tableName,
|
|
761
|
+
fields: keys,
|
|
762
|
+
entities: values,
|
|
763
|
+
upsert: option?.upsert?.updateColumns,
|
|
764
|
+
ignore: option?.ignore
|
|
765
|
+
}, this.tableInfo);
|
|
766
|
+
return await this.client.rawCommand(sql);
|
|
767
|
+
}
|
|
768
|
+
async upsert(entity, updateFields = this.primaryKeys) {
|
|
769
|
+
return this.create(entity, { upsert: { updateColumns: this.fieldToColumn(updateFields) } });
|
|
770
|
+
}
|
|
771
|
+
update(entity) {
|
|
772
|
+
const sql = updateToSql({
|
|
773
|
+
table: this.tableName,
|
|
774
|
+
values: Object.entries(entity).filter(([, value]) => value !== void 0).map(([column, value]) => ({
|
|
775
|
+
field: column,
|
|
776
|
+
value
|
|
777
|
+
})),
|
|
778
|
+
where: this.createIdentifiableExpression(entity)
|
|
779
|
+
}, this.tableInfo);
|
|
780
|
+
return this.client.rawCommand(sql);
|
|
781
|
+
}
|
|
782
|
+
updateWhere(update, condition) {
|
|
783
|
+
const sql = updateToSql({
|
|
784
|
+
table: this.tableName,
|
|
785
|
+
values: Object.entries(update).filter(([, value]) => value !== void 0).map(([column, value]) => ({
|
|
786
|
+
field: column,
|
|
787
|
+
value
|
|
788
|
+
})),
|
|
789
|
+
where: condition
|
|
790
|
+
}, this.tableInfo);
|
|
791
|
+
return this.client.rawCommand(sql);
|
|
792
|
+
}
|
|
793
|
+
async delete(entity) {
|
|
794
|
+
return this.deleteWhere(this.createIdentifiableExpression(entity));
|
|
795
|
+
}
|
|
796
|
+
async deleteWhere(condition) {
|
|
797
|
+
const sql = deleteToSql({
|
|
798
|
+
table: this.tableName,
|
|
799
|
+
where: condition
|
|
800
|
+
});
|
|
801
|
+
return this.client.rawCommand(sql);
|
|
802
|
+
}
|
|
803
|
+
async first(fields, option, context) {
|
|
804
|
+
const result = await this.find(fields, option, context);
|
|
805
|
+
if (result.length !== 0) return result[0];
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
808
|
+
async find(fields = { fields: this.fields }, options, context) {
|
|
809
|
+
const query = createQuery(this.tableName, fields, options, this.tableInfo, this.relationMap, context);
|
|
810
|
+
return this.executeQuery(query, fields);
|
|
811
|
+
}
|
|
812
|
+
async findPageable(paging, fields = { fields: this.fields }, options, context) {
|
|
813
|
+
const query = createPagingFieldQuery({
|
|
814
|
+
baseTableName: this.tableName,
|
|
815
|
+
fields,
|
|
816
|
+
tableInfo: this.tableInfo,
|
|
817
|
+
relationMap: this.relationMap,
|
|
818
|
+
pagingOption: paging,
|
|
819
|
+
queryOption: options,
|
|
820
|
+
context
|
|
821
|
+
});
|
|
822
|
+
return this.executeQuery(query, fields);
|
|
823
|
+
}
|
|
824
|
+
async executeQuery(query, fields) {
|
|
825
|
+
const info = createQueryResolveInfo(this.tableName, fields, this.relationMap, this.tableInfo);
|
|
826
|
+
const sql = queryToSql(query);
|
|
827
|
+
return hydrate(await this.client.rawQuery(sql), info);
|
|
828
|
+
}
|
|
829
|
+
createIdentifiableExpression(entity) {
|
|
830
|
+
const expr = this.identifyFields.map((it) => {
|
|
831
|
+
const value = entity[it];
|
|
832
|
+
if (!value) throw new Error(`field ${it} is required`);
|
|
833
|
+
return QExpr.eq(QExpr.field(this.tableName, this.tableInfo[this.tableName].columnMap[it]), QExpr.value(value));
|
|
834
|
+
});
|
|
835
|
+
return QExpr.and(...expr);
|
|
836
|
+
}
|
|
837
|
+
getRelationMap() {
|
|
838
|
+
return this.relationMap[this.tableName];
|
|
839
|
+
}
|
|
840
|
+
fieldToColumn(fields) {
|
|
841
|
+
return fields.map((it) => this.tableInfo[this.tableName].columnMap[it] || it);
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
//#endregion
|
|
845
|
+
//#region src/util/dateUtil.ts
|
|
877
846
|
const getCurrentDateTimeString = () => {
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
const makeArgs = (args) => {
|
|
889
|
-
if (!args || args.length === 0) return "";
|
|
890
|
-
return `(${args.map((arg) => `${arg.name}: ${arg.type}`).join(", ")})`;
|
|
891
|
-
};
|
|
892
|
-
const makeTypedefString = (typeName, typedef, type) => {
|
|
893
|
-
const entries = Object.entries(typedef);
|
|
894
|
-
if (entries.length === 0) return "";
|
|
895
|
-
return `${type} ${typeName} {
|
|
896
|
-
${entries.map(([field, value]) => {
|
|
897
|
-
if (!value.return)
|
|
898
|
-
throw new Error(`Return type required: ${typeName}.${field}`);
|
|
899
|
-
return ` ${field}${makeArgs(value.args)}: ${value.return}`;
|
|
900
|
-
}).join("\n")}
|
|
901
|
-
}
|
|
902
|
-
`;
|
|
903
|
-
};
|
|
904
|
-
const createTypeDef = (typeDefs, inputs) => {
|
|
905
|
-
const types = Object.entries(typeDefs).map(
|
|
906
|
-
([type, fields]) => makeTypedefString(type, fields, "type")
|
|
907
|
-
);
|
|
908
|
-
const input = Object.entries(inputs).map(
|
|
909
|
-
([type, fields]) => makeTypedefString(type, fields, "input")
|
|
910
|
-
);
|
|
911
|
-
return types.join("\n") + input.join("\n");
|
|
912
|
-
};
|
|
913
|
-
|
|
914
|
-
var Comparison = /* @__PURE__ */ ((Comparison2) => {
|
|
915
|
-
Comparison2["eq"] = "=";
|
|
916
|
-
Comparison2["gt"] = ">";
|
|
917
|
-
Comparison2["lt"] = "<";
|
|
918
|
-
Comparison2["gte"] = ">=";
|
|
919
|
-
Comparison2["lte"] = "<=";
|
|
920
|
-
Comparison2["neq"] = "<>";
|
|
921
|
-
Comparison2["like"] = "LIKE";
|
|
922
|
-
Comparison2["notLike"] = "NOT LIKE";
|
|
923
|
-
return Comparison2;
|
|
924
|
-
})(Comparison || {});
|
|
925
|
-
const comparisonExpressionToSql = (exp) => {
|
|
926
|
-
const type = Object.prototype.hasOwnProperty.call(exp, "__type") ? exp.__type || "AND" : "AND";
|
|
927
|
-
return Object.entries(exp).map(([key, value]) => {
|
|
928
|
-
const column = SqlString__namespace.escapeId(key);
|
|
929
|
-
if (!Array.isArray(value))
|
|
930
|
-
return `${column} = ${SqlString__namespace.escape(value)}`;
|
|
931
|
-
if (value[0] === "IS NULL") return `${column} IS NULL`;
|
|
932
|
-
if (value[0] === "IS NOT NULL") return `${column} IS NOT NULL`;
|
|
933
|
-
if (value[0] === "IN") {
|
|
934
|
-
const [, ...columns] = value;
|
|
935
|
-
return `${column} IN (${[
|
|
936
|
-
columns.map((column2) => SqlString__namespace.escape(column2)).join(", ")
|
|
937
|
-
]})`;
|
|
938
|
-
}
|
|
939
|
-
if (value[0] === "BETWEEN")
|
|
940
|
-
return `${column} BETWEEN ${SqlString__namespace.escape(
|
|
941
|
-
value[1]
|
|
942
|
-
)} AND ${SqlString__namespace.escape(value[2])}`;
|
|
943
|
-
if (Object.keys(Comparison).includes(value[0]))
|
|
944
|
-
return `${column} ${value[0]} ${SqlString__namespace.escape(value[1])}`;
|
|
945
|
-
throw new error.SasatError("SQL PARSE ERROR");
|
|
946
|
-
}).join(` ${type} `);
|
|
947
|
-
};
|
|
948
|
-
|
|
949
|
-
const conditionExpressionToSql = (exp) => {
|
|
950
|
-
if (Array.isArray(exp)) {
|
|
951
|
-
return CompositeCondition.and(exp).toSQL();
|
|
952
|
-
}
|
|
953
|
-
if (exp instanceof CompositeCondition) return exp.toSQL();
|
|
954
|
-
return comparisonExpressionToSql(exp);
|
|
955
|
-
};
|
|
956
|
-
|
|
957
|
-
class CompositeCondition {
|
|
958
|
-
constructor(type, conditions) {
|
|
959
|
-
this.type = type;
|
|
960
|
-
this.conditions = conditions;
|
|
961
|
-
}
|
|
962
|
-
static or(conditions) {
|
|
963
|
-
return new CompositeCondition("OR", conditions);
|
|
964
|
-
}
|
|
965
|
-
static and(conditions) {
|
|
966
|
-
return new CompositeCondition("AND", conditions);
|
|
967
|
-
}
|
|
968
|
-
toSQL() {
|
|
969
|
-
return "(" + this.conditions.map(conditionExpressionToSql).join(this.type) + ")";
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
const dateOffset = (date, timeZoneHour) => {
|
|
974
|
-
const offset = timeZoneHour ? timeZoneHour * 60 * 60 * 1e3 : date.getTimezoneOffset() * 6e4;
|
|
975
|
-
return new Date(date.getTime() + offset);
|
|
976
|
-
};
|
|
977
|
-
const zeroPad = (v, len) => v.toString().padStart(len, "0");
|
|
978
|
-
const dateToDatetimeString = (d) => {
|
|
979
|
-
const year = d.getUTCFullYear();
|
|
980
|
-
const month = d.getUTCMonth() + 1;
|
|
981
|
-
const day = d.getUTCDate();
|
|
982
|
-
const hour = d.getUTCHours();
|
|
983
|
-
const minute = d.getUTCMinutes();
|
|
984
|
-
const second = d.getUTCSeconds();
|
|
985
|
-
const millisecond = d.getUTCMilliseconds();
|
|
986
|
-
return zeroPad(year, 4) + "-" + zeroPad(month, 2) + "-" + zeroPad(day, 2) + " " + zeroPad(hour, 2) + ":" + zeroPad(minute, 2) + ":" + zeroPad(second, 2) + "." + zeroPad(millisecond, 3);
|
|
987
|
-
};
|
|
988
|
-
const dateToDateString = (d) => {
|
|
989
|
-
const year = d.getUTCFullYear();
|
|
990
|
-
const month = d.getUTCMonth() + 1;
|
|
991
|
-
const day = d.getUTCDate();
|
|
992
|
-
return zeroPad(year, 4) + "-" + zeroPad(month, 2) + "-" + zeroPad(day, 2);
|
|
993
|
-
};
|
|
994
|
-
const getTodayDateString = (timeZoneHour) => {
|
|
995
|
-
const date = /* @__PURE__ */ new Date();
|
|
996
|
-
date.setHours(0, 0, 0, 0);
|
|
997
|
-
return dateToDateString(dateOffset(date, timeZoneHour));
|
|
998
|
-
};
|
|
999
|
-
const getTodayDateTimeString = (timeZoneHour) => {
|
|
1000
|
-
const date = /* @__PURE__ */ new Date();
|
|
1001
|
-
date.setHours(0, 0, 0, 0);
|
|
1002
|
-
return dateToDatetimeString(dateOffset(date, timeZoneHour));
|
|
1003
|
-
};
|
|
1004
|
-
const getDayRange = (date, timeZoneHour) => {
|
|
1005
|
-
date.setHours(0, 0, 0, 0);
|
|
1006
|
-
const d = dateOffset(date, timeZoneHour || 0);
|
|
1007
|
-
const begin = dateToDatetimeString(d);
|
|
1008
|
-
d.setDate(d.getDate() + 1);
|
|
1009
|
-
return [begin, dateToDatetimeString(d)];
|
|
1010
|
-
};
|
|
1011
|
-
const getDayRangeQExpr = (date, timeZoneHour) => {
|
|
1012
|
-
return getDayRange(date, timeZoneHour).map(QExpr.value);
|
|
1013
|
-
};
|
|
1014
|
-
|
|
1015
|
-
const makeResolver = (resolver, middlewares = []) => {
|
|
1016
|
-
return (...args) => {
|
|
1017
|
-
const newArgs = middlewares.reduce(
|
|
1018
|
-
(args2, middleware) => {
|
|
1019
|
-
return middleware(args2);
|
|
1020
|
-
},
|
|
1021
|
-
args
|
|
1022
|
-
);
|
|
1023
|
-
return resolver(...newArgs);
|
|
1024
|
-
};
|
|
1025
|
-
};
|
|
1026
|
-
|
|
847
|
+
const pad = (number) => {
|
|
848
|
+
if (number < 10) return "0" + number;
|
|
849
|
+
return number;
|
|
850
|
+
};
|
|
851
|
+
const date = /* @__PURE__ */ new Date();
|
|
852
|
+
return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + " " + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
|
|
853
|
+
};
|
|
854
|
+
//#endregion
|
|
855
|
+
//#region src/runtime/pagingOption.ts
|
|
1027
856
|
const pagingOption = (option) => {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
exports.Conditions = error.Conditions;
|
|
1038
|
-
exports.SqlString = error.SqlString;
|
|
1039
|
-
exports.assignDeep = error.assignDeep;
|
|
1040
|
-
exports.formatQuery = error.formatQuery;
|
|
1041
|
-
exports.getDbClient = error.getDbClient;
|
|
1042
|
-
exports.pick = error.pick;
|
|
1043
|
-
exports.setConfig = error.setConfig;
|
|
857
|
+
const sort = option.order ? [QExpr.sort(QExpr.field("t1", option.order), option?.asc === false ? "DESC" : "ASC")] : [];
|
|
858
|
+
return {
|
|
859
|
+
numberOfItem: option.numberOfItem,
|
|
860
|
+
offset: option.offset,
|
|
861
|
+
sort
|
|
862
|
+
};
|
|
863
|
+
};
|
|
864
|
+
//#endregion
|
|
1044
865
|
exports.CompositeCondition = CompositeCondition;
|
|
866
|
+
exports.Conditions = require_migrate.Conditions;
|
|
1045
867
|
exports.Mutations = Mutations;
|
|
868
|
+
exports.MysqlClient = MysqlClient;
|
|
1046
869
|
exports.QExpr = QExpr;
|
|
1047
870
|
exports.Queries = Queries;
|
|
1048
871
|
exports.SasatDBDatasource = SasatDBDatasource;
|
|
1049
872
|
exports.Sql = Sql;
|
|
873
|
+
exports.SqlString = require_migrate.SqlString;
|
|
874
|
+
exports.assignDeep = require_migrate.assignDeep;
|
|
1050
875
|
exports.createTypeDef = createTypeDef;
|
|
1051
876
|
exports.dateOffset = dateOffset;
|
|
1052
877
|
exports.dateToDateString = dateToDateString;
|
|
1053
878
|
exports.dateToDatetimeString = dateToDatetimeString;
|
|
879
|
+
exports.formatQuery = require_migrate.formatQuery;
|
|
1054
880
|
exports.getCurrentDateTimeString = getCurrentDateTimeString;
|
|
1055
881
|
exports.getDayRange = getDayRange;
|
|
1056
882
|
exports.getDayRangeQExpr = getDayRangeQExpr;
|
|
883
|
+
exports.getDbClient = require_migrate.getDbClient;
|
|
1057
884
|
exports.getTodayDateString = getTodayDateString;
|
|
1058
885
|
exports.getTodayDateTimeString = getTodayDateTimeString;
|
|
1059
886
|
exports.gqlResolveInfoToField = gqlResolveInfoToField;
|
|
1060
887
|
exports.makeNumberIdEncoder = makeNumberIdEncoder;
|
|
1061
888
|
exports.makeParamsMiddleware = makeParamsMiddleware;
|
|
1062
889
|
exports.makeResolver = makeResolver;
|
|
890
|
+
exports.migrate = require_migrate.migrate;
|
|
1063
891
|
exports.pagingOption = pagingOption;
|
|
892
|
+
exports.pick = require_migrate.pick;
|
|
1064
893
|
exports.qe = QExpr;
|
|
1065
894
|
exports.queryToSql = queryToSql;
|
|
895
|
+
exports.setConfig = require_migrate.setConfig;
|