sasat 0.22.4 → 0.22.6
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 +16 -15
- package/dist/cli/index.mjs +2 -1
- package/dist/comparison-BpTtc1iD.d.mts +42 -0
- package/dist/comparison-CIb9xu5T.d.cts +42 -0
- package/dist/dbClient-2mFJTQw7.d.mts +35 -0
- package/dist/dbClient-Bnk1JKBU.d.cts +35 -0
- package/dist/index.cjs +33 -95
- package/dist/index.d.cts +5 -739
- package/dist/index.d.mts +6 -740
- package/dist/index.mjs +5 -63
- package/dist/makeCondition-3MdYUm5k.mjs +134 -0
- package/dist/makeCondition-DKSyAkri.cjs +139 -0
- package/dist/{migrate-BRNxkHCx.cjs → migrate-BDE07ocG.cjs} +3013 -3511
- package/dist/{migrate-Cbj2OVbY.mjs → migrate-DnqBdQ6r.mjs} +3019 -3385
- package/dist/migration/index.cjs +64 -0
- package/dist/migration/index.d.cts +657 -0
- package/dist/migration/index.d.mts +657 -0
- package/dist/migration/index.mjs +61 -0
- package/dist/testing/index.cjs +3 -0
- package/dist/testing/index.d.cts +12 -0
- package/dist/testing/index.d.mts +12 -0
- package/dist/testing/index.mjs +2 -0
- package/dist/util-C5Jevn5B.cjs +377 -0
- package/dist/util-Dkw5bD7a.mjs +239 -0
- package/package.json +9 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { t as Conditions } from "../makeCondition-3MdYUm5k.mjs";
|
|
2
|
+
//#region src/migration/makeMutaion.ts
|
|
3
|
+
const formatSubscription = (subscription) => {
|
|
4
|
+
if (subscription === void 0 || subscription === false) return {
|
|
5
|
+
enabled: false,
|
|
6
|
+
subscriptionFilter: []
|
|
7
|
+
};
|
|
8
|
+
if (subscription === true) return {
|
|
9
|
+
enabled: true,
|
|
10
|
+
subscriptionFilter: []
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
enabled: subscription.enabled,
|
|
14
|
+
subscriptionFilter: subscription.subscriptionFilter || []
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const formatOptions = (type, option) => ({
|
|
18
|
+
type,
|
|
19
|
+
noReFetch: option?.noRefetch || false,
|
|
20
|
+
middlewares: option?.middlewares || [],
|
|
21
|
+
contextFields: option?.contextFields || [],
|
|
22
|
+
subscription: formatSubscription(option?.subscription)
|
|
23
|
+
});
|
|
24
|
+
const Mutations = {
|
|
25
|
+
create: (options) => formatOptions("create", options),
|
|
26
|
+
update: (options) => formatOptions("update", options),
|
|
27
|
+
delete: (options) => formatOptions("delete", options)
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/migration/makeQuery.ts
|
|
31
|
+
const single = (name, options) => ({
|
|
32
|
+
type: "single",
|
|
33
|
+
name,
|
|
34
|
+
conditions: options?.conditions || [],
|
|
35
|
+
middlewares: options?.middlewares || []
|
|
36
|
+
});
|
|
37
|
+
const listAll = (name, options) => ({
|
|
38
|
+
type: "list-all",
|
|
39
|
+
name,
|
|
40
|
+
conditions: options?.conditions || [],
|
|
41
|
+
middlewares: options?.middlewares || []
|
|
42
|
+
});
|
|
43
|
+
const paging = (name, options) => ({
|
|
44
|
+
type: "list-paging",
|
|
45
|
+
name,
|
|
46
|
+
conditions: options?.conditions || [],
|
|
47
|
+
middlewares: options?.middlewares || []
|
|
48
|
+
});
|
|
49
|
+
const primary = (middlewares = []) => ({
|
|
50
|
+
type: "primary",
|
|
51
|
+
conditions: [],
|
|
52
|
+
middlewares
|
|
53
|
+
});
|
|
54
|
+
const Queries = {
|
|
55
|
+
single,
|
|
56
|
+
listAll,
|
|
57
|
+
paging,
|
|
58
|
+
primary
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { Conditions, Mutations, Queries };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { n as DBClient } from "../dbClient-Bnk1JKBU.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cli/commands/migrate.d.ts
|
|
4
|
+
type MigrateCommandOption = {
|
|
5
|
+
generateFiles: boolean;
|
|
6
|
+
silent: boolean;
|
|
7
|
+
dry: boolean;
|
|
8
|
+
skipBuild: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const migrate: (client: DBClient, options: MigrateCommandOption) => Promise<void>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { migrate };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { n as DBClient } from "../dbClient-2mFJTQw7.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cli/commands/migrate.d.ts
|
|
4
|
+
type MigrateCommandOption = {
|
|
5
|
+
generateFiles: boolean;
|
|
6
|
+
silent: boolean;
|
|
7
|
+
dry: boolean;
|
|
8
|
+
skipBuild: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const migrate: (client: DBClient, options: MigrateCommandOption) => Promise<void>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { migrate };
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
let node_fs = require("node:fs");
|
|
24
|
+
let node_path = require("node:path");
|
|
25
|
+
node_path = __toESM(node_path, 1);
|
|
26
|
+
let node_fs_promises = require("node:fs/promises");
|
|
27
|
+
let js_yaml = require("js-yaml");
|
|
28
|
+
js_yaml = __toESM(js_yaml, 1);
|
|
29
|
+
let mysql2_promise = require("mysql2/promise");
|
|
30
|
+
let sqlstring = require("sqlstring");
|
|
31
|
+
sqlstring = __toESM(sqlstring, 1);
|
|
32
|
+
//#region src/util/assignDeep.ts
|
|
33
|
+
const assignDeep = (base, ...objects) => {
|
|
34
|
+
const assign = (target, key, value) => {
|
|
35
|
+
if (key === "__proto__" || key === "constructor") return;
|
|
36
|
+
if (Array.isArray(target[key]) && Array.isArray(value)) target[key] = [...target[key], ...value];
|
|
37
|
+
else if (typeof target[key] === "object" && typeof value === "object") assignDeep(target[key], value);
|
|
38
|
+
else target[key] = value;
|
|
39
|
+
};
|
|
40
|
+
objects.forEach((obj) => {
|
|
41
|
+
if (typeof obj === "object") Object.entries(obj).forEach(([key, value]) => {
|
|
42
|
+
assign(base, key, value);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
return base;
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/util/fsUtil.ts
|
|
49
|
+
const readYmlFile = (filepath) => js_yaml.default.load((0, node_fs.readFileSync)(filepath, "utf8"));
|
|
50
|
+
const mkDirIfNotExist = (path) => {
|
|
51
|
+
if (!(0, node_fs.existsSync)(path)) (0, node_fs.mkdirSync)(path, { recursive: true });
|
|
52
|
+
};
|
|
53
|
+
const writeFileIfNotExist = (path, data) => {
|
|
54
|
+
if ((0, node_fs.existsSync)(path)) return Promise.resolve();
|
|
55
|
+
return (0, node_fs_promises.writeFile)(path, data);
|
|
56
|
+
};
|
|
57
|
+
const writeYmlFile = (path, fileName, obj) => {
|
|
58
|
+
mkDirIfNotExist(path);
|
|
59
|
+
(0, node_fs.writeFileSync)((0, node_path.join)(path, fileName), js_yaml.default.dump(obj, {
|
|
60
|
+
skipInvalid: true,
|
|
61
|
+
noRefs: true,
|
|
62
|
+
sortKeys: (a, b) => {
|
|
63
|
+
if (b === "tableName") return 1;
|
|
64
|
+
if (a === "tableName") return -1;
|
|
65
|
+
if (a > b) return 1;
|
|
66
|
+
if (a < b) return -1;
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
}));
|
|
70
|
+
};
|
|
71
|
+
const readInitialSchema = () => {
|
|
72
|
+
return readYmlFile((0, node_path.join)(config().migration.dir, "initialSchema.yml"));
|
|
73
|
+
};
|
|
74
|
+
const writeCurrentSchema = (schema) => {
|
|
75
|
+
writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/config/loader.ts
|
|
79
|
+
var SasatConfigLoader = class SasatConfigLoader {
|
|
80
|
+
static loadConfig() {
|
|
81
|
+
const filepath = node_path.default.join(process.cwd(), "sasat.yml");
|
|
82
|
+
if (!(0, node_fs.existsSync)(filepath)) return defaultConf;
|
|
83
|
+
return readYmlFile(filepath);
|
|
84
|
+
}
|
|
85
|
+
constructor() {
|
|
86
|
+
const conf = this.readValue({
|
|
87
|
+
...defaultConf,
|
|
88
|
+
...SasatConfigLoader.loadConfig()
|
|
89
|
+
});
|
|
90
|
+
this.conf = { ...conf };
|
|
91
|
+
}
|
|
92
|
+
getConfig() {
|
|
93
|
+
return this.conf;
|
|
94
|
+
}
|
|
95
|
+
readValue(value) {
|
|
96
|
+
if (!value) return value;
|
|
97
|
+
if (Array.isArray(value)) return value.map((it) => this.readValue(it));
|
|
98
|
+
if (typeof value === "string" && value.startsWith("$")) return process.env[value.slice(1)];
|
|
99
|
+
if (typeof value === "object") {
|
|
100
|
+
for (const key in value) if (Object.hasOwn(value, key)) value[key] = this.readValue(value[key]);
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const defaultConf = {
|
|
107
|
+
db: {
|
|
108
|
+
host: "127.0.0.1",
|
|
109
|
+
port: 3306,
|
|
110
|
+
user: "root",
|
|
111
|
+
database: "sasat",
|
|
112
|
+
password: ""
|
|
113
|
+
},
|
|
114
|
+
migration: {
|
|
115
|
+
table: "__migrate__",
|
|
116
|
+
dir: "migrations",
|
|
117
|
+
out: "sasat"
|
|
118
|
+
},
|
|
119
|
+
generator: {
|
|
120
|
+
addJsExtToImportStatement: false,
|
|
121
|
+
gql: { subscription: true }
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
let conf;
|
|
125
|
+
const config = () => {
|
|
126
|
+
if (conf === void 0) conf = new SasatConfigLoader().getConfig();
|
|
127
|
+
return conf;
|
|
128
|
+
};
|
|
129
|
+
function setConfig(update) {
|
|
130
|
+
conf = assignDeep(config(), update);
|
|
131
|
+
return conf;
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/runtime/sql/sqlString.ts
|
|
135
|
+
const { escape, escapeId } = sqlstring.default;
|
|
136
|
+
const SqlString = {
|
|
137
|
+
escape: (value) => escape(value, true),
|
|
138
|
+
escapeId: (name) => escapeId(name)
|
|
139
|
+
};
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/db/formatQuery.ts
|
|
142
|
+
const formatQuery = (str, ...params) => {
|
|
143
|
+
let ret = str[0];
|
|
144
|
+
for (let i = 0; i < params.length; i++) {
|
|
145
|
+
if (typeof params[i] === "function") ret += params[i]();
|
|
146
|
+
else if (Array.isArray(params[i])) ret += params[i].map((it) => SqlString.escape(it)).join(", ");
|
|
147
|
+
else ret += SqlString.escape(params[i]);
|
|
148
|
+
ret += str[i + 1];
|
|
149
|
+
}
|
|
150
|
+
return ret;
|
|
151
|
+
};
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/db/connectors/dbClient.ts
|
|
154
|
+
const noop = () => {};
|
|
155
|
+
var SQLClient = class {
|
|
156
|
+
constructor() {
|
|
157
|
+
this.logger = noop;
|
|
158
|
+
}
|
|
159
|
+
rawQuery(sql) {
|
|
160
|
+
this.logger(sql);
|
|
161
|
+
return this.execSql(sql);
|
|
162
|
+
}
|
|
163
|
+
rawCommand(sql) {
|
|
164
|
+
this.logger(sql);
|
|
165
|
+
return this.execSql(sql);
|
|
166
|
+
}
|
|
167
|
+
query(templateString, ...params) {
|
|
168
|
+
return this.rawQuery(formatQuery(templateString, ...params));
|
|
169
|
+
}
|
|
170
|
+
command(templateString, ...params) {
|
|
171
|
+
return this.rawCommand(formatQuery(templateString, ...params));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var SQLTransaction = class extends SQLClient {};
|
|
175
|
+
var DBClient = class extends SQLClient {
|
|
176
|
+
constructor(logger = noop) {
|
|
177
|
+
super();
|
|
178
|
+
this._released = false;
|
|
179
|
+
this.logger = logger;
|
|
180
|
+
}
|
|
181
|
+
isReleased() {
|
|
182
|
+
return this._released;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/db/connectors/mysql/transaction.ts
|
|
187
|
+
var MySqlTransaction = class extends SQLTransaction {
|
|
188
|
+
constructor(connection) {
|
|
189
|
+
super();
|
|
190
|
+
this.connection = connection;
|
|
191
|
+
}
|
|
192
|
+
async commit() {
|
|
193
|
+
const result = await this.connection.commit();
|
|
194
|
+
await this.connection.end();
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
async rollback() {
|
|
198
|
+
await this.connection.rollback();
|
|
199
|
+
await this.connection.end();
|
|
200
|
+
}
|
|
201
|
+
async execSql(sql) {
|
|
202
|
+
return (await this.connection.query(sql))[0];
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/db/connectors/mysql/poolClient.ts
|
|
207
|
+
var MysqlPoolClient = class extends DBClient {
|
|
208
|
+
constructor(poolOption, logger) {
|
|
209
|
+
super(logger);
|
|
210
|
+
this.poolOption = poolOption;
|
|
211
|
+
this.pool = (0, mysql2_promise.createPool)({
|
|
212
|
+
dateStrings: true,
|
|
213
|
+
...poolOption
|
|
214
|
+
});
|
|
215
|
+
this.release = this.release.bind(this);
|
|
216
|
+
}
|
|
217
|
+
async transaction() {
|
|
218
|
+
const connection = await (0, mysql2_promise.createConnection)({
|
|
219
|
+
...config().db,
|
|
220
|
+
dateStrings: true,
|
|
221
|
+
...this.poolOption
|
|
222
|
+
});
|
|
223
|
+
await connection.beginTransaction();
|
|
224
|
+
return new MySqlTransaction(connection);
|
|
225
|
+
}
|
|
226
|
+
async release() {
|
|
227
|
+
await this.pool.end();
|
|
228
|
+
this._released = true;
|
|
229
|
+
}
|
|
230
|
+
async execSql(sql) {
|
|
231
|
+
return (await this.pool.query(sql))[0];
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/db/getDbClient.ts
|
|
236
|
+
let client;
|
|
237
|
+
const getDbClient = (option, logger) => {
|
|
238
|
+
if (client && !client.isReleased()) return client;
|
|
239
|
+
client = new MysqlPoolClient({
|
|
240
|
+
...config().db,
|
|
241
|
+
...option
|
|
242
|
+
}, logger);
|
|
243
|
+
return client;
|
|
244
|
+
};
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/error.ts
|
|
247
|
+
var SasatError = class extends Error {
|
|
248
|
+
constructor(message) {
|
|
249
|
+
super(message);
|
|
250
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
251
|
+
this.name = "SasatError";
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region src/runtime/util.ts
|
|
256
|
+
const pick = (target, keys) => Object.fromEntries(keys.map((key) => [key, target[key]]));
|
|
257
|
+
const unique = (array) => {
|
|
258
|
+
const result = [];
|
|
259
|
+
for (let i = 0, l = array.length; i < l; i += 1) if (!result.includes(array[i])) result.push(array[i]);
|
|
260
|
+
return result;
|
|
261
|
+
};
|
|
262
|
+
const nonNullable = (value) => value != null;
|
|
263
|
+
//#endregion
|
|
264
|
+
Object.defineProperty(exports, "DBClient", {
|
|
265
|
+
enumerable: true,
|
|
266
|
+
get: function() {
|
|
267
|
+
return DBClient;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
Object.defineProperty(exports, "MySqlTransaction", {
|
|
271
|
+
enumerable: true,
|
|
272
|
+
get: function() {
|
|
273
|
+
return MySqlTransaction;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
Object.defineProperty(exports, "SasatError", {
|
|
277
|
+
enumerable: true,
|
|
278
|
+
get: function() {
|
|
279
|
+
return SasatError;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
Object.defineProperty(exports, "SqlString", {
|
|
283
|
+
enumerable: true,
|
|
284
|
+
get: function() {
|
|
285
|
+
return SqlString;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
Object.defineProperty(exports, "__toESM", {
|
|
289
|
+
enumerable: true,
|
|
290
|
+
get: function() {
|
|
291
|
+
return __toESM;
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
Object.defineProperty(exports, "assignDeep", {
|
|
295
|
+
enumerable: true,
|
|
296
|
+
get: function() {
|
|
297
|
+
return assignDeep;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
Object.defineProperty(exports, "config", {
|
|
301
|
+
enumerable: true,
|
|
302
|
+
get: function() {
|
|
303
|
+
return config;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
Object.defineProperty(exports, "defaultConf", {
|
|
307
|
+
enumerable: true,
|
|
308
|
+
get: function() {
|
|
309
|
+
return defaultConf;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
Object.defineProperty(exports, "formatQuery", {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
get: function() {
|
|
315
|
+
return formatQuery;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
Object.defineProperty(exports, "getDbClient", {
|
|
319
|
+
enumerable: true,
|
|
320
|
+
get: function() {
|
|
321
|
+
return getDbClient;
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
Object.defineProperty(exports, "mkDirIfNotExist", {
|
|
325
|
+
enumerable: true,
|
|
326
|
+
get: function() {
|
|
327
|
+
return mkDirIfNotExist;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
Object.defineProperty(exports, "nonNullable", {
|
|
331
|
+
enumerable: true,
|
|
332
|
+
get: function() {
|
|
333
|
+
return nonNullable;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
Object.defineProperty(exports, "pick", {
|
|
337
|
+
enumerable: true,
|
|
338
|
+
get: function() {
|
|
339
|
+
return pick;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
Object.defineProperty(exports, "readInitialSchema", {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
get: function() {
|
|
345
|
+
return readInitialSchema;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
Object.defineProperty(exports, "setConfig", {
|
|
349
|
+
enumerable: true,
|
|
350
|
+
get: function() {
|
|
351
|
+
return setConfig;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
Object.defineProperty(exports, "unique", {
|
|
355
|
+
enumerable: true,
|
|
356
|
+
get: function() {
|
|
357
|
+
return unique;
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
Object.defineProperty(exports, "writeCurrentSchema", {
|
|
361
|
+
enumerable: true,
|
|
362
|
+
get: function() {
|
|
363
|
+
return writeCurrentSchema;
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
Object.defineProperty(exports, "writeFileIfNotExist", {
|
|
367
|
+
enumerable: true,
|
|
368
|
+
get: function() {
|
|
369
|
+
return writeFileIfNotExist;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
Object.defineProperty(exports, "writeYmlFile", {
|
|
373
|
+
enumerable: true,
|
|
374
|
+
get: function() {
|
|
375
|
+
return writeYmlFile;
|
|
376
|
+
}
|
|
377
|
+
});
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path, { join } from "node:path";
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
import { createConnection, createPool } from "mysql2/promise";
|
|
6
|
+
import pkg from "sqlstring";
|
|
7
|
+
//#region src/util/assignDeep.ts
|
|
8
|
+
const assignDeep = (base, ...objects) => {
|
|
9
|
+
const assign = (target, key, value) => {
|
|
10
|
+
if (key === "__proto__" || key === "constructor") return;
|
|
11
|
+
if (Array.isArray(target[key]) && Array.isArray(value)) target[key] = [...target[key], ...value];
|
|
12
|
+
else if (typeof target[key] === "object" && typeof value === "object") assignDeep(target[key], value);
|
|
13
|
+
else target[key] = value;
|
|
14
|
+
};
|
|
15
|
+
objects.forEach((obj) => {
|
|
16
|
+
if (typeof obj === "object") Object.entries(obj).forEach(([key, value]) => {
|
|
17
|
+
assign(base, key, value);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
return base;
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/util/fsUtil.ts
|
|
24
|
+
const readYmlFile = (filepath) => yaml.load(readFileSync(filepath, "utf8"));
|
|
25
|
+
const mkDirIfNotExist = (path) => {
|
|
26
|
+
if (!existsSync(path)) mkdirSync(path, { recursive: true });
|
|
27
|
+
};
|
|
28
|
+
const writeFileIfNotExist = (path, data) => {
|
|
29
|
+
if (existsSync(path)) return Promise.resolve();
|
|
30
|
+
return writeFile(path, data);
|
|
31
|
+
};
|
|
32
|
+
const writeYmlFile = (path, fileName, obj) => {
|
|
33
|
+
mkDirIfNotExist(path);
|
|
34
|
+
writeFileSync(join(path, fileName), yaml.dump(obj, {
|
|
35
|
+
skipInvalid: true,
|
|
36
|
+
noRefs: true,
|
|
37
|
+
sortKeys: (a, b) => {
|
|
38
|
+
if (b === "tableName") return 1;
|
|
39
|
+
if (a === "tableName") return -1;
|
|
40
|
+
if (a > b) return 1;
|
|
41
|
+
if (a < b) return -1;
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
const readInitialSchema = () => {
|
|
47
|
+
return readYmlFile(join(config().migration.dir, "initialSchema.yml"));
|
|
48
|
+
};
|
|
49
|
+
const writeCurrentSchema = (schema) => {
|
|
50
|
+
writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/config/loader.ts
|
|
54
|
+
var SasatConfigLoader = class SasatConfigLoader {
|
|
55
|
+
static loadConfig() {
|
|
56
|
+
const filepath = path.join(process.cwd(), "sasat.yml");
|
|
57
|
+
if (!existsSync(filepath)) return defaultConf;
|
|
58
|
+
return readYmlFile(filepath);
|
|
59
|
+
}
|
|
60
|
+
constructor() {
|
|
61
|
+
const conf = this.readValue({
|
|
62
|
+
...defaultConf,
|
|
63
|
+
...SasatConfigLoader.loadConfig()
|
|
64
|
+
});
|
|
65
|
+
this.conf = { ...conf };
|
|
66
|
+
}
|
|
67
|
+
getConfig() {
|
|
68
|
+
return this.conf;
|
|
69
|
+
}
|
|
70
|
+
readValue(value) {
|
|
71
|
+
if (!value) return value;
|
|
72
|
+
if (Array.isArray(value)) return value.map((it) => this.readValue(it));
|
|
73
|
+
if (typeof value === "string" && value.startsWith("$")) return process.env[value.slice(1)];
|
|
74
|
+
if (typeof value === "object") {
|
|
75
|
+
for (const key in value) if (Object.hasOwn(value, key)) value[key] = this.readValue(value[key]);
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const defaultConf = {
|
|
82
|
+
db: {
|
|
83
|
+
host: "127.0.0.1",
|
|
84
|
+
port: 3306,
|
|
85
|
+
user: "root",
|
|
86
|
+
database: "sasat",
|
|
87
|
+
password: ""
|
|
88
|
+
},
|
|
89
|
+
migration: {
|
|
90
|
+
table: "__migrate__",
|
|
91
|
+
dir: "migrations",
|
|
92
|
+
out: "sasat"
|
|
93
|
+
},
|
|
94
|
+
generator: {
|
|
95
|
+
addJsExtToImportStatement: false,
|
|
96
|
+
gql: { subscription: true }
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
let conf;
|
|
100
|
+
const config = () => {
|
|
101
|
+
if (conf === void 0) conf = new SasatConfigLoader().getConfig();
|
|
102
|
+
return conf;
|
|
103
|
+
};
|
|
104
|
+
function setConfig(update) {
|
|
105
|
+
conf = assignDeep(config(), update);
|
|
106
|
+
return conf;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/runtime/sql/sqlString.ts
|
|
110
|
+
const { escape, escapeId } = pkg;
|
|
111
|
+
const SqlString = {
|
|
112
|
+
escape: (value) => escape(value, true),
|
|
113
|
+
escapeId: (name) => escapeId(name)
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/db/formatQuery.ts
|
|
117
|
+
const formatQuery = (str, ...params) => {
|
|
118
|
+
let ret = str[0];
|
|
119
|
+
for (let i = 0; i < params.length; i++) {
|
|
120
|
+
if (typeof params[i] === "function") ret += params[i]();
|
|
121
|
+
else if (Array.isArray(params[i])) ret += params[i].map((it) => SqlString.escape(it)).join(", ");
|
|
122
|
+
else ret += SqlString.escape(params[i]);
|
|
123
|
+
ret += str[i + 1];
|
|
124
|
+
}
|
|
125
|
+
return ret;
|
|
126
|
+
};
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/db/connectors/dbClient.ts
|
|
129
|
+
const noop = () => {};
|
|
130
|
+
var SQLClient = class {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.logger = noop;
|
|
133
|
+
}
|
|
134
|
+
rawQuery(sql) {
|
|
135
|
+
this.logger(sql);
|
|
136
|
+
return this.execSql(sql);
|
|
137
|
+
}
|
|
138
|
+
rawCommand(sql) {
|
|
139
|
+
this.logger(sql);
|
|
140
|
+
return this.execSql(sql);
|
|
141
|
+
}
|
|
142
|
+
query(templateString, ...params) {
|
|
143
|
+
return this.rawQuery(formatQuery(templateString, ...params));
|
|
144
|
+
}
|
|
145
|
+
command(templateString, ...params) {
|
|
146
|
+
return this.rawCommand(formatQuery(templateString, ...params));
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
var SQLTransaction = class extends SQLClient {};
|
|
150
|
+
var DBClient = class extends SQLClient {
|
|
151
|
+
constructor(logger = noop) {
|
|
152
|
+
super();
|
|
153
|
+
this._released = false;
|
|
154
|
+
this.logger = logger;
|
|
155
|
+
}
|
|
156
|
+
isReleased() {
|
|
157
|
+
return this._released;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/db/connectors/mysql/transaction.ts
|
|
162
|
+
var MySqlTransaction = class extends SQLTransaction {
|
|
163
|
+
constructor(connection) {
|
|
164
|
+
super();
|
|
165
|
+
this.connection = connection;
|
|
166
|
+
}
|
|
167
|
+
async commit() {
|
|
168
|
+
const result = await this.connection.commit();
|
|
169
|
+
await this.connection.end();
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
async rollback() {
|
|
173
|
+
await this.connection.rollback();
|
|
174
|
+
await this.connection.end();
|
|
175
|
+
}
|
|
176
|
+
async execSql(sql) {
|
|
177
|
+
return (await this.connection.query(sql))[0];
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/db/connectors/mysql/poolClient.ts
|
|
182
|
+
var MysqlPoolClient = class extends DBClient {
|
|
183
|
+
constructor(poolOption, logger) {
|
|
184
|
+
super(logger);
|
|
185
|
+
this.poolOption = poolOption;
|
|
186
|
+
this.pool = createPool({
|
|
187
|
+
dateStrings: true,
|
|
188
|
+
...poolOption
|
|
189
|
+
});
|
|
190
|
+
this.release = this.release.bind(this);
|
|
191
|
+
}
|
|
192
|
+
async transaction() {
|
|
193
|
+
const connection = await createConnection({
|
|
194
|
+
...config().db,
|
|
195
|
+
dateStrings: true,
|
|
196
|
+
...this.poolOption
|
|
197
|
+
});
|
|
198
|
+
await connection.beginTransaction();
|
|
199
|
+
return new MySqlTransaction(connection);
|
|
200
|
+
}
|
|
201
|
+
async release() {
|
|
202
|
+
await this.pool.end();
|
|
203
|
+
this._released = true;
|
|
204
|
+
}
|
|
205
|
+
async execSql(sql) {
|
|
206
|
+
return (await this.pool.query(sql))[0];
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/db/getDbClient.ts
|
|
211
|
+
let client;
|
|
212
|
+
const getDbClient = (option, logger) => {
|
|
213
|
+
if (client && !client.isReleased()) return client;
|
|
214
|
+
client = new MysqlPoolClient({
|
|
215
|
+
...config().db,
|
|
216
|
+
...option
|
|
217
|
+
}, logger);
|
|
218
|
+
return client;
|
|
219
|
+
};
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/error.ts
|
|
222
|
+
var SasatError = class extends Error {
|
|
223
|
+
constructor(message) {
|
|
224
|
+
super(message);
|
|
225
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
226
|
+
this.name = "SasatError";
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/runtime/util.ts
|
|
231
|
+
const pick = (target, keys) => Object.fromEntries(keys.map((key) => [key, target[key]]));
|
|
232
|
+
const unique = (array) => {
|
|
233
|
+
const result = [];
|
|
234
|
+
for (let i = 0, l = array.length; i < l; i += 1) if (!result.includes(array[i])) result.push(array[i]);
|
|
235
|
+
return result;
|
|
236
|
+
};
|
|
237
|
+
const nonNullable = (value) => value != null;
|
|
238
|
+
//#endregion
|
|
239
|
+
export { writeYmlFile as _, getDbClient as a, formatQuery as c, defaultConf as d, setConfig as f, writeFileIfNotExist as g, writeCurrentSchema as h, SasatError as i, SqlString as l, readInitialSchema as m, pick as n, MySqlTransaction as o, mkDirIfNotExist as p, unique as r, DBClient as s, nonNullable as t, config as u, assignDeep as v };
|