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,351 +0,0 @@
1
- import {
2
- addQueryOn,
3
- CreateCtx,
4
- getQueryAs,
5
- HasOneRelation,
6
- InsertQueryData,
7
- isQueryReturnsAll,
8
- JoinCallback,
9
- Query,
10
- QueryBase,
11
- UpdateCtx,
12
- VirtualColumn,
13
- WhereArg,
14
- WhereResult,
15
- } from 'pqb';
16
- import { Table } from '../table';
17
- import {
18
- RelationData,
19
- RelationInfo,
20
- RelationThunkBase,
21
- RelationThunks,
22
- } from './relations';
23
- import {
24
- getSourceRelation,
25
- getThroughRelation,
26
- hasRelationHandleCreate,
27
- hasRelationHandleUpdate,
28
- NestedInsertOneItem,
29
- NestedUpdateOneItem,
30
- } from './utils';
31
-
32
- export interface HasOne extends RelationThunkBase {
33
- type: 'hasOne';
34
- returns: 'one';
35
- options: HasOneRelation['options'];
36
- }
37
-
38
- export type HasOneInfo<
39
- T extends Table,
40
- Relations extends RelationThunks,
41
- Relation extends HasOne,
42
- > = {
43
- params: Relation['options'] extends { primaryKey: string }
44
- ? Record<
45
- Relation['options']['primaryKey'],
46
- T['columns']['shape'][Relation['options']['primaryKey']]['type']
47
- >
48
- : Relation['options'] extends { through: string }
49
- ? RelationInfo<
50
- T,
51
- Relations,
52
- Relations[Relation['options']['through']]
53
- >['params']
54
- : never;
55
- populate: Relation['options'] extends { foreignKey: string }
56
- ? Relation['options']['foreignKey']
57
- : never;
58
- chainedCreate: Relation['options'] extends { primaryKey: string }
59
- ? true
60
- : false;
61
- chainedDelete: true;
62
- };
63
-
64
- type State = {
65
- query: Query;
66
- primaryKey: string;
67
- foreignKey: string;
68
- };
69
-
70
- export type HasOneNestedInsert = (
71
- query: Query,
72
- data: [
73
- selfData: Record<string, unknown>,
74
- relationData: NestedInsertOneItem,
75
- ][],
76
- ) => Promise<void>;
77
-
78
- export type HasOneNestedUpdate = (
79
- query: Query,
80
- data: Record<string, unknown>[],
81
- relationData: NestedUpdateOneItem,
82
- ) => Promise<void>;
83
-
84
- class HasOneVirtualColumn extends VirtualColumn {
85
- private readonly nestedInsert: HasOneNestedInsert;
86
- private readonly nestedUpdate: HasOneNestedUpdate;
87
-
88
- constructor(private key: string, private state: State) {
89
- super();
90
- this.nestedInsert = nestedInsert(state);
91
- this.nestedUpdate = nestedUpdate(state);
92
- }
93
-
94
- create(
95
- q: Query,
96
- ctx: CreateCtx,
97
- item: Record<string, unknown>,
98
- rowIndex: number,
99
- ) {
100
- hasRelationHandleCreate(
101
- q,
102
- ctx,
103
- item,
104
- rowIndex,
105
- this.key,
106
- this.state.primaryKey,
107
- this.nestedInsert,
108
- );
109
- }
110
-
111
- update(q: Query, ctx: UpdateCtx, set: Record<string, unknown>) {
112
- hasRelationHandleUpdate(
113
- q,
114
- ctx,
115
- set,
116
- this.key,
117
- this.state.primaryKey,
118
- this.nestedUpdate,
119
- );
120
- }
121
- }
122
-
123
- export const makeHasOneMethod = (
124
- table: Query,
125
- relation: HasOne,
126
- relationName: string,
127
- query: Query,
128
- ): RelationData => {
129
- if (relation.options.required) {
130
- query._take();
131
- } else {
132
- query._takeOptional();
133
- }
134
-
135
- if ('through' in relation.options) {
136
- const { through, source } = relation.options;
137
-
138
- type TableWithQueryMethod = Record<
139
- string,
140
- (params: Record<string, unknown>) => Query
141
- >;
142
-
143
- const throughRelation = getThroughRelation(table, through);
144
- const sourceRelation = getSourceRelation(throughRelation, source);
145
- const sourceQuery = sourceRelation
146
- .joinQuery(throughRelation.query, sourceRelation.query)
147
- .as(relationName);
148
-
149
- const whereExistsCallback = () => sourceQuery;
150
-
151
- return {
152
- returns: 'one',
153
- method: (params: Record<string, unknown>) => {
154
- const throughQuery = (table as unknown as TableWithQueryMethod)[
155
- through
156
- ](params);
157
-
158
- return query.whereExists<Query, Query>(
159
- throughQuery,
160
- whereExistsCallback as unknown as JoinCallback<Query, Query>,
161
- );
162
- },
163
- joinQuery(fromQuery, toQuery) {
164
- return toQuery.whereExists<Query, Query>(
165
- throughRelation.joinQuery(fromQuery, throughRelation.query),
166
- (() => {
167
- const as = getQueryAs(toQuery);
168
- return sourceRelation.joinQuery(
169
- throughRelation.query,
170
- sourceRelation.query.as(as),
171
- );
172
- }) as unknown as JoinCallback<Query, Query>,
173
- );
174
- },
175
- reverseJoin(fromQuery, toQuery) {
176
- return fromQuery.whereExists<Query, Query>(
177
- throughRelation.joinQuery(fromQuery, throughRelation.query),
178
- (() => {
179
- const as = getQueryAs(toQuery);
180
- return sourceRelation.joinQuery(
181
- throughRelation.query,
182
- sourceRelation.query.as(as),
183
- );
184
- }) as unknown as JoinCallback<Query, Query>,
185
- );
186
- },
187
- primaryKey: sourceRelation.primaryKey,
188
- };
189
- }
190
-
191
- const { primaryKey, foreignKey } = relation.options;
192
- const state: State = { query, primaryKey, foreignKey };
193
-
194
- const fromQuerySelect = [{ selectAs: { [foreignKey]: primaryKey } }];
195
-
196
- return {
197
- returns: 'one',
198
- method: (params: Record<string, unknown>) => {
199
- const values = { [foreignKey]: params[primaryKey] };
200
- return query.where(values)._defaults(values);
201
- },
202
- virtualColumn: new HasOneVirtualColumn(relationName, state),
203
- joinQuery(fromQuery, toQuery) {
204
- return addQueryOn(toQuery, fromQuery, toQuery, foreignKey, primaryKey);
205
- },
206
- reverseJoin(fromQuery, toQuery) {
207
- return addQueryOn(fromQuery, toQuery, fromQuery, primaryKey, foreignKey);
208
- },
209
- primaryKey,
210
- modifyRelatedQuery(relationQuery) {
211
- return (query) => {
212
- const fromQuery = query.clone();
213
- fromQuery.query.select = fromQuerySelect;
214
- (relationQuery.query as InsertQueryData).fromQuery = fromQuery;
215
- };
216
- },
217
- };
218
- };
219
-
220
- const nestedInsert = ({ query, primaryKey, foreignKey }: State) => {
221
- return (async (q, data) => {
222
- const connect = data.filter(
223
- (
224
- item,
225
- ): item is [
226
- Record<string, unknown>,
227
- (
228
- | {
229
- connect: WhereArg<QueryBase>;
230
- }
231
- | {
232
- connectOrCreate: {
233
- where: WhereArg<QueryBase>;
234
- create: Record<string, unknown>;
235
- };
236
- }
237
- ),
238
- ] => Boolean(item[1].connect || item[1].connectOrCreate),
239
- );
240
-
241
- const t = query.transacting(q);
242
-
243
- let connected: number[];
244
- if (connect.length) {
245
- connected = await Promise.all(
246
- connect.map(([selfData, item]) => {
247
- const data = { [foreignKey]: selfData[primaryKey] };
248
- return 'connect' in item
249
- ? (
250
- t.where(item.connect) as WhereResult<Query> & {
251
- hasSelect: false;
252
- }
253
- )._updateOrThrow(data)
254
- : (
255
- t.where(item.connectOrCreate.where) as WhereResult<Query> & {
256
- hasSelect: false;
257
- }
258
- )._update(data);
259
- }),
260
- );
261
- } else {
262
- connected = [];
263
- }
264
-
265
- let connectedI = 0;
266
- const create = data.filter(
267
- (
268
- item,
269
- ): item is [
270
- Record<string, unknown>,
271
- (
272
- | { create: Record<string, unknown> }
273
- | {
274
- connectOrCreate: {
275
- where: WhereArg<QueryBase>;
276
- create: Record<string, unknown>;
277
- };
278
- }
279
- ),
280
- ] => {
281
- if (item[1].connectOrCreate) {
282
- return !connected[connectedI++];
283
- }
284
- return Boolean(item[1].create);
285
- },
286
- );
287
-
288
- if (create.length) {
289
- await t._count()._createMany(
290
- create.map(([selfData, item]) => ({
291
- [foreignKey]: selfData[primaryKey],
292
- ...('create' in item ? item.create : item.connectOrCreate.create),
293
- })),
294
- );
295
- }
296
- }) as HasOneNestedInsert;
297
- };
298
-
299
- const nestedUpdate = ({ query, primaryKey, foreignKey }: State) => {
300
- return (async (q, data, params) => {
301
- if (
302
- (params.set || params.create || params.upsert) &&
303
- isQueryReturnsAll(q)
304
- ) {
305
- const key = params.set ? 'set' : params.create ? 'create' : 'upsert';
306
- throw new Error(`\`${key}\` option is not allowed in a batch update`);
307
- }
308
-
309
- const t = query.transacting(q);
310
- const ids = data.map((item) => item[primaryKey]);
311
- const currentRelationsQuery = t.where({
312
- [foreignKey]: { in: ids },
313
- });
314
-
315
- if (params.create || params.disconnect || params.set) {
316
- await currentRelationsQuery._update({ [foreignKey]: null });
317
-
318
- if (params.create) {
319
- await t._count()._create({
320
- ...params.create,
321
- [foreignKey]: data[0][primaryKey],
322
- });
323
- }
324
- if (params.set) {
325
- await t
326
- ._where<Query>(params.set)
327
- ._update({ [foreignKey]: data[0][primaryKey] });
328
- }
329
- } else if (params.update) {
330
- await currentRelationsQuery._update<WhereResult<Query>>(params.update);
331
- } else if (params.delete) {
332
- await currentRelationsQuery._delete();
333
- } else if (params.upsert) {
334
- const { update, create } = params.upsert;
335
- const updatedIds: unknown[] = await currentRelationsQuery
336
- ._pluck(foreignKey)
337
- ._update<WhereResult<Query & { hasSelect: true }>>(update);
338
-
339
- if (updatedIds.length < ids.length) {
340
- await t.createMany(
341
- ids
342
- .filter((id) => !updatedIds.includes(id))
343
- .map((id) => ({
344
- ...create,
345
- [foreignKey]: id,
346
- })),
347
- );
348
- }
349
- }
350
- }) as HasOneNestedUpdate;
351
- };
@@ -1,37 +0,0 @@
1
- import { db } from '../test-utils/test-db';
2
- import { expectSql } from '../test-utils/test-utils';
3
-
4
- describe('relations', () => {
5
- it('should select multiple relations', () => {
6
- const query = db.user.select({
7
- profile: (q) => q.profile.where({ bio: 'bio' }),
8
- messages: (q) => q.messages.where({ text: 'text' }),
9
- });
10
-
11
- expectSql(
12
- query.toSql(),
13
- `
14
- SELECT
15
- (
16
- SELECT row_to_json("t".*)
17
- FROM (
18
- SELECT * FROM "profile"
19
- WHERE "profile"."bio" = $1
20
- AND "profile"."userId" = "user"."id"
21
- LIMIT $2
22
- ) AS "t"
23
- ) AS "profile",
24
- (
25
- SELECT COALESCE(json_agg(row_to_json("t".*)), '[]')
26
- FROM (
27
- SELECT * FROM "message" AS "messages"
28
- WHERE "messages"."text" = $3
29
- AND "messages"."authorId" = "user"."id"
30
- ) AS "t"
31
- ) AS "messages"
32
- FROM "user"
33
- `,
34
- ['bio', 1, 'text'],
35
- );
36
- });
37
- });