sasat 0.21.21 → 0.22.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.cjs +4709 -930
- package/dist/index.d.cts +1047 -987
- package/dist/index.d.mts +1047 -987
- package/dist/index.mjs +4677 -912
- package/package.json +22 -25
- 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
|
@@ -1,398 +0,0 @@
|
|
|
1
|
-
import SqlString__default from 'sqlstring';
|
|
2
|
-
import * as mysql from 'mysql2';
|
|
3
|
-
import { promisify } from 'util';
|
|
4
|
-
import path__default, { join } from 'path';
|
|
5
|
-
import fs from 'fs-extra';
|
|
6
|
-
import yaml from 'js-yaml';
|
|
7
|
-
|
|
8
|
-
const { escapeId, escape } = SqlString__default;
|
|
9
|
-
const SqlString = {
|
|
10
|
-
escape: (value) => escape(value, true),
|
|
11
|
-
escapeId: (name) => escapeId(name)
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const assignDeep = (base, ...objects) => {
|
|
15
|
-
const assign = (target, key, value) => {
|
|
16
|
-
if (key === "__proto__" || key === "constructor") {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (Array.isArray(target[key]) && Array.isArray(value)) {
|
|
20
|
-
target[key] = [...target[key], ...value];
|
|
21
|
-
} else if (typeof target[key] === "object" && typeof value === "object") {
|
|
22
|
-
assignDeep(target[key], value);
|
|
23
|
-
} else {
|
|
24
|
-
target[key] = value;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
objects.forEach((obj) => {
|
|
28
|
-
if (typeof obj === "object") {
|
|
29
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
30
|
-
assign(base, key, value);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
return base;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const readYmlFile = (filepath) => yaml.load(fs.readFileSync(filepath, "utf8"));
|
|
38
|
-
const mkDirIfNotExist = (path) => {
|
|
39
|
-
if (!fs.pathExistsSync(path)) fs.mkdirpSync(path);
|
|
40
|
-
};
|
|
41
|
-
const writeFileIfNotExist = (path, data) => {
|
|
42
|
-
if (fs.existsSync(path)) return Promise.resolve();
|
|
43
|
-
return fs.writeFile(path, data);
|
|
44
|
-
};
|
|
45
|
-
const writeYmlFile = (path, fileName, obj) => {
|
|
46
|
-
mkDirIfNotExist(path);
|
|
47
|
-
fs.writeFileSync(
|
|
48
|
-
join(path, fileName),
|
|
49
|
-
yaml.dump(obj, {
|
|
50
|
-
skipInvalid: true,
|
|
51
|
-
noRefs: true,
|
|
52
|
-
sortKeys: (a, b) => {
|
|
53
|
-
if (b === "tableName") return 1;
|
|
54
|
-
if (a === "tableName") return -1;
|
|
55
|
-
if (a > b) return 1;
|
|
56
|
-
if (a < b) return -1;
|
|
57
|
-
return 0;
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
);
|
|
61
|
-
};
|
|
62
|
-
const readInitialSchema = () => {
|
|
63
|
-
return readYmlFile(join(config().migration.dir, "initialSchema.yml"));
|
|
64
|
-
};
|
|
65
|
-
const writeCurrentSchema = (schema) => {
|
|
66
|
-
writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
class SasatConfigLoader {
|
|
70
|
-
static loadConfig() {
|
|
71
|
-
const fileName = "sasat.yml";
|
|
72
|
-
const filepath = path__default.join(process.cwd(), fileName);
|
|
73
|
-
if (!fs.existsSync(filepath)) return defaultConf;
|
|
74
|
-
return readYmlFile(filepath);
|
|
75
|
-
}
|
|
76
|
-
conf;
|
|
77
|
-
constructor() {
|
|
78
|
-
const conf = this.readValue({
|
|
79
|
-
...defaultConf,
|
|
80
|
-
...SasatConfigLoader.loadConfig()
|
|
81
|
-
});
|
|
82
|
-
this.conf = {
|
|
83
|
-
...conf
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
getConfig() {
|
|
87
|
-
return this.conf;
|
|
88
|
-
}
|
|
89
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
90
|
-
readValue(value) {
|
|
91
|
-
if (!value) return value;
|
|
92
|
-
if (Array.isArray(value)) return value.map((it) => this.readValue(it));
|
|
93
|
-
if (typeof value === "string" && value.startsWith("$"))
|
|
94
|
-
return process.env[value.slice(1)];
|
|
95
|
-
if (typeof value === "object") {
|
|
96
|
-
for (const key in value) {
|
|
97
|
-
if (Object.prototype.hasOwnProperty.call(value, key))
|
|
98
|
-
value[key] = this.readValue(value[key]);
|
|
99
|
-
}
|
|
100
|
-
return value;
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const defaultConfDb = {
|
|
107
|
-
host: "127.0.0.1",
|
|
108
|
-
port: 3306,
|
|
109
|
-
user: "root",
|
|
110
|
-
database: "sasat",
|
|
111
|
-
password: ""
|
|
112
|
-
};
|
|
113
|
-
const defaultConfMigration = {
|
|
114
|
-
table: "__migrate__",
|
|
115
|
-
dir: "migrations",
|
|
116
|
-
out: "sasat"
|
|
117
|
-
};
|
|
118
|
-
const defaultConf = {
|
|
119
|
-
db: defaultConfDb,
|
|
120
|
-
migration: defaultConfMigration,
|
|
121
|
-
generator: {
|
|
122
|
-
addJsExtToImportStatement: false,
|
|
123
|
-
gql: {
|
|
124
|
-
subscription: true
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
// redis: defaultCofRedis,
|
|
128
|
-
};
|
|
129
|
-
let conf;
|
|
130
|
-
const config = () => {
|
|
131
|
-
if (conf === void 0) conf = new SasatConfigLoader().getConfig();
|
|
132
|
-
return conf;
|
|
133
|
-
};
|
|
134
|
-
function setConfig(update) {
|
|
135
|
-
conf = assignDeep(config(), update);
|
|
136
|
-
return conf;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const formatQuery = (str, ...params) => {
|
|
140
|
-
let ret = str[0];
|
|
141
|
-
for (let i = 0; i < params.length; i++) {
|
|
142
|
-
if (typeof params[i] === "function") ret += params[i]();
|
|
143
|
-
else if (Array.isArray(params[i]))
|
|
144
|
-
ret += params[i].map((it) => SqlString.escape(it)).join(", ");
|
|
145
|
-
else ret += SqlString.escape(params[i]);
|
|
146
|
-
ret += str[i + 1];
|
|
147
|
-
}
|
|
148
|
-
return ret;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const parent = (field2) => ({
|
|
152
|
-
kind: "parent",
|
|
153
|
-
field: field2
|
|
154
|
-
});
|
|
155
|
-
const child = (field2) => ({
|
|
156
|
-
kind: "child",
|
|
157
|
-
field: field2
|
|
158
|
-
});
|
|
159
|
-
const contextOrError = (field2, errorMessage) => ({
|
|
160
|
-
kind: "context",
|
|
161
|
-
field: field2,
|
|
162
|
-
onNotDefined: {
|
|
163
|
-
action: "error",
|
|
164
|
-
message: errorMessage
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
const contextOrDefault = (field2, defaultValue) => ({
|
|
168
|
-
kind: "context",
|
|
169
|
-
field: field2,
|
|
170
|
-
onNotDefined: {
|
|
171
|
-
action: "defaultValue",
|
|
172
|
-
value: defaultValue
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
const fixed = (value) => ({
|
|
176
|
-
kind: "fixed",
|
|
177
|
-
value
|
|
178
|
-
});
|
|
179
|
-
const today = (thresholdHour, date) => ({
|
|
180
|
-
kind: "today",
|
|
181
|
-
type: date ? "date" : "datetime",
|
|
182
|
-
thresholdHour
|
|
183
|
-
});
|
|
184
|
-
const now = () => ({
|
|
185
|
-
kind: "now"
|
|
186
|
-
});
|
|
187
|
-
const values = (begin, end) => ({
|
|
188
|
-
kind: "range",
|
|
189
|
-
begin,
|
|
190
|
-
end
|
|
191
|
-
});
|
|
192
|
-
const betweenToday = (thresholdHour) => ({
|
|
193
|
-
kind: "date-range",
|
|
194
|
-
range: "today",
|
|
195
|
-
thresholdHour
|
|
196
|
-
});
|
|
197
|
-
const custom = (conditionName, parentRequiredFields, childRequiredFields) => ({
|
|
198
|
-
kind: "custom",
|
|
199
|
-
conditionName,
|
|
200
|
-
parentRequiredFields,
|
|
201
|
-
childRequiredFields
|
|
202
|
-
});
|
|
203
|
-
const field = (column) => ({
|
|
204
|
-
kind: "field",
|
|
205
|
-
column
|
|
206
|
-
});
|
|
207
|
-
const arg = (name, type) => ({
|
|
208
|
-
kind: "arg",
|
|
209
|
-
name,
|
|
210
|
-
type
|
|
211
|
-
});
|
|
212
|
-
const betweenRel = (left, range) => ({
|
|
213
|
-
kind: "comparison",
|
|
214
|
-
left,
|
|
215
|
-
operator: "BETWEEN",
|
|
216
|
-
right: range
|
|
217
|
-
});
|
|
218
|
-
const betweenQuery = (left, begin, end) => ({
|
|
219
|
-
kind: "between",
|
|
220
|
-
operator: "BETWEEN",
|
|
221
|
-
left,
|
|
222
|
-
begin,
|
|
223
|
-
end
|
|
224
|
-
});
|
|
225
|
-
const comparisonRel = (left, operator, right) => ({
|
|
226
|
-
kind: "comparison",
|
|
227
|
-
left,
|
|
228
|
-
right,
|
|
229
|
-
operator
|
|
230
|
-
});
|
|
231
|
-
const inRel = (left, right) => ({
|
|
232
|
-
kind: "comparison",
|
|
233
|
-
left,
|
|
234
|
-
right,
|
|
235
|
-
operator: "IN"
|
|
236
|
-
});
|
|
237
|
-
const isNullRel = (value) => ({
|
|
238
|
-
kind: "isNull",
|
|
239
|
-
value,
|
|
240
|
-
not: false
|
|
241
|
-
});
|
|
242
|
-
const isNotNullRel = (value) => ({
|
|
243
|
-
kind: "isNull",
|
|
244
|
-
value,
|
|
245
|
-
not: true
|
|
246
|
-
});
|
|
247
|
-
const comparisonQuery = (left, operator, right) => ({
|
|
248
|
-
kind: "comparison",
|
|
249
|
-
left,
|
|
250
|
-
right,
|
|
251
|
-
operator
|
|
252
|
-
});
|
|
253
|
-
const Conditions = {
|
|
254
|
-
betweenRel,
|
|
255
|
-
betweenQuery,
|
|
256
|
-
custom,
|
|
257
|
-
rel: {
|
|
258
|
-
between: betweenRel,
|
|
259
|
-
comparison: comparisonRel,
|
|
260
|
-
in: inRel,
|
|
261
|
-
isNull: isNullRel,
|
|
262
|
-
isNotNull: isNotNullRel
|
|
263
|
-
},
|
|
264
|
-
query: {
|
|
265
|
-
between: betweenQuery,
|
|
266
|
-
comparison: comparisonQuery
|
|
267
|
-
},
|
|
268
|
-
value: {
|
|
269
|
-
parent,
|
|
270
|
-
child,
|
|
271
|
-
contextOrError,
|
|
272
|
-
contextOrDefault,
|
|
273
|
-
fixed,
|
|
274
|
-
today,
|
|
275
|
-
now,
|
|
276
|
-
field,
|
|
277
|
-
arg
|
|
278
|
-
},
|
|
279
|
-
range: {
|
|
280
|
-
values,
|
|
281
|
-
today: betweenToday
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
const pick = (target, keys) => Object.fromEntries(keys.map((key) => [key, target[key]]));
|
|
286
|
-
const unique = (array) => {
|
|
287
|
-
const result = [];
|
|
288
|
-
for (let i = 0, l = array.length; i < l; i += 1) {
|
|
289
|
-
if (!result.includes(array[i])) {
|
|
290
|
-
result.push(array[i]);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return result;
|
|
294
|
-
};
|
|
295
|
-
const nonNullable = (value) => value != null;
|
|
296
|
-
|
|
297
|
-
const noop = () => {
|
|
298
|
-
};
|
|
299
|
-
class SQLClient {
|
|
300
|
-
logger = noop;
|
|
301
|
-
rawQuery(sql) {
|
|
302
|
-
this.logger(sql);
|
|
303
|
-
return this.execSql(sql);
|
|
304
|
-
}
|
|
305
|
-
rawCommand(sql) {
|
|
306
|
-
this.logger(sql);
|
|
307
|
-
return this.execSql(sql);
|
|
308
|
-
}
|
|
309
|
-
query(templateString, ...params) {
|
|
310
|
-
return this.rawQuery(formatQuery(templateString, ...params));
|
|
311
|
-
}
|
|
312
|
-
command(templateString, ...params) {
|
|
313
|
-
return this.rawCommand(formatQuery(templateString, ...params));
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
class SQLTransaction extends SQLClient {
|
|
317
|
-
}
|
|
318
|
-
class DBClient extends SQLClient {
|
|
319
|
-
_released;
|
|
320
|
-
constructor(logger = noop) {
|
|
321
|
-
super();
|
|
322
|
-
this._released = false;
|
|
323
|
-
this.logger = logger;
|
|
324
|
-
}
|
|
325
|
-
isReleased() {
|
|
326
|
-
return this._released;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
class MySqlTransaction extends SQLTransaction {
|
|
331
|
-
constructor(connection) {
|
|
332
|
-
super();
|
|
333
|
-
this.connection = connection;
|
|
334
|
-
}
|
|
335
|
-
commit() {
|
|
336
|
-
const result = promisify(this.connection.commit).bind(this.connection)();
|
|
337
|
-
this.connection.end();
|
|
338
|
-
return result;
|
|
339
|
-
}
|
|
340
|
-
async rollback() {
|
|
341
|
-
await promisify(this.connection.rollback).bind(this.connection)();
|
|
342
|
-
this.connection.end();
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
execSql(sql) {
|
|
346
|
-
return promisify(this.connection.query).bind(this.connection)(
|
|
347
|
-
sql
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
class MysqlClient extends DBClient {
|
|
353
|
-
constructor(connectionOption, poolOption, logger) {
|
|
354
|
-
super(logger);
|
|
355
|
-
this.connectionOption = connectionOption;
|
|
356
|
-
this.pool = mysql.createPool({
|
|
357
|
-
...config().db,
|
|
358
|
-
dateStrings: true,
|
|
359
|
-
...connectionOption,
|
|
360
|
-
...poolOption
|
|
361
|
-
});
|
|
362
|
-
this.release = this.release.bind(this);
|
|
363
|
-
}
|
|
364
|
-
pool;
|
|
365
|
-
async transaction() {
|
|
366
|
-
const connection = mysql.createConnection({
|
|
367
|
-
...config().db,
|
|
368
|
-
dateStrings: true,
|
|
369
|
-
...this.connectionOption
|
|
370
|
-
});
|
|
371
|
-
await promisify(connection.beginTransaction).bind(connection)();
|
|
372
|
-
return new MySqlTransaction(connection);
|
|
373
|
-
}
|
|
374
|
-
async release() {
|
|
375
|
-
await promisify(this.pool.end).bind(this.pool)();
|
|
376
|
-
this._released = true;
|
|
377
|
-
}
|
|
378
|
-
execSql(sql) {
|
|
379
|
-
return promisify(this.pool.query).bind(this.pool)(sql);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
let client;
|
|
384
|
-
const getDbClient = (...config) => {
|
|
385
|
-
if (client && !client.isReleased()) return client;
|
|
386
|
-
client = new MysqlClient(...config);
|
|
387
|
-
return client;
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
class SasatError extends Error {
|
|
391
|
-
constructor(message) {
|
|
392
|
-
super(message);
|
|
393
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
394
|
-
this.name = "SasatError";
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
export { Conditions as C, SqlString as S, SasatError as a, assignDeep as b, config as c, writeCurrentSchema as d, writeYmlFile as e, formatQuery as f, getDbClient as g, defaultConf as h, mkDirIfNotExist as m, nonNullable as n, pick as p, readInitialSchema as r, setConfig as s, unique as u, writeFileIfNotExist as w };
|