orchid-orm 1.6.40 → 1.7.0
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.d.ts +5 -2
- package/dist/index.js +45 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -55
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey,
|
|
1
|
+
import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Adapter, Db, anyShape, getClonedQueryData, columnsShapeToCode, primaryKeyToCode, indexToCode, constraintToCode, columnIndexesToCode, columnForeignKeysToCode, columnCheckToCode, identityToCode } from 'pqb';
|
|
2
2
|
export { OrchidOrmError, OrchidOrmInternalError, columnTypes } from 'pqb';
|
|
3
3
|
import { snakeCaseKey, toSnakeCase, emptyObject, pathToLog, toCamelCase, toPascalCase, singleQuote, codeToString, quoteObjectKey, addCode, columnDefaultArgumentToCode, deepCompare } from 'orchid-core';
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
5
|
import * as path from 'path';
|
|
5
6
|
import path__default from 'path';
|
|
6
7
|
import fs from 'fs/promises';
|
|
@@ -142,11 +143,11 @@ const makeBelongsToMethod = (relation, relationName, query) => {
|
|
|
142
143
|
};
|
|
143
144
|
};
|
|
144
145
|
const nestedInsert$3 = ({ query, primaryKey }) => {
|
|
145
|
-
return async (
|
|
146
|
+
return async (_, data) => {
|
|
146
147
|
const connectOrCreate = data.filter(
|
|
147
148
|
(item) => Boolean(item.connectOrCreate)
|
|
148
149
|
);
|
|
149
|
-
const t = query.
|
|
150
|
+
const t = query.clone();
|
|
150
151
|
let connectOrCreated;
|
|
151
152
|
if (connectOrCreate.length) {
|
|
152
153
|
connectOrCreated = await Promise.all(
|
|
@@ -210,12 +211,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
210
211
|
if (primaryKey in params.set) {
|
|
211
212
|
update[foreignKey] = params.set[primaryKey];
|
|
212
213
|
} else {
|
|
213
|
-
update[foreignKey] = await query.
|
|
214
|
+
update[foreignKey] = await query.findBy(params.set)._get(primaryKey);
|
|
214
215
|
}
|
|
215
216
|
} else if (params.create) {
|
|
216
|
-
update[foreignKey] = await query.
|
|
217
|
+
update[foreignKey] = await query.get(primaryKey)._create(params.create);
|
|
217
218
|
} else if (params.delete) {
|
|
218
|
-
const selectQuery = q2.
|
|
219
|
+
const selectQuery = q2.clone();
|
|
219
220
|
selectQuery.query.type = void 0;
|
|
220
221
|
idForDelete = await selectQuery._getOptional(foreignKey);
|
|
221
222
|
update[foreignKey] = null;
|
|
@@ -237,10 +238,10 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
237
238
|
const data = await handleResult(q2, queryResult);
|
|
238
239
|
const id = data[0][foreignKey];
|
|
239
240
|
if (id !== null) {
|
|
240
|
-
await query.
|
|
241
|
+
await query.findBy({ [primaryKey]: id })._update(upsert.update);
|
|
241
242
|
} else {
|
|
242
243
|
state.updateLaterPromises.push(
|
|
243
|
-
query.
|
|
244
|
+
query.select(primaryKey)._create(upsert.create).then((result) => {
|
|
244
245
|
state.updateLater[foreignKey] = result[primaryKey];
|
|
245
246
|
})
|
|
246
247
|
);
|
|
@@ -248,12 +249,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
248
249
|
return data;
|
|
249
250
|
};
|
|
250
251
|
} else if (params.delete || params.update) {
|
|
251
|
-
q._afterQuery(async (
|
|
252
|
+
q._afterQuery(async (_, data) => {
|
|
252
253
|
const id = params.delete ? idForDelete : Array.isArray(data) ? data.length === 0 ? null : {
|
|
253
254
|
in: data.map((item) => item[foreignKey]).filter((id2) => id2 !== null)
|
|
254
255
|
} : data[foreignKey];
|
|
255
256
|
if (id !== void 0 && id !== null) {
|
|
256
|
-
const t = query.
|
|
257
|
+
const t = query.findBy({
|
|
257
258
|
[primaryKey]: id
|
|
258
259
|
});
|
|
259
260
|
if (params.delete) {
|
|
@@ -445,11 +446,11 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
445
446
|
};
|
|
446
447
|
};
|
|
447
448
|
const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
|
|
448
|
-
return async (
|
|
449
|
+
return async (_, data) => {
|
|
449
450
|
const connect = data.filter(
|
|
450
451
|
(item) => Boolean(item[1].connect || item[1].connectOrCreate)
|
|
451
452
|
);
|
|
452
|
-
const t = query.
|
|
453
|
+
const t = query.clone();
|
|
453
454
|
let connected;
|
|
454
455
|
if (connect.length) {
|
|
455
456
|
connected = await Promise.all(
|
|
@@ -485,7 +486,7 @@ const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
485
486
|
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
486
487
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
487
488
|
}
|
|
488
|
-
const t = query.
|
|
489
|
+
const t = query.clone();
|
|
489
490
|
const ids = data.map((item) => item[primaryKey]);
|
|
490
491
|
const currentRelationsQuery = t.where({
|
|
491
492
|
[foreignKey]: { in: ids }
|
|
@@ -654,11 +655,11 @@ const getWhereForNestedUpdate = (data, params, primaryKey, foreignKey) => {
|
|
|
654
655
|
return where;
|
|
655
656
|
};
|
|
656
657
|
const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
|
|
657
|
-
return async (
|
|
658
|
+
return async (_, data) => {
|
|
658
659
|
const connect = data.filter(
|
|
659
660
|
(item) => Boolean(item[1].connect)
|
|
660
661
|
);
|
|
661
|
-
const t = query.
|
|
662
|
+
const t = query.clone();
|
|
662
663
|
if (connect.length) {
|
|
663
664
|
await Promise.all(
|
|
664
665
|
connect.flatMap(
|
|
@@ -721,7 +722,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
721
722
|
const key = params.set ? "set" : "create";
|
|
722
723
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
723
724
|
}
|
|
724
|
-
const t = query.
|
|
725
|
+
const t = query.clone();
|
|
725
726
|
if (params.create) {
|
|
726
727
|
await t._count()._createMany(
|
|
727
728
|
params.create.map((create) => __spreadProps$4(__spreadValues$6({}, create), {
|
|
@@ -895,10 +896,10 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
895
896
|
pushQueryValue(
|
|
896
897
|
relationQuery,
|
|
897
898
|
"afterCreate",
|
|
898
|
-
async (
|
|
899
|
+
async (_, result) => {
|
|
899
900
|
const fromQuery = ref.query.clone();
|
|
900
901
|
fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
|
|
901
|
-
const createdCount = await subQuery.
|
|
902
|
+
const createdCount = await subQuery.count()._createFrom(
|
|
902
903
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
903
904
|
fromQuery,
|
|
904
905
|
{
|
|
@@ -916,8 +917,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
916
917
|
}
|
|
917
918
|
};
|
|
918
919
|
};
|
|
919
|
-
const queryJoinTable = (state,
|
|
920
|
-
const t = state.joinTableQuery.
|
|
920
|
+
const queryJoinTable = (state, data, conditions) => {
|
|
921
|
+
const t = state.joinTableQuery.clone();
|
|
921
922
|
const where = {
|
|
922
923
|
[state.foreignKey]: { in: data.map((item) => item[state.primaryKey]) }
|
|
923
924
|
};
|
|
@@ -930,8 +931,10 @@ const queryJoinTable = (state, q, data, conditions) => {
|
|
|
930
931
|
}
|
|
931
932
|
return t._where(where);
|
|
932
933
|
};
|
|
933
|
-
const queryRelatedTable = (query,
|
|
934
|
-
return query.
|
|
934
|
+
const queryRelatedTable = (query, conditions) => {
|
|
935
|
+
return query.where(
|
|
936
|
+
Array.isArray(conditions) ? { OR: conditions } : conditions
|
|
937
|
+
);
|
|
935
938
|
};
|
|
936
939
|
const insertToJoinTable = (state, joinTableTransaction, data, ids) => {
|
|
937
940
|
return joinTableTransaction._count()._createMany(
|
|
@@ -951,11 +954,11 @@ const nestedInsert = ({
|
|
|
951
954
|
associationPrimaryKey,
|
|
952
955
|
associationForeignKey
|
|
953
956
|
}) => {
|
|
954
|
-
return async (
|
|
957
|
+
return async (_, data) => {
|
|
955
958
|
const connect = data.filter(
|
|
956
959
|
(item) => Boolean(item[1].connect)
|
|
957
960
|
);
|
|
958
|
-
const t = relatedTableQuery.
|
|
961
|
+
const t = relatedTableQuery.clone();
|
|
959
962
|
let connected;
|
|
960
963
|
if (connect.length) {
|
|
961
964
|
connected = await Promise.all(
|
|
@@ -1040,7 +1043,7 @@ const nestedInsert = ({
|
|
|
1040
1043
|
connectI += len;
|
|
1041
1044
|
}
|
|
1042
1045
|
});
|
|
1043
|
-
await joinTableQuery.
|
|
1046
|
+
await joinTableQuery.count()._createMany(
|
|
1044
1047
|
allKeys.flatMap(([selfData, relationKeys]) => {
|
|
1045
1048
|
const selfKey = selfData[primaryKey];
|
|
1046
1049
|
return relationKeys.map((relationData) => ({
|
|
@@ -1052,10 +1055,10 @@ const nestedInsert = ({
|
|
|
1052
1055
|
};
|
|
1053
1056
|
};
|
|
1054
1057
|
const nestedUpdate = (state) => {
|
|
1055
|
-
return async (
|
|
1058
|
+
return async (_, data, params) => {
|
|
1056
1059
|
if (params.create) {
|
|
1057
|
-
const ids = await state.relatedTableQuery.
|
|
1058
|
-
await state.joinTableQuery.
|
|
1060
|
+
const ids = await state.relatedTableQuery.pluck(state.associationPrimaryKey)._createMany(params.create);
|
|
1061
|
+
await state.joinTableQuery.createMany(
|
|
1059
1062
|
data.flatMap(
|
|
1060
1063
|
(item) => ids.map((id) => ({
|
|
1061
1064
|
[state.foreignKey]: item[state.primaryKey],
|
|
@@ -1065,11 +1068,11 @@ const nestedUpdate = (state) => {
|
|
|
1065
1068
|
);
|
|
1066
1069
|
}
|
|
1067
1070
|
if (params.update) {
|
|
1068
|
-
await state.relatedTableQuery.
|
|
1071
|
+
await state.relatedTableQuery.whereExists(
|
|
1069
1072
|
state.joinTableQuery,
|
|
1070
|
-
(
|
|
1073
|
+
(q) => (
|
|
1071
1074
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1072
|
-
|
|
1075
|
+
q._on(
|
|
1073
1076
|
state.associationForeignKeyFull,
|
|
1074
1077
|
state.associationPrimaryKeyFull
|
|
1075
1078
|
)._where({
|
|
@@ -1084,22 +1087,21 @@ const nestedUpdate = (state) => {
|
|
|
1084
1087
|
)._update(params.update.data);
|
|
1085
1088
|
}
|
|
1086
1089
|
if (params.disconnect) {
|
|
1087
|
-
await queryJoinTable(state,
|
|
1090
|
+
await queryJoinTable(state, data, params.disconnect)._delete();
|
|
1088
1091
|
}
|
|
1089
1092
|
if (params.delete) {
|
|
1090
|
-
const j = queryJoinTable(state,
|
|
1093
|
+
const j = queryJoinTable(state, data, params.delete);
|
|
1091
1094
|
const ids = await j._pluck(state.associationForeignKey)._delete();
|
|
1092
|
-
await queryRelatedTable(state.relatedTableQuery,
|
|
1095
|
+
await queryRelatedTable(state.relatedTableQuery, {
|
|
1093
1096
|
[state.associationPrimaryKey]: { in: ids }
|
|
1094
1097
|
})._delete();
|
|
1095
1098
|
}
|
|
1096
1099
|
if (params.set) {
|
|
1097
|
-
const j = queryJoinTable(state,
|
|
1100
|
+
const j = queryJoinTable(state, data);
|
|
1098
1101
|
await j._delete();
|
|
1099
1102
|
delete j.query[toSqlCacheKey];
|
|
1100
1103
|
const ids = await queryRelatedTable(
|
|
1101
1104
|
state.relatedTableQuery,
|
|
1102
|
-
q,
|
|
1103
1105
|
params.set
|
|
1104
1106
|
)._pluck(state.associationPrimaryKey);
|
|
1105
1107
|
await insertToJoinTable(state, j, data, ids);
|
|
@@ -1274,25 +1276,11 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
|
|
|
1274
1276
|
});
|
|
1275
1277
|
};
|
|
1276
1278
|
|
|
1277
|
-
function transaction(fn) {
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
const orm = {};
|
|
1283
|
-
for (const key in this) {
|
|
1284
|
-
const value = this[key];
|
|
1285
|
-
if (value instanceof Db) {
|
|
1286
|
-
const table = value.transacting(q);
|
|
1287
|
-
table.baseQuery = table;
|
|
1288
|
-
table.db = orm;
|
|
1289
|
-
orm[key] = table;
|
|
1290
|
-
} else {
|
|
1291
|
-
orm[key] = value;
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
return fn(orm);
|
|
1295
|
-
});
|
|
1279
|
+
function transaction(fnOrOptions, fn) {
|
|
1280
|
+
return this.$queryBuilder.transaction(
|
|
1281
|
+
fnOrOptions,
|
|
1282
|
+
fn
|
|
1283
|
+
);
|
|
1296
1284
|
}
|
|
1297
1285
|
|
|
1298
1286
|
var __defProp$4 = Object.defineProperty;
|
|
@@ -1345,12 +1333,14 @@ const orchidORM = (_a, tables) => {
|
|
|
1345
1333
|
autoPreparedStatements,
|
|
1346
1334
|
noPrimaryKey
|
|
1347
1335
|
};
|
|
1336
|
+
const transactionStorage = new AsyncLocalStorage();
|
|
1348
1337
|
const qb = new Db(
|
|
1349
1338
|
adapter,
|
|
1350
1339
|
void 0,
|
|
1351
1340
|
void 0,
|
|
1352
1341
|
anyShape,
|
|
1353
1342
|
columnTypes,
|
|
1343
|
+
transactionStorage,
|
|
1354
1344
|
commonOptions
|
|
1355
1345
|
);
|
|
1356
1346
|
qb.queryBuilder = qb;
|
|
@@ -1380,6 +1370,7 @@ const orchidORM = (_a, tables) => {
|
|
|
1380
1370
|
table.table,
|
|
1381
1371
|
table.columns.shape,
|
|
1382
1372
|
table.columnTypes,
|
|
1373
|
+
transactionStorage,
|
|
1383
1374
|
options2
|
|
1384
1375
|
);
|
|
1385
1376
|
dbTable.definedAs = key;
|