orchid-orm 1.5.29 → 1.5.31

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.
Files changed (59) hide show
  1. package/dist/index.d.ts +6 -14
  2. package/dist/index.js +26 -21
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +26 -21
  5. package/dist/index.mjs.map +1 -1
  6. package/package.json +12 -22
  7. package/.env.example +0 -1
  8. package/.turbo/turbo-check.log +0 -26
  9. package/.turbo/turbo-test.log +0 -26
  10. package/.turbo/turbo-test:ci.log +0 -26
  11. package/CHANGELOG.md +0 -382
  12. package/coverage/coverage-summary.json +0 -28
  13. package/jest-setup.ts +0 -11
  14. package/rollup.config.js +0 -18
  15. package/src/bin/bin.ts +0 -3
  16. package/src/bin/init.test.ts +0 -810
  17. package/src/bin/init.ts +0 -529
  18. package/src/codegen/appCodeUpdater.test.ts +0 -75
  19. package/src/codegen/appCodeUpdater.ts +0 -53
  20. package/src/codegen/createBaseTableFile.test.ts +0 -53
  21. package/src/codegen/createBaseTableFile.ts +0 -31
  22. package/src/codegen/fileChanges.ts +0 -41
  23. package/src/codegen/testUtils.ts +0 -56
  24. package/src/codegen/tsUtils.ts +0 -180
  25. package/src/codegen/updateMainFile.test.ts +0 -253
  26. package/src/codegen/updateMainFile.ts +0 -210
  27. package/src/codegen/updateTableFile/changeTable.test.ts +0 -804
  28. package/src/codegen/updateTableFile/changeTable.ts +0 -536
  29. package/src/codegen/updateTableFile/createTable.test.ts +0 -139
  30. package/src/codegen/updateTableFile/createTable.ts +0 -51
  31. package/src/codegen/updateTableFile/renameTable.test.ts +0 -124
  32. package/src/codegen/updateTableFile/renameTable.ts +0 -67
  33. package/src/codegen/updateTableFile/updateTableFile.ts +0 -22
  34. package/src/codegen/utils.ts +0 -13
  35. package/src/index.ts +0 -5
  36. package/src/orm.test.ts +0 -92
  37. package/src/orm.ts +0 -98
  38. package/src/relations/belongsTo.test.ts +0 -1122
  39. package/src/relations/belongsTo.ts +0 -352
  40. package/src/relations/hasAndBelongsToMany.test.ts +0 -1335
  41. package/src/relations/hasAndBelongsToMany.ts +0 -472
  42. package/src/relations/hasMany.test.ts +0 -2616
  43. package/src/relations/hasMany.ts +0 -401
  44. package/src/relations/hasOne.test.ts +0 -1701
  45. package/src/relations/hasOne.ts +0 -351
  46. package/src/relations/relations.test.ts +0 -37
  47. package/src/relations/relations.ts +0 -363
  48. package/src/relations/utils.ts +0 -162
  49. package/src/repo.test.ts +0 -200
  50. package/src/repo.ts +0 -119
  51. package/src/table.test.ts +0 -121
  52. package/src/table.ts +0 -184
  53. package/src/test-utils/test-db.ts +0 -32
  54. package/src/test-utils/test-tables.ts +0 -194
  55. package/src/test-utils/test-utils.ts +0 -119
  56. package/src/transaction.test.ts +0 -47
  57. package/src/transaction.ts +0 -27
  58. package/src/utils.ts +0 -9
  59. 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
- };