orchid-orm 1.5.28 → 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.
Files changed (57) hide show
  1. package/dist/index.js +10 -10
  2. package/dist/index.js.map +1 -1
  3. package/dist/index.mjs +10 -10
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +13 -22
  6. package/.env.example +0 -1
  7. package/.turbo/turbo-test.log +0 -26
  8. package/.turbo/turbo-test:ci.log +0 -26
  9. package/CHANGELOG.md +0 -375
  10. package/coverage/coverage-summary.json +0 -28
  11. package/jest-setup.ts +0 -11
  12. package/rollup.config.js +0 -18
  13. package/src/bin/bin.ts +0 -3
  14. package/src/bin/init.test.ts +0 -810
  15. package/src/bin/init.ts +0 -529
  16. package/src/codegen/appCodeUpdater.test.ts +0 -75
  17. package/src/codegen/appCodeUpdater.ts +0 -53
  18. package/src/codegen/createBaseTableFile.test.ts +0 -53
  19. package/src/codegen/createBaseTableFile.ts +0 -31
  20. package/src/codegen/fileChanges.ts +0 -41
  21. package/src/codegen/testUtils.ts +0 -56
  22. package/src/codegen/tsUtils.ts +0 -180
  23. package/src/codegen/updateMainFile.test.ts +0 -253
  24. package/src/codegen/updateMainFile.ts +0 -210
  25. package/src/codegen/updateTableFile/changeTable.test.ts +0 -804
  26. package/src/codegen/updateTableFile/changeTable.ts +0 -536
  27. package/src/codegen/updateTableFile/createTable.test.ts +0 -139
  28. package/src/codegen/updateTableFile/createTable.ts +0 -51
  29. package/src/codegen/updateTableFile/renameTable.test.ts +0 -124
  30. package/src/codegen/updateTableFile/renameTable.ts +0 -67
  31. package/src/codegen/updateTableFile/updateTableFile.ts +0 -22
  32. package/src/codegen/utils.ts +0 -13
  33. package/src/index.ts +0 -5
  34. package/src/orm.test.ts +0 -92
  35. package/src/orm.ts +0 -98
  36. package/src/relations/belongsTo.test.ts +0 -1122
  37. package/src/relations/belongsTo.ts +0 -352
  38. package/src/relations/hasAndBelongsToMany.test.ts +0 -1335
  39. package/src/relations/hasAndBelongsToMany.ts +0 -472
  40. package/src/relations/hasMany.test.ts +0 -2616
  41. package/src/relations/hasMany.ts +0 -401
  42. package/src/relations/hasOne.test.ts +0 -1701
  43. package/src/relations/hasOne.ts +0 -351
  44. package/src/relations/relations.test.ts +0 -37
  45. package/src/relations/relations.ts +0 -363
  46. package/src/relations/utils.ts +0 -162
  47. package/src/repo.test.ts +0 -200
  48. package/src/repo.ts +0 -119
  49. package/src/table.test.ts +0 -121
  50. package/src/table.ts +0 -184
  51. package/src/test-utils/test-db.ts +0 -32
  52. package/src/test-utils/test-tables.ts +0 -194
  53. package/src/test-utils/test-utils.ts +0 -119
  54. package/src/transaction.test.ts +0 -47
  55. package/src/transaction.ts +0 -27
  56. package/src/utils.ts +0 -9
  57. package/tsconfig.json +0 -14
@@ -1,31 +0,0 @@
1
- import { AppCodeUpdaterConfig } from './appCodeUpdater';
2
- import fs from 'fs/promises';
3
- import path from 'path';
4
-
5
- type CreateBaseTableFileParams = Pick<
6
- AppCodeUpdaterConfig,
7
- 'baseTablePath' | 'baseTableName'
8
- >;
9
-
10
- export const createBaseTableFile = async ({
11
- baseTableName,
12
- baseTablePath,
13
- }: CreateBaseTableFileParams) => {
14
- await fs.mkdir(path.dirname(baseTablePath), { recursive: true });
15
-
16
- await fs
17
- .writeFile(
18
- baseTablePath,
19
- `import { createBaseTable } from 'orchid-orm';
20
-
21
- export const ${baseTableName} = createBaseTable();
22
- `,
23
- {
24
- flag: 'wx',
25
- },
26
- )
27
- .catch((err) => {
28
- if (err.code === 'EEXIST') return;
29
- throw err;
30
- });
31
- };
@@ -1,41 +0,0 @@
1
- export class FileChanges {
2
- private ranges: ([from: number, to: number] | string)[] = [];
3
-
4
- constructor(public content: string) {}
5
-
6
- add(at: number, text: string, end = at) {
7
- if (this.ranges.length === 0) {
8
- this.ranges.push([0, at], text, [end, this.content.length]);
9
- } else {
10
- const last = this.ranges[this.ranges.length - 1] as [number, number];
11
- last[1] = at;
12
- this.ranges.push(text, [end, this.content.length]);
13
- }
14
- }
15
-
16
- replace(from: number, to: number, text: string) {
17
- this.add(from, text, to);
18
- }
19
-
20
- remove(from: number, to: number) {
21
- if (this.ranges.length === 0) {
22
- this.ranges.push([0, from], [to, this.content.length]);
23
- } else {
24
- const last = this.ranges[this.ranges.length - 1] as [number, number];
25
- last[1] = from;
26
- this.ranges.push([to, this.content.length]);
27
- }
28
- }
29
-
30
- apply() {
31
- return this.ranges.length
32
- ? this.ranges
33
- .map((item) =>
34
- typeof item === 'string'
35
- ? item
36
- : this.content.slice(item[0], item[1]),
37
- )
38
- .join('')
39
- : this.content;
40
- }
41
- }
@@ -1,56 +0,0 @@
1
- import { RakeDbAst } from 'rake-db';
2
- import { columnTypes } from 'pqb';
3
- import path from 'path';
4
- import fs from 'fs/promises';
5
-
6
- export const asMock = (fn: unknown) => fn as jest.Mock;
7
- export const tablePath = (table: string) =>
8
- path.resolve(`tables/${table}.table.ts`);
9
-
10
- const makeAst = () => {
11
- const addTable: RakeDbAst.Table = {
12
- type: 'table',
13
- action: 'create',
14
- name: 'some',
15
- shape: {
16
- id: columnTypes.serial().primaryKey(),
17
- },
18
- noPrimaryKey: 'error',
19
- indexes: [],
20
- foreignKeys: [],
21
- };
22
-
23
- const dropTable: RakeDbAst.Table = {
24
- ...addTable,
25
- action: 'drop',
26
- };
27
-
28
- const renameTable: RakeDbAst.RenameTable = {
29
- type: 'renameTable',
30
- from: 'some',
31
- to: 'another',
32
- };
33
-
34
- const tableData = {
35
- indexes: [],
36
- foreignKeys: [],
37
- };
38
-
39
- const changeTable: RakeDbAst.ChangeTable = {
40
- type: 'changeTable',
41
- name: 'some',
42
- shape: {},
43
- add: tableData,
44
- drop: tableData,
45
- };
46
-
47
- return { addTable, dropTable, renameTable, changeTable };
48
- };
49
-
50
- export const ast = makeAst();
51
-
52
- export const makeTestWritten = (path: string) => (expected: string) => {
53
- const [args] = asMock(fs.writeFile).mock.calls;
54
- expect(args[0]).toBe(path);
55
- expect(args[1]).toBe(expected);
56
- };
@@ -1,180 +0,0 @@
1
- import {
2
- CallExpression,
3
- ClassDeclaration,
4
- ComputedPropertyName,
5
- Expression,
6
- Identifier,
7
- ImportDeclaration,
8
- NamedImports,
9
- Node,
10
- NodeArray,
11
- NumericLiteral,
12
- ObjectLiteralElement,
13
- ObjectLiteralExpression,
14
- PrivateIdentifier,
15
- PropertyAccessExpression,
16
- PropertyAssignment,
17
- ShorthandPropertyAssignment,
18
- Statement,
19
- StringLiteral,
20
- VariableStatement,
21
- ThisExpression,
22
- ArrowFunction,
23
- ParenthesizedExpression,
24
- PropertyName,
25
- SpreadAssignment,
26
- ArrayLiteralExpression,
27
- } from 'typescript';
28
- import typescript from 'typescript';
29
- const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
30
-
31
- const iterate = <T>(
32
- kind: number,
33
- ): ((statements: NodeArray<Statement>) => Generator<T, void, unknown>) => {
34
- return function* (statements: NodeArray<Statement>) {
35
- for (const node of statements) {
36
- if (node.kind === kind) {
37
- yield node as T;
38
- }
39
- }
40
- };
41
- };
42
-
43
- const isNode = <T extends Expression | ObjectLiteralElement | Node>(
44
- kind: number,
45
- ) => {
46
- return (node?: Expression | ObjectLiteralElement | Node): node is T => {
47
- return node?.kind === kind;
48
- };
49
- };
50
-
51
- export const ts = {
52
- getStatements(content: string): NodeArray<Statement> {
53
- const { statements } = createSourceFile(
54
- 'file.ts',
55
- content,
56
- ScriptTarget.Latest,
57
- true,
58
- );
59
- return statements;
60
- },
61
- is: {
62
- call: isNode<CallExpression>(SyntaxKind.CallExpression),
63
- objectLiteral: isNode<ObjectLiteralExpression>(
64
- SyntaxKind.ObjectLiteralExpression,
65
- ),
66
- propertyAssignment: isNode<PropertyAssignment>(
67
- SyntaxKind.PropertyAssignment,
68
- ),
69
- shorthandPropertyAssignment: isNode<ShorthandPropertyAssignment>(
70
- SyntaxKind.ShorthandPropertyAssignment,
71
- ),
72
- identifier: isNode<Identifier>(SyntaxKind.Identifier),
73
- stringLiteral: isNode<StringLiteral>(SyntaxKind.StringLiteral),
74
- arrayLiteral: isNode<ArrayLiteralExpression>(
75
- SyntaxKind.ArrayLiteralExpression,
76
- ),
77
- numericLiteral: isNode<NumericLiteral>(SyntaxKind.NumericLiteral),
78
- computedPropertyName: isNode<ComputedPropertyName>(
79
- SyntaxKind.ComputedPropertyName,
80
- ),
81
- privateIdentifier: isNode<PrivateIdentifier>(SyntaxKind.PrivateIdentifier),
82
- this: isNode<ThisExpression>(SyntaxKind.ThisKeyword),
83
- propertyAccess: isNode<PropertyAccessExpression>(
84
- SyntaxKind.PropertyAccessExpression,
85
- ),
86
- arrowFunction: isNode<ArrowFunction>(SyntaxKind.ArrowFunction),
87
- parenthesizedExpression: isNode<ParenthesizedExpression>(
88
- SyntaxKind.ParenthesizedExpression,
89
- ),
90
- spreadAssignment: isNode<SpreadAssignment>(SyntaxKind.SpreadAssignment),
91
- },
92
- import: {
93
- iterate: iterate<ImportDeclaration>(SyntaxKind.ImportDeclaration),
94
- *iterateWithSource(statements: NodeArray<Statement>, path: string) {
95
- for (const node of ts.import.iterate(statements)) {
96
- if (ts.import.getSource(node) !== path) continue;
97
- yield node;
98
- }
99
- },
100
- getSource(node: ImportDeclaration) {
101
- return node.moduleSpecifier.getText().slice(1, -1);
102
- },
103
- getEndPos(statements: NodeArray<Statement>) {
104
- let end = 0;
105
- for (const node of ts.import.iterate(statements)) {
106
- end = node.end;
107
- }
108
- return end;
109
- },
110
- getStatementsImportedName(
111
- statements: NodeArray<Statement>,
112
- path: string,
113
- key: string,
114
- ) {
115
- for (const node of ts.import.iterateWithSource(statements, path)) {
116
- const name = ts.import.getImportName(node, key);
117
- if (name) return name;
118
- }
119
-
120
- return;
121
- },
122
- getImportName(node: ImportDeclaration, key: string) {
123
- if (!node.importClause) return;
124
-
125
- const elements = (node.importClause.namedBindings as NamedImports)
126
- ?.elements;
127
-
128
- if (!elements) return;
129
-
130
- for (const element of elements) {
131
- if (
132
- element.propertyName?.escapedText === key ||
133
- element.name.escapedText === key
134
- ) {
135
- return element.name.escapedText.toString();
136
- }
137
- }
138
-
139
- return;
140
- },
141
- },
142
- variable: {
143
- iterate: iterate<VariableStatement>(SyntaxKind.VariableStatement),
144
- *iterateDeclarations(statements: NodeArray<Statement>) {
145
- for (const node of ts.variable.iterate(statements)) {
146
- for (const dec of node.declarationList.declarations) {
147
- yield dec;
148
- }
149
- }
150
- },
151
- },
152
- class: {
153
- iterate: iterate<ClassDeclaration>(SyntaxKind.ClassDeclaration),
154
- },
155
- prop: {
156
- getName({ name }: { name?: PropertyName }) {
157
- if (ts.is.identifier(name)) {
158
- return name.escapedText;
159
- } else {
160
- return name?.getText();
161
- }
162
- },
163
- getValue(prop: ObjectLiteralElement) {
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: string, pos: number) {
175
- const lines = content.slice(0, pos).split('\n');
176
- const last = lines[lines.length - 1];
177
- return last.match(/^\s+/)?.[0] || '';
178
- },
179
- },
180
- };
@@ -1,253 +0,0 @@
1
- import { updateMainFile } from './updateMainFile';
2
- import * as path from 'path';
3
- import * as fs from 'fs/promises';
4
- import { asMock, ast, makeTestWritten, tablePath } from './testUtils';
5
-
6
- jest.mock('fs/promises', () => ({
7
- readFile: jest.fn(),
8
- writeFile: jest.fn(),
9
- mkdir: jest.fn(),
10
- }));
11
-
12
- const mainFilePath = path.resolve('db.ts');
13
- const testWritten = makeTestWritten(mainFilePath);
14
- const options = { databaseURL: 'url' };
15
-
16
- describe('updateMainFile', () => {
17
- beforeEach(() => {
18
- jest.resetAllMocks();
19
- });
20
-
21
- describe('add table', () => {
22
- it('should create file if not exist and add a table', async () => {
23
- asMock(fs.readFile).mockRejectedValue(
24
- Object.assign(new Error(), { code: 'ENOENT' }),
25
- );
26
-
27
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
28
-
29
- expect(asMock(fs.mkdir)).toBeCalledWith(path.dirname(mainFilePath), {
30
- recursive: true,
31
- });
32
-
33
- testWritten(`import { orchidORM } from 'orchid-orm';
34
- import { SomeTable } from './tables/some.table';
35
-
36
- export const db = orchidORM(
37
- {
38
- databaseURL: 'url',
39
- },
40
- {
41
- some: SomeTable,
42
- }
43
- );
44
- `);
45
- });
46
-
47
- it('should add table', async () => {
48
- asMock(fs.readFile).mockResolvedValue(`
49
- import { orchidORM } from 'orchid-orm';
50
-
51
- export const db = orchidORM({}, {});
52
- `);
53
-
54
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
55
-
56
- testWritten(`
57
- import { orchidORM } from 'orchid-orm';
58
- import { SomeTable } from './tables/some.table';
59
-
60
- export const db = orchidORM({}, {
61
- some: SomeTable,
62
- });
63
- `);
64
- });
65
-
66
- it('should handle import as', async () => {
67
- asMock(fs.readFile).mockResolvedValue(`
68
- import { orchidORM as custom } from 'orchid-orm';
69
-
70
- export const db = custom({}, {});
71
- `);
72
-
73
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
74
-
75
- testWritten(`
76
- import { orchidORM as custom } from 'orchid-orm';
77
- import { SomeTable } from './tables/some.table';
78
-
79
- export const db = custom({}, {
80
- some: SomeTable,
81
- });
82
- `);
83
- });
84
-
85
- it('should handle object list with elements', async () => {
86
- asMock(fs.readFile).mockResolvedValue(`
87
- import { orchidORM } from 'orchid-orm';
88
- import { Other } from './tables/other';
89
-
90
- export const db = orchidORM({}, {
91
- other: Other,
92
- });
93
- `);
94
-
95
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
96
-
97
- testWritten(`
98
- import { orchidORM } from 'orchid-orm';
99
- import { Other } from './tables/other';
100
- import { SomeTable } from './tables/some.table';
101
-
102
- export const db = orchidORM({}, {
103
- other: Other,
104
- some: SomeTable,
105
- });
106
- `);
107
- });
108
-
109
- it('should handle object list without ending coma', async () => {
110
- asMock(fs.readFile).mockResolvedValue(`
111
- import { orchidORM } from 'orchid-orm';
112
- import { MyTable } from './tables/my.table';
113
-
114
- export const db = orchidORM({}, {
115
- my: MyTable,
116
- });
117
- `);
118
-
119
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
120
-
121
- testWritten(`
122
- import { orchidORM } from 'orchid-orm';
123
- import { MyTable } from './tables/my.table';
124
- import { SomeTable } from './tables/some.table';
125
-
126
- export const db = orchidORM({}, {
127
- my: MyTable,
128
- some: SomeTable,
129
- });
130
- `);
131
- });
132
-
133
- it('should not add table if it is already added', async () => {
134
- asMock(fs.readFile).mockResolvedValue(`
135
- import { orchidORM } from 'orchid-orm';
136
- import { SomeTable } from './tables/some.table';
137
-
138
- export const db = orchidORM({}, {
139
- some: SomeTable
140
- });
141
- `);
142
-
143
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
144
-
145
- expect(fs.writeFile).not.toBeCalled();
146
- });
147
- });
148
-
149
- describe('drop table', () => {
150
- it('should remove table', async () => {
151
- asMock(fs.readFile).mockResolvedValue(`
152
- import { orchidORM } from 'orchid-orm';
153
- import { SomeTable } from './tables/some.table';
154
-
155
- export const db = orchidORM({}, {
156
- some: SomeTable,
157
- });
158
- `);
159
-
160
- await updateMainFile(mainFilePath, tablePath, ast.dropTable, options);
161
-
162
- testWritten(`
163
- import { orchidORM } from 'orchid-orm';
164
-
165
- export const db = orchidORM({}, {
166
- });
167
- `);
168
- });
169
-
170
- it('should remove aliased import', async () => {
171
- asMock(fs.readFile).mockResolvedValue(`
172
- import { orchidORM } from 'orchid-orm';
173
- import { SomeTable as koko } from './tables/some.table';
174
-
175
- export const db = orchidORM({}, {
176
- koko: koko,
177
- });
178
- `);
179
-
180
- await updateMainFile(mainFilePath, tablePath, ast.dropTable, options);
181
-
182
- testWritten(`
183
- import { orchidORM } from 'orchid-orm';
184
-
185
- export const db = orchidORM({}, {
186
- });
187
- `);
188
- });
189
-
190
- it('should remove short form of key and value', async () => {
191
- asMock(fs.readFile).mockResolvedValue(`
192
- import { orchidORM } from 'orchid-orm';
193
- import { SomeTable as koko } from './tables/some.table';
194
-
195
- export const db = orchidORM({}, {
196
- koko,
197
- });
198
- `);
199
-
200
- await updateMainFile(mainFilePath, tablePath, ast.dropTable, options);
201
-
202
- testWritten(`
203
- import { orchidORM } from 'orchid-orm';
204
-
205
- export const db = orchidORM({}, {
206
- });
207
- `);
208
- });
209
-
210
- it('should not remove other tables', async () => {
211
- asMock(fs.readFile).mockResolvedValue(`
212
- import { orchidORM } from 'orchid-orm';
213
- import { One } from './tables/one';
214
- import { SomeTable } from './tables/some.table';
215
- import { Two } from './tables/two';
216
-
217
- export const db = orchidORM({}, {
218
- one,
219
- some: SomeTable,
220
- two,
221
- });
222
- `);
223
-
224
- await updateMainFile(mainFilePath, tablePath, ast.dropTable, options);
225
-
226
- testWritten(`
227
- import { orchidORM } from 'orchid-orm';
228
- import { One } from './tables/one';
229
- import { Two } from './tables/two';
230
-
231
- export const db = orchidORM({}, {
232
- one,
233
- two,
234
- });
235
- `);
236
- });
237
-
238
- it('should not insert table if table with same key exists, disregarding the import path', async () => {
239
- asMock(fs.readFile).mockResolvedValue(`
240
- import { orchidORM } from 'orchid-orm';
241
- import { X } from './x';
242
-
243
- export const db = orchidORM({}, {
244
- some: X,
245
- });
246
- `);
247
-
248
- await updateMainFile(mainFilePath, tablePath, ast.addTable, options);
249
-
250
- expect(fs.writeFile).not.toBeCalled();
251
- });
252
- });
253
- });