orchid-orm 1.10.6 → 1.11.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,17 +1,8 @@
1
- import { addQueryHook, columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Adapter, Db, anyShape, getClonedQueryData } from 'pqb';
1
+ import { columnTypes, QueryHooks, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, OrchidOrmInternalError, NotFoundError, relationQueryKey, Adapter, Db, anyShape, getClonedQueryData } from 'pqb';
2
2
  export { OrchidOrmError, OrchidOrmInternalError, columnTypes, testTransaction } from 'pqb';
3
- import { getCallerFilePath, snakeCaseKey, toSnakeCase } from 'orchid-core';
3
+ import { getCallerFilePath, applyMixins, snakeCaseKey, toSnakeCase } from 'orchid-core';
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
5
5
 
6
- const addTableHooks = (table, hooks) => {
7
- for (const key in hooks) {
8
- addQueryHook(
9
- table.baseQuery,
10
- key,
11
- hooks[key]
12
- );
13
- }
14
- };
15
6
  const createBaseTable = ({
16
7
  columnTypes: columnTypes$1,
17
8
  snakeCase,
@@ -39,10 +30,11 @@ const create = (columnTypes, filePath, snakeCase, nowSQL, exportAs = "BaseTable"
39
30
  const base = (_a = class {
40
31
  constructor() {
41
32
  this.snakeCase = snakeCase;
33
+ this.query = {};
42
34
  this.columnTypes = columnTypes;
43
35
  }
44
- setHooks(callbacks) {
45
- return callbacks;
36
+ clone() {
37
+ return this;
46
38
  }
47
39
  setColumns(fn) {
48
40
  if (!this.filePath) {
@@ -105,6 +97,7 @@ const create = (columnTypes, filePath, snakeCase, nowSQL, exportAs = "BaseTable"
105
97
  };
106
98
  }
107
99
  }, _a.filePath = filePath, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a);
100
+ applyMixins(base, [QueryHooks]);
108
101
  base.prototype.columnTypes = columnTypes;
109
102
  return base;
110
103
  };
@@ -224,9 +217,9 @@ const nestedInsert$3 = ({ query, primaryKey }) => {
224
217
  let createdI = 0;
225
218
  let connectedI = 0;
226
219
  connectOrCreatedI = 0;
227
- return data.map(
228
- (item) => item.connectOrCreate ? connectOrCreated[connectOrCreatedI++] || created[createdI++] : item.connect ? connected[connectedI++] : created[createdI++]
229
- );
220
+ return data.map((item) => {
221
+ return item.connectOrCreate ? connectOrCreated[connectOrCreatedI++] || created[createdI++] : item.connect ? connected[connectedI++] : created[createdI++];
222
+ });
230
223
  };
231
224
  };
232
225
  const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
@@ -235,7 +228,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
235
228
  if (params.upsert && isQueryReturnsAll(q)) {
236
229
  throw new Error("`upsert` option is not allowed in a batch update");
237
230
  }
238
- let idForDelete;
231
+ let idsForDelete;
239
232
  q._beforeUpdate(async (q2) => {
240
233
  if (params.disconnect) {
241
234
  update[foreignKey] = null;
@@ -250,7 +243,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
250
243
  } else if (params.delete) {
251
244
  const selectQuery = q2.clone();
252
245
  selectQuery.query.type = void 0;
253
- idForDelete = await selectQuery._getOptional(foreignKey);
246
+ idsForDelete = await selectQuery._pluck(foreignKey);
254
247
  update[foreignKey] = null;
255
248
  }
256
249
  });
@@ -273,12 +266,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
273
266
  }
274
267
  });
275
268
  } else if (params.delete || params.update) {
276
- q._afterQuery(async (_, data) => {
277
- const id = params.delete ? idForDelete : Array.isArray(data) ? data.length === 0 ? null : {
269
+ q._afterUpdate([], async (data) => {
270
+ const id = params.delete ? { in: idsForDelete } : Array.isArray(data) ? data.length === 0 ? null : {
278
271
  in: data.map((item) => item[foreignKey]).filter((id2) => id2 !== null)
279
272
  } : data[foreignKey];
280
273
  if (id !== void 0 && id !== null) {
281
- const t = query.findBy({
274
+ const t = query.where({
282
275
  [primaryKey]: id
283
276
  });
284
277
  if (params.delete) {
@@ -312,22 +305,20 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nested
312
305
  return;
313
306
  }
314
307
  q.query.wrapInTransaction = true;
315
- ctx.returnTypeAll = true;
316
- ctx.requiredReturning[primaryKey] = true;
317
308
  const relationData = [values];
318
309
  store.hasRelation[key] = relationData;
319
- pushQueryValue(q, "afterCreate", async (q2) => {
320
- const { resultAll } = ctx;
321
- return nestedInsert(
310
+ q._afterCreate(
311
+ [primaryKey],
312
+ (rows, q2) => nestedInsert(
322
313
  q2,
323
314
  relationData.map(([rowIndex2, data]) => [
324
- resultAll[rowIndex2],
315
+ rows[rowIndex2],
325
316
  data
326
317
  ])
327
- );
328
- });
318
+ )
319
+ );
329
320
  };
330
- const hasRelationHandleUpdate = (q, ctx, set, key, primaryKey, nestedUpdate) => {
321
+ const hasRelationHandleUpdate = (q, set, key, primaryKey, nestedUpdate) => {
331
322
  var _a, _b;
332
323
  const value = set[key];
333
324
  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))
@@ -336,11 +327,10 @@ const hasRelationHandleUpdate = (q, ctx, set, key, primaryKey, nestedUpdate) =>
336
327
  q._select(primaryKey);
337
328
  }
338
329
  q.query.wrapInTransaction = true;
339
- ctx.returnTypeAll = true;
340
- pushQueryValue(q, "afterUpdate", (q2) => {
330
+ q._afterUpdate(q.primaryKeys, (rows, q2) => {
341
331
  return nestedUpdate(
342
332
  q2,
343
- ctx.resultAll,
333
+ rows,
344
334
  value
345
335
  );
346
336
  });
@@ -384,10 +374,14 @@ class HasOneVirtualColumn extends VirtualColumn {
384
374
  this.nestedInsert
385
375
  );
386
376
  }
387
- update(q, ctx, set) {
377
+ update(q, _, set) {
378
+ const params = set[this.key];
379
+ if ((params.set || params.create || params.upsert) && isQueryReturnsAll(q)) {
380
+ const key = params.set ? "set" : params.create ? "create" : "upsert";
381
+ throw new Error(`\`${key}\` option is not allowed in a batch update`);
382
+ }
388
383
  hasRelationHandleUpdate(
389
384
  q,
390
- ctx,
391
385
  set,
392
386
  this.key,
393
387
  this.state.primaryKey,
@@ -500,11 +494,7 @@ const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
500
494
  };
501
495
  };
502
496
  const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
503
- return async (q, data, params) => {
504
- if ((params.set || params.create || params.upsert) && isQueryReturnsAll(q)) {
505
- const key = params.set ? "set" : params.create ? "create" : "upsert";
506
- throw new Error(`\`${key}\` option is not allowed in a batch update`);
507
- }
497
+ return async (_, data, params) => {
508
498
  const t = query.clone();
509
499
  const ids = data.map((item) => item[primaryKey]);
510
500
  const currentRelationsQuery = t.where({
@@ -577,10 +567,14 @@ class HasManyVirtualColumn extends VirtualColumn {
577
567
  this.nestedInsert
578
568
  );
579
569
  }
580
- update(q, ctx, set) {
570
+ update(q, _, set) {
571
+ const params = set[this.key];
572
+ if ((params.set || params.create) && isQueryReturnsAll(q)) {
573
+ const key = params.set ? "set" : "create";
574
+ throw new Error(`\`${key}\` option is not allowed in a batch update`);
575
+ }
581
576
  hasRelationHandleUpdate(
582
577
  q,
583
- ctx,
584
578
  set,
585
579
  this.key,
586
580
  this.state.primaryKey,
@@ -736,12 +730,8 @@ const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
736
730
  };
737
731
  };
738
732
  const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
739
- return async (q, data, params) => {
733
+ return async (_, data, params) => {
740
734
  var _a;
741
- if ((params.set || params.create) && isQueryReturnsAll(q)) {
742
- const key = params.set ? "set" : "create";
743
- throw new Error(`\`${key}\` option is not allowed in a batch update`);
744
- }
745
735
  const t = query.clone();
746
736
  if (params.create) {
747
737
  await t._count()._createMany(
@@ -771,7 +761,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
771
761
  }
772
762
  if (params.delete || params.update) {
773
763
  delete t.query[toSqlCacheKey];
774
- const q2 = t._where(
764
+ const q = t._where(
775
765
  getWhereForNestedUpdate(
776
766
  data,
777
767
  params.delete || ((_a = params.update) == null ? void 0 : _a.where),
@@ -780,9 +770,9 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
780
770
  )
781
771
  );
782
772
  if (params.delete) {
783
- await q2._delete();
773
+ await q._delete();
784
774
  } else if (params.update) {
785
- await q2._update(params.update.data);
775
+ await q._update(params.update.data);
786
776
  }
787
777
  }
788
778
  };
@@ -826,10 +816,9 @@ class HasAndBelongsToManyVirtualColumn extends VirtualColumn {
826
816
  this.nestedInsert
827
817
  );
828
818
  }
829
- update(q, ctx, set) {
819
+ update(q, _, set) {
830
820
  hasRelationHandleUpdate(
831
821
  q,
832
- ctx,
833
822
  set,
834
823
  this.key,
835
824
  this.state.primaryKey,
@@ -909,24 +898,26 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
909
898
  primaryKey: pk,
910
899
  modifyRelatedQuery(relationQuery) {
911
900
  const ref = {};
912
- pushQueryValue(
913
- relationQuery,
914
- "afterCreate",
915
- async (_, result) => {
916
- const fromQuery = ref.query.clone();
917
- fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
918
- const createdCount = await subQuery.count()._createFrom(
919
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
920
- fromQuery,
921
- {
922
- [afk]: result[apk]
923
- }
901
+ relationQuery._afterCreate([], async (result) => {
902
+ if (result.length > 1) {
903
+ throw new OrchidOrmInternalError(
904
+ relationQuery,
905
+ "Creating multiple `hasAndBelongsToMany` records is not yet supported"
924
906
  );
925
- if (createdCount === 0) {
926
- throw new NotFoundError(fromQuery);
907
+ }
908
+ const fromQuery = ref.query.clone();
909
+ fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
910
+ const createdCount = await subQuery.count()._createFrom(
911
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
912
+ fromQuery,
913
+ {
914
+ [afk]: result[0][apk]
927
915
  }
916
+ );
917
+ if (createdCount === 0) {
918
+ throw new NotFoundError(fromQuery);
928
919
  }
929
- );
920
+ });
930
921
  return (q) => {
931
922
  ref.query = q;
932
923
  };
@@ -1437,11 +1428,16 @@ const orchidORM = (_a, tables) => {
1437
1428
  dbTable.db = result;
1438
1429
  dbTable.filePath = table.filePath;
1439
1430
  dbTable.name = table.constructor.name;
1440
- if (table.hooks)
1441
- addTableHooks(dbTable, table.hooks);
1442
1431
  result[key] = dbTable;
1443
1432
  }
1444
1433
  applyRelations(qb, tableInstances, result);
1434
+ for (const key in tables) {
1435
+ const table = tableInstances[key];
1436
+ if (table.init) {
1437
+ table.init(result);
1438
+ Object.assign(result[key].baseQuery.query, table.query);
1439
+ }
1440
+ }
1445
1441
  return result;
1446
1442
  };
1447
1443
 
@@ -1488,5 +1484,5 @@ const createRepo = (table, methods) => {
1488
1484
  });
1489
1485
  };
1490
1486
 
1491
- export { addTableHooks, createBaseTable, createRepo, orchidORM };
1487
+ export { createBaseTable, createRepo, orchidORM };
1492
1488
  //# sourceMappingURL=index.mjs.map