orchid-orm 1.6.40 → 1.8.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.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Db, Adapter, anyShape, getClonedQueryData, columnsShapeToCode, primaryKeyToCode, indexToCode, constraintToCode, columnIndexesToCode, columnForeignKeysToCode, columnCheckToCode, identityToCode } from 'pqb';
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
- import { snakeCaseKey, toSnakeCase, emptyObject, pathToLog, toCamelCase, toPascalCase, singleQuote, codeToString, quoteObjectKey, addCode, columnDefaultArgumentToCode, deepCompare } from 'orchid-core';
3
+ import { snakeCaseKey, toSnakeCase, emptyObject, pathToLog, toCamelCase, toPascalCase, getImportPath, 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';
@@ -17,7 +18,7 @@ const createBaseTable = ({
17
18
  );
18
19
  };
19
20
  const create = (columnTypes, snakeCase) => {
20
- return class BaseTable {
21
+ const base = class BaseTable {
21
22
  constructor() {
22
23
  this.snakeCase = snakeCase;
23
24
  this.setColumns = (fn) => {
@@ -74,6 +75,8 @@ const create = (columnTypes, snakeCase) => {
74
75
  };
75
76
  }
76
77
  };
78
+ base.prototype.columnTypes = columnTypes;
79
+ return base;
77
80
  };
78
81
 
79
82
  class BelongsToVirtualColumn extends VirtualColumn {
@@ -142,11 +145,11 @@ const makeBelongsToMethod = (relation, relationName, query) => {
142
145
  };
143
146
  };
144
147
  const nestedInsert$3 = ({ query, primaryKey }) => {
145
- return async (q, data) => {
148
+ return async (_, data) => {
146
149
  const connectOrCreate = data.filter(
147
150
  (item) => Boolean(item.connectOrCreate)
148
151
  );
149
- const t = query.transacting(q);
152
+ const t = query.clone();
150
153
  let connectOrCreated;
151
154
  if (connectOrCreate.length) {
152
155
  connectOrCreated = await Promise.all(
@@ -210,12 +213,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
210
213
  if (primaryKey in params.set) {
211
214
  update[foreignKey] = params.set[primaryKey];
212
215
  } else {
213
- update[foreignKey] = await query.transacting(q2)._findBy(params.set)._get(primaryKey);
216
+ update[foreignKey] = await query.findBy(params.set)._get(primaryKey);
214
217
  }
215
218
  } else if (params.create) {
216
- update[foreignKey] = await query.transacting(q2)._get(primaryKey)._create(params.create);
219
+ update[foreignKey] = await query.get(primaryKey)._create(params.create);
217
220
  } else if (params.delete) {
218
- const selectQuery = q2.transacting(q2);
221
+ const selectQuery = q2.clone();
219
222
  selectQuery.query.type = void 0;
220
223
  idForDelete = await selectQuery._getOptional(foreignKey);
221
224
  update[foreignKey] = null;
@@ -237,10 +240,10 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
237
240
  const data = await handleResult(q2, queryResult);
238
241
  const id = data[0][foreignKey];
239
242
  if (id !== null) {
240
- await query.transacting(q2)._findBy({ [primaryKey]: id })._update(upsert.update);
243
+ await query.findBy({ [primaryKey]: id })._update(upsert.update);
241
244
  } else {
242
245
  state.updateLaterPromises.push(
243
- query.transacting(q2)._select(primaryKey)._create(upsert.create).then((result) => {
246
+ query.select(primaryKey)._create(upsert.create).then((result) => {
244
247
  state.updateLater[foreignKey] = result[primaryKey];
245
248
  })
246
249
  );
@@ -248,12 +251,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
248
251
  return data;
249
252
  };
250
253
  } else if (params.delete || params.update) {
251
- q._afterQuery(async (q2, data) => {
254
+ q._afterQuery(async (_, data) => {
252
255
  const id = params.delete ? idForDelete : Array.isArray(data) ? data.length === 0 ? null : {
253
256
  in: data.map((item) => item[foreignKey]).filter((id2) => id2 !== null)
254
257
  } : data[foreignKey];
255
258
  if (id !== void 0 && id !== null) {
256
- const t = query.transacting(q2)._findBy({
259
+ const t = query.findBy({
257
260
  [primaryKey]: id
258
261
  });
259
262
  if (params.delete) {
@@ -445,11 +448,11 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
445
448
  };
446
449
  };
447
450
  const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
448
- return async (q, data) => {
451
+ return async (_, data) => {
449
452
  const connect = data.filter(
450
453
  (item) => Boolean(item[1].connect || item[1].connectOrCreate)
451
454
  );
452
- const t = query.transacting(q);
455
+ const t = query.clone();
453
456
  let connected;
454
457
  if (connect.length) {
455
458
  connected = await Promise.all(
@@ -485,7 +488,7 @@ const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
485
488
  const key = params.set ? "set" : params.create ? "create" : "upsert";
486
489
  throw new Error(`\`${key}\` option is not allowed in a batch update`);
487
490
  }
488
- const t = query.transacting(q);
491
+ const t = query.clone();
489
492
  const ids = data.map((item) => item[primaryKey]);
490
493
  const currentRelationsQuery = t.where({
491
494
  [foreignKey]: { in: ids }
@@ -654,11 +657,11 @@ const getWhereForNestedUpdate = (data, params, primaryKey, foreignKey) => {
654
657
  return where;
655
658
  };
656
659
  const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
657
- return async (q, data) => {
660
+ return async (_, data) => {
658
661
  const connect = data.filter(
659
662
  (item) => Boolean(item[1].connect)
660
663
  );
661
- const t = query.transacting(q);
664
+ const t = query.clone();
662
665
  if (connect.length) {
663
666
  await Promise.all(
664
667
  connect.flatMap(
@@ -721,7 +724,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
721
724
  const key = params.set ? "set" : "create";
722
725
  throw new Error(`\`${key}\` option is not allowed in a batch update`);
723
726
  }
724
- const t = query.transacting(q);
727
+ const t = query.clone();
725
728
  if (params.create) {
726
729
  await t._count()._createMany(
727
730
  params.create.map((create) => __spreadProps$4(__spreadValues$6({}, create), {
@@ -895,10 +898,10 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
895
898
  pushQueryValue(
896
899
  relationQuery,
897
900
  "afterCreate",
898
- async (q, result) => {
901
+ async (_, result) => {
899
902
  const fromQuery = ref.query.clone();
900
903
  fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
901
- const createdCount = await subQuery.transacting(q).count()._createFrom(
904
+ const createdCount = await subQuery.count()._createFrom(
902
905
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
903
906
  fromQuery,
904
907
  {
@@ -916,8 +919,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
916
919
  }
917
920
  };
918
921
  };
919
- const queryJoinTable = (state, q, data, conditions) => {
920
- const t = state.joinTableQuery.transacting(q);
922
+ const queryJoinTable = (state, data, conditions) => {
923
+ const t = state.joinTableQuery.clone();
921
924
  const where = {
922
925
  [state.foreignKey]: { in: data.map((item) => item[state.primaryKey]) }
923
926
  };
@@ -930,8 +933,10 @@ const queryJoinTable = (state, q, data, conditions) => {
930
933
  }
931
934
  return t._where(where);
932
935
  };
933
- const queryRelatedTable = (query, q, conditions) => {
934
- return query.transacting(q)._where(Array.isArray(conditions) ? { OR: conditions } : conditions);
936
+ const queryRelatedTable = (query, conditions) => {
937
+ return query.where(
938
+ Array.isArray(conditions) ? { OR: conditions } : conditions
939
+ );
935
940
  };
936
941
  const insertToJoinTable = (state, joinTableTransaction, data, ids) => {
937
942
  return joinTableTransaction._count()._createMany(
@@ -951,11 +956,11 @@ const nestedInsert = ({
951
956
  associationPrimaryKey,
952
957
  associationForeignKey
953
958
  }) => {
954
- return async (q, data) => {
959
+ return async (_, data) => {
955
960
  const connect = data.filter(
956
961
  (item) => Boolean(item[1].connect)
957
962
  );
958
- const t = relatedTableQuery.transacting(q);
963
+ const t = relatedTableQuery.clone();
959
964
  let connected;
960
965
  if (connect.length) {
961
966
  connected = await Promise.all(
@@ -1040,7 +1045,7 @@ const nestedInsert = ({
1040
1045
  connectI += len;
1041
1046
  }
1042
1047
  });
1043
- await joinTableQuery.transacting(q)._count()._createMany(
1048
+ await joinTableQuery.count()._createMany(
1044
1049
  allKeys.flatMap(([selfData, relationKeys]) => {
1045
1050
  const selfKey = selfData[primaryKey];
1046
1051
  return relationKeys.map((relationData) => ({
@@ -1052,10 +1057,10 @@ const nestedInsert = ({
1052
1057
  };
1053
1058
  };
1054
1059
  const nestedUpdate = (state) => {
1055
- return async (q, data, params) => {
1060
+ return async (_, data, params) => {
1056
1061
  if (params.create) {
1057
- const ids = await state.relatedTableQuery.transacting(q)._pluck(state.associationPrimaryKey)._createMany(params.create);
1058
- await state.joinTableQuery.transacting(q)._createMany(
1062
+ const ids = await state.relatedTableQuery.pluck(state.associationPrimaryKey)._createMany(params.create);
1063
+ await state.joinTableQuery.createMany(
1059
1064
  data.flatMap(
1060
1065
  (item) => ids.map((id) => ({
1061
1066
  [state.foreignKey]: item[state.primaryKey],
@@ -1065,11 +1070,11 @@ const nestedUpdate = (state) => {
1065
1070
  );
1066
1071
  }
1067
1072
  if (params.update) {
1068
- await state.relatedTableQuery.transacting(q)._whereExists(
1073
+ await state.relatedTableQuery.whereExists(
1069
1074
  state.joinTableQuery,
1070
- (q2) => (
1075
+ (q) => (
1071
1076
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1072
- q2._on(
1077
+ q._on(
1073
1078
  state.associationForeignKeyFull,
1074
1079
  state.associationPrimaryKeyFull
1075
1080
  )._where({
@@ -1084,22 +1089,21 @@ const nestedUpdate = (state) => {
1084
1089
  )._update(params.update.data);
1085
1090
  }
1086
1091
  if (params.disconnect) {
1087
- await queryJoinTable(state, q, data, params.disconnect)._delete();
1092
+ await queryJoinTable(state, data, params.disconnect)._delete();
1088
1093
  }
1089
1094
  if (params.delete) {
1090
- const j = queryJoinTable(state, q, data, params.delete);
1095
+ const j = queryJoinTable(state, data, params.delete);
1091
1096
  const ids = await j._pluck(state.associationForeignKey)._delete();
1092
- await queryRelatedTable(state.relatedTableQuery, q, {
1097
+ await queryRelatedTable(state.relatedTableQuery, {
1093
1098
  [state.associationPrimaryKey]: { in: ids }
1094
1099
  })._delete();
1095
1100
  }
1096
1101
  if (params.set) {
1097
- const j = queryJoinTable(state, q, data);
1102
+ const j = queryJoinTable(state, data);
1098
1103
  await j._delete();
1099
1104
  delete j.query[toSqlCacheKey];
1100
1105
  const ids = await queryRelatedTable(
1101
1106
  state.relatedTableQuery,
1102
- q,
1103
1107
  params.set
1104
1108
  )._pluck(state.associationPrimaryKey);
1105
1109
  await insertToJoinTable(state, j, data, ids);
@@ -1274,25 +1278,11 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
1274
1278
  });
1275
1279
  };
1276
1280
 
1277
- function transaction(fn) {
1278
- if (fn.length === 0) {
1279
- throw new Error("Argument of $transaction callback should be used");
1280
- }
1281
- return this.$queryBuilder.transaction((q) => {
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
- });
1281
+ function transaction(fnOrOptions, fn) {
1282
+ return this.$queryBuilder.transaction(
1283
+ fnOrOptions,
1284
+ fn
1285
+ );
1296
1286
  }
1297
1287
 
1298
1288
  var __defProp$4 = Object.defineProperty;
@@ -1345,12 +1335,14 @@ const orchidORM = (_a, tables) => {
1345
1335
  autoPreparedStatements,
1346
1336
  noPrimaryKey
1347
1337
  };
1338
+ const transactionStorage = new AsyncLocalStorage();
1348
1339
  const qb = new Db(
1349
1340
  adapter,
1350
1341
  void 0,
1351
1342
  void 0,
1352
1343
  anyShape,
1353
1344
  columnTypes,
1345
+ transactionStorage,
1354
1346
  commonOptions
1355
1347
  );
1356
1348
  qb.queryBuilder = qb;
@@ -1380,6 +1372,7 @@ const orchidORM = (_a, tables) => {
1380
1372
  table.table,
1381
1373
  table.columns.shape,
1382
1374
  table.columnTypes,
1375
+ transactionStorage,
1383
1376
  options2
1384
1377
  );
1385
1378
  dbTable.definedAs = key;
@@ -1605,12 +1598,6 @@ const ts = {
1605
1598
  }
1606
1599
  };
1607
1600
 
1608
- const getImportPath = (from, to) => {
1609
- const rel = path__default.relative(path__default.dirname(from), to).split(path__default.sep).join(path__default.posix.sep);
1610
- const importPath = rel.startsWith("./") || rel.startsWith("../") ? rel : `./${rel}`;
1611
- return importPath.replace(/\.[tj]s$/, "");
1612
- };
1613
-
1614
1601
  const libraryName = "orchid-orm";
1615
1602
  const importKey = "orchidORM";
1616
1603
  const newFile = (options) => `import { orchidORM } from 'orchid-orm';