orchid-orm 1.5.29 → 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.
- package/dist/index.js +10 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -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
package/src/relations/hasMany.ts
DELETED
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
RelationData,
|
|
3
|
-
RelationInfo,
|
|
4
|
-
RelationThunkBase,
|
|
5
|
-
RelationThunks,
|
|
6
|
-
} from './relations';
|
|
7
|
-
import { Table } from '../table';
|
|
8
|
-
import {
|
|
9
|
-
addQueryOn,
|
|
10
|
-
getQueryAs,
|
|
11
|
-
HasManyRelation,
|
|
12
|
-
CreateData,
|
|
13
|
-
JoinCallback,
|
|
14
|
-
MaybeArray,
|
|
15
|
-
Query,
|
|
16
|
-
QueryBase,
|
|
17
|
-
toSqlCacheKey,
|
|
18
|
-
WhereArg,
|
|
19
|
-
WhereResult,
|
|
20
|
-
InsertQueryData,
|
|
21
|
-
isQueryReturnsAll,
|
|
22
|
-
VirtualColumn,
|
|
23
|
-
CreateCtx,
|
|
24
|
-
UpdateCtx,
|
|
25
|
-
} from 'pqb';
|
|
26
|
-
import {
|
|
27
|
-
getSourceRelation,
|
|
28
|
-
getThroughRelation,
|
|
29
|
-
hasRelationHandleCreate,
|
|
30
|
-
hasRelationHandleUpdate,
|
|
31
|
-
NestedInsertManyItems,
|
|
32
|
-
NestedUpdateManyItems,
|
|
33
|
-
} from './utils';
|
|
34
|
-
|
|
35
|
-
export interface HasMany extends RelationThunkBase {
|
|
36
|
-
type: 'hasMany';
|
|
37
|
-
returns: 'many';
|
|
38
|
-
options: HasManyRelation['options'];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type HasManyInfo<
|
|
42
|
-
T extends Table,
|
|
43
|
-
Relations extends RelationThunks,
|
|
44
|
-
Relation extends HasMany,
|
|
45
|
-
> = {
|
|
46
|
-
params: Relation['options'] extends { primaryKey: string }
|
|
47
|
-
? Record<
|
|
48
|
-
Relation['options']['primaryKey'],
|
|
49
|
-
T['columns']['shape'][Relation['options']['primaryKey']]['type']
|
|
50
|
-
>
|
|
51
|
-
: Relation['options'] extends { through: string }
|
|
52
|
-
? RelationInfo<
|
|
53
|
-
T,
|
|
54
|
-
Relations,
|
|
55
|
-
Relations[Relation['options']['through']]
|
|
56
|
-
>['params']
|
|
57
|
-
: never;
|
|
58
|
-
populate: Relation['options'] extends { foreignKey: string }
|
|
59
|
-
? Relation['options']['foreignKey']
|
|
60
|
-
: never;
|
|
61
|
-
chainedCreate: Relation['options'] extends { primaryKey: string }
|
|
62
|
-
? true
|
|
63
|
-
: false;
|
|
64
|
-
chainedDelete: true;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
type State = {
|
|
68
|
-
query: Query;
|
|
69
|
-
primaryKey: string;
|
|
70
|
-
foreignKey: string;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export type HasManyNestedUpdate = (
|
|
74
|
-
query: Query,
|
|
75
|
-
data: Record<string, unknown>[],
|
|
76
|
-
relationData: NestedUpdateManyItems,
|
|
77
|
-
) => Promise<void>;
|
|
78
|
-
|
|
79
|
-
export type HasManyNestedInsert = (
|
|
80
|
-
query: Query,
|
|
81
|
-
data: [
|
|
82
|
-
selfData: Record<string, unknown>,
|
|
83
|
-
relationData: NestedInsertManyItems,
|
|
84
|
-
][],
|
|
85
|
-
) => Promise<void>;
|
|
86
|
-
|
|
87
|
-
class HasManyVirtualColumn extends VirtualColumn {
|
|
88
|
-
private readonly nestedInsert: HasManyNestedInsert;
|
|
89
|
-
private readonly nestedUpdate: HasManyNestedUpdate;
|
|
90
|
-
|
|
91
|
-
constructor(private key: string, private state: State) {
|
|
92
|
-
super();
|
|
93
|
-
this.nestedInsert = nestedInsert(state);
|
|
94
|
-
this.nestedUpdate = nestedUpdate(state);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
create(
|
|
98
|
-
q: Query,
|
|
99
|
-
ctx: CreateCtx,
|
|
100
|
-
item: Record<string, unknown>,
|
|
101
|
-
rowIndex: number,
|
|
102
|
-
) {
|
|
103
|
-
hasRelationHandleCreate(
|
|
104
|
-
q,
|
|
105
|
-
ctx,
|
|
106
|
-
item,
|
|
107
|
-
rowIndex,
|
|
108
|
-
this.key,
|
|
109
|
-
this.state.primaryKey,
|
|
110
|
-
this.nestedInsert,
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
update(q: Query, ctx: UpdateCtx, set: Record<string, unknown>) {
|
|
115
|
-
hasRelationHandleUpdate(
|
|
116
|
-
q,
|
|
117
|
-
ctx,
|
|
118
|
-
set,
|
|
119
|
-
this.key,
|
|
120
|
-
this.state.primaryKey,
|
|
121
|
-
this.nestedUpdate,
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export const makeHasManyMethod = (
|
|
127
|
-
table: Query,
|
|
128
|
-
relation: HasMany,
|
|
129
|
-
relationName: string,
|
|
130
|
-
query: Query,
|
|
131
|
-
): RelationData => {
|
|
132
|
-
if ('through' in relation.options) {
|
|
133
|
-
const { through, source } = relation.options;
|
|
134
|
-
|
|
135
|
-
type TableWithQueryMethod = Record<
|
|
136
|
-
string,
|
|
137
|
-
(params: Record<string, unknown>) => Query
|
|
138
|
-
>;
|
|
139
|
-
|
|
140
|
-
const throughRelation = getThroughRelation(table, through);
|
|
141
|
-
const sourceRelation = getSourceRelation(throughRelation, source);
|
|
142
|
-
const sourceRelationQuery = sourceRelation.query.as(relationName);
|
|
143
|
-
const sourceQuery = sourceRelation.joinQuery(
|
|
144
|
-
throughRelation.query,
|
|
145
|
-
sourceRelationQuery,
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
const whereExistsCallback = () => sourceQuery;
|
|
149
|
-
|
|
150
|
-
return {
|
|
151
|
-
returns: 'many',
|
|
152
|
-
method: (params: Record<string, unknown>) => {
|
|
153
|
-
const throughQuery = (table as unknown as TableWithQueryMethod)[
|
|
154
|
-
through
|
|
155
|
-
](params);
|
|
156
|
-
|
|
157
|
-
return query.whereExists<Query, Query>(
|
|
158
|
-
throughQuery,
|
|
159
|
-
whereExistsCallback as unknown as JoinCallback<Query, Query>,
|
|
160
|
-
);
|
|
161
|
-
},
|
|
162
|
-
joinQuery(fromQuery, toQuery) {
|
|
163
|
-
return toQuery.whereExists<Query, Query>(
|
|
164
|
-
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
165
|
-
(() => {
|
|
166
|
-
const as = getQueryAs(toQuery);
|
|
167
|
-
return sourceRelation.joinQuery(
|
|
168
|
-
throughRelation.query,
|
|
169
|
-
sourceRelation.query.as(as),
|
|
170
|
-
);
|
|
171
|
-
}) as unknown as JoinCallback<Query, Query>,
|
|
172
|
-
);
|
|
173
|
-
},
|
|
174
|
-
reverseJoin(fromQuery, toQuery) {
|
|
175
|
-
return fromQuery.whereExists<Query, Query>(
|
|
176
|
-
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
177
|
-
(() => {
|
|
178
|
-
const as = getQueryAs(toQuery);
|
|
179
|
-
return sourceRelation.joinQuery(
|
|
180
|
-
throughRelation.query,
|
|
181
|
-
sourceRelation.query.as(as),
|
|
182
|
-
);
|
|
183
|
-
}) as unknown as JoinCallback<Query, Query>,
|
|
184
|
-
);
|
|
185
|
-
},
|
|
186
|
-
primaryKey: sourceRelation.primaryKey,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const { primaryKey, foreignKey } = relation.options;
|
|
191
|
-
const state: State = { query, primaryKey, foreignKey };
|
|
192
|
-
|
|
193
|
-
const fromQuerySelect = [{ selectAs: { [foreignKey]: primaryKey } }];
|
|
194
|
-
|
|
195
|
-
return {
|
|
196
|
-
returns: 'many',
|
|
197
|
-
method: (params: Record<string, unknown>) => {
|
|
198
|
-
const values = { [foreignKey]: params[primaryKey] };
|
|
199
|
-
return query.where(values)._defaults(values);
|
|
200
|
-
},
|
|
201
|
-
virtualColumn: new HasManyVirtualColumn(relationName, state),
|
|
202
|
-
joinQuery(fromQuery, toQuery) {
|
|
203
|
-
return addQueryOn(toQuery, fromQuery, toQuery, foreignKey, primaryKey);
|
|
204
|
-
},
|
|
205
|
-
reverseJoin(fromQuery, toQuery) {
|
|
206
|
-
return addQueryOn(fromQuery, toQuery, fromQuery, primaryKey, foreignKey);
|
|
207
|
-
},
|
|
208
|
-
primaryKey,
|
|
209
|
-
modifyRelatedQuery(relationQuery) {
|
|
210
|
-
return (query) => {
|
|
211
|
-
const fromQuery = query.clone();
|
|
212
|
-
fromQuery.query.select = fromQuerySelect;
|
|
213
|
-
(relationQuery.query as InsertQueryData).fromQuery = fromQuery;
|
|
214
|
-
};
|
|
215
|
-
},
|
|
216
|
-
};
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
const getWhereForNestedUpdate = (
|
|
220
|
-
data: Record<string, unknown>[],
|
|
221
|
-
params: MaybeArray<WhereArg<QueryBase>> | undefined,
|
|
222
|
-
primaryKey: string,
|
|
223
|
-
foreignKey: string,
|
|
224
|
-
) => {
|
|
225
|
-
const where: WhereArg<Query> = {
|
|
226
|
-
[foreignKey]: { in: data.map((item) => item[primaryKey]) },
|
|
227
|
-
};
|
|
228
|
-
if (params) {
|
|
229
|
-
if (Array.isArray(params)) {
|
|
230
|
-
where.OR = params;
|
|
231
|
-
} else {
|
|
232
|
-
Object.assign(where, params);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return where;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
const nestedInsert = ({ query, primaryKey, foreignKey }: State) => {
|
|
239
|
-
return (async (q, data) => {
|
|
240
|
-
const connect = data.filter(
|
|
241
|
-
(
|
|
242
|
-
item,
|
|
243
|
-
): item is [
|
|
244
|
-
Record<string, unknown>,
|
|
245
|
-
{ connect: WhereArg<QueryBase>[] },
|
|
246
|
-
] => Boolean(item[1].connect),
|
|
247
|
-
);
|
|
248
|
-
|
|
249
|
-
const t = query.transacting(q);
|
|
250
|
-
|
|
251
|
-
if (connect.length) {
|
|
252
|
-
await Promise.all(
|
|
253
|
-
connect.flatMap(([selfData, { connect }]) =>
|
|
254
|
-
t
|
|
255
|
-
.or<Query>(...connect)
|
|
256
|
-
._updateOrThrow({ [foreignKey]: selfData[primaryKey] }),
|
|
257
|
-
),
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const connectOrCreate = data.filter(
|
|
262
|
-
(
|
|
263
|
-
item,
|
|
264
|
-
): item is [
|
|
265
|
-
Record<string, unknown>,
|
|
266
|
-
{
|
|
267
|
-
connectOrCreate: {
|
|
268
|
-
where: WhereArg<QueryBase>;
|
|
269
|
-
create: Record<string, unknown>;
|
|
270
|
-
}[];
|
|
271
|
-
},
|
|
272
|
-
] => Boolean(item[1].connectOrCreate),
|
|
273
|
-
);
|
|
274
|
-
|
|
275
|
-
let connected: number[];
|
|
276
|
-
if (connectOrCreate.length) {
|
|
277
|
-
connected = await Promise.all(
|
|
278
|
-
connectOrCreate.flatMap(([selfData, { connectOrCreate }]) =>
|
|
279
|
-
connectOrCreate.map((item) =>
|
|
280
|
-
(
|
|
281
|
-
t.where(item.where) as WhereResult<Query & { hasSelect: false }>
|
|
282
|
-
)._update({
|
|
283
|
-
[foreignKey]: selfData[primaryKey],
|
|
284
|
-
}),
|
|
285
|
-
),
|
|
286
|
-
),
|
|
287
|
-
);
|
|
288
|
-
} else {
|
|
289
|
-
connected = [];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
let connectedI = 0;
|
|
293
|
-
const create = data.filter(
|
|
294
|
-
(
|
|
295
|
-
item,
|
|
296
|
-
): item is [
|
|
297
|
-
Record<string, unknown>,
|
|
298
|
-
{
|
|
299
|
-
create?: Record<string, unknown>[];
|
|
300
|
-
connectOrCreate?: {
|
|
301
|
-
where: WhereArg<QueryBase>;
|
|
302
|
-
create: Record<string, unknown>;
|
|
303
|
-
}[];
|
|
304
|
-
},
|
|
305
|
-
] => {
|
|
306
|
-
if (item[1].connectOrCreate) {
|
|
307
|
-
const length = item[1].connectOrCreate.length;
|
|
308
|
-
connectedI += length;
|
|
309
|
-
for (let i = length; i > 0; i--) {
|
|
310
|
-
if (connected[connectedI - i] === 0) return true;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
return Boolean(item[1].create);
|
|
314
|
-
},
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
connectedI = 0;
|
|
318
|
-
if (create.length) {
|
|
319
|
-
await t._createMany(
|
|
320
|
-
create.flatMap(([selfData, { create = [], connectOrCreate = [] }]) => {
|
|
321
|
-
return [
|
|
322
|
-
...create.map((item) => ({
|
|
323
|
-
[foreignKey]: selfData[primaryKey],
|
|
324
|
-
...item,
|
|
325
|
-
})),
|
|
326
|
-
...connectOrCreate
|
|
327
|
-
.filter(() => connected[connectedI++] === 0)
|
|
328
|
-
.map((item) => ({
|
|
329
|
-
[foreignKey]: selfData[primaryKey],
|
|
330
|
-
...item.create,
|
|
331
|
-
})),
|
|
332
|
-
];
|
|
333
|
-
}) as CreateData<Query>[],
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
}) as HasManyNestedInsert;
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
const nestedUpdate = ({ query, primaryKey, foreignKey }: State) => {
|
|
340
|
-
return (async (q, data, params) => {
|
|
341
|
-
if ((params.set || params.create) && isQueryReturnsAll(q)) {
|
|
342
|
-
const key = params.set ? 'set' : 'create';
|
|
343
|
-
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const t = query.transacting(q);
|
|
347
|
-
if (params.create) {
|
|
348
|
-
await t._count()._createMany(
|
|
349
|
-
params.create.map((create) => ({
|
|
350
|
-
...create,
|
|
351
|
-
[foreignKey]: data[0][primaryKey],
|
|
352
|
-
})),
|
|
353
|
-
);
|
|
354
|
-
delete t.query[toSqlCacheKey];
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (params.disconnect || params.set) {
|
|
358
|
-
await t
|
|
359
|
-
.where<Query>(
|
|
360
|
-
getWhereForNestedUpdate(
|
|
361
|
-
data,
|
|
362
|
-
params.disconnect,
|
|
363
|
-
primaryKey,
|
|
364
|
-
foreignKey,
|
|
365
|
-
),
|
|
366
|
-
)
|
|
367
|
-
._update({ [foreignKey]: null });
|
|
368
|
-
|
|
369
|
-
if (params.set) {
|
|
370
|
-
delete t.query[toSqlCacheKey];
|
|
371
|
-
await t
|
|
372
|
-
.where<Query>(
|
|
373
|
-
Array.isArray(params.set)
|
|
374
|
-
? {
|
|
375
|
-
OR: params.set,
|
|
376
|
-
}
|
|
377
|
-
: params.set,
|
|
378
|
-
)
|
|
379
|
-
._update({ [foreignKey]: data[0][primaryKey] });
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (params.delete || params.update) {
|
|
384
|
-
delete t.query[toSqlCacheKey];
|
|
385
|
-
const q = t._where(
|
|
386
|
-
getWhereForNestedUpdate(
|
|
387
|
-
data,
|
|
388
|
-
params.delete || params.update?.where,
|
|
389
|
-
primaryKey,
|
|
390
|
-
foreignKey,
|
|
391
|
-
),
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
if (params.delete) {
|
|
395
|
-
await q._delete();
|
|
396
|
-
} else if (params.update) {
|
|
397
|
-
await q._update<WhereResult<Query>>(params.update.data);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}) as HasManyNestedUpdate;
|
|
401
|
-
};
|