orchid-orm 1.3.14 → 1.3.16

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.
@@ -1,16 +1,20 @@
1
1
  import { Model } from '../model';
2
2
  import {
3
3
  addQueryOn,
4
- BelongsToNestedInsert,
5
- BelongsToNestedUpdate,
6
4
  BelongsToRelation,
5
+ CreateCtx,
6
+ InsertQueryData,
7
7
  isQueryReturnsAll,
8
+ pushQueryValue,
8
9
  Query,
9
10
  QueryBase,
11
+ UpdateCtx,
12
+ VirtualColumn,
10
13
  WhereArg,
11
14
  WhereResult,
12
15
  } from 'pqb';
13
16
  import { RelationData, RelationThunkBase } from './relations';
17
+ import { NestedInsertOneItem, NestedUpdateOneItem } from './utils';
14
18
 
15
19
  export interface BelongsTo extends RelationThunkBase {
16
20
  type: 'belongsTo';
@@ -29,219 +33,320 @@ export type BelongsToInfo<
29
33
  chainedDelete: false;
30
34
  };
31
35
 
36
+ type State = {
37
+ query: Query;
38
+ primaryKey: string;
39
+ foreignKey: string;
40
+ };
41
+
42
+ type BelongsToNestedInsert = (
43
+ query: Query,
44
+ relationData: NestedInsertOneItem[],
45
+ ) => Promise<Record<string, unknown>[]>;
46
+
47
+ type BelongsToNestedUpdate = (
48
+ q: Query,
49
+ update: Record<string, unknown>,
50
+ params: NestedUpdateOneItem,
51
+ state: {
52
+ updateLater?: Record<string, unknown>;
53
+ updateLaterPromises?: Promise<void>[];
54
+ },
55
+ ) => boolean;
56
+
57
+ class BelongsToVirtualColumn extends VirtualColumn {
58
+ private readonly nestedInsert: BelongsToNestedInsert;
59
+ private readonly nestedUpdate: BelongsToNestedUpdate;
60
+
61
+ constructor(private key: string, private state: State) {
62
+ super();
63
+ this.nestedInsert = nestedInsert(this.state);
64
+ this.nestedUpdate = nestedUpdate(this.state);
65
+ }
66
+
67
+ create(
68
+ q: Query,
69
+ ctx: CreateCtx,
70
+ item: Record<string, unknown>,
71
+ rowIndex: number,
72
+ ) {
73
+ const {
74
+ key,
75
+ state: { foreignKey, primaryKey },
76
+ } = this;
77
+
78
+ let columnIndex = ctx.columns.get(foreignKey);
79
+ if (columnIndex === undefined) {
80
+ ctx.columns.set(foreignKey, (columnIndex = ctx.columns.size));
81
+ }
82
+
83
+ const store = ctx as unknown as {
84
+ belongsTo?: Record<string, [number, number, unknown][]>;
85
+ };
86
+
87
+ if (!store.belongsTo) store.belongsTo = {};
88
+
89
+ const values = [rowIndex, columnIndex, item[key]] as [
90
+ number,
91
+ number,
92
+ unknown,
93
+ ];
94
+
95
+ if (store.belongsTo[key]) {
96
+ store.belongsTo[key].push(values);
97
+ return;
98
+ }
99
+
100
+ const relationData = [values];
101
+ store.belongsTo[key] = relationData;
102
+ q.query.wrapInTransaction = true;
103
+
104
+ pushQueryValue(q, 'beforeCreate', async (q: Query) => {
105
+ const inserted = await this.nestedInsert(
106
+ q,
107
+ relationData.map(([, , data]) => data as NestedInsertOneItem),
108
+ );
109
+
110
+ const { values } = q.query as InsertQueryData;
111
+ relationData.forEach(([rowIndex, columnIndex], index) => {
112
+ (values as unknown[][])[rowIndex][columnIndex] =
113
+ inserted[index][primaryKey];
114
+ });
115
+ });
116
+ }
117
+
118
+ update(q: Query, ctx: UpdateCtx, set: Record<string, unknown>) {
119
+ q.query.wrapInTransaction = true;
120
+
121
+ const data = set[this.key] as NestedUpdateOneItem;
122
+ if (this.nestedUpdate(q, set, data, ctx)) {
123
+ ctx.willSetKeys = true;
124
+ }
125
+ }
126
+ }
127
+
32
128
  export const makeBelongsToMethod = (
33
129
  relation: BelongsTo,
130
+ relationName: string,
34
131
  query: Query,
35
132
  ): RelationData => {
36
133
  const { primaryKey, foreignKey } = relation.options;
134
+ const state: State = { query, primaryKey, foreignKey };
37
135
 
38
136
  return {
39
137
  returns: 'one',
40
138
  method: (params: Record<string, unknown>) => {
41
139
  return query.findBy({ [primaryKey]: params[foreignKey] });
42
140
  },
43
- nestedInsert: (async (q, data) => {
44
- const connectOrCreate = data.filter(
45
- (
46
- item,
47
- ): item is {
48
- connectOrCreate: {
49
- where: WhereArg<QueryBase>;
50
- create: Record<string, unknown>;
51
- };
52
- } => Boolean(item.connectOrCreate),
53
- );
141
+ virtualColumn: new BelongsToVirtualColumn(relationName, state),
142
+ joinQuery(fromQuery, toQuery) {
143
+ return addQueryOn(toQuery, fromQuery, toQuery, primaryKey, foreignKey);
144
+ },
145
+ reverseJoin(fromQuery, toQuery) {
146
+ return addQueryOn(fromQuery, toQuery, fromQuery, foreignKey, primaryKey);
147
+ },
148
+ primaryKey,
149
+ };
150
+ };
54
151
 
55
- const t = query.transacting(q);
152
+ const nestedInsert = ({ query, primaryKey }: State) => {
153
+ return (async (q, data) => {
154
+ const connectOrCreate = data.filter(
155
+ (
156
+ item,
157
+ ): item is {
158
+ connectOrCreate: {
159
+ where: WhereArg<QueryBase>;
160
+ create: Record<string, unknown>;
161
+ };
162
+ } => Boolean(item.connectOrCreate),
163
+ );
56
164
 
57
- let connectOrCreated: unknown[];
58
- if (connectOrCreate.length) {
59
- connectOrCreated = await Promise.all(
60
- connectOrCreate.map((item) =>
61
- t.findBy(item.connectOrCreate.where)._takeOptional(),
62
- ),
63
- );
64
- } else {
65
- connectOrCreated = [];
66
- }
165
+ const t = query.transacting(q);
67
166
 
68
- let connectOrCreatedI = 0;
69
- const create = data.filter(
70
- (
71
- item,
72
- ): item is
73
- | {
74
- create: Record<string, unknown>;
75
- }
76
- | {
77
- connectOrCreate: {
78
- where: WhereArg<QueryBase>;
79
- create: Record<string, unknown>;
80
- };
81
- } => {
82
- if (item.connectOrCreate) {
83
- return !connectOrCreated[connectOrCreatedI++];
84
- } else {
85
- return Boolean(item.create);
86
- }
87
- },
167
+ let connectOrCreated: unknown[];
168
+ if (connectOrCreate.length) {
169
+ connectOrCreated = await Promise.all(
170
+ connectOrCreate.map((item) =>
171
+ t.findBy(item.connectOrCreate.where)._takeOptional(),
172
+ ),
88
173
  );
174
+ } else {
175
+ connectOrCreated = [];
176
+ }
89
177
 
90
- let created: unknown[];
91
- if (create.length) {
92
- created = (await t
93
- .select(primaryKey)
94
- ._createMany(
95
- create.map((item) =>
96
- 'create' in item ? item.create : item.connectOrCreate.create,
97
- ),
98
- )) as unknown[];
99
- } else {
100
- created = [];
101
- }
178
+ let connectOrCreatedI = 0;
179
+ const create = data.filter(
180
+ (
181
+ item,
182
+ ): item is
183
+ | {
184
+ create: Record<string, unknown>;
185
+ }
186
+ | {
187
+ connectOrCreate: {
188
+ where: WhereArg<QueryBase>;
189
+ create: Record<string, unknown>;
190
+ };
191
+ } => {
192
+ if (item.connectOrCreate) {
193
+ return !connectOrCreated[connectOrCreatedI++];
194
+ } else {
195
+ return Boolean(item.create);
196
+ }
197
+ },
198
+ );
102
199
 
103
- const connect = data.filter(
104
- (
105
- item,
106
- ): item is {
107
- connect: WhereArg<QueryBase>;
108
- } => Boolean(item.connect),
109
- );
200
+ let created: unknown[];
201
+ if (create.length) {
202
+ created = (await t
203
+ .select(primaryKey)
204
+ ._createMany(
205
+ create.map((item) =>
206
+ 'create' in item ? item.create : item.connectOrCreate.create,
207
+ ),
208
+ )) as unknown[];
209
+ } else {
210
+ created = [];
211
+ }
110
212
 
111
- let connected: unknown[];
112
- if (connect.length) {
113
- connected = await Promise.all(
114
- connect.map((item) => t.findBy(item.connect)._take()),
115
- );
116
- } else {
117
- connected = [];
118
- }
213
+ const connect = data.filter(
214
+ (
215
+ item,
216
+ ): item is {
217
+ connect: WhereArg<QueryBase>;
218
+ } => Boolean(item.connect),
219
+ );
119
220
 
120
- let createdI = 0;
121
- let connectedI = 0;
122
- connectOrCreatedI = 0;
123
- return data.map((item) =>
124
- item.connectOrCreate
125
- ? connectOrCreated[connectOrCreatedI++] || created[createdI++]
126
- : item.connect
127
- ? connected[connectedI++]
128
- : created[createdI++],
221
+ let connected: unknown[];
222
+ if (connect.length) {
223
+ connected = await Promise.all(
224
+ connect.map((item) => t.findBy(item.connect)._take()),
129
225
  );
130
- }) as BelongsToNestedInsert,
131
- nestedUpdate: ((q, update, params, state) => {
132
- if (params.upsert && isQueryReturnsAll(q)) {
133
- throw new Error('`upsert` option is not allowed in a batch update');
134
- }
226
+ } else {
227
+ connected = [];
228
+ }
135
229
 
136
- let idForDelete: unknown;
137
-
138
- q._beforeUpdate(async (q) => {
139
- if (params.disconnect) {
140
- update[foreignKey] = null;
141
- } else if (params.set) {
142
- if (primaryKey in params.set) {
143
- update[foreignKey] =
144
- params.set[primaryKey as keyof typeof params.set];
145
- } else {
146
- update[foreignKey] = await query
147
- .transacting(q)
148
- ._findBy(params.set)
149
- ._get(primaryKey);
150
- }
151
- } else if (params.create) {
230
+ let createdI = 0;
231
+ let connectedI = 0;
232
+ connectOrCreatedI = 0;
233
+ return data.map((item) =>
234
+ item.connectOrCreate
235
+ ? connectOrCreated[connectOrCreatedI++] || created[createdI++]
236
+ : item.connect
237
+ ? connected[connectedI++]
238
+ : created[createdI++],
239
+ ) as Record<string, unknown>[];
240
+ }) as BelongsToNestedInsert;
241
+ };
242
+
243
+ const nestedUpdate = ({ query, primaryKey, foreignKey }: State) => {
244
+ return ((q, update, params, state) => {
245
+ if (params.upsert && isQueryReturnsAll(q)) {
246
+ throw new Error('`upsert` option is not allowed in a batch update');
247
+ }
248
+
249
+ let idForDelete: unknown;
250
+
251
+ q._beforeUpdate(async (q) => {
252
+ if (params.disconnect) {
253
+ update[foreignKey] = null;
254
+ } else if (params.set) {
255
+ if (primaryKey in params.set) {
256
+ update[foreignKey] =
257
+ params.set[primaryKey as keyof typeof params.set];
258
+ } else {
152
259
  update[foreignKey] = await query
153
260
  .transacting(q)
154
- ._get(primaryKey)
155
- ._create(params.create);
156
- } else if (params.delete) {
157
- const selectQuery = q.transacting(q);
158
- selectQuery.query.type = undefined;
159
- idForDelete = await selectQuery._getOptional(foreignKey);
160
- update[foreignKey] = null;
261
+ ._findBy(params.set)
262
+ ._get(primaryKey);
161
263
  }
162
- });
264
+ } else if (params.create) {
265
+ update[foreignKey] = await query
266
+ .transacting(q)
267
+ ._get(primaryKey)
268
+ ._create(params.create);
269
+ } else if (params.delete) {
270
+ const selectQuery = q.transacting(q);
271
+ selectQuery.query.type = undefined;
272
+ idForDelete = await selectQuery._getOptional(foreignKey);
273
+ update[foreignKey] = null;
274
+ }
275
+ });
163
276
 
164
- const { upsert } = params;
165
- if (upsert || params.update || params.delete) {
166
- if (
167
- !q.query.select?.includes('*') &&
168
- !q.query.select?.includes(foreignKey)
169
- ) {
170
- q._select(foreignKey);
171
- }
277
+ const { upsert } = params;
278
+ if (upsert || params.update || params.delete) {
279
+ if (
280
+ !q.query.select?.includes('*') &&
281
+ !q.query.select?.includes(foreignKey)
282
+ ) {
283
+ q._select(foreignKey);
172
284
  }
285
+ }
173
286
 
174
- if (upsert) {
175
- if (!state.updateLater) {
176
- state.updateLater = {};
177
- state.updateLaterPromises = [];
178
- }
287
+ if (upsert) {
288
+ if (!state.updateLater) {
289
+ state.updateLater = {};
290
+ state.updateLaterPromises = [];
291
+ }
179
292
 
180
- const { handleResult } = q.query;
181
- q.query.handleResult = async (q, queryResult) => {
182
- const data = (await handleResult(q, queryResult)) as Record<
183
- string,
184
- unknown
185
- >[];
293
+ const { handleResult } = q.query;
294
+ q.query.handleResult = async (q, queryResult) => {
295
+ const data = (await handleResult(q, queryResult)) as Record<
296
+ string,
297
+ unknown
298
+ >[];
186
299
 
187
- const id = data[0][foreignKey];
188
- if (id !== null) {
189
- await query
300
+ const id = data[0][foreignKey];
301
+ if (id !== null) {
302
+ await query
303
+ .transacting(q)
304
+ ._findBy({ [primaryKey]: id })
305
+ ._update<WhereResult<Query>>(upsert.update);
306
+ } else {
307
+ (state.updateLaterPromises as Promise<void>[]).push(
308
+ query
190
309
  .transacting(q)
191
- ._findBy({ [primaryKey]: id })
192
- ._update<WhereResult<Query>>(upsert.update);
193
- } else {
194
- (state.updateLaterPromises as Promise<void>[]).push(
195
- query
196
- .transacting(q)
197
- ._select(primaryKey)
198
- ._create(upsert.create)
199
- .then((result) => {
200
- (state.updateLater as Record<string, unknown>)[foreignKey] = (
201
- result as Record<string, unknown>
202
- )[primaryKey];
203
- }) as unknown as Promise<void>,
204
- );
205
- }
310
+ ._select(primaryKey)
311
+ ._create(upsert.create)
312
+ .then((result) => {
313
+ (state.updateLater as Record<string, unknown>)[foreignKey] = (
314
+ result as Record<string, unknown>
315
+ )[primaryKey];
316
+ }) as unknown as Promise<void>,
317
+ );
318
+ }
206
319
 
207
- return data;
208
- };
209
- } else if (params.delete || params.update) {
210
- q._afterQuery(async (q, data) => {
211
- const id = params.delete
212
- ? idForDelete
213
- : Array.isArray(data)
214
- ? data.length === 0
215
- ? null
216
- : {
217
- in: data
218
- .map((item) => item[foreignKey])
219
- .filter((id) => id !== null),
220
- }
221
- : (data as Record<string, unknown>)[foreignKey];
222
-
223
- if (id !== undefined && id !== null) {
224
- const t = query.transacting(q)._findBy({
225
- [primaryKey]: id,
226
- });
227
-
228
- if (params.delete) {
229
- await t._delete();
230
- } else if (params.update) {
231
- await t._update<WhereResult<Query>>(params.update);
232
- }
320
+ return data;
321
+ };
322
+ } else if (params.delete || params.update) {
323
+ q._afterQuery(async (q, data) => {
324
+ const id = params.delete
325
+ ? idForDelete
326
+ : Array.isArray(data)
327
+ ? data.length === 0
328
+ ? null
329
+ : {
330
+ in: data
331
+ .map((item) => item[foreignKey])
332
+ .filter((id) => id !== null),
333
+ }
334
+ : (data as Record<string, unknown>)[foreignKey];
335
+
336
+ if (id !== undefined && id !== null) {
337
+ const t = query.transacting(q)._findBy({
338
+ [primaryKey]: id,
339
+ });
340
+
341
+ if (params.delete) {
342
+ await t._delete();
343
+ } else if (params.update) {
344
+ await t._update<WhereResult<Query>>(params.update);
233
345
  }
234
- });
235
- }
346
+ }
347
+ });
348
+ }
236
349
 
237
- return !params.update && !params.upsert;
238
- }) as BelongsToNestedUpdate,
239
- joinQuery(fromQuery, toQuery) {
240
- return addQueryOn(toQuery, fromQuery, toQuery, primaryKey, foreignKey);
241
- },
242
- reverseJoin(fromQuery, toQuery) {
243
- return addQueryOn(fromQuery, toQuery, fromQuery, foreignKey, primaryKey);
244
- },
245
- primaryKey,
246
- };
350
+ return !params.update && !params.upsert;
351
+ }) as BelongsToNestedUpdate;
247
352
  };