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,194 +0,0 @@
1
- import { createBaseTable } from '../table';
2
- import { tableToZod } from 'orchid-orm-schema-to-zod';
3
-
4
- export const BaseTable = createBaseTable({
5
- columnTypes: (t) => ({
6
- ...t,
7
- text: (min = 0, max = Infinity) => t.text(min, max),
8
- timestamp() {
9
- return t.timestamp().parse((input) => new Date(input));
10
- },
11
- }),
12
- });
13
-
14
- export type User = UserTable['columns']['type'];
15
- export class UserTable extends BaseTable {
16
- table = 'user';
17
- columns = this.setColumns((t) => ({
18
- id: t.serial().primaryKey(),
19
- name: t.text(),
20
- password: t.text(),
21
- picture: t.text().nullable(),
22
- data: t
23
- .json((j) =>
24
- j.object({
25
- name: j.string(),
26
- tags: j.string().array(),
27
- }),
28
- )
29
- .nullable(),
30
- age: t.integer().nullable(),
31
- active: t.boolean().nullable(),
32
- ...t.timestamps(),
33
- }));
34
-
35
- relations = {
36
- profile: this.hasOne(() => ProfileTable, {
37
- required: true,
38
- primaryKey: 'id',
39
- foreignKey: 'userId',
40
- }),
41
-
42
- messages: this.hasMany(() => MessageTable, {
43
- primaryKey: 'id',
44
- foreignKey: 'authorId',
45
- }),
46
-
47
- chats: this.hasAndBelongsToMany(() => ChatTable, {
48
- primaryKey: 'id',
49
- foreignKey: 'userId',
50
- associationPrimaryKey: 'id',
51
- associationForeignKey: 'chatId',
52
- joinTable: 'chatUser',
53
- }),
54
- };
55
- }
56
- export const UserSchema = tableToZod(UserTable);
57
-
58
- export type Profile = ProfileTable['columns']['type'];
59
- export class ProfileTable extends BaseTable {
60
- table = 'profile';
61
- columns = this.setColumns((t) => ({
62
- id: t.serial().primaryKey(),
63
- userId: t
64
- .integer()
65
- .nullable()
66
- .foreignKey(() => UserTable, 'id'),
67
- bio: t.text().nullable(),
68
- ...t.timestamps(),
69
- }));
70
-
71
- relations = {
72
- user: this.belongsTo(() => UserTable, {
73
- required: true,
74
- primaryKey: 'id',
75
- foreignKey: 'userId',
76
- }),
77
-
78
- chats: this.hasMany(() => ChatTable, {
79
- through: 'user',
80
- source: 'chats',
81
- }),
82
- };
83
- }
84
- export const ProfileSchema = tableToZod(ProfileTable);
85
-
86
- export type Chat = ChatTable['columns']['type'];
87
- export class ChatTable extends BaseTable {
88
- table = 'chat';
89
- columns = this.setColumns((t) => ({
90
- id: t.serial().primaryKey(),
91
- title: t.text(),
92
- ...t.timestamps(),
93
- }));
94
-
95
- relations = {
96
- users: this.hasAndBelongsToMany(() => UserTable, {
97
- primaryKey: 'id',
98
- foreignKey: 'chatId',
99
- associationPrimaryKey: 'id',
100
- associationForeignKey: 'userId',
101
- joinTable: 'chatUser',
102
- }),
103
-
104
- profiles: this.hasMany(() => ProfileTable, {
105
- through: 'users',
106
- source: 'profile',
107
- }),
108
-
109
- messages: this.hasMany(() => MessageTable, {
110
- primaryKey: 'id',
111
- foreignKey: 'chatId',
112
- }),
113
- };
114
- }
115
- export const ChatSchema = tableToZod(ChatTable);
116
-
117
- export type Message = MessageTable['columns']['type'];
118
- export class MessageTable extends BaseTable {
119
- table = 'message';
120
- columns = this.setColumns((t) => ({
121
- id: t.serial().primaryKey(),
122
- chatId: t.integer().foreignKey(() => ChatTable, 'id'),
123
- authorId: t
124
- .integer()
125
- .nullable()
126
- .foreignKey(() => UserTable, 'id'),
127
- text: t.text(),
128
- ...t.timestamps(),
129
- }));
130
-
131
- relations = {
132
- user: this.belongsTo(() => UserTable, {
133
- primaryKey: 'id',
134
- foreignKey: 'authorId',
135
- }),
136
-
137
- chat: this.belongsTo(() => ChatTable, {
138
- primaryKey: 'id',
139
- foreignKey: 'chatId',
140
- }),
141
-
142
- profile: this.hasOne(() => ProfileTable, {
143
- required: true,
144
- through: 'user',
145
- source: 'profile',
146
- }),
147
- };
148
- }
149
- export const MessageSchema = tableToZod(MessageTable);
150
-
151
- export type Post = PostTable['columns']['type'];
152
- export class PostTable extends BaseTable {
153
- table = 'post';
154
- columns = this.setColumns((t) => ({
155
- id: t.serial().primaryKey(),
156
- userId: t.integer().foreignKey(() => UserTable, 'id'),
157
- title: t.text(),
158
- ...t.timestamps(),
159
- }));
160
-
161
- relations = {
162
- postTags: this.hasMany(() => PostTagTable, {
163
- primaryKey: 'id',
164
- foreignKey: 'postId',
165
- }),
166
- };
167
- }
168
- export const PostSchema = tableToZod(PostTable);
169
-
170
- export type PostTag = PostTagTable['columns']['type'];
171
- export class PostTagTable extends BaseTable {
172
- table = 'postTag';
173
- columns = this.setColumns((t) => ({
174
- postId: t.integer().foreignKey(() => PostTable, 'id'),
175
- tag: t.text().foreignKey(() => TagTable, 'tag'),
176
- ...t.primaryKey(['postId', 'tag']),
177
- }));
178
-
179
- relations = {
180
- tag: this.belongsTo(() => TagTable, {
181
- primaryKey: 'tag',
182
- foreignKey: 'tag',
183
- }),
184
- };
185
- }
186
- export const PostTagSchema = tableToZod(PostTagTable);
187
-
188
- export type Tag = TagTable['columns']['type'];
189
- export class TagTable extends BaseTable {
190
- table = 'tag';
191
- columns = this.setColumns((t) => ({
192
- tag: t.text().primaryKey(),
193
- }));
194
- }
@@ -1,119 +0,0 @@
1
- import {
2
- patchPgForTransactions,
3
- rollbackTransaction,
4
- startTransaction,
5
- } from 'pg-transactional-tests';
6
- import { db } from './test-db';
7
- import { DeleteQueryData, InsertQueryData, Query, UpdateQueryData } from 'pqb';
8
-
9
- type AssertEqual<T, Expected> = [T] extends [Expected]
10
- ? [Expected] extends [T]
11
- ? true
12
- : false
13
- : false;
14
-
15
- export const assertType = <T, Expected>(
16
- ..._: AssertEqual<T, Expected> extends true ? [] : ['invalid type']
17
- ) => {
18
- // noop
19
- };
20
-
21
- export const line = (s: string) =>
22
- s.trim().replace(/\s+/g, ' ').replace(/\( /g, '(').replace(/ \)/g, ')');
23
-
24
- export const expectSql = (
25
- sql: { text: string; values: unknown[] },
26
- text: string,
27
- values: unknown[] = [],
28
- ) => {
29
- expect(sql.text).toBe(line(text));
30
- expect(sql.values).toEqual(values);
31
- };
32
-
33
- export const toLine = (s: string) => {
34
- return s.trim().replace(/\n\s*/g, ' ');
35
- };
36
-
37
- export const now = new Date();
38
- export const userData = {
39
- name: 'name',
40
- password: 'password',
41
- updatedAt: now,
42
- createdAt: now,
43
- };
44
-
45
- export const profileData = {
46
- bio: 'bio',
47
- updatedAt: now,
48
- createdAt: now,
49
- };
50
-
51
- export const chatData = {
52
- title: 'chat',
53
- updatedAt: now,
54
- createdAt: now,
55
- };
56
-
57
- export const messageData = {
58
- text: 'text',
59
- updatedAt: now,
60
- createdAt: now,
61
- };
62
-
63
- export const useTestDatabase = () => {
64
- beforeAll(patchPgForTransactions);
65
- beforeEach(startTransaction);
66
- afterEach(rollbackTransaction);
67
- afterAll(async () => {
68
- await db.$close();
69
- });
70
- };
71
-
72
- export const useRelationCallback = (rel: { query: Query }) => {
73
- const beforeCreate = jest.fn();
74
- const afterCreate = jest.fn();
75
- const beforeUpdate = jest.fn();
76
- const afterUpdate = jest.fn();
77
- const beforeDelete = jest.fn();
78
- const afterDelete = jest.fn();
79
- const relQuery = rel.query;
80
-
81
- beforeAll(() => {
82
- relQuery._beforeCreate(beforeCreate);
83
- relQuery._afterCreate(afterCreate);
84
- relQuery._beforeUpdate(beforeUpdate);
85
- relQuery._afterUpdate(afterUpdate);
86
- relQuery._beforeDelete(beforeDelete);
87
- relQuery._afterDelete(afterDelete);
88
- });
89
-
90
- afterAll(() => {
91
- let q;
92
- q = relQuery.query as InsertQueryData;
93
- q.beforeCreate?.pop();
94
- q.afterCreate?.pop();
95
- q = relQuery.query as UpdateQueryData;
96
- q.beforeUpdate?.pop();
97
- q.afterUpdate?.pop();
98
- q = relQuery.query as DeleteQueryData;
99
- q.beforeDelete?.pop();
100
- q.afterDelete?.pop();
101
- });
102
-
103
- return {
104
- beforeCreate,
105
- afterCreate,
106
- beforeUpdate,
107
- afterUpdate,
108
- beforeDelete,
109
- afterDelete,
110
- resetMocks() {
111
- beforeCreate.mockReset();
112
- afterCreate.mockReset();
113
- beforeUpdate.mockReset();
114
- afterUpdate.mockReset();
115
- beforeDelete.mockReset();
116
- afterDelete.mockReset();
117
- },
118
- };
119
- };
@@ -1,47 +0,0 @@
1
- import { db } from './test-utils/test-db';
2
- import { profileData, toLine, userData } from './test-utils/test-utils';
3
- import { Client } from 'pg';
4
- import { noop } from 'pqb';
5
-
6
- describe('transaction', () => {
7
- afterAll(db.$close);
8
-
9
- it('should have override transaction method which implicitly connects tables with a single transaction', async () => {
10
- const spy = jest.spyOn(Client.prototype, 'query');
11
-
12
- await db
13
- .$transaction(async (db) => {
14
- await db.user.create(userData);
15
- await db.profile.create(profileData);
16
- throw new Error('Throw error to rollback');
17
- })
18
- .catch(noop);
19
-
20
- expect(
21
- spy.mock.calls.map(
22
- (call) => (call[0] as unknown as { text: string }).text,
23
- ),
24
- ).toEqual([
25
- 'BEGIN',
26
- toLine(`
27
- INSERT INTO "user"("name", "password", "updatedAt", "createdAt")
28
- VALUES ($1, $2, $3, $4)
29
- RETURNING *
30
- `),
31
- toLine(`
32
- INSERT INTO "profile"("bio", "updatedAt", "createdAt")
33
- VALUES ($1, $2, $3)
34
- RETURNING *
35
- `),
36
- 'ROLLBACK',
37
- ]);
38
- });
39
-
40
- it('should throw error if argument is forgotten', async () => {
41
- expect(() =>
42
- db.$transaction(async () => {
43
- // noop
44
- }),
45
- ).toThrow('Argument of $transaction callback should be used');
46
- });
47
- });
@@ -1,27 +0,0 @@
1
- import { Db } from 'pqb';
2
-
3
- export function transaction<T extends { $queryBuilder: Db }, Result>(
4
- this: T,
5
- fn: (db: T) => Promise<Result>,
6
- ): Promise<Result> {
7
- if (fn.length === 0) {
8
- throw new Error('Argument of $transaction callback should be used');
9
- }
10
-
11
- return this.$queryBuilder.transaction((q) => {
12
- const orm = {} as T;
13
- for (const key in this) {
14
- const value = this[key];
15
- if (value instanceof Db) {
16
- const table = value.transacting(q);
17
- table.__table = table;
18
- (table as unknown as { db: unknown }).db = orm;
19
- orm[key] = table;
20
- } else {
21
- orm[key] = value;
22
- }
23
- }
24
-
25
- return fn(orm);
26
- });
27
- }
package/src/utils.ts DELETED
@@ -1,9 +0,0 @@
1
- export const toPascalCase = (s: string) => {
2
- const words = s.match(/(\w)(\w*)/g) || [];
3
- return words.map((word) => word[0].toUpperCase() + word.slice(1)).join('');
4
- };
5
-
6
- export const toCamelCase = (s: string) => {
7
- const pascal = toPascalCase(s);
8
- return pascal[0].toLowerCase() + pascal.slice(1);
9
- };
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["./src", "jest-setup.ts"],
4
- "compilerOptions": {
5
- "outDir": "./dist",
6
- "noEmit": false,
7
- "baseUrl": ".",
8
- "paths": {
9
- "pqb": ["../qb/pqb/src"],
10
- "rake-db": ["../rake-db/src"],
11
- "orchid-orm-schema-to-zod": ["../schema-to-zod/src"]
12
- }
13
- }
14
- }