orchid-orm 1.4.21 → 1.4.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orchid-orm",
3
- "version": "1.4.21",
3
+ "version": "1.4.22",
4
4
  "description": "Postgres ORM",
5
5
  "homepage": "https://orchid-orm.netlify.app/guide/orm-setup-and-overview.html",
6
6
  "repository": {
@@ -31,14 +31,14 @@
31
31
  "author": "Roman Kushyn",
32
32
  "license": "ISC",
33
33
  "dependencies": {
34
- "pqb": "0.8.4"
34
+ "pqb": "0.8.5"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@swc/core": "^1.3.19",
38
38
  "rollup": "^2.79.0",
39
39
  "rollup-plugin-dts": "^4.2.2",
40
40
  "rollup-plugin-esbuild": "^4.10.1",
41
- "orchid-orm-schema-to-zod": "0.2.4",
41
+ "orchid-orm-schema-to-zod": "0.2.5",
42
42
  "@swc/jest": "^0.2.21",
43
43
  "@types/jest": "^28.1.2",
44
44
  "@types/node": "^18.0.1",
@@ -50,7 +50,7 @@
50
50
  "rimraf": "^3.0.2",
51
51
  "tslib": "^2.4.0",
52
52
  "typescript": "^4.7.4",
53
- "rake-db": "2.2.4"
53
+ "rake-db": "2.2.6"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "typescript": "*"
@@ -15,11 +15,53 @@ const params = { baseTablePath, baseTableName, tablePath };
15
15
 
16
16
  const testWritten = makeTestWritten(tablePath('table'));
17
17
 
18
+ const template = ({
19
+ schema,
20
+ columns,
21
+ noPrimaryKey,
22
+ }: {
23
+ schema?: string;
24
+ columns: string;
25
+ noPrimaryKey?: boolean;
26
+ }) => `import { BaseTable } from '../baseTable';
27
+
28
+ export class Table extends BaseTable {
29
+ ${schema ? `schema = '${schema}';\n ` : ''}table = 'table';${
30
+ noPrimaryKey ? '\n noPrimaryKey = true;' : ''
31
+ }
32
+ columns = this.setColumns((t) => (${columns}));
33
+ }
34
+ `;
35
+
18
36
  describe('createTable', () => {
19
37
  beforeEach(() => {
20
38
  jest.resetAllMocks();
21
39
  });
22
40
 
41
+ it('should add table', async () => {
42
+ await updateTableFile({
43
+ ...params,
44
+ ast: {
45
+ ...ast.addTable,
46
+ schema: 'schema',
47
+ shape: {},
48
+ },
49
+ });
50
+
51
+ expect(asMock(fs.mkdir)).toBeCalledWith(path.dirname(tablePath('table')), {
52
+ recursive: true,
53
+ });
54
+
55
+ testWritten(
56
+ template({
57
+ schema: 'schema',
58
+ columns: `{
59
+
60
+ }`,
61
+ }),
62
+ );
63
+ });
64
+
23
65
  it('should add table', async () => {
24
66
  await updateTableFile({
25
67
  ...params,
@@ -46,15 +88,9 @@ describe('createTable', () => {
46
88
  },
47
89
  });
48
90
 
49
- expect(asMock(fs.mkdir)).toBeCalledWith(path.dirname(tablePath('table')), {
50
- recursive: true,
51
- });
52
-
53
- testWritten(`import { BaseTable } from '../baseTable';
54
-
55
- export class Table extends BaseTable {
56
- table = 'table';
57
- columns = this.setColumns((t) => ({
91
+ testWritten(
92
+ template({
93
+ columns: `{
58
94
  id: t.serial().primaryKey(),
59
95
  ...t.primaryKey(['one', 'two'], { name: 'name' }),
60
96
  ...t.index(['one', 'two'], {
@@ -69,9 +105,9 @@ export class Table extends BaseTable {
69
105
  name: 'foreignKeyName',
70
106
  },
71
107
  ),
72
- }));
73
- }
74
- `);
108
+ }`,
109
+ }),
110
+ );
75
111
  });
76
112
 
77
113
  it('should add noPrimaryKey prop when noPrimaryKey is `ignore` in ast', async () => {
@@ -80,15 +116,13 @@ export class Table extends BaseTable {
80
116
  ast: { ...ast.addTable, noPrimaryKey: 'ignore' },
81
117
  });
82
118
 
83
- testWritten(`import { BaseTable } from '../baseTable';
84
-
85
- export class Table extends BaseTable {
86
- table = 'table';
87
- noPrimaryKey = true;
88
- columns = this.setColumns((t) => ({
119
+ testWritten(
120
+ template({
121
+ noPrimaryKey: true,
122
+ columns: `{
89
123
  id: t.serial().primaryKey(),
90
- }));
91
- }
92
- `);
124
+ }`,
125
+ }),
126
+ );
93
127
  });
94
128
  });
@@ -13,7 +13,14 @@ export const createTable = async ({
13
13
  const tablePath = params.tablePath(ast.name);
14
14
  const baseTablePath = getImportPath(tablePath, params.baseTablePath);
15
15
 
16
- const props: Code[] = [`table = ${singleQuote(ast.name)};`];
16
+ const props: Code[] = [];
17
+
18
+ if (ast.schema) {
19
+ props.push(`schema = ${singleQuote(ast.schema)};`);
20
+ }
21
+
22
+ props.push(`table = ${singleQuote(ast.name)};`);
23
+
17
24
  if (ast.noPrimaryKey === 'ignore') {
18
25
  props.push('noPrimaryKey = true;');
19
26
  }
@@ -20,7 +20,7 @@ describe('renameTable', () => {
20
20
  jest.resetAllMocks();
21
21
  });
22
22
 
23
- it('should only change `table` property', async () => {
23
+ it('should change `table` property', async () => {
24
24
  asMock(fs.readFile)
25
25
  .mockResolvedValue(`import { BaseTable } from '../baseTable';
26
26
 
@@ -39,6 +39,86 @@ export class Table extends BaseTable {
39
39
  export class Table extends BaseTable {
40
40
  table = 'renamedTable';
41
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 Table extends BaseTable {
50
+ schema = 'one';
51
+ table = 'table';
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 Table extends BaseTable {
67
+ schema = 'two';
68
+ table = 'renamedTable';
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 Table extends BaseTable {
78
+ schema = 'one';
79
+ table = 'table';
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 Table extends BaseTable {
94
+ table = 'renamedTable';
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 Table extends BaseTable {
104
+ table = 'table';
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 Table extends BaseTable {
119
+ schema = 'schema';
120
+ table = 'renamedTable';
121
+ columns = this.setColumns((t) => ({}));
42
122
  }`);
43
123
  });
44
124
  });
@@ -19,12 +19,27 @@ export const renameTable = async ({
19
19
  const statements = ts.getStatements(content);
20
20
  const className = toPascalCase(ast.from);
21
21
 
22
+ const changeSchema = ast.fromSchema !== ast.toSchema;
23
+
22
24
  for (const node of ts.class.iterate(statements)) {
23
25
  if (node.name?.escapedText !== className) continue;
24
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
+
25
39
  for (const member of node.members) {
26
40
  const name = ts.prop.getName(member);
27
- if (name !== 'table') continue;
41
+
42
+ if (name !== 'table' && !(changeSchema && name === 'schema')) continue;
28
43
 
29
44
  const { initializer: value } = member as unknown as {
30
45
  initializer?: Expression;
@@ -32,7 +47,19 @@ export const renameTable = async ({
32
47
 
33
48
  if (!value) continue;
34
49
 
35
- changes.replace(value.pos, value.end, ` ${singleQuote(ast.to)}`);
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
+ }
36
63
  }
37
64
  }
38
65