orchid-orm 1.10.6 → 1.11.1
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/codegen/index.d.ts +1 -1
- package/codegen/index.js +8 -4
- package/codegen/index.js.map +1 -1
- package/codegen/index.mjs +8 -4
- package/codegen/index.mjs.map +1 -1
- package/dist/index.d.ts +32 -20
- package/dist/index.js +92 -86
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +95 -88
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -4,15 +4,6 @@ var pqb = require('pqb');
|
|
|
4
4
|
var orchidCore = require('orchid-core');
|
|
5
5
|
var node_async_hooks = require('node:async_hooks');
|
|
6
6
|
|
|
7
|
-
const addTableHooks = (table, hooks) => {
|
|
8
|
-
for (const key in hooks) {
|
|
9
|
-
pqb.addQueryHook(
|
|
10
|
-
table.baseQuery,
|
|
11
|
-
key,
|
|
12
|
-
hooks[key]
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
7
|
const createBaseTable = ({
|
|
17
8
|
columnTypes,
|
|
18
9
|
snakeCase,
|
|
@@ -21,40 +12,52 @@ const createBaseTable = ({
|
|
|
21
12
|
exportAs
|
|
22
13
|
} = { columnTypes: pqb.columnTypes }) => {
|
|
23
14
|
const ct = typeof columnTypes === "function" ? columnTypes(pqb.columnTypes) : columnTypes || pqb.columnTypes;
|
|
24
|
-
filePath != null ? filePath : filePath = orchidCore.getCallerFilePath();
|
|
25
|
-
if (!filePath) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
`Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
15
|
return create(
|
|
31
16
|
ct,
|
|
32
|
-
filePath
|
|
17
|
+
// stack is needed only if filePath wasn't given
|
|
18
|
+
filePath || orchidCore.getStackTrace(),
|
|
33
19
|
snakeCase,
|
|
34
20
|
nowSQL,
|
|
35
21
|
exportAs
|
|
36
22
|
);
|
|
37
23
|
};
|
|
38
|
-
const create = (columnTypes,
|
|
24
|
+
const create = (columnTypes, filePathOrStack, snakeCase, nowSQL, exportAs = "BaseTable") => {
|
|
39
25
|
var _a;
|
|
26
|
+
let filePath;
|
|
40
27
|
const base = (_a = class {
|
|
41
28
|
constructor() {
|
|
42
29
|
this.snakeCase = snakeCase;
|
|
43
30
|
this.columnTypes = columnTypes;
|
|
31
|
+
this.query = {};
|
|
44
32
|
}
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
static getFilePath() {
|
|
34
|
+
if (filePath)
|
|
35
|
+
return filePath;
|
|
36
|
+
if (typeof filePathOrStack === "string")
|
|
37
|
+
return filePath = filePathOrStack;
|
|
38
|
+
filePath = orchidCore.getCallerFilePath(filePathOrStack);
|
|
39
|
+
if (filePath)
|
|
40
|
+
return filePath;
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
clone() {
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
getFilePath() {
|
|
49
|
+
if (this.filePath)
|
|
50
|
+
return this.filePath;
|
|
51
|
+
if (typeof filePathOrStack === "string")
|
|
52
|
+
return this.filePath = filePathOrStack;
|
|
53
|
+
const filePath2 = orchidCore.getCallerFilePath(filePathOrStack);
|
|
54
|
+
if (filePath2)
|
|
55
|
+
return this.filePath = filePath2;
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Failed to determine file path for table ${this.constructor.name}. Please set \`filePath\` property manually`
|
|
58
|
+
);
|
|
47
59
|
}
|
|
48
60
|
setColumns(fn) {
|
|
49
|
-
if (!this.filePath) {
|
|
50
|
-
const filePath2 = orchidCore.getCallerFilePath();
|
|
51
|
-
if (!filePath2) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
`Failed to determine file path for table ${this.constructor.name}. Please set \`filePath\` property manually`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
this.filePath = filePath2;
|
|
57
|
-
}
|
|
58
61
|
columnTypes[orchidCore.snakeCaseKey] = this.snakeCase;
|
|
59
62
|
const shape = pqb.getColumnTypes(columnTypes, fn, nowSQL);
|
|
60
63
|
if (this.snakeCase) {
|
|
@@ -105,7 +108,8 @@ const create = (columnTypes, filePath, snakeCase, nowSQL, exportAs = "BaseTable"
|
|
|
105
108
|
options
|
|
106
109
|
};
|
|
107
110
|
}
|
|
108
|
-
}, _a.
|
|
111
|
+
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a);
|
|
112
|
+
orchidCore.applyMixins(base, [pqb.QueryHooks]);
|
|
109
113
|
base.prototype.columnTypes = columnTypes;
|
|
110
114
|
return base;
|
|
111
115
|
};
|
|
@@ -225,9 +229,9 @@ const nestedInsert$3 = ({ query, primaryKey }) => {
|
|
|
225
229
|
let createdI = 0;
|
|
226
230
|
let connectedI = 0;
|
|
227
231
|
connectOrCreatedI = 0;
|
|
228
|
-
return data.map(
|
|
229
|
-
|
|
230
|
-
);
|
|
232
|
+
return data.map((item) => {
|
|
233
|
+
return item.connectOrCreate ? connectOrCreated[connectOrCreatedI++] || created[createdI++] : item.connect ? connected[connectedI++] : created[createdI++];
|
|
234
|
+
});
|
|
231
235
|
};
|
|
232
236
|
};
|
|
233
237
|
const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
@@ -236,7 +240,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
236
240
|
if (params.upsert && pqb.isQueryReturnsAll(q)) {
|
|
237
241
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
238
242
|
}
|
|
239
|
-
let
|
|
243
|
+
let idsForDelete;
|
|
240
244
|
q._beforeUpdate(async (q2) => {
|
|
241
245
|
if (params.disconnect) {
|
|
242
246
|
update[foreignKey] = null;
|
|
@@ -251,7 +255,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
251
255
|
} else if (params.delete) {
|
|
252
256
|
const selectQuery = q2.clone();
|
|
253
257
|
selectQuery.query.type = void 0;
|
|
254
|
-
|
|
258
|
+
idsForDelete = await selectQuery._pluck(foreignKey);
|
|
255
259
|
update[foreignKey] = null;
|
|
256
260
|
}
|
|
257
261
|
});
|
|
@@ -274,12 +278,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
274
278
|
}
|
|
275
279
|
});
|
|
276
280
|
} else if (params.delete || params.update) {
|
|
277
|
-
q.
|
|
278
|
-
const id = params.delete ?
|
|
281
|
+
q._afterUpdate([], async (data) => {
|
|
282
|
+
const id = params.delete ? { in: idsForDelete } : Array.isArray(data) ? data.length === 0 ? null : {
|
|
279
283
|
in: data.map((item) => item[foreignKey]).filter((id2) => id2 !== null)
|
|
280
284
|
} : data[foreignKey];
|
|
281
285
|
if (id !== void 0 && id !== null) {
|
|
282
|
-
const t = query.
|
|
286
|
+
const t = query.where({
|
|
283
287
|
[primaryKey]: id
|
|
284
288
|
});
|
|
285
289
|
if (params.delete) {
|
|
@@ -313,22 +317,20 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nested
|
|
|
313
317
|
return;
|
|
314
318
|
}
|
|
315
319
|
q.query.wrapInTransaction = true;
|
|
316
|
-
ctx.returnTypeAll = true;
|
|
317
|
-
ctx.requiredReturning[primaryKey] = true;
|
|
318
320
|
const relationData = [values];
|
|
319
321
|
store.hasRelation[key] = relationData;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
322
|
+
q._afterCreate(
|
|
323
|
+
[primaryKey],
|
|
324
|
+
(rows, q2) => nestedInsert(
|
|
323
325
|
q2,
|
|
324
326
|
relationData.map(([rowIndex2, data]) => [
|
|
325
|
-
|
|
327
|
+
rows[rowIndex2],
|
|
326
328
|
data
|
|
327
329
|
])
|
|
328
|
-
)
|
|
329
|
-
|
|
330
|
+
)
|
|
331
|
+
);
|
|
330
332
|
};
|
|
331
|
-
const hasRelationHandleUpdate = (q,
|
|
333
|
+
const hasRelationHandleUpdate = (q, set, key, primaryKey, nestedUpdate) => {
|
|
332
334
|
var _a, _b;
|
|
333
335
|
const value = set[key];
|
|
334
336
|
if (!value.set && !("upsert" in value) && (!value.disconnect || Array.isArray(value.disconnect) && value.disconnect.length === 0) && (!value.delete || Array.isArray(value.delete) && value.delete.length === 0) && (!value.update || Array.isArray(value.update.where) && value.update.where.length === 0) && (!value.create || Array.isArray(value.create) && value.create.length === 0))
|
|
@@ -337,11 +339,10 @@ const hasRelationHandleUpdate = (q, ctx, set, key, primaryKey, nestedUpdate) =>
|
|
|
337
339
|
q._select(primaryKey);
|
|
338
340
|
}
|
|
339
341
|
q.query.wrapInTransaction = true;
|
|
340
|
-
|
|
341
|
-
pqb.pushQueryValue(q, "afterUpdate", (q2) => {
|
|
342
|
+
q._afterUpdate(q.primaryKeys, (rows, q2) => {
|
|
342
343
|
return nestedUpdate(
|
|
343
344
|
q2,
|
|
344
|
-
|
|
345
|
+
rows,
|
|
345
346
|
value
|
|
346
347
|
);
|
|
347
348
|
});
|
|
@@ -385,10 +386,14 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
385
386
|
this.nestedInsert
|
|
386
387
|
);
|
|
387
388
|
}
|
|
388
|
-
update(q,
|
|
389
|
+
update(q, _, set) {
|
|
390
|
+
const params = set[this.key];
|
|
391
|
+
if ((params.set || params.create || params.upsert) && pqb.isQueryReturnsAll(q)) {
|
|
392
|
+
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
393
|
+
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
394
|
+
}
|
|
389
395
|
hasRelationHandleUpdate(
|
|
390
396
|
q,
|
|
391
|
-
ctx,
|
|
392
397
|
set,
|
|
393
398
|
this.key,
|
|
394
399
|
this.state.primaryKey,
|
|
@@ -501,11 +506,7 @@ const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
501
506
|
};
|
|
502
507
|
};
|
|
503
508
|
const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
504
|
-
return async (
|
|
505
|
-
if ((params.set || params.create || params.upsert) && pqb.isQueryReturnsAll(q)) {
|
|
506
|
-
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
507
|
-
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
508
|
-
}
|
|
509
|
+
return async (_, data, params) => {
|
|
509
510
|
const t = query.clone();
|
|
510
511
|
const ids = data.map((item) => item[primaryKey]);
|
|
511
512
|
const currentRelationsQuery = t.where({
|
|
@@ -578,10 +579,14 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
578
579
|
this.nestedInsert
|
|
579
580
|
);
|
|
580
581
|
}
|
|
581
|
-
update(q,
|
|
582
|
+
update(q, _, set) {
|
|
583
|
+
const params = set[this.key];
|
|
584
|
+
if ((params.set || params.create) && pqb.isQueryReturnsAll(q)) {
|
|
585
|
+
const key = params.set ? "set" : "create";
|
|
586
|
+
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
587
|
+
}
|
|
582
588
|
hasRelationHandleUpdate(
|
|
583
589
|
q,
|
|
584
|
-
ctx,
|
|
585
590
|
set,
|
|
586
591
|
this.key,
|
|
587
592
|
this.state.primaryKey,
|
|
@@ -737,12 +742,8 @@ const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
737
742
|
};
|
|
738
743
|
};
|
|
739
744
|
const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
740
|
-
return async (
|
|
745
|
+
return async (_, data, params) => {
|
|
741
746
|
var _a;
|
|
742
|
-
if ((params.set || params.create) && pqb.isQueryReturnsAll(q)) {
|
|
743
|
-
const key = params.set ? "set" : "create";
|
|
744
|
-
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
745
|
-
}
|
|
746
747
|
const t = query.clone();
|
|
747
748
|
if (params.create) {
|
|
748
749
|
await t._count()._createMany(
|
|
@@ -772,7 +773,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
772
773
|
}
|
|
773
774
|
if (params.delete || params.update) {
|
|
774
775
|
delete t.query[pqb.toSqlCacheKey];
|
|
775
|
-
const
|
|
776
|
+
const q = t._where(
|
|
776
777
|
getWhereForNestedUpdate(
|
|
777
778
|
data,
|
|
778
779
|
params.delete || ((_a = params.update) == null ? void 0 : _a.where),
|
|
@@ -781,9 +782,9 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
781
782
|
)
|
|
782
783
|
);
|
|
783
784
|
if (params.delete) {
|
|
784
|
-
await
|
|
785
|
+
await q._delete();
|
|
785
786
|
} else if (params.update) {
|
|
786
|
-
await
|
|
787
|
+
await q._update(params.update.data);
|
|
787
788
|
}
|
|
788
789
|
}
|
|
789
790
|
};
|
|
@@ -827,10 +828,9 @@ class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
827
828
|
this.nestedInsert
|
|
828
829
|
);
|
|
829
830
|
}
|
|
830
|
-
update(q,
|
|
831
|
+
update(q, _, set) {
|
|
831
832
|
hasRelationHandleUpdate(
|
|
832
833
|
q,
|
|
833
|
-
ctx,
|
|
834
834
|
set,
|
|
835
835
|
this.key,
|
|
836
836
|
this.state.primaryKey,
|
|
@@ -910,24 +910,26 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
910
910
|
primaryKey: pk,
|
|
911
911
|
modifyRelatedQuery(relationQuery) {
|
|
912
912
|
const ref = {};
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
|
|
919
|
-
const createdCount = await subQuery.count()._createFrom(
|
|
920
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
921
|
-
fromQuery,
|
|
922
|
-
{
|
|
923
|
-
[afk]: result[apk]
|
|
924
|
-
}
|
|
913
|
+
relationQuery._afterCreate([], async (result) => {
|
|
914
|
+
if (result.length > 1) {
|
|
915
|
+
throw new pqb.OrchidOrmInternalError(
|
|
916
|
+
relationQuery,
|
|
917
|
+
"Creating multiple `hasAndBelongsToMany` records is not yet supported"
|
|
925
918
|
);
|
|
926
|
-
|
|
927
|
-
|
|
919
|
+
}
|
|
920
|
+
const fromQuery = ref.query.clone();
|
|
921
|
+
fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
|
|
922
|
+
const createdCount = await subQuery.count()._createFrom(
|
|
923
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
924
|
+
fromQuery,
|
|
925
|
+
{
|
|
926
|
+
[afk]: result[0][apk]
|
|
928
927
|
}
|
|
928
|
+
);
|
|
929
|
+
if (createdCount === 0) {
|
|
930
|
+
throw new pqb.NotFoundError(fromQuery);
|
|
929
931
|
}
|
|
930
|
-
);
|
|
932
|
+
});
|
|
931
933
|
return (q) => {
|
|
932
934
|
ref.query = q;
|
|
933
935
|
};
|
|
@@ -1438,11 +1440,16 @@ const orchidORM = (_a, tables) => {
|
|
|
1438
1440
|
dbTable.db = result;
|
|
1439
1441
|
dbTable.filePath = table.filePath;
|
|
1440
1442
|
dbTable.name = table.constructor.name;
|
|
1441
|
-
if (table.hooks)
|
|
1442
|
-
addTableHooks(dbTable, table.hooks);
|
|
1443
1443
|
result[key] = dbTable;
|
|
1444
1444
|
}
|
|
1445
1445
|
applyRelations(qb, tableInstances, result);
|
|
1446
|
+
for (const key in tables) {
|
|
1447
|
+
const table = tableInstances[key];
|
|
1448
|
+
if (table.init) {
|
|
1449
|
+
table.init(result);
|
|
1450
|
+
Object.assign(result[key].baseQuery.query, table.query);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1446
1453
|
return result;
|
|
1447
1454
|
};
|
|
1448
1455
|
|
|
@@ -1505,7 +1512,6 @@ Object.defineProperty(exports, 'testTransaction', {
|
|
|
1505
1512
|
enumerable: true,
|
|
1506
1513
|
get: function () { return pqb.testTransaction; }
|
|
1507
1514
|
});
|
|
1508
|
-
exports.addTableHooks = addTableHooks;
|
|
1509
1515
|
exports.createBaseTable = createBaseTable;
|
|
1510
1516
|
exports.createRepo = createRepo;
|
|
1511
1517
|
exports.orchidORM = orchidORM;
|