orchid-orm 1.5.29 → 1.5.30
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.js +10 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -22
- package/.env.example +0 -1
- package/.turbo/turbo-check.log +0 -26
- package/.turbo/turbo-test.log +0 -26
- package/.turbo/turbo-test:ci.log +0 -26
- package/CHANGELOG.md +0 -382
- package/coverage/coverage-summary.json +0 -28
- package/jest-setup.ts +0 -11
- package/rollup.config.js +0 -18
- package/src/bin/bin.ts +0 -3
- package/src/bin/init.test.ts +0 -810
- package/src/bin/init.ts +0 -529
- package/src/codegen/appCodeUpdater.test.ts +0 -75
- package/src/codegen/appCodeUpdater.ts +0 -53
- package/src/codegen/createBaseTableFile.test.ts +0 -53
- package/src/codegen/createBaseTableFile.ts +0 -31
- package/src/codegen/fileChanges.ts +0 -41
- package/src/codegen/testUtils.ts +0 -56
- package/src/codegen/tsUtils.ts +0 -180
- package/src/codegen/updateMainFile.test.ts +0 -253
- package/src/codegen/updateMainFile.ts +0 -210
- package/src/codegen/updateTableFile/changeTable.test.ts +0 -804
- package/src/codegen/updateTableFile/changeTable.ts +0 -536
- package/src/codegen/updateTableFile/createTable.test.ts +0 -139
- package/src/codegen/updateTableFile/createTable.ts +0 -51
- package/src/codegen/updateTableFile/renameTable.test.ts +0 -124
- package/src/codegen/updateTableFile/renameTable.ts +0 -67
- package/src/codegen/updateTableFile/updateTableFile.ts +0 -22
- package/src/codegen/utils.ts +0 -13
- package/src/index.ts +0 -5
- package/src/orm.test.ts +0 -92
- package/src/orm.ts +0 -98
- package/src/relations/belongsTo.test.ts +0 -1122
- package/src/relations/belongsTo.ts +0 -352
- package/src/relations/hasAndBelongsToMany.test.ts +0 -1335
- package/src/relations/hasAndBelongsToMany.ts +0 -472
- package/src/relations/hasMany.test.ts +0 -2616
- package/src/relations/hasMany.ts +0 -401
- package/src/relations/hasOne.test.ts +0 -1701
- package/src/relations/hasOne.ts +0 -351
- package/src/relations/relations.test.ts +0 -37
- package/src/relations/relations.ts +0 -363
- package/src/relations/utils.ts +0 -162
- package/src/repo.test.ts +0 -200
- package/src/repo.ts +0 -119
- package/src/table.test.ts +0 -121
- package/src/table.ts +0 -184
- package/src/test-utils/test-db.ts +0 -32
- package/src/test-utils/test-tables.ts +0 -194
- package/src/test-utils/test-utils.ts +0 -119
- package/src/transaction.test.ts +0 -47
- package/src/transaction.ts +0 -27
- package/src/utils.ts +0 -9
- package/tsconfig.json +0 -14
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { RakeDbAst } from 'rake-db';
|
|
2
|
-
import fs from 'fs/promises';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { NodeArray, ObjectLiteralExpression, Statement } from 'typescript';
|
|
5
|
-
import { toCamelCase, toPascalCase } from '../utils';
|
|
6
|
-
import { AppCodeUpdaterError } from './appCodeUpdater';
|
|
7
|
-
import { FileChanges } from './fileChanges';
|
|
8
|
-
import { ts } from './tsUtils';
|
|
9
|
-
import { getImportPath } from './utils';
|
|
10
|
-
import { AdapterOptions, singleQuote } from 'pqb';
|
|
11
|
-
|
|
12
|
-
type Context = {
|
|
13
|
-
filePath: string;
|
|
14
|
-
tablePath: (name: string) => string;
|
|
15
|
-
statements: NodeArray<Statement>;
|
|
16
|
-
object: ObjectLiteralExpression;
|
|
17
|
-
content: string;
|
|
18
|
-
spaces: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const libraryName = 'orchid-orm';
|
|
22
|
-
const importKey = 'orchidORM';
|
|
23
|
-
|
|
24
|
-
const newFile = (
|
|
25
|
-
options: AdapterOptions,
|
|
26
|
-
) => `import { orchidORM } from 'orchid-orm';
|
|
27
|
-
|
|
28
|
-
export const db = orchidORM(
|
|
29
|
-
{
|
|
30
|
-
${optionsToString(options)}
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
}
|
|
34
|
-
);
|
|
35
|
-
`;
|
|
36
|
-
|
|
37
|
-
const optionsToString = (options: AdapterOptions) => {
|
|
38
|
-
const lines: string[] = [];
|
|
39
|
-
for (const key in options) {
|
|
40
|
-
const value = options[key as keyof AdapterOptions];
|
|
41
|
-
if (typeof value !== 'object' && typeof value !== 'function') {
|
|
42
|
-
lines.push(
|
|
43
|
-
`${key}: ${typeof value === 'string' ? singleQuote(value) : value},`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return lines.join('\n ');
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const updateMainFile = async (
|
|
51
|
-
filePath: string,
|
|
52
|
-
tablePath: (name: string) => string,
|
|
53
|
-
ast: RakeDbAst,
|
|
54
|
-
options: AdapterOptions,
|
|
55
|
-
) => {
|
|
56
|
-
const result = await fs.readFile(filePath, 'utf-8').then(
|
|
57
|
-
(content) => ({ error: undefined, content }),
|
|
58
|
-
(error) => {
|
|
59
|
-
return { error, content: undefined };
|
|
60
|
-
},
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
if (result.error && result.error.code !== 'ENOENT') throw result.error;
|
|
64
|
-
const content = result.content || newFile(options);
|
|
65
|
-
|
|
66
|
-
const statements = ts.getStatements(content);
|
|
67
|
-
|
|
68
|
-
const importName = ts.import.getStatementsImportedName(
|
|
69
|
-
statements,
|
|
70
|
-
libraryName,
|
|
71
|
-
importKey,
|
|
72
|
-
);
|
|
73
|
-
if (!importName) {
|
|
74
|
-
throw new AppCodeUpdaterError(
|
|
75
|
-
`Main file does not contain import of orchid-orm`,
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const object = getTablesListObject(importName, statements);
|
|
80
|
-
if (!object) {
|
|
81
|
-
throw new Error('List of tables is not found in main file');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const spaces = ts.spaces.getAtLine(content, object.end);
|
|
85
|
-
|
|
86
|
-
const context: Context = {
|
|
87
|
-
filePath,
|
|
88
|
-
tablePath,
|
|
89
|
-
statements,
|
|
90
|
-
object,
|
|
91
|
-
content,
|
|
92
|
-
spaces,
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
let write: string | undefined;
|
|
96
|
-
if (ast.type === 'table') {
|
|
97
|
-
if (ast.action === 'create') {
|
|
98
|
-
write = createTable(context, ast);
|
|
99
|
-
} else {
|
|
100
|
-
write = dropTable(context, ast);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// rename table is not handled because renaming of the class and the file is better to be done by the editor,
|
|
104
|
-
// editor can scan all project files, rename import path and imported class name
|
|
105
|
-
|
|
106
|
-
if (write) {
|
|
107
|
-
if (result.error) {
|
|
108
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
await fs.writeFile(filePath, write);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
const createTable = (
|
|
116
|
-
{ filePath, tablePath, statements, object, content, spaces }: Context,
|
|
117
|
-
ast: RakeDbAst.Table,
|
|
118
|
-
) => {
|
|
119
|
-
const key = toCamelCase(ast.name);
|
|
120
|
-
const value = `${toPascalCase(ast.name)}Table`;
|
|
121
|
-
|
|
122
|
-
const changes = new FileChanges(content);
|
|
123
|
-
|
|
124
|
-
const importPath = getImportPath(filePath, tablePath(ast.name));
|
|
125
|
-
|
|
126
|
-
const existing = Array.from(
|
|
127
|
-
ts.import.iterateWithSource(statements, importPath),
|
|
128
|
-
);
|
|
129
|
-
if (existing.length) return;
|
|
130
|
-
|
|
131
|
-
for (const prop of object.properties) {
|
|
132
|
-
if (key === ts.prop.getName(prop)) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const importPos = ts.import.getEndPos(statements);
|
|
138
|
-
changes.add(
|
|
139
|
-
importPos,
|
|
140
|
-
`${importPos === 0 ? '' : '\n'}import { ${value} } from '${importPath}';`,
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
let insert = `\n${spaces} ${key}: ${value},`;
|
|
144
|
-
if (object.properties.length && !object.properties.hasTrailingComma) {
|
|
145
|
-
insert = `,${insert}`;
|
|
146
|
-
}
|
|
147
|
-
if (!content.slice(object.properties.end, object.end).includes('\n')) {
|
|
148
|
-
insert += `\n${spaces}`;
|
|
149
|
-
}
|
|
150
|
-
changes.add(object.properties.end, insert);
|
|
151
|
-
|
|
152
|
-
return changes.apply();
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
const dropTable = (
|
|
156
|
-
{ filePath, tablePath, statements, object, content }: Context,
|
|
157
|
-
ast: RakeDbAst.Table,
|
|
158
|
-
) => {
|
|
159
|
-
const changes = new FileChanges(content);
|
|
160
|
-
|
|
161
|
-
const importPath = getImportPath(filePath, tablePath(ast.name));
|
|
162
|
-
const tableClassName = `${toPascalCase(ast.name)}Table`;
|
|
163
|
-
const importNames: string[] = [];
|
|
164
|
-
for (const node of ts.import.iterateWithSource(statements, importPath)) {
|
|
165
|
-
changes.remove(node.pos, node.end);
|
|
166
|
-
|
|
167
|
-
const name = ts.import.getImportName(node, tableClassName);
|
|
168
|
-
if (name && !importNames.includes(name)) {
|
|
169
|
-
importNames.push(name);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
for (const prop of object.properties) {
|
|
174
|
-
const name = ts.prop.getValue(prop);
|
|
175
|
-
if (!name || !importNames.includes(name)) continue;
|
|
176
|
-
|
|
177
|
-
let { end } = prop;
|
|
178
|
-
if (content[end] === ',') end++;
|
|
179
|
-
changes.remove(prop.pos, end);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return changes.apply();
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
const getTablesListObject = (
|
|
186
|
-
importName: string,
|
|
187
|
-
statements: NodeArray<Statement>,
|
|
188
|
-
): ObjectLiteralExpression | undefined => {
|
|
189
|
-
for (const node of ts.variable.iterateDeclarations(statements)) {
|
|
190
|
-
const call = node.initializer;
|
|
191
|
-
if (!ts.is.call(call)) continue;
|
|
192
|
-
|
|
193
|
-
if (call.expression.getText() !== importName) continue;
|
|
194
|
-
|
|
195
|
-
if (call.arguments.length !== 2) {
|
|
196
|
-
throw new Error(
|
|
197
|
-
'Invalid number of arguments when initializing orchid orm',
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const object = call.arguments[1];
|
|
202
|
-
if (!ts.is.objectLiteral(object)) {
|
|
203
|
-
throw new Error('Second argument of orchidORM must be an object literal');
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return object;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return;
|
|
210
|
-
};
|