orchid-orm 1.24.7 → 1.25.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/index.d.ts +5 -7
- package/dist/index.js +15 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -12
- package/dist/index.mjs.map +1 -1
- package/dist/migrations.d.ts +16 -0
- package/dist/migrations.js +2847 -0
- package/dist/migrations.js.map +1 -0
- package/dist/migrations.mjs +2839 -0
- package/dist/migrations.mjs.map +1 -0
- package/package.json +11 -6
- package/codegen/index.d.ts +0 -41
- package/codegen/index.js +0 -1460
- package/codegen/index.js.map +0 -1
- package/codegen/index.mjs +0 -1439
- package/codegen/index.mjs.map +0 -1
package/codegen/index.mjs
DELETED
|
@@ -1,1439 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import path__default from 'path';
|
|
3
|
-
import fs from 'fs/promises';
|
|
4
|
-
import typescript from 'typescript';
|
|
5
|
-
import { pathToLog, toCamelCase, toPascalCase, getImportPath, singleQuote, codeToString, quoteObjectKey, addCode, columnDefaultArgumentToCode, deepCompare } from 'orchid-core';
|
|
6
|
-
import { columnsShapeToCode, ColumnType, primaryKeyToCode, indexToCode, constraintToCode, columnIndexesToCode, columnForeignKeysToCode, columnCheckToCode, identityToCode, Db } from 'pqb';
|
|
7
|
-
import { pluralize } from 'inflection';
|
|
8
|
-
|
|
9
|
-
class FileChanges {
|
|
10
|
-
constructor(content) {
|
|
11
|
-
this.content = content;
|
|
12
|
-
this.ranges = [];
|
|
13
|
-
}
|
|
14
|
-
add(at, text, end = at) {
|
|
15
|
-
if (this.ranges.length === 0) {
|
|
16
|
-
this.ranges.push([0, at], text, [end, this.content.length]);
|
|
17
|
-
} else {
|
|
18
|
-
const last = this.ranges[this.ranges.length - 1];
|
|
19
|
-
last[1] = at;
|
|
20
|
-
this.ranges.push(text, [end, this.content.length]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
replace(from, to, text) {
|
|
24
|
-
this.add(from, text, to);
|
|
25
|
-
}
|
|
26
|
-
remove(from, to) {
|
|
27
|
-
if (this.ranges.length === 0) {
|
|
28
|
-
this.ranges.push([0, from], [to, this.content.length]);
|
|
29
|
-
} else {
|
|
30
|
-
const last = this.ranges[this.ranges.length - 1];
|
|
31
|
-
last[1] = from;
|
|
32
|
-
this.ranges.push([to, this.content.length]);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
apply() {
|
|
36
|
-
return this.ranges.length ? this.ranges.map(
|
|
37
|
-
(item) => typeof item === "string" ? item : this.content.slice(item[0], item[1])
|
|
38
|
-
).join("") : this.content;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
|
|
43
|
-
const iterate = (kind) => {
|
|
44
|
-
return function* (statements) {
|
|
45
|
-
for (const node of statements) {
|
|
46
|
-
if (node.kind === kind) {
|
|
47
|
-
yield node;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
const isNode = (kind) => {
|
|
53
|
-
return (node) => {
|
|
54
|
-
return (node == null ? void 0 : node.kind) === kind;
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
const ts = {
|
|
58
|
-
getStatements(content) {
|
|
59
|
-
const { statements } = createSourceFile(
|
|
60
|
-
"file.ts",
|
|
61
|
-
content,
|
|
62
|
-
ScriptTarget.Latest,
|
|
63
|
-
true
|
|
64
|
-
);
|
|
65
|
-
return statements;
|
|
66
|
-
},
|
|
67
|
-
is: {
|
|
68
|
-
call: isNode(SyntaxKind.CallExpression),
|
|
69
|
-
objectLiteral: isNode(
|
|
70
|
-
SyntaxKind.ObjectLiteralExpression
|
|
71
|
-
),
|
|
72
|
-
propertyAssignment: isNode(
|
|
73
|
-
SyntaxKind.PropertyAssignment
|
|
74
|
-
),
|
|
75
|
-
shorthandPropertyAssignment: isNode(
|
|
76
|
-
SyntaxKind.ShorthandPropertyAssignment
|
|
77
|
-
),
|
|
78
|
-
identifier: isNode(SyntaxKind.Identifier),
|
|
79
|
-
stringLiteral: isNode(SyntaxKind.StringLiteral),
|
|
80
|
-
arrayLiteral: isNode(
|
|
81
|
-
SyntaxKind.ArrayLiteralExpression
|
|
82
|
-
),
|
|
83
|
-
numericLiteral: isNode(SyntaxKind.NumericLiteral),
|
|
84
|
-
computedPropertyName: isNode(
|
|
85
|
-
SyntaxKind.ComputedPropertyName
|
|
86
|
-
),
|
|
87
|
-
privateIdentifier: isNode(SyntaxKind.PrivateIdentifier),
|
|
88
|
-
this: isNode(SyntaxKind.ThisKeyword),
|
|
89
|
-
propertyAccess: isNode(
|
|
90
|
-
SyntaxKind.PropertyAccessExpression
|
|
91
|
-
),
|
|
92
|
-
arrowFunction: isNode(SyntaxKind.ArrowFunction),
|
|
93
|
-
parenthesizedExpression: isNode(
|
|
94
|
-
SyntaxKind.ParenthesizedExpression
|
|
95
|
-
),
|
|
96
|
-
spreadAssignment: isNode(SyntaxKind.SpreadAssignment)
|
|
97
|
-
},
|
|
98
|
-
import: {
|
|
99
|
-
iterate: iterate(SyntaxKind.ImportDeclaration),
|
|
100
|
-
*iterateWithSource(statements, path) {
|
|
101
|
-
for (const node of ts.import.iterate(statements)) {
|
|
102
|
-
if (ts.import.getSource(node) !== path)
|
|
103
|
-
continue;
|
|
104
|
-
yield node;
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
getSource(node) {
|
|
108
|
-
return node.moduleSpecifier.getText().slice(1, -1);
|
|
109
|
-
},
|
|
110
|
-
getEndPos(statements) {
|
|
111
|
-
let end = 0;
|
|
112
|
-
for (const node of ts.import.iterate(statements)) {
|
|
113
|
-
end = node.end;
|
|
114
|
-
}
|
|
115
|
-
return end;
|
|
116
|
-
},
|
|
117
|
-
getStatementsImportedName(statements, path, key) {
|
|
118
|
-
for (const node of ts.import.iterateWithSource(statements, path)) {
|
|
119
|
-
const name = ts.import.getImportName(node, key);
|
|
120
|
-
if (name)
|
|
121
|
-
return name;
|
|
122
|
-
}
|
|
123
|
-
return;
|
|
124
|
-
},
|
|
125
|
-
getImportName(node, key) {
|
|
126
|
-
var _a, _b;
|
|
127
|
-
if (!node.importClause)
|
|
128
|
-
return;
|
|
129
|
-
const elements = (_a = node.importClause.namedBindings) == null ? void 0 : _a.elements;
|
|
130
|
-
if (!elements)
|
|
131
|
-
return;
|
|
132
|
-
for (const element of elements) {
|
|
133
|
-
if (((_b = element.propertyName) == null ? void 0 : _b.escapedText) === key || element.name.escapedText === key) {
|
|
134
|
-
return element.name.escapedText.toString();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
variable: {
|
|
141
|
-
iterate: iterate(SyntaxKind.VariableStatement),
|
|
142
|
-
*iterateDeclarations(statements) {
|
|
143
|
-
for (const node of ts.variable.iterate(statements)) {
|
|
144
|
-
for (const dec of node.declarationList.declarations) {
|
|
145
|
-
yield dec;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
class: {
|
|
151
|
-
iterate: iterate(SyntaxKind.ClassDeclaration)
|
|
152
|
-
},
|
|
153
|
-
prop: {
|
|
154
|
-
getName({ name }) {
|
|
155
|
-
if (ts.is.identifier(name)) {
|
|
156
|
-
return name.escapedText;
|
|
157
|
-
} else if (name && "text" in name) {
|
|
158
|
-
return name.text;
|
|
159
|
-
} else {
|
|
160
|
-
return name == null ? void 0 : name.getText();
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
getValue(prop) {
|
|
164
|
-
if (ts.is.propertyAssignment(prop)) {
|
|
165
|
-
return prop.initializer.getText();
|
|
166
|
-
} else if (ts.is.shorthandPropertyAssignment(prop)) {
|
|
167
|
-
return prop.name.escapedText.toString();
|
|
168
|
-
} else {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
spaces: {
|
|
174
|
-
getAtLine(content, pos) {
|
|
175
|
-
var _a;
|
|
176
|
-
const lines = content.slice(0, pos).split("\n");
|
|
177
|
-
const last = lines[lines.length - 1];
|
|
178
|
-
return ((_a = last.match(/^\s+/)) == null ? void 0 : _a[0]) || "";
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const libraryName = "orchid-orm";
|
|
184
|
-
const importKey = "orchidORM";
|
|
185
|
-
const newFile = (options) => `import { orchidORM } from 'orchid-orm';
|
|
186
|
-
|
|
187
|
-
export const db = orchidORM(
|
|
188
|
-
{
|
|
189
|
-
${optionsToString(options)}
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
}
|
|
193
|
-
);
|
|
194
|
-
`;
|
|
195
|
-
const optionsToString = (options) => {
|
|
196
|
-
const lines = [];
|
|
197
|
-
for (const key in options) {
|
|
198
|
-
const value = options[key];
|
|
199
|
-
if (typeof value !== "object" && typeof value !== "function") {
|
|
200
|
-
lines.push(
|
|
201
|
-
`${key}: ${typeof value === "string" ? singleQuote(value) : value},`
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return lines.join("\n ");
|
|
206
|
-
};
|
|
207
|
-
const updateMainFile = async (filePath, tablePath, ast, options, logger) => {
|
|
208
|
-
const result = await fs.readFile(filePath, "utf-8").then(
|
|
209
|
-
(content2) => ({ error: void 0, content: content2 }),
|
|
210
|
-
(error) => {
|
|
211
|
-
return { error, content: void 0 };
|
|
212
|
-
}
|
|
213
|
-
);
|
|
214
|
-
if (result.error && result.error.code !== "ENOENT")
|
|
215
|
-
throw result.error;
|
|
216
|
-
const content = result.content || newFile(options);
|
|
217
|
-
const statements = ts.getStatements(content);
|
|
218
|
-
const importName = ts.import.getStatementsImportedName(
|
|
219
|
-
statements,
|
|
220
|
-
libraryName,
|
|
221
|
-
importKey
|
|
222
|
-
);
|
|
223
|
-
if (!importName) {
|
|
224
|
-
throw new AppCodeUpdaterError(
|
|
225
|
-
`Main file does not contain import of orchid-orm`
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
const object = getTablesListObject(importName, statements);
|
|
229
|
-
if (!object) {
|
|
230
|
-
throw new Error("List of tables is not found in main file");
|
|
231
|
-
}
|
|
232
|
-
const spaces = ts.spaces.getAtLine(content, object.end);
|
|
233
|
-
const context = {
|
|
234
|
-
filePath,
|
|
235
|
-
tablePath,
|
|
236
|
-
statements,
|
|
237
|
-
object,
|
|
238
|
-
content,
|
|
239
|
-
spaces
|
|
240
|
-
};
|
|
241
|
-
let write;
|
|
242
|
-
if (ast.type === "table") {
|
|
243
|
-
if (ast.action === "create") {
|
|
244
|
-
write = createTable$1(context, ast);
|
|
245
|
-
} else {
|
|
246
|
-
write = dropTable(context, ast);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (write) {
|
|
250
|
-
if (result.error) {
|
|
251
|
-
await fs.mkdir(path__default.dirname(filePath), { recursive: true });
|
|
252
|
-
}
|
|
253
|
-
await fs.writeFile(filePath, write);
|
|
254
|
-
logger == null ? void 0 : logger.log(
|
|
255
|
-
`${result.content ? "Updated" : "Created"} ${pathToLog(filePath)}`
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
};
|
|
259
|
-
const createTable$1 = ({ filePath, tablePath, statements, object, content, spaces }, ast) => {
|
|
260
|
-
const key = toCamelCase(ast.name);
|
|
261
|
-
const value = `${toPascalCase(ast.name)}Table`;
|
|
262
|
-
const changes = new FileChanges(content);
|
|
263
|
-
const importPath = getImportPath(filePath, tablePath(toCamelCase(ast.name)));
|
|
264
|
-
const existing = Array.from(
|
|
265
|
-
ts.import.iterateWithSource(statements, importPath)
|
|
266
|
-
);
|
|
267
|
-
if (existing.length)
|
|
268
|
-
return;
|
|
269
|
-
for (const prop of object.properties) {
|
|
270
|
-
if (key === ts.prop.getName(prop)) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
const importPos = ts.import.getEndPos(statements);
|
|
275
|
-
changes.add(
|
|
276
|
-
importPos,
|
|
277
|
-
`${importPos === 0 ? "" : "\n"}import { ${value} } from '${importPath}';`
|
|
278
|
-
);
|
|
279
|
-
let insert = `
|
|
280
|
-
${spaces} ${key}: ${value},`;
|
|
281
|
-
if (object.properties.length && !object.properties.hasTrailingComma) {
|
|
282
|
-
insert = `,${insert}`;
|
|
283
|
-
}
|
|
284
|
-
if (!content.slice(object.properties.end, object.end).includes("\n")) {
|
|
285
|
-
insert += `
|
|
286
|
-
${spaces}`;
|
|
287
|
-
}
|
|
288
|
-
changes.add(object.properties.end, insert);
|
|
289
|
-
return changes.apply();
|
|
290
|
-
};
|
|
291
|
-
const dropTable = ({ filePath, tablePath, statements, object, content }, ast) => {
|
|
292
|
-
const changes = new FileChanges(content);
|
|
293
|
-
const importPath = getImportPath(filePath, tablePath(toCamelCase(ast.name)));
|
|
294
|
-
const tableClassName = `${toPascalCase(ast.name)}Table`;
|
|
295
|
-
const importNames = [];
|
|
296
|
-
for (const node of ts.import.iterateWithSource(statements, importPath)) {
|
|
297
|
-
changes.remove(node.pos, node.end);
|
|
298
|
-
const name = ts.import.getImportName(node, tableClassName);
|
|
299
|
-
if (name && !importNames.includes(name)) {
|
|
300
|
-
importNames.push(name);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
for (const prop of object.properties) {
|
|
304
|
-
const name = ts.prop.getValue(prop);
|
|
305
|
-
if (!name || !importNames.includes(name))
|
|
306
|
-
continue;
|
|
307
|
-
let { end } = prop;
|
|
308
|
-
if (content[end] === ",")
|
|
309
|
-
end++;
|
|
310
|
-
changes.remove(prop.pos, end);
|
|
311
|
-
}
|
|
312
|
-
return changes.apply();
|
|
313
|
-
};
|
|
314
|
-
const getTablesListObject = (importName, statements) => {
|
|
315
|
-
for (const node of ts.variable.iterateDeclarations(statements)) {
|
|
316
|
-
const call = node.initializer;
|
|
317
|
-
if (!ts.is.call(call))
|
|
318
|
-
continue;
|
|
319
|
-
if (call.expression.getText() !== importName)
|
|
320
|
-
continue;
|
|
321
|
-
if (call.arguments.length !== 2) {
|
|
322
|
-
throw new Error(
|
|
323
|
-
"Invalid number of arguments when initializing orchid orm"
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
const object = call.arguments[1];
|
|
327
|
-
if (!ts.is.objectLiteral(object)) {
|
|
328
|
-
throw new Error("Second argument of orchidORM must be an object literal");
|
|
329
|
-
}
|
|
330
|
-
return object;
|
|
331
|
-
}
|
|
332
|
-
return;
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const handleForeignKey = async ({
|
|
336
|
-
getTable,
|
|
337
|
-
relations,
|
|
338
|
-
tableName,
|
|
339
|
-
columns,
|
|
340
|
-
foreignTableName,
|
|
341
|
-
foreignColumns,
|
|
342
|
-
skipBelongsTo
|
|
343
|
-
}) => {
|
|
344
|
-
var _a, _b;
|
|
345
|
-
const table = await getTable(tableName);
|
|
346
|
-
if (!table)
|
|
347
|
-
return;
|
|
348
|
-
const foreignTable = await getTable(foreignTableName);
|
|
349
|
-
if (!foreignTable)
|
|
350
|
-
return;
|
|
351
|
-
if (!skipBelongsTo) {
|
|
352
|
-
(_a = relations[tableName]) != null ? _a : relations[tableName] = {
|
|
353
|
-
path: table.path,
|
|
354
|
-
relations: []
|
|
355
|
-
};
|
|
356
|
-
relations[tableName].relations.push({
|
|
357
|
-
kind: "belongsTo",
|
|
358
|
-
columns,
|
|
359
|
-
className: foreignTable.name,
|
|
360
|
-
path: foreignTable.path,
|
|
361
|
-
foreignColumns
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
(_b = relations[foreignTableName]) != null ? _b : relations[foreignTableName] = {
|
|
365
|
-
path: foreignTable.path,
|
|
366
|
-
relations: []
|
|
367
|
-
};
|
|
368
|
-
relations[foreignTableName].relations.push({
|
|
369
|
-
kind: "hasMany",
|
|
370
|
-
columns: foreignColumns,
|
|
371
|
-
className: table.name,
|
|
372
|
-
path: table.path,
|
|
373
|
-
foreignColumns: columns
|
|
374
|
-
});
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
378
|
-
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
379
|
-
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
380
|
-
var __objRest$3 = (source, exclude) => {
|
|
381
|
-
var target = {};
|
|
382
|
-
for (var prop in source)
|
|
383
|
-
if (__hasOwnProp$4.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
384
|
-
target[prop] = source[prop];
|
|
385
|
-
if (source != null && __getOwnPropSymbols$4)
|
|
386
|
-
for (var prop of __getOwnPropSymbols$4(source)) {
|
|
387
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$4.call(source, prop))
|
|
388
|
-
target[prop] = source[prop];
|
|
389
|
-
}
|
|
390
|
-
return target;
|
|
391
|
-
};
|
|
392
|
-
const createTable = async (_a) => {
|
|
393
|
-
var _b = _a, {
|
|
394
|
-
ast,
|
|
395
|
-
logger,
|
|
396
|
-
getTable,
|
|
397
|
-
relations,
|
|
398
|
-
tables,
|
|
399
|
-
delayed
|
|
400
|
-
} = _b, params = __objRest$3(_b, [
|
|
401
|
-
"ast",
|
|
402
|
-
"logger",
|
|
403
|
-
"getTable",
|
|
404
|
-
"relations",
|
|
405
|
-
"tables",
|
|
406
|
-
"delayed"
|
|
407
|
-
]);
|
|
408
|
-
const key = toCamelCase(ast.name);
|
|
409
|
-
const tablePath = params.tablePath(key);
|
|
410
|
-
const baseTablePath = getImportPath(
|
|
411
|
-
tablePath,
|
|
412
|
-
params.baseTable.getFilePath()
|
|
413
|
-
);
|
|
414
|
-
const className = `${toPascalCase(ast.name)}Table`;
|
|
415
|
-
tables[ast.name] = {
|
|
416
|
-
key,
|
|
417
|
-
name: className,
|
|
418
|
-
path: tablePath
|
|
419
|
-
};
|
|
420
|
-
const imports = {
|
|
421
|
-
[baseTablePath]: params.baseTable.exportAs
|
|
422
|
-
};
|
|
423
|
-
const props = [];
|
|
424
|
-
if (ast.schema) {
|
|
425
|
-
props.push(`schema = ${singleQuote(ast.schema)};`);
|
|
426
|
-
}
|
|
427
|
-
props.push(`readonly table = ${singleQuote(ast.name)};`);
|
|
428
|
-
if (ast.noPrimaryKey === "ignore") {
|
|
429
|
-
props.push("noPrimaryKey = true;");
|
|
430
|
-
}
|
|
431
|
-
props.push(
|
|
432
|
-
"columns = this.setColumns((t) => ({",
|
|
433
|
-
columnsShapeToCode(ast.shape, ast, "t"),
|
|
434
|
-
"}));"
|
|
435
|
-
);
|
|
436
|
-
const importsCode = importsToCode(imports);
|
|
437
|
-
const code = [
|
|
438
|
-
`export class ${className} extends ${params.baseTable.exportAs} {`,
|
|
439
|
-
props,
|
|
440
|
-
"}\n"
|
|
441
|
-
];
|
|
442
|
-
await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
|
|
443
|
-
try {
|
|
444
|
-
const content = importsCode + "\n\n" + codeToString(code, "", " ");
|
|
445
|
-
await fs.writeFile(tablePath, content, { flag: "wx" });
|
|
446
|
-
delayed.push(async () => {
|
|
447
|
-
const imports2 = {};
|
|
448
|
-
const relCode = await getRelations(
|
|
449
|
-
ast,
|
|
450
|
-
getTable,
|
|
451
|
-
tablePath,
|
|
452
|
-
imports2,
|
|
453
|
-
relations,
|
|
454
|
-
ast.name
|
|
455
|
-
);
|
|
456
|
-
if (relCode) {
|
|
457
|
-
const code2 = codeToString(relCode, " ", " ");
|
|
458
|
-
const updated = content.slice(0, importsCode.length) + `
|
|
459
|
-
${importsToCode(imports2)}` + content.slice(importsCode.length, -2) + " \n" + code2 + "\n" + content.slice(-2);
|
|
460
|
-
await fs.writeFile(tablePath, updated);
|
|
461
|
-
}
|
|
462
|
-
logger == null ? void 0 : logger.log(`Created ${pathToLog(tablePath)}`);
|
|
463
|
-
});
|
|
464
|
-
} catch (err) {
|
|
465
|
-
if (err.code !== "EEXIST") {
|
|
466
|
-
throw err;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
function importsToCode(imports) {
|
|
471
|
-
return Object.entries(imports).map(([from, name]) => `import { ${name} } from '${from}';`).join("\n");
|
|
472
|
-
}
|
|
473
|
-
const getRelations = async (ast, getTable, tablePath, imports, relations, tableName) => {
|
|
474
|
-
const refs = [];
|
|
475
|
-
for (const key in ast.shape) {
|
|
476
|
-
const item = ast.shape[key];
|
|
477
|
-
if (!(item instanceof ColumnType) || !item.data.foreignKeys)
|
|
478
|
-
continue;
|
|
479
|
-
for (const fkey of item.data.foreignKeys) {
|
|
480
|
-
if ("table" in fkey) {
|
|
481
|
-
refs.push({
|
|
482
|
-
table: fkey.table,
|
|
483
|
-
columns: [key],
|
|
484
|
-
foreignColumns: fkey.columns
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
if (ast.constraints) {
|
|
490
|
-
for (const { references: ref } of ast.constraints) {
|
|
491
|
-
if (ref && typeof ref.fnOrTable === "string") {
|
|
492
|
-
refs.push({
|
|
493
|
-
table: ref.fnOrTable,
|
|
494
|
-
columns: ref.columns,
|
|
495
|
-
foreignColumns: ref.foreignColumns
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
if (!refs.length)
|
|
501
|
-
return;
|
|
502
|
-
const code = [];
|
|
503
|
-
for (const ref of refs) {
|
|
504
|
-
const { columns, foreignColumns } = ref;
|
|
505
|
-
if (columns.length > 1 || foreignColumns.length > 1)
|
|
506
|
-
continue;
|
|
507
|
-
const info = await getTable(ref.table);
|
|
508
|
-
if (!info)
|
|
509
|
-
continue;
|
|
510
|
-
const path2 = getImportPath(tablePath, info.path);
|
|
511
|
-
imports[path2] = info.name;
|
|
512
|
-
code.push(
|
|
513
|
-
`${info.key}: this.belongsTo(() => ${info.name}, {`,
|
|
514
|
-
[
|
|
515
|
-
`columns: [${columns.map(singleQuote).join(", ")}],`,
|
|
516
|
-
`references: [${foreignColumns.map(singleQuote).join(", ")}],`
|
|
517
|
-
],
|
|
518
|
-
"}),"
|
|
519
|
-
);
|
|
520
|
-
await handleForeignKey({
|
|
521
|
-
getTable,
|
|
522
|
-
relations,
|
|
523
|
-
tableName,
|
|
524
|
-
columns: ref.columns,
|
|
525
|
-
foreignTableName: ref.table,
|
|
526
|
-
foreignColumns: ref.foreignColumns,
|
|
527
|
-
skipBelongsTo: true
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
return code.length ? ["relations = {", code, "};"] : void 0;
|
|
531
|
-
};
|
|
532
|
-
|
|
533
|
-
var __defProp$2 = Object.defineProperty;
|
|
534
|
-
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
535
|
-
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
536
|
-
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
537
|
-
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
538
|
-
var __spreadValues$2 = (a, b) => {
|
|
539
|
-
for (var prop in b || (b = {}))
|
|
540
|
-
if (__hasOwnProp$3.call(b, prop))
|
|
541
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
542
|
-
if (__getOwnPropSymbols$3)
|
|
543
|
-
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
544
|
-
if (__propIsEnum$3.call(b, prop))
|
|
545
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
546
|
-
}
|
|
547
|
-
return a;
|
|
548
|
-
};
|
|
549
|
-
var __objRest$2 = (source, exclude) => {
|
|
550
|
-
var target = {};
|
|
551
|
-
for (var prop in source)
|
|
552
|
-
if (__hasOwnProp$3.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
553
|
-
target[prop] = source[prop];
|
|
554
|
-
if (source != null && __getOwnPropSymbols$3)
|
|
555
|
-
for (var prop of __getOwnPropSymbols$3(source)) {
|
|
556
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$3.call(source, prop))
|
|
557
|
-
target[prop] = source[prop];
|
|
558
|
-
}
|
|
559
|
-
return target;
|
|
560
|
-
};
|
|
561
|
-
const changeTable = async (_a) => {
|
|
562
|
-
var _b = _a, {
|
|
563
|
-
ast,
|
|
564
|
-
logger
|
|
565
|
-
} = _b, params = __objRest$2(_b, [
|
|
566
|
-
"ast",
|
|
567
|
-
"logger"
|
|
568
|
-
]);
|
|
569
|
-
const tablePath = params.tablePath(toCamelCase(ast.name));
|
|
570
|
-
const content = await fs.readFile(tablePath, "utf-8").catch(() => void 0);
|
|
571
|
-
if (!content)
|
|
572
|
-
return;
|
|
573
|
-
const changes = new FileChanges(content);
|
|
574
|
-
const statements = ts.getStatements(content);
|
|
575
|
-
const className = toPascalCase(ast.name) + "Table";
|
|
576
|
-
for (const { t, object } of iterateColumnsShapes(statements, className)) {
|
|
577
|
-
const context = makeChangeContext(changes, ast, content, object, t);
|
|
578
|
-
prependSpaces(context);
|
|
579
|
-
applySchemaChanges(context);
|
|
580
|
-
appendTrailingComma(context);
|
|
581
|
-
addColumns(context);
|
|
582
|
-
addTableData(context);
|
|
583
|
-
}
|
|
584
|
-
await fs.writeFile(tablePath, changes.apply());
|
|
585
|
-
logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
|
|
586
|
-
};
|
|
587
|
-
function* iterateColumnsShapes(statements, className) {
|
|
588
|
-
var _a, _b;
|
|
589
|
-
for (const node of ts.class.iterate(statements)) {
|
|
590
|
-
if (((_a = node.name) == null ? void 0 : _a.escapedText) !== className)
|
|
591
|
-
continue;
|
|
592
|
-
for (const member of node.members) {
|
|
593
|
-
const name = ts.prop.getName(member);
|
|
594
|
-
const { initializer: call } = member;
|
|
595
|
-
if (name !== "columns" || !call || !ts.is.call(call))
|
|
596
|
-
continue;
|
|
597
|
-
const { expression } = call;
|
|
598
|
-
if (!ts.is.propertyAccess(expression) || !ts.is.this(expression.expression) || expression.name.escapedText !== "setColumns")
|
|
599
|
-
continue;
|
|
600
|
-
const [arg] = call.arguments;
|
|
601
|
-
if (!ts.is.arrowFunction(arg))
|
|
602
|
-
continue;
|
|
603
|
-
const { parameters, body } = arg;
|
|
604
|
-
const param = (_b = parameters[0]) == null ? void 0 : _b.name;
|
|
605
|
-
if (!ts.is.identifier(param) || !ts.is.parenthesizedExpression(body))
|
|
606
|
-
continue;
|
|
607
|
-
const { expression: object } = body;
|
|
608
|
-
if (!ts.is.objectLiteral(object))
|
|
609
|
-
continue;
|
|
610
|
-
yield { t: param.escapedText.toString(), object };
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
const makeChangeContext = (changes, ast, content, object, t) => {
|
|
615
|
-
const add = {};
|
|
616
|
-
const drop = {};
|
|
617
|
-
const change = {};
|
|
618
|
-
const { properties: props } = object;
|
|
619
|
-
const existingColumns = getExistingColumns(props);
|
|
620
|
-
for (const key in ast.shape) {
|
|
621
|
-
const item = ast.shape[key];
|
|
622
|
-
if (item.type === "add" && !existingColumns[key]) {
|
|
623
|
-
add[key] = item.item;
|
|
624
|
-
}
|
|
625
|
-
if (!existingColumns[key])
|
|
626
|
-
continue;
|
|
627
|
-
if (item.type === "drop" && existingColumns[key]) {
|
|
628
|
-
drop[key] = true;
|
|
629
|
-
} else if (item.type === "change" && existingColumns[key]) {
|
|
630
|
-
change[key] = item;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
const spaces = ts.spaces.getAtLine(content, object.end);
|
|
634
|
-
const shape = { add, drop, change };
|
|
635
|
-
return {
|
|
636
|
-
changes,
|
|
637
|
-
props,
|
|
638
|
-
shape,
|
|
639
|
-
spaces,
|
|
640
|
-
t,
|
|
641
|
-
object,
|
|
642
|
-
add: ast.add,
|
|
643
|
-
drop: ast.drop
|
|
644
|
-
};
|
|
645
|
-
};
|
|
646
|
-
const getExistingColumns = (props) => {
|
|
647
|
-
const existingColumns = {};
|
|
648
|
-
props.map((prop) => ts.is.propertyAssignment(prop) && ts.prop.getName(prop)).filter((name) => !!name).forEach((name) => existingColumns[name] = true);
|
|
649
|
-
for (const prop of props) {
|
|
650
|
-
if (!ts.is.propertyAssignment(prop))
|
|
651
|
-
continue;
|
|
652
|
-
const name = ts.prop.getName(prop);
|
|
653
|
-
if (name)
|
|
654
|
-
existingColumns[name] = true;
|
|
655
|
-
}
|
|
656
|
-
return existingColumns;
|
|
657
|
-
};
|
|
658
|
-
const prependSpaces = ({
|
|
659
|
-
props,
|
|
660
|
-
shape: { add },
|
|
661
|
-
changes,
|
|
662
|
-
spaces
|
|
663
|
-
}) => {
|
|
664
|
-
if (Object.keys(add).length && props.pos === props.end) {
|
|
665
|
-
changes.add(props.pos, `
|
|
666
|
-
${spaces}`);
|
|
667
|
-
}
|
|
668
|
-
};
|
|
669
|
-
const applySchemaChanges = (context) => {
|
|
670
|
-
const {
|
|
671
|
-
props,
|
|
672
|
-
shape: { drop: dropColumns, change: changeColumns },
|
|
673
|
-
add,
|
|
674
|
-
drop
|
|
675
|
-
} = context;
|
|
676
|
-
props.forEach((prop, i) => {
|
|
677
|
-
if (ts.is.spreadAssignment(prop)) {
|
|
678
|
-
const call = prop.expression;
|
|
679
|
-
if (!ts.is.call(call))
|
|
680
|
-
return;
|
|
681
|
-
const access = call.expression;
|
|
682
|
-
if (!ts.is.propertyAccess(access))
|
|
683
|
-
return;
|
|
684
|
-
const name = access.name.escapedText.toString();
|
|
685
|
-
if (name === "primaryKey") {
|
|
686
|
-
if (drop.primaryKey || add.primaryKey) {
|
|
687
|
-
removeProp(context, prop, i);
|
|
688
|
-
}
|
|
689
|
-
} else if (name === "index") {
|
|
690
|
-
dropMatchingIndexes(context, prop, i, call, drop.indexes);
|
|
691
|
-
} else if (name === "foreignKey") {
|
|
692
|
-
dropMatchingForeignKey(context, prop, i, call, drop.constraints);
|
|
693
|
-
} else if (name === "check") {
|
|
694
|
-
dropMatchingCheck(context, prop, i, call, drop.constraints);
|
|
695
|
-
} else if (name === "constraint") {
|
|
696
|
-
dropMatchingConstraint(context, prop, i, call, drop.constraints);
|
|
697
|
-
}
|
|
698
|
-
} else if (ts.is.propertyAssignment(prop)) {
|
|
699
|
-
const name = ts.prop.getName(prop);
|
|
700
|
-
if (!name)
|
|
701
|
-
return;
|
|
702
|
-
if (dropColumns[name]) {
|
|
703
|
-
removeProp(context, prop, i);
|
|
704
|
-
} else {
|
|
705
|
-
const changeItem = changeColumns[name];
|
|
706
|
-
if (changeItem) {
|
|
707
|
-
changeColumn(context, changeItem, prop);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
});
|
|
712
|
-
};
|
|
713
|
-
const removeProp = ({ props, changes }, prop, i) => {
|
|
714
|
-
var _a;
|
|
715
|
-
const end = ((_a = props[i + 1]) == null ? void 0 : _a.pos) || props.end;
|
|
716
|
-
changes.remove(prop.pos, end);
|
|
717
|
-
};
|
|
718
|
-
const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
|
|
719
|
-
const { from, to } = changeItem;
|
|
720
|
-
if ((from.type !== to.type || !!from.identity !== !!to.identity) && to.column) {
|
|
721
|
-
changes.replace(
|
|
722
|
-
prop.initializer.pos,
|
|
723
|
-
prop.end,
|
|
724
|
-
` ${codeToString(to.column.toCode(t), spaces + " ", " ").trim()}`
|
|
725
|
-
);
|
|
726
|
-
return;
|
|
727
|
-
}
|
|
728
|
-
const items = [];
|
|
729
|
-
let chain = prop.initializer;
|
|
730
|
-
while (ts.is.call(chain) && ts.is.propertyAccess(chain.expression)) {
|
|
731
|
-
items.push(chain);
|
|
732
|
-
chain = chain.expression.expression;
|
|
733
|
-
}
|
|
734
|
-
const propsToChange = {};
|
|
735
|
-
for (const key in from) {
|
|
736
|
-
if (to[key] !== from[key]) {
|
|
737
|
-
propsToChange[key] = true;
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
for (const key in to) {
|
|
741
|
-
if (to[key] !== from[key]) {
|
|
742
|
-
propsToChange[key] = true;
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
const changedProps = {};
|
|
746
|
-
const replaced = {};
|
|
747
|
-
for (const item of items.reverse()) {
|
|
748
|
-
if (!ts.is.propertyAccess(item.expression))
|
|
749
|
-
continue;
|
|
750
|
-
const { name } = item.expression;
|
|
751
|
-
let key = name.escapedText.toString();
|
|
752
|
-
if (key === "index")
|
|
753
|
-
key = "indexes";
|
|
754
|
-
else if (key === "foreignKey")
|
|
755
|
-
key = "foreignKeys";
|
|
756
|
-
if (!propsToChange[key])
|
|
757
|
-
continue;
|
|
758
|
-
let remove = true;
|
|
759
|
-
if (!replaced[key]) {
|
|
760
|
-
let code = getColumnMethodArgs(t, to, key, to.type);
|
|
761
|
-
if (!code && key === "identity" && to.type) {
|
|
762
|
-
code = [`.${to.type}()`];
|
|
763
|
-
}
|
|
764
|
-
if (code) {
|
|
765
|
-
changes.replace(
|
|
766
|
-
item.expression.expression.end,
|
|
767
|
-
item.end,
|
|
768
|
-
codeToString(code, spaces + " ", " ").trim()
|
|
769
|
-
);
|
|
770
|
-
replaced[key] = true;
|
|
771
|
-
remove = false;
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
if (remove) {
|
|
775
|
-
changes.remove(item.expression.expression.end, item.end);
|
|
776
|
-
}
|
|
777
|
-
changedProps[key] = true;
|
|
778
|
-
}
|
|
779
|
-
let append = "";
|
|
780
|
-
for (const key in propsToChange) {
|
|
781
|
-
if (changedProps[key])
|
|
782
|
-
continue;
|
|
783
|
-
const code = getColumnMethodArgs(t, to, key);
|
|
784
|
-
if (code) {
|
|
785
|
-
append += codeToString(code, spaces + " ", " ").trim();
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
if (append) {
|
|
789
|
-
changes.add(prop.end, append);
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
const appendTrailingComma = ({ props, changes }) => {
|
|
793
|
-
if (!props.hasTrailingComma) {
|
|
794
|
-
const last = props[props.length - 1];
|
|
795
|
-
if (last) {
|
|
796
|
-
changes.add(last.end, ",");
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
};
|
|
800
|
-
const addColumns = ({
|
|
801
|
-
shape: { add },
|
|
802
|
-
changes,
|
|
803
|
-
object,
|
|
804
|
-
t,
|
|
805
|
-
spaces
|
|
806
|
-
}) => {
|
|
807
|
-
const end = object.end - 1;
|
|
808
|
-
for (const key in add) {
|
|
809
|
-
const code = codeToString(add[key].toCode(t), spaces + " ", " ");
|
|
810
|
-
changes.add(end, ` ${quoteObjectKey(key)}: ${code.trim()},
|
|
811
|
-
${spaces}`);
|
|
812
|
-
}
|
|
813
|
-
};
|
|
814
|
-
const addTableData = ({ add, changes, object, t, spaces }) => {
|
|
815
|
-
const end = object.end - 1;
|
|
816
|
-
if (add.primaryKey) {
|
|
817
|
-
const code = codeToString(
|
|
818
|
-
primaryKeyToCode(add.primaryKey, t),
|
|
819
|
-
spaces,
|
|
820
|
-
" "
|
|
821
|
-
);
|
|
822
|
-
changes.add(end, ` ${code.trim()}
|
|
823
|
-
${spaces}`);
|
|
824
|
-
}
|
|
825
|
-
if (add.indexes) {
|
|
826
|
-
for (const item of add.indexes) {
|
|
827
|
-
const code = codeToString(indexToCode(item, t), spaces + " ", " ");
|
|
828
|
-
changes.add(end, ` ${code.trim()}
|
|
829
|
-
${spaces}`);
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
if (add.constraints) {
|
|
833
|
-
for (const item of add.constraints) {
|
|
834
|
-
const code = codeToString(constraintToCode(item, t), spaces + " ", " ");
|
|
835
|
-
changes.add(end, ` ${code.trim()}
|
|
836
|
-
${spaces}`);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
const getColumnMethodArgs = (t, to, key, dataType) => {
|
|
841
|
-
const value = to[key];
|
|
842
|
-
if (!value)
|
|
843
|
-
return;
|
|
844
|
-
if (key === "indexes") {
|
|
845
|
-
return columnIndexesToCode(
|
|
846
|
-
value
|
|
847
|
-
);
|
|
848
|
-
}
|
|
849
|
-
if (key === "foreignKeys") {
|
|
850
|
-
return columnForeignKeysToCode(value);
|
|
851
|
-
}
|
|
852
|
-
if (key === "check") {
|
|
853
|
-
return [columnCheckToCode(t, value)];
|
|
854
|
-
}
|
|
855
|
-
if (key === "identity") {
|
|
856
|
-
const code2 = identityToCode(value, dataType);
|
|
857
|
-
code2[0] = `.${code2[0]}`;
|
|
858
|
-
return code2;
|
|
859
|
-
}
|
|
860
|
-
const code = [`.${key}(`];
|
|
861
|
-
if (key === "collate" || key === "compression") {
|
|
862
|
-
addCode(code, singleQuote(value));
|
|
863
|
-
} else if (key === "default") {
|
|
864
|
-
addCode(code, columnDefaultArgumentToCode(t, value));
|
|
865
|
-
} else if (key !== "nullable" && key !== "primaryKey") {
|
|
866
|
-
return;
|
|
867
|
-
}
|
|
868
|
-
addCode(code, ")");
|
|
869
|
-
return code;
|
|
870
|
-
};
|
|
871
|
-
const dropMatchingIndexes = (context, prop, i, call, items) => {
|
|
872
|
-
if (!(items == null ? void 0 : items.length))
|
|
873
|
-
return;
|
|
874
|
-
const [columnsNode, optionsNode] = call.arguments;
|
|
875
|
-
const columns = [];
|
|
876
|
-
if (ts.is.stringLiteral(columnsNode)) {
|
|
877
|
-
columns.push({ column: columnsNode.text });
|
|
878
|
-
} else if (ts.is.arrayLiteral(columnsNode)) {
|
|
879
|
-
for (const node of columnsNode.elements) {
|
|
880
|
-
if (ts.is.stringLiteral(node)) {
|
|
881
|
-
columns.push({ column: node.text });
|
|
882
|
-
} else if (ts.is.objectLiteral(node)) {
|
|
883
|
-
const object = collectObjectFromCode(node);
|
|
884
|
-
if (!object)
|
|
885
|
-
return;
|
|
886
|
-
columns.push(object);
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
} else {
|
|
890
|
-
return;
|
|
891
|
-
}
|
|
892
|
-
const options = ts.is.objectLiteral(optionsNode) && collectObjectFromCode(optionsNode) || {};
|
|
893
|
-
for (const item of items) {
|
|
894
|
-
if (deepCompare(columns, item.columns) && deepCompare(options, item.options)) {
|
|
895
|
-
removeProp(context, prop, i);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
};
|
|
899
|
-
const dropMatchingForeignKey = (context, prop, i, call, items) => {
|
|
900
|
-
if (!(items == null ? void 0 : items.length))
|
|
901
|
-
return;
|
|
902
|
-
const existing = parseReferencesArgs(context, call.arguments);
|
|
903
|
-
if (!existing)
|
|
904
|
-
return;
|
|
905
|
-
for (const item of items) {
|
|
906
|
-
if (compareReferences(existing, item.references))
|
|
907
|
-
removeProp(context, prop, i);
|
|
908
|
-
}
|
|
909
|
-
};
|
|
910
|
-
const parseReferencesArgs = (context, args) => {
|
|
911
|
-
const columns = collectStringArrayFromCode(args[0]);
|
|
912
|
-
if (!columns)
|
|
913
|
-
return;
|
|
914
|
-
const fnOrTableNode = args[1];
|
|
915
|
-
let fnOrTable;
|
|
916
|
-
if (ts.is.stringLiteral(fnOrTableNode)) {
|
|
917
|
-
fnOrTable = fnOrTableNode.text;
|
|
918
|
-
} else if (ts.is.arrowFunction(fnOrTableNode)) {
|
|
919
|
-
fnOrTable = context.changes.content.slice(fnOrTableNode.pos, fnOrTableNode.end).replaceAll(/\s/g, "");
|
|
920
|
-
} else {
|
|
921
|
-
return;
|
|
922
|
-
}
|
|
923
|
-
const foreignColumns = collectStringArrayFromCode(args[2]);
|
|
924
|
-
if (!foreignColumns)
|
|
925
|
-
return;
|
|
926
|
-
const options = ts.is.objectLiteral(args[3]) && collectObjectFromCode(args[3]) || {};
|
|
927
|
-
return { columns, fnOrTable, foreignColumns, options };
|
|
928
|
-
};
|
|
929
|
-
const compareReferences = (existing, item) => {
|
|
930
|
-
if (!item)
|
|
931
|
-
return;
|
|
932
|
-
const itemOptions = item.options ? __spreadValues$2({}, item.options) : void 0;
|
|
933
|
-
if (itemOptions) {
|
|
934
|
-
delete itemOptions.dropMode;
|
|
935
|
-
}
|
|
936
|
-
return deepCompare(existing == null ? void 0 : existing.columns, item == null ? void 0 : item.columns) && deepCompare(existing == null ? void 0 : existing.fnOrTable, item == null ? void 0 : item.fnOrTable.toString()) && deepCompare(existing == null ? void 0 : existing.foreignColumns, item == null ? void 0 : item.foreignColumns) && deepCompare(existing == null ? void 0 : existing.options, itemOptions);
|
|
937
|
-
};
|
|
938
|
-
const dropMatchingCheck = (context, prop, i, call, items) => {
|
|
939
|
-
if (!(items == null ? void 0 : items.length))
|
|
940
|
-
return;
|
|
941
|
-
const sql = parseCheckArg(context, call.arguments[0]);
|
|
942
|
-
for (const item of items) {
|
|
943
|
-
const { check } = item;
|
|
944
|
-
if (!check)
|
|
945
|
-
continue;
|
|
946
|
-
if (check._sql === sql) {
|
|
947
|
-
removeProp(context, prop, i);
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
};
|
|
951
|
-
const parseCheckArg = (context, arg) => {
|
|
952
|
-
if (!arg || !ts.is.call(arg))
|
|
953
|
-
return;
|
|
954
|
-
const { expression } = arg;
|
|
955
|
-
if (!ts.is.propertyAccess(expression) || !ts.is.identifier(expression.expression) || expression.expression.escapedText !== context.t || expression.name.escapedText !== "sql")
|
|
956
|
-
return;
|
|
957
|
-
const [obj] = arg.arguments;
|
|
958
|
-
if (!obj || !ts.is.objectLiteral(obj))
|
|
959
|
-
return;
|
|
960
|
-
for (const prop of obj.properties) {
|
|
961
|
-
if (!ts.is.propertyAssignment(prop))
|
|
962
|
-
continue;
|
|
963
|
-
const name = ts.prop.getName(prop);
|
|
964
|
-
if (name !== "raw")
|
|
965
|
-
continue;
|
|
966
|
-
const init = prop.initializer;
|
|
967
|
-
if (!ts.is.stringLiteral(init) || !("text" in init))
|
|
968
|
-
continue;
|
|
969
|
-
return init.text;
|
|
970
|
-
}
|
|
971
|
-
return;
|
|
972
|
-
};
|
|
973
|
-
const dropMatchingConstraint = (context, prop, i, call, items) => {
|
|
974
|
-
var _a;
|
|
975
|
-
if (!(items == null ? void 0 : items.length))
|
|
976
|
-
return;
|
|
977
|
-
const {
|
|
978
|
-
arguments: [arg]
|
|
979
|
-
} = call;
|
|
980
|
-
if (!arg || !ts.is.objectLiteral(arg))
|
|
981
|
-
return;
|
|
982
|
-
const existing = {};
|
|
983
|
-
for (const prop2 of arg.properties) {
|
|
984
|
-
if (!ts.is.propertyAssignment(prop2))
|
|
985
|
-
return;
|
|
986
|
-
const name = ts.prop.getName(prop2);
|
|
987
|
-
if (!name)
|
|
988
|
-
return;
|
|
989
|
-
const init = prop2.initializer;
|
|
990
|
-
if (name === "name") {
|
|
991
|
-
if (!ts.is.stringLiteral(init))
|
|
992
|
-
return;
|
|
993
|
-
existing.name = init.text;
|
|
994
|
-
} else if (name === "references") {
|
|
995
|
-
if (!ts.is.arrayLiteral(init))
|
|
996
|
-
return;
|
|
997
|
-
const refs = parseReferencesArgs(context, init.elements);
|
|
998
|
-
if (!refs)
|
|
999
|
-
return;
|
|
1000
|
-
existing.references = refs;
|
|
1001
|
-
} else if (name === "check") {
|
|
1002
|
-
existing.check = parseCheckArg(context, init);
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
for (const item of items) {
|
|
1006
|
-
if (existing.name !== item.name)
|
|
1007
|
-
continue;
|
|
1008
|
-
if (existing.check !== ((_a = item.check) == null ? void 0 : _a._sql))
|
|
1009
|
-
continue;
|
|
1010
|
-
if ((existing.references || item.references) && !compareReferences(existing.references, item.references))
|
|
1011
|
-
continue;
|
|
1012
|
-
removeProp(context, prop, i);
|
|
1013
|
-
}
|
|
1014
|
-
};
|
|
1015
|
-
const collectStringArrayFromCode = (node) => {
|
|
1016
|
-
if (!ts.is.arrayLiteral(node))
|
|
1017
|
-
return;
|
|
1018
|
-
const result = node.elements.filter(ts.is.stringLiteral).map((item) => item.text);
|
|
1019
|
-
return result.length === node.elements.length ? result : void 0;
|
|
1020
|
-
};
|
|
1021
|
-
const collectObjectFromCode = (node) => {
|
|
1022
|
-
const object = {};
|
|
1023
|
-
for (const prop of node.properties) {
|
|
1024
|
-
if (!ts.is.propertyAssignment(prop))
|
|
1025
|
-
return;
|
|
1026
|
-
const name = ts.prop.getName(prop);
|
|
1027
|
-
if (!name)
|
|
1028
|
-
return;
|
|
1029
|
-
const init = prop.initializer;
|
|
1030
|
-
if (ts.is.stringLiteral(init)) {
|
|
1031
|
-
object[name] = init.text;
|
|
1032
|
-
} else if (ts.is.numericLiteral(init)) {
|
|
1033
|
-
object[name] = parseFloat(init.text);
|
|
1034
|
-
} else {
|
|
1035
|
-
return;
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
return object;
|
|
1039
|
-
};
|
|
1040
|
-
|
|
1041
|
-
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
1042
|
-
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
1043
|
-
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
1044
|
-
var __objRest$1 = (source, exclude) => {
|
|
1045
|
-
var target = {};
|
|
1046
|
-
for (var prop in source)
|
|
1047
|
-
if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1048
|
-
target[prop] = source[prop];
|
|
1049
|
-
if (source != null && __getOwnPropSymbols$2)
|
|
1050
|
-
for (var prop of __getOwnPropSymbols$2(source)) {
|
|
1051
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
|
|
1052
|
-
target[prop] = source[prop];
|
|
1053
|
-
}
|
|
1054
|
-
return target;
|
|
1055
|
-
};
|
|
1056
|
-
const renameTable = async (_a) => {
|
|
1057
|
-
var _b = _a, {
|
|
1058
|
-
ast,
|
|
1059
|
-
logger
|
|
1060
|
-
} = _b, params = __objRest$1(_b, [
|
|
1061
|
-
"ast",
|
|
1062
|
-
"logger"
|
|
1063
|
-
]);
|
|
1064
|
-
var _a2;
|
|
1065
|
-
const tablePath = params.tablePath(toCamelCase(ast.from));
|
|
1066
|
-
const content = await fs.readFile(tablePath, "utf-8").catch(() => void 0);
|
|
1067
|
-
if (!content)
|
|
1068
|
-
return;
|
|
1069
|
-
const changes = new FileChanges(content);
|
|
1070
|
-
const statements = ts.getStatements(content);
|
|
1071
|
-
const className = toPascalCase(ast.from) + "Table";
|
|
1072
|
-
const changeSchema = ast.fromSchema !== ast.toSchema;
|
|
1073
|
-
for (const node of ts.class.iterate(statements)) {
|
|
1074
|
-
if (((_a2 = node.name) == null ? void 0 : _a2.escapedText) !== className)
|
|
1075
|
-
continue;
|
|
1076
|
-
const addSchema = changeSchema && ast.toSchema && !node.members.some((member) => ts.prop.getName(member) === "schema");
|
|
1077
|
-
if (addSchema && ast.toSchema) {
|
|
1078
|
-
changes.add(
|
|
1079
|
-
node.members.pos,
|
|
1080
|
-
`
|
|
1081
|
-
schema = ${singleQuote(ast.toSchema)};`
|
|
1082
|
-
);
|
|
1083
|
-
}
|
|
1084
|
-
for (const member of node.members) {
|
|
1085
|
-
const name = ts.prop.getName(member);
|
|
1086
|
-
if (name !== "table" && !(changeSchema && name === "schema"))
|
|
1087
|
-
continue;
|
|
1088
|
-
const { initializer: value } = member;
|
|
1089
|
-
if (!value)
|
|
1090
|
-
continue;
|
|
1091
|
-
if (name === "schema") {
|
|
1092
|
-
if (ast.toSchema) {
|
|
1093
|
-
changes.replace(
|
|
1094
|
-
value.pos,
|
|
1095
|
-
value.end,
|
|
1096
|
-
` ${singleQuote(ast.toSchema)}`
|
|
1097
|
-
);
|
|
1098
|
-
} else {
|
|
1099
|
-
changes.remove(member.pos, member.end);
|
|
1100
|
-
}
|
|
1101
|
-
} else {
|
|
1102
|
-
changes.replace(value.pos, value.end, ` ${singleQuote(ast.to)}`);
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
await fs.writeFile(tablePath, changes.apply());
|
|
1107
|
-
logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
|
|
1108
|
-
};
|
|
1109
|
-
|
|
1110
|
-
var __defProp$1 = Object.defineProperty;
|
|
1111
|
-
var __defProps$1 = Object.defineProperties;
|
|
1112
|
-
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
1113
|
-
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
1114
|
-
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
1115
|
-
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
1116
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1117
|
-
var __spreadValues$1 = (a, b) => {
|
|
1118
|
-
for (var prop in b || (b = {}))
|
|
1119
|
-
if (__hasOwnProp$1.call(b, prop))
|
|
1120
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
1121
|
-
if (__getOwnPropSymbols$1)
|
|
1122
|
-
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
1123
|
-
if (__propIsEnum$1.call(b, prop))
|
|
1124
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
1125
|
-
}
|
|
1126
|
-
return a;
|
|
1127
|
-
};
|
|
1128
|
-
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1129
|
-
const updateTableFile = async (params) => {
|
|
1130
|
-
const { ast } = params;
|
|
1131
|
-
if (ast.type === "table" && ast.action === "create") {
|
|
1132
|
-
await createTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
1133
|
-
} else if (ast.type === "changeTable") {
|
|
1134
|
-
await changeTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
1135
|
-
} else if (ast.type === "renameType" && ast.table) {
|
|
1136
|
-
await renameTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
1137
|
-
} else if (ast.type === "constraint" && ast.references) {
|
|
1138
|
-
const ref = ast.references;
|
|
1139
|
-
if (typeof ref.fnOrTable === "string") {
|
|
1140
|
-
await handleForeignKey({
|
|
1141
|
-
getTable: params.getTable,
|
|
1142
|
-
relations: params.relations,
|
|
1143
|
-
tableName: ast.tableName,
|
|
1144
|
-
columns: ref.columns,
|
|
1145
|
-
foreignTableName: ref.fnOrTable,
|
|
1146
|
-
foreignColumns: ref.foreignColumns
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
};
|
|
1151
|
-
|
|
1152
|
-
const createBaseTableFile = async ({
|
|
1153
|
-
baseTable,
|
|
1154
|
-
logger
|
|
1155
|
-
}) => {
|
|
1156
|
-
const filePath = baseTable.getFilePath();
|
|
1157
|
-
await fs.mkdir(path__default.dirname(filePath), { recursive: true });
|
|
1158
|
-
await fs.writeFile(
|
|
1159
|
-
filePath,
|
|
1160
|
-
`import { createBaseTable } from 'orchid-orm';
|
|
1161
|
-
|
|
1162
|
-
export const ${baseTable.exportAs} = createBaseTable();
|
|
1163
|
-
`,
|
|
1164
|
-
{
|
|
1165
|
-
flag: "wx"
|
|
1166
|
-
}
|
|
1167
|
-
).then(() => {
|
|
1168
|
-
logger == null ? void 0 : logger.log(`Created ${pathToLog(filePath)}`);
|
|
1169
|
-
}).catch((err) => {
|
|
1170
|
-
if (err.code === "EEXIST")
|
|
1171
|
-
return;
|
|
1172
|
-
throw err;
|
|
1173
|
-
});
|
|
1174
|
-
};
|
|
1175
|
-
|
|
1176
|
-
const updateRelations = async ({
|
|
1177
|
-
relations,
|
|
1178
|
-
logger
|
|
1179
|
-
}) => {
|
|
1180
|
-
await Promise.all(
|
|
1181
|
-
Object.entries(relations).map(
|
|
1182
|
-
([tableName, item]) => updateRelationItem(tableName, item, logger)
|
|
1183
|
-
)
|
|
1184
|
-
);
|
|
1185
|
-
};
|
|
1186
|
-
const updateRelationItem = async (tableName, item, logger) => {
|
|
1187
|
-
var _a;
|
|
1188
|
-
const content = await fs.readFile(item.path, "utf-8").catch(() => void 0);
|
|
1189
|
-
if (!content)
|
|
1190
|
-
return;
|
|
1191
|
-
const changes = new FileChanges(content);
|
|
1192
|
-
const statements = ts.getStatements(content);
|
|
1193
|
-
const dirName = path__default.dirname(item.path);
|
|
1194
|
-
const imports = {};
|
|
1195
|
-
for (const relation of item.relations) {
|
|
1196
|
-
if (!imports[relation.path]) {
|
|
1197
|
-
imports[relation.path] = relation.className;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
let importsEnd = 0;
|
|
1201
|
-
for (const node of ts.import.iterate(statements)) {
|
|
1202
|
-
const source = ts.import.getSource(node);
|
|
1203
|
-
const full = path__default.join(dirName, source + ".ts");
|
|
1204
|
-
if (imports[full]) {
|
|
1205
|
-
delete imports[full];
|
|
1206
|
-
}
|
|
1207
|
-
importsEnd = node.end;
|
|
1208
|
-
}
|
|
1209
|
-
const addImports = Object.entries(imports).map(
|
|
1210
|
-
([path2, name]) => `import { ${name} } from '${getImportPath(item.path, path2)}';`
|
|
1211
|
-
).join("\n");
|
|
1212
|
-
if (addImports) {
|
|
1213
|
-
changes.add(importsEnd, `
|
|
1214
|
-
${addImports}`);
|
|
1215
|
-
}
|
|
1216
|
-
const targetClass = findClassByTableName(statements, tableName);
|
|
1217
|
-
if (targetClass) {
|
|
1218
|
-
const relationsMember = findRelationsMember(targetClass.members);
|
|
1219
|
-
if (relationsMember) {
|
|
1220
|
-
const { initializer } = relationsMember;
|
|
1221
|
-
const takenKeys = {};
|
|
1222
|
-
if (ts.is.objectLiteral(initializer)) {
|
|
1223
|
-
const props = initializer.properties;
|
|
1224
|
-
for (const prop of props) {
|
|
1225
|
-
const name = (_a = prop.name) == null ? void 0 : _a.getText();
|
|
1226
|
-
if (name)
|
|
1227
|
-
takenKeys[name] = true;
|
|
1228
|
-
}
|
|
1229
|
-
const addRelations = [];
|
|
1230
|
-
for (const rel of item.relations) {
|
|
1231
|
-
if (!checkRelation(rel))
|
|
1232
|
-
continue;
|
|
1233
|
-
const name = makeRelationName(rel);
|
|
1234
|
-
if (takenKeys[name])
|
|
1235
|
-
continue;
|
|
1236
|
-
addRelations.push(relationToCode(rel));
|
|
1237
|
-
}
|
|
1238
|
-
if (addRelations.length) {
|
|
1239
|
-
const pos = props.end;
|
|
1240
|
-
changes.add(pos, addRelations.join(""));
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
} else {
|
|
1244
|
-
changes.add(
|
|
1245
|
-
targetClass.end - 1,
|
|
1246
|
-
`
|
|
1247
|
-
relations = {${item.relations.filter(checkRelation).map((rel) => relationToCode(rel)).join("")}
|
|
1248
|
-
};
|
|
1249
|
-
`
|
|
1250
|
-
);
|
|
1251
|
-
}
|
|
1252
|
-
}
|
|
1253
|
-
await fs.writeFile(item.path, changes.apply());
|
|
1254
|
-
logger == null ? void 0 : logger.log(`Updated ${pathToLog(item.path)}`);
|
|
1255
|
-
};
|
|
1256
|
-
const findClassByTableName = (statements, tableName) => {
|
|
1257
|
-
for (const node of ts.class.iterate(statements)) {
|
|
1258
|
-
for (const member of node.members) {
|
|
1259
|
-
const name = ts.prop.getName(member);
|
|
1260
|
-
if (name !== "table")
|
|
1261
|
-
continue;
|
|
1262
|
-
const { initializer: value } = member;
|
|
1263
|
-
if (!value || !ts.is.stringLiteral(value))
|
|
1264
|
-
continue;
|
|
1265
|
-
if (value.text === tableName) {
|
|
1266
|
-
return node;
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
return;
|
|
1271
|
-
};
|
|
1272
|
-
const findRelationsMember = (members) => {
|
|
1273
|
-
for (const member of members) {
|
|
1274
|
-
const name = ts.prop.getName(member);
|
|
1275
|
-
if (name === "relations")
|
|
1276
|
-
return member;
|
|
1277
|
-
}
|
|
1278
|
-
return;
|
|
1279
|
-
};
|
|
1280
|
-
const checkRelation = (rel) => {
|
|
1281
|
-
return rel.columns.length === 1 && rel.foreignColumns.length === 1;
|
|
1282
|
-
};
|
|
1283
|
-
const makeRelationName = (rel) => {
|
|
1284
|
-
return pluralize(
|
|
1285
|
-
rel.className[0].toLowerCase() + rel.className.slice(1).replace(/Table$/, "")
|
|
1286
|
-
);
|
|
1287
|
-
};
|
|
1288
|
-
const relationToCode = (rel, name = makeRelationName(rel)) => {
|
|
1289
|
-
const code = [`
|
|
1290
|
-
${name}: this.${rel.kind}(() => ${rel.className}, {`];
|
|
1291
|
-
const pk = rel[rel.kind === "hasMany" ? "columns" : "foreignColumns"];
|
|
1292
|
-
const fk = rel[rel.kind === "hasMany" ? "foreignColumns" : "columns"];
|
|
1293
|
-
code.push(
|
|
1294
|
-
` columns: [${pk.map(singleQuote).join(", ")}],`,
|
|
1295
|
-
` references: [${fk.map(singleQuote).join(", ")}],`,
|
|
1296
|
-
" }),"
|
|
1297
|
-
);
|
|
1298
|
-
return code.join("\n");
|
|
1299
|
-
};
|
|
1300
|
-
|
|
1301
|
-
var __defProp = Object.defineProperty;
|
|
1302
|
-
var __defProps = Object.defineProperties;
|
|
1303
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
1304
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
1305
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
1306
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
1307
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1308
|
-
var __spreadValues = (a, b) => {
|
|
1309
|
-
for (var prop in b || (b = {}))
|
|
1310
|
-
if (__hasOwnProp.call(b, prop))
|
|
1311
|
-
__defNormalProp(a, prop, b[prop]);
|
|
1312
|
-
if (__getOwnPropSymbols)
|
|
1313
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
1314
|
-
if (__propIsEnum.call(b, prop))
|
|
1315
|
-
__defNormalProp(a, prop, b[prop]);
|
|
1316
|
-
}
|
|
1317
|
-
return a;
|
|
1318
|
-
};
|
|
1319
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1320
|
-
var __objRest = (source, exclude) => {
|
|
1321
|
-
var target = {};
|
|
1322
|
-
for (var prop in source)
|
|
1323
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1324
|
-
target[prop] = source[prop];
|
|
1325
|
-
if (source != null && __getOwnPropSymbols)
|
|
1326
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
1327
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
1328
|
-
target[prop] = source[prop];
|
|
1329
|
-
}
|
|
1330
|
-
return target;
|
|
1331
|
-
};
|
|
1332
|
-
class AppCodeUpdaterError extends Error {
|
|
1333
|
-
}
|
|
1334
|
-
const makeGetTable = (path2, ormExportedAs, tables, imp) => {
|
|
1335
|
-
let orm;
|
|
1336
|
-
return async (tableName) => {
|
|
1337
|
-
var _a;
|
|
1338
|
-
if (tables[tableName])
|
|
1339
|
-
return tables[tableName];
|
|
1340
|
-
if (!orm) {
|
|
1341
|
-
const mod = await imp(path2).catch((err) => {
|
|
1342
|
-
if (err.code === "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
1343
|
-
return require(path2);
|
|
1344
|
-
} else {
|
|
1345
|
-
throw err;
|
|
1346
|
-
}
|
|
1347
|
-
});
|
|
1348
|
-
orm = mod[ormExportedAs];
|
|
1349
|
-
if (!orm) {
|
|
1350
|
-
throw new Error(`ORM is not exported as ${ormExportedAs} from ${path2}`);
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
for (const key in orm) {
|
|
1354
|
-
const table = orm[key];
|
|
1355
|
-
if (!table || typeof table !== "object" || !(table instanceof Db) || table.table !== tableName)
|
|
1356
|
-
continue;
|
|
1357
|
-
const name = table.name;
|
|
1358
|
-
if (!name)
|
|
1359
|
-
continue;
|
|
1360
|
-
const path3 = (_a = table.getFilePath) == null ? void 0 : _a.call(table);
|
|
1361
|
-
if (!path3)
|
|
1362
|
-
continue;
|
|
1363
|
-
return tables[tableName] = {
|
|
1364
|
-
key,
|
|
1365
|
-
name,
|
|
1366
|
-
path: path3
|
|
1367
|
-
};
|
|
1368
|
-
}
|
|
1369
|
-
return;
|
|
1370
|
-
};
|
|
1371
|
-
};
|
|
1372
|
-
const appCodeUpdater = ({
|
|
1373
|
-
tablePath,
|
|
1374
|
-
ormPath,
|
|
1375
|
-
ormExportedAs = "db"
|
|
1376
|
-
}) => ({
|
|
1377
|
-
async process(_a) {
|
|
1378
|
-
var _b = _a, { ast, options, basePath, logger, baseTable } = _b, config = __objRest(_b, ["ast", "options", "basePath", "logger", "baseTable"]);
|
|
1379
|
-
var _a2, _b2;
|
|
1380
|
-
const params = {
|
|
1381
|
-
tablePath(name) {
|
|
1382
|
-
const file = tablePath(name);
|
|
1383
|
-
return resolvePath(basePath, file);
|
|
1384
|
-
},
|
|
1385
|
-
ormPath: resolvePath(basePath, ormPath),
|
|
1386
|
-
ormExportedAs,
|
|
1387
|
-
logger
|
|
1388
|
-
};
|
|
1389
|
-
const cache = config.cache;
|
|
1390
|
-
(_a2 = cache.relations) != null ? _a2 : cache.relations = {};
|
|
1391
|
-
(_b2 = cache.tables) != null ? _b2 : cache.tables = {};
|
|
1392
|
-
const getTable = makeGetTable(
|
|
1393
|
-
params.ormPath,
|
|
1394
|
-
ormExportedAs,
|
|
1395
|
-
cache.tables,
|
|
1396
|
-
config.import
|
|
1397
|
-
);
|
|
1398
|
-
await updateMainFile(
|
|
1399
|
-
params.ormPath,
|
|
1400
|
-
params.tablePath,
|
|
1401
|
-
ast,
|
|
1402
|
-
options,
|
|
1403
|
-
logger
|
|
1404
|
-
);
|
|
1405
|
-
const delayed = [];
|
|
1406
|
-
const promises = [
|
|
1407
|
-
updateTableFile(__spreadProps(__spreadValues({}, params), {
|
|
1408
|
-
ast,
|
|
1409
|
-
baseTable,
|
|
1410
|
-
getTable,
|
|
1411
|
-
relations: cache.relations,
|
|
1412
|
-
tables: cache.tables,
|
|
1413
|
-
delayed
|
|
1414
|
-
}))
|
|
1415
|
-
];
|
|
1416
|
-
if (!cache.createdBaseTable) {
|
|
1417
|
-
promises.push(
|
|
1418
|
-
createBaseTableFile({ logger: params.logger, baseTable }).then(() => {
|
|
1419
|
-
cache.createdBaseTable = true;
|
|
1420
|
-
})
|
|
1421
|
-
);
|
|
1422
|
-
}
|
|
1423
|
-
await Promise.all(promises);
|
|
1424
|
-
await Promise.all(delayed.map((fn) => fn()));
|
|
1425
|
-
},
|
|
1426
|
-
async afterAll({ cache, logger }) {
|
|
1427
|
-
const { relations } = cache;
|
|
1428
|
-
if (!relations)
|
|
1429
|
-
return;
|
|
1430
|
-
await updateRelations({
|
|
1431
|
-
relations,
|
|
1432
|
-
logger
|
|
1433
|
-
});
|
|
1434
|
-
}
|
|
1435
|
-
});
|
|
1436
|
-
const resolvePath = (basePath, filePath) => path.isAbsolute(filePath) ? filePath : path.resolve(basePath, filePath);
|
|
1437
|
-
|
|
1438
|
-
export { AppCodeUpdaterError, appCodeUpdater };
|
|
1439
|
-
//# sourceMappingURL=index.mjs.map
|