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.
- package/dist/index.d.ts +6 -14
- package/dist/index.js +26 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -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,124 +0,0 @@
|
|
|
1
|
-
import { updateTableFile } from './updateTableFile';
|
|
2
|
-
import { asMock, ast, makeTestWritten, tablePath } from '../testUtils';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import fs from 'fs/promises';
|
|
5
|
-
|
|
6
|
-
jest.mock('fs/promises', () => ({
|
|
7
|
-
mkdir: jest.fn(),
|
|
8
|
-
readFile: jest.fn(),
|
|
9
|
-
writeFile: jest.fn(),
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
const baseTablePath = path.resolve('baseTable.ts');
|
|
13
|
-
const baseTableName = 'BaseTable';
|
|
14
|
-
const params = { baseTablePath, baseTableName, tablePath };
|
|
15
|
-
|
|
16
|
-
const testWritten = makeTestWritten(tablePath('some'));
|
|
17
|
-
|
|
18
|
-
describe('renameTable', () => {
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
jest.resetAllMocks();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should change `table` property', async () => {
|
|
24
|
-
asMock(fs.readFile)
|
|
25
|
-
.mockResolvedValue(`import { BaseTable } from '../baseTable';
|
|
26
|
-
|
|
27
|
-
export class SomeTable extends BaseTable {
|
|
28
|
-
table = 'some';
|
|
29
|
-
columns = this.setColumns((t) => ({}));
|
|
30
|
-
}`);
|
|
31
|
-
|
|
32
|
-
await updateTableFile({
|
|
33
|
-
...params,
|
|
34
|
-
ast: ast.renameTable,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
testWritten(`import { BaseTable } from '../baseTable';
|
|
38
|
-
|
|
39
|
-
export class SomeTable extends BaseTable {
|
|
40
|
-
table = 'another';
|
|
41
|
-
columns = this.setColumns((t) => ({}));
|
|
42
|
-
}`);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should change `schema` property', async () => {
|
|
46
|
-
asMock(fs.readFile)
|
|
47
|
-
.mockResolvedValue(`import { BaseTable } from '../baseTable';
|
|
48
|
-
|
|
49
|
-
export class SomeTable extends BaseTable {
|
|
50
|
-
schema = 'one';
|
|
51
|
-
table = 'some';
|
|
52
|
-
columns = this.setColumns((t) => ({}));
|
|
53
|
-
}`);
|
|
54
|
-
|
|
55
|
-
await updateTableFile({
|
|
56
|
-
...params,
|
|
57
|
-
ast: {
|
|
58
|
-
...ast.renameTable,
|
|
59
|
-
fromSchema: 'one',
|
|
60
|
-
toSchema: 'two',
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
testWritten(`import { BaseTable } from '../baseTable';
|
|
65
|
-
|
|
66
|
-
export class SomeTable extends BaseTable {
|
|
67
|
-
schema = 'two';
|
|
68
|
-
table = 'another';
|
|
69
|
-
columns = this.setColumns((t) => ({}));
|
|
70
|
-
}`);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should remove `schema` property', async () => {
|
|
74
|
-
asMock(fs.readFile)
|
|
75
|
-
.mockResolvedValue(`import { BaseTable } from '../baseTable';
|
|
76
|
-
|
|
77
|
-
export class SomeTable extends BaseTable {
|
|
78
|
-
schema = 'one';
|
|
79
|
-
table = 'some';
|
|
80
|
-
columns = this.setColumns((t) => ({}));
|
|
81
|
-
}`);
|
|
82
|
-
|
|
83
|
-
await updateTableFile({
|
|
84
|
-
...params,
|
|
85
|
-
ast: {
|
|
86
|
-
...ast.renameTable,
|
|
87
|
-
fromSchema: 'one',
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
testWritten(`import { BaseTable } from '../baseTable';
|
|
92
|
-
|
|
93
|
-
export class SomeTable extends BaseTable {
|
|
94
|
-
table = 'another';
|
|
95
|
-
columns = this.setColumns((t) => ({}));
|
|
96
|
-
}`);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('should add `schema` property', async () => {
|
|
100
|
-
asMock(fs.readFile)
|
|
101
|
-
.mockResolvedValue(`import { BaseTable } from '../baseTable';
|
|
102
|
-
|
|
103
|
-
export class SomeTable extends BaseTable {
|
|
104
|
-
table = 'some';
|
|
105
|
-
columns = this.setColumns((t) => ({}));
|
|
106
|
-
}`);
|
|
107
|
-
|
|
108
|
-
await updateTableFile({
|
|
109
|
-
...params,
|
|
110
|
-
ast: {
|
|
111
|
-
...ast.renameTable,
|
|
112
|
-
toSchema: 'schema',
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
testWritten(`import { BaseTable } from '../baseTable';
|
|
117
|
-
|
|
118
|
-
export class SomeTable extends BaseTable {
|
|
119
|
-
schema = 'schema';
|
|
120
|
-
table = 'another';
|
|
121
|
-
columns = this.setColumns((t) => ({}));
|
|
122
|
-
}`);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { UpdateTableFileParams } from './updateTableFile';
|
|
2
|
-
import { RakeDbAst } from 'rake-db';
|
|
3
|
-
import fs from 'fs/promises';
|
|
4
|
-
import { FileChanges } from '../fileChanges';
|
|
5
|
-
import { ts } from '../tsUtils';
|
|
6
|
-
import { toPascalCase } from '../../utils';
|
|
7
|
-
import { Expression } from 'typescript';
|
|
8
|
-
import { singleQuote } from 'pqb';
|
|
9
|
-
|
|
10
|
-
export const renameTable = async ({
|
|
11
|
-
ast,
|
|
12
|
-
...params
|
|
13
|
-
}: UpdateTableFileParams & { ast: RakeDbAst.RenameTable }) => {
|
|
14
|
-
const tablePath = params.tablePath(ast.from);
|
|
15
|
-
const content = await fs.readFile(tablePath, 'utf-8').catch(() => undefined);
|
|
16
|
-
if (!content) return;
|
|
17
|
-
|
|
18
|
-
const changes = new FileChanges(content);
|
|
19
|
-
const statements = ts.getStatements(content);
|
|
20
|
-
const className = toPascalCase(ast.from) + 'Table';
|
|
21
|
-
|
|
22
|
-
const changeSchema = ast.fromSchema !== ast.toSchema;
|
|
23
|
-
|
|
24
|
-
for (const node of ts.class.iterate(statements)) {
|
|
25
|
-
if (node.name?.escapedText !== className) continue;
|
|
26
|
-
|
|
27
|
-
const addSchema =
|
|
28
|
-
changeSchema &&
|
|
29
|
-
ast.toSchema &&
|
|
30
|
-
!node.members.some((member) => ts.prop.getName(member) === 'schema');
|
|
31
|
-
|
|
32
|
-
if (addSchema && ast.toSchema) {
|
|
33
|
-
changes.add(
|
|
34
|
-
node.members.pos,
|
|
35
|
-
`\n schema = ${singleQuote(ast.toSchema)};`,
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
for (const member of node.members) {
|
|
40
|
-
const name = ts.prop.getName(member);
|
|
41
|
-
|
|
42
|
-
if (name !== 'table' && !(changeSchema && name === 'schema')) continue;
|
|
43
|
-
|
|
44
|
-
const { initializer: value } = member as unknown as {
|
|
45
|
-
initializer?: Expression;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
if (!value) continue;
|
|
49
|
-
|
|
50
|
-
if (name === 'schema') {
|
|
51
|
-
if (ast.toSchema) {
|
|
52
|
-
changes.replace(
|
|
53
|
-
value.pos,
|
|
54
|
-
value.end,
|
|
55
|
-
` ${singleQuote(ast.toSchema)}`,
|
|
56
|
-
);
|
|
57
|
-
} else {
|
|
58
|
-
changes.remove(member.pos, member.end);
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
changes.replace(value.pos, value.end, ` ${singleQuote(ast.to)}`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
await fs.writeFile(tablePath, changes.apply());
|
|
67
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { RakeDbAst } from 'rake-db';
|
|
2
|
-
import { createTable } from './createTable';
|
|
3
|
-
import { changeTable } from './changeTable';
|
|
4
|
-
import { renameTable } from './renameTable';
|
|
5
|
-
|
|
6
|
-
export type UpdateTableFileParams = {
|
|
7
|
-
baseTablePath: string;
|
|
8
|
-
baseTableName: string;
|
|
9
|
-
tablePath: (name: string) => string;
|
|
10
|
-
ast: RakeDbAst;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const updateTableFile = async (params: UpdateTableFileParams) => {
|
|
14
|
-
const { ast } = params;
|
|
15
|
-
if (ast.type === 'table' && ast.action === 'create') {
|
|
16
|
-
await createTable({ ...params, ast });
|
|
17
|
-
} else if (ast.type === 'changeTable') {
|
|
18
|
-
await changeTable({ ...params, ast });
|
|
19
|
-
} else if (ast.type === 'renameTable') {
|
|
20
|
-
await renameTable({ ...params, ast });
|
|
21
|
-
}
|
|
22
|
-
};
|
package/src/codegen/utils.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
|
|
3
|
-
export const getImportPath = (from: string, to: string) => {
|
|
4
|
-
const rel = path
|
|
5
|
-
.relative(path.dirname(from), to)
|
|
6
|
-
.split(path.sep)
|
|
7
|
-
.join(path.posix.sep);
|
|
8
|
-
|
|
9
|
-
const importPath =
|
|
10
|
-
rel.startsWith('./') || rel.startsWith('../') ? rel : `./${rel}`;
|
|
11
|
-
|
|
12
|
-
return importPath.replace(/\.[tj]s$/, '');
|
|
13
|
-
};
|
package/src/index.ts
DELETED
package/src/orm.test.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { OrchidORM, orchidORM } from './orm';
|
|
2
|
-
import {
|
|
3
|
-
assertType,
|
|
4
|
-
expectSql,
|
|
5
|
-
userData,
|
|
6
|
-
useTestDatabase,
|
|
7
|
-
} from './test-utils/test-utils';
|
|
8
|
-
import { pgConfig } from './test-utils/test-db';
|
|
9
|
-
import { createBaseTable } from './table';
|
|
10
|
-
|
|
11
|
-
describe('orm', () => {
|
|
12
|
-
useTestDatabase();
|
|
13
|
-
|
|
14
|
-
let db:
|
|
15
|
-
| OrchidORM<{ user: typeof UserTable; profile: typeof ProfileTable }>
|
|
16
|
-
| undefined;
|
|
17
|
-
|
|
18
|
-
afterEach(async () => {
|
|
19
|
-
if (db) await db.$close();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const BaseTable = createBaseTable();
|
|
23
|
-
|
|
24
|
-
type User = UserTable['columns']['type'];
|
|
25
|
-
class UserTable extends BaseTable {
|
|
26
|
-
table = 'user';
|
|
27
|
-
columns = this.setColumns((t) => ({
|
|
28
|
-
id: t.serial().primaryKey(),
|
|
29
|
-
name: t.text(1, 10),
|
|
30
|
-
password: t.text(1, 10),
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
class ProfileTable extends BaseTable {
|
|
35
|
-
table = 'profile';
|
|
36
|
-
columns = this.setColumns((t) => ({
|
|
37
|
-
id: t.serial().primaryKey(),
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
it('should return object with provided adapter, close and transaction method, tables', () => {
|
|
42
|
-
db = orchidORM(pgConfig, {
|
|
43
|
-
user: UserTable,
|
|
44
|
-
profile: ProfileTable,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
expect('$adapter' in db).toBe(true);
|
|
48
|
-
expect(db.$close).toBeInstanceOf(Function);
|
|
49
|
-
expect(db.$transaction).toBeInstanceOf(Function);
|
|
50
|
-
expect(Object.keys(db)).toEqual(
|
|
51
|
-
expect.arrayContaining(['user', 'profile']),
|
|
52
|
-
);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should return table which is a queryable interface', async () => {
|
|
56
|
-
db = orchidORM(pgConfig, {
|
|
57
|
-
user: UserTable,
|
|
58
|
-
profile: ProfileTable,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
const { id, name } = await db.user.create(userData);
|
|
62
|
-
|
|
63
|
-
const query = db.user.select('id', 'name').where({ id: { gt: 0 } });
|
|
64
|
-
|
|
65
|
-
expectSql(
|
|
66
|
-
query.toSql(),
|
|
67
|
-
`
|
|
68
|
-
SELECT "user"."id", "user"."name"
|
|
69
|
-
FROM "user"
|
|
70
|
-
WHERE "user"."id" > $1
|
|
71
|
-
`,
|
|
72
|
-
[0],
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const result = await query;
|
|
76
|
-
expect(result).toEqual([{ id, name }]);
|
|
77
|
-
|
|
78
|
-
assertType<typeof result, Pick<User, 'id' | 'name'>[]>();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should be able to turn on autoPreparedStatements', () => {
|
|
82
|
-
db = orchidORM(
|
|
83
|
-
{ ...pgConfig, autoPreparedStatements: true },
|
|
84
|
-
{
|
|
85
|
-
user: UserTable,
|
|
86
|
-
profile: ProfileTable,
|
|
87
|
-
},
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
expect(db.user.query.autoPreparedStatements).toBe(true);
|
|
91
|
-
});
|
|
92
|
-
});
|
package/src/orm.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Adapter,
|
|
3
|
-
Db,
|
|
4
|
-
AdapterOptions,
|
|
5
|
-
QueryLogOptions,
|
|
6
|
-
columnTypes,
|
|
7
|
-
NoPrimaryKeyOption,
|
|
8
|
-
anyShape,
|
|
9
|
-
DbTableOptions,
|
|
10
|
-
} from 'pqb';
|
|
11
|
-
import { DbTable, Table, TableClasses } from './table';
|
|
12
|
-
import { applyRelations } from './relations/relations';
|
|
13
|
-
import { transaction } from './transaction';
|
|
14
|
-
|
|
15
|
-
export type OrchidORM<T extends TableClasses> = {
|
|
16
|
-
[K in keyof T]: DbTable<T[K]>;
|
|
17
|
-
} & {
|
|
18
|
-
$transaction: typeof transaction;
|
|
19
|
-
$adapter: Adapter;
|
|
20
|
-
$queryBuilder: Db;
|
|
21
|
-
$close(): Promise<void>;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const orchidORM = <T extends TableClasses>(
|
|
25
|
-
{
|
|
26
|
-
log,
|
|
27
|
-
logger,
|
|
28
|
-
autoPreparedStatements,
|
|
29
|
-
noPrimaryKey = 'error',
|
|
30
|
-
...options
|
|
31
|
-
}: ({ adapter: Adapter } | Omit<AdapterOptions, 'log'>) &
|
|
32
|
-
QueryLogOptions & {
|
|
33
|
-
autoPreparedStatements?: boolean;
|
|
34
|
-
noPrimaryKey?: NoPrimaryKeyOption;
|
|
35
|
-
},
|
|
36
|
-
tables: T,
|
|
37
|
-
): OrchidORM<T> => {
|
|
38
|
-
const adapter = 'adapter' in options ? options.adapter : new Adapter(options);
|
|
39
|
-
const commonOptions = {
|
|
40
|
-
log,
|
|
41
|
-
logger,
|
|
42
|
-
autoPreparedStatements,
|
|
43
|
-
noPrimaryKey,
|
|
44
|
-
};
|
|
45
|
-
const qb = new Db(
|
|
46
|
-
adapter,
|
|
47
|
-
undefined as unknown as Db,
|
|
48
|
-
undefined,
|
|
49
|
-
anyShape,
|
|
50
|
-
columnTypes,
|
|
51
|
-
commonOptions,
|
|
52
|
-
);
|
|
53
|
-
qb.queryBuilder = qb as unknown as Db;
|
|
54
|
-
|
|
55
|
-
const result = {
|
|
56
|
-
$transaction: transaction,
|
|
57
|
-
$adapter: adapter,
|
|
58
|
-
$queryBuilder: qb,
|
|
59
|
-
$close: () => adapter.close(),
|
|
60
|
-
} as unknown as OrchidORM<TableClasses>;
|
|
61
|
-
|
|
62
|
-
const tableInstances: Record<string, Table> = {};
|
|
63
|
-
|
|
64
|
-
for (const key in tables) {
|
|
65
|
-
if (key[0] === '$') {
|
|
66
|
-
throw new Error(`Table class name must not start with $`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const table = new tables[key]();
|
|
70
|
-
tableInstances[key] = table;
|
|
71
|
-
|
|
72
|
-
const options: DbTableOptions = {
|
|
73
|
-
...commonOptions,
|
|
74
|
-
schema: table.schema,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
if (table.noPrimaryKey) options.noPrimaryKey = 'ignore';
|
|
78
|
-
|
|
79
|
-
const dbTable = new Db(
|
|
80
|
-
adapter,
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
-
qb as any,
|
|
83
|
-
table.table,
|
|
84
|
-
table.columns.shape,
|
|
85
|
-
table.columnTypes,
|
|
86
|
-
options,
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
(dbTable as unknown as { definedAs: string }).definedAs = key;
|
|
90
|
-
(dbTable as unknown as { db: unknown }).db = result;
|
|
91
|
-
|
|
92
|
-
(result as Record<string, unknown>)[key] = dbTable;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
applyRelations(qb, tableInstances, result);
|
|
96
|
-
|
|
97
|
-
return result as unknown as OrchidORM<T>;
|
|
98
|
-
};
|