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
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
import { Table } from '../table';
|
|
2
|
-
import {
|
|
3
|
-
addQueryOn,
|
|
4
|
-
BelongsToRelation,
|
|
5
|
-
CreateCtx,
|
|
6
|
-
InsertQueryData,
|
|
7
|
-
isQueryReturnsAll,
|
|
8
|
-
pushQueryValue,
|
|
9
|
-
Query,
|
|
10
|
-
QueryBase,
|
|
11
|
-
UpdateCtx,
|
|
12
|
-
VirtualColumn,
|
|
13
|
-
WhereArg,
|
|
14
|
-
WhereResult,
|
|
15
|
-
} from 'pqb';
|
|
16
|
-
import { RelationData, RelationThunkBase } from './relations';
|
|
17
|
-
import { NestedInsertOneItem, NestedUpdateOneItem } from './utils';
|
|
18
|
-
|
|
19
|
-
export interface BelongsTo extends RelationThunkBase {
|
|
20
|
-
type: 'belongsTo';
|
|
21
|
-
returns: 'one';
|
|
22
|
-
options: BelongsToRelation['options'];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type BelongsToInfo<
|
|
26
|
-
T extends Table,
|
|
27
|
-
Relation extends BelongsTo,
|
|
28
|
-
FK extends string = Relation['options']['foreignKey'],
|
|
29
|
-
> = {
|
|
30
|
-
params: Record<FK, T['columns']['shape'][FK]['type']>;
|
|
31
|
-
populate: never;
|
|
32
|
-
chainedCreate: false;
|
|
33
|
-
chainedDelete: false;
|
|
34
|
-
};
|
|
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
|
-
|
|
128
|
-
export const makeBelongsToMethod = (
|
|
129
|
-
relation: BelongsTo,
|
|
130
|
-
relationName: string,
|
|
131
|
-
query: Query,
|
|
132
|
-
): RelationData => {
|
|
133
|
-
const { primaryKey, foreignKey } = relation.options;
|
|
134
|
-
const state: State = { query, primaryKey, foreignKey };
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
returns: 'one',
|
|
138
|
-
method: (params: Record<string, unknown>) => {
|
|
139
|
-
return query.findBy({ [primaryKey]: params[foreignKey] });
|
|
140
|
-
},
|
|
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
|
-
};
|
|
151
|
-
|
|
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
|
-
);
|
|
164
|
-
|
|
165
|
-
const t = query.transacting(q);
|
|
166
|
-
|
|
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
|
-
),
|
|
173
|
-
);
|
|
174
|
-
} else {
|
|
175
|
-
connectOrCreated = [];
|
|
176
|
-
}
|
|
177
|
-
|
|
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
|
-
);
|
|
199
|
-
|
|
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
|
-
}
|
|
212
|
-
|
|
213
|
-
const connect = data.filter(
|
|
214
|
-
(
|
|
215
|
-
item,
|
|
216
|
-
): item is {
|
|
217
|
-
connect: WhereArg<QueryBase>;
|
|
218
|
-
} => Boolean(item.connect),
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
let connected: unknown[];
|
|
222
|
-
if (connect.length) {
|
|
223
|
-
connected = await Promise.all(
|
|
224
|
-
connect.map((item) => t.findBy(item.connect)._take()),
|
|
225
|
-
);
|
|
226
|
-
} else {
|
|
227
|
-
connected = [];
|
|
228
|
-
}
|
|
229
|
-
|
|
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 {
|
|
259
|
-
update[foreignKey] = await query
|
|
260
|
-
.transacting(q)
|
|
261
|
-
._findBy(params.set)
|
|
262
|
-
._get(primaryKey);
|
|
263
|
-
}
|
|
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
|
-
});
|
|
276
|
-
|
|
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);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
if (upsert) {
|
|
288
|
-
if (!state.updateLater) {
|
|
289
|
-
state.updateLater = {};
|
|
290
|
-
state.updateLaterPromises = [];
|
|
291
|
-
}
|
|
292
|
-
|
|
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
|
-
>[];
|
|
299
|
-
|
|
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
|
|
309
|
-
.transacting(q)
|
|
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
|
-
}
|
|
319
|
-
|
|
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);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return !params.update && !params.upsert;
|
|
351
|
-
}) as BelongsToNestedUpdate;
|
|
352
|
-
};
|