orchid-orm 1.40.2 → 1.41.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,4 +1,4 @@
1
- import { makeColumnTypes, QueryHooks, defaultSchemaConfig, raw, getColumnTypes, parseTableData, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValue, pushQueryOnForOuter, VirtualColumn, pushQueryValue, _queryCreateMany, isQueryReturnsAll, _queryHookBeforeUpdate, _queryFindBy, _queryCreate, _queryRows, _queryUpdate, _queryDelete, _queryDefaults, _queryUpdateOrThrow, _queryWhere, OrchidOrmInternalError, _queryJoinOn, _queryCreateFrom, NotFoundError, _queryFindByOptional, _querySelect, _queryWhereExists, _queryTake, _queryTakeOptional, Adapter, _initQueryBuilder, Db, getClonedQueryData } from 'pqb';
1
+ import { makeColumnTypes, QueryHooks, defaultSchemaConfig, raw, getColumnTypes, parseTableData, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValueImmutable, pushQueryOnForOuter, _queryWhere, _queryDefaults, VirtualColumn, pushQueryValueImmutable, _queryCreateMany, isQueryReturnsAll, _queryHookBeforeUpdate, _queryFindBy, _queryCreate, _queryRows, _queryUpdate, _queryDelete, _queryUpdateOrThrow, OrchidOrmInternalError, _queryJoinOn, _queryCreateFrom, NotFoundError, _queryFindByOptional, _querySelect, _queryWhereExists, _queryTake, _queryTakeOptional, Adapter, _initQueryBuilder, Db, getClonedQueryData } from 'pqb';
2
2
  export * from 'pqb';
3
3
  import { getStackTrace, applyMixins, getCallerFilePath, snakeCaseKey, toSnakeCase, emptyObject, emptyArray, toArray, objectHasValues, pick } from 'orchid-core';
4
4
  export * from 'orchid-core';
@@ -204,13 +204,6 @@ const selectIfNotSelected = (q, columns) => {
204
204
  q.q.select = select;
205
205
  }
206
206
  };
207
- const relationWhere = (len, keys, valueKeys) => (params) => {
208
- const obj = {};
209
- for (let i = 0; i < len; i++) {
210
- obj[keys[i]] = params[valueKeys[i]];
211
- }
212
- return obj;
213
- };
214
207
  function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
215
208
  return q.whereExists(
216
209
  throughRelation.joinQuery(throughRelation.query, baseQuery),
@@ -224,14 +217,9 @@ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelat
224
217
  );
225
218
  }
226
219
  function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
227
- const q = joiningQuery.clone();
228
- setQueryObjectValue(
229
- q,
230
- "joinedShapes",
231
- baseQuery.q.as || baseQuery.table,
232
- baseQuery.q.shape
233
- );
234
220
  const baseAs = getQueryAs(baseQuery);
221
+ const q = joiningQuery.clone();
222
+ setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
235
223
  for (let i = 0; i < len; i++) {
236
224
  pushQueryOnForOuter(
237
225
  q,
@@ -340,7 +328,7 @@ class BelongsToVirtualColumn extends VirtualColumn {
340
328
  const relationData = [values];
341
329
  store.belongsTo[key] = relationData;
342
330
  q.q.wrapInTransaction = true;
343
- pushQueryValue(q, "beforeCreate", async (q2) => {
331
+ pushQueryValueImmutable(q, "beforeCreate", async (q2) => {
344
332
  const inserted = await this.nestedInsert(
345
333
  q2,
346
334
  relationData.map(([, , data]) => data)
@@ -364,9 +352,13 @@ class BelongsToVirtualColumn extends VirtualColumn {
364
352
  const makeBelongsToMethod = (tableConfig, table, relation, relationName, query) => {
365
353
  const primaryKeys = relation.options.references;
366
354
  const foreignKeys = relation.options.columns;
355
+ const { on } = relation.options;
356
+ if (on) {
357
+ _queryWhere(query, [on]);
358
+ _queryDefaults(query, on);
359
+ }
367
360
  const len = primaryKeys.length;
368
- const state = { query, primaryKeys, foreignKeys, len };
369
- const makeWhere = relationWhere(len, primaryKeys, foreignKeys);
361
+ const state = { query, primaryKeys, foreignKeys, len, on };
370
362
  addAutoForeignKey(
371
363
  tableConfig,
372
364
  table,
@@ -376,14 +368,9 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
376
368
  relation.options
377
369
  );
378
370
  const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
379
- const q = joiningQuery.clone();
380
- setQueryObjectValue(
381
- q,
382
- "joinedShapes",
383
- baseQuery.q.as || baseQuery.table,
384
- baseQuery.q.shape
385
- );
386
371
  const baseAs = getQueryAs(baseQuery);
372
+ const q = joiningQuery.clone();
373
+ setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
387
374
  for (let i = 0; i < len; i++) {
388
375
  pushQueryOnForOuter(
389
376
  q,
@@ -406,7 +393,11 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
406
393
  return {
407
394
  returns: "one",
408
395
  queryRelated(params) {
409
- return query.where(makeWhere(params));
396
+ const obj = {};
397
+ for (let i = 0; i < len; i++) {
398
+ obj[primaryKeys[i]] = params[foreignKeys[i]];
399
+ }
400
+ return query.where(obj);
410
401
  },
411
402
  virtualColumn: new BelongsToVirtualColumn(
412
403
  defaultSchemaConfig,
@@ -420,13 +411,21 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
420
411
  reverseJoin
421
412
  };
422
413
  };
423
- const nestedInsert$3 = ({ query, primaryKeys }) => {
414
+ const nestedInsert$3 = ({ query, primaryKeys, on }) => {
424
415
  return async (_, data) => {
425
416
  const t = query.clone();
426
417
  const items = [];
427
418
  for (const item of data) {
428
419
  if (item.connectOrCreate) {
429
- items.push(item);
420
+ items.push(
421
+ on ? {
422
+ ...item,
423
+ connectOrCreate: {
424
+ ...item.connectOrCreate,
425
+ where: { ...item.connectOrCreate.where, ...on }
426
+ }
427
+ } : item
428
+ );
430
429
  }
431
430
  }
432
431
  let connectOrCreated;
@@ -464,7 +463,9 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
464
463
  items.length = 0;
465
464
  for (const item of data) {
466
465
  if (item.connect) {
467
- items.push(item);
466
+ items.push(
467
+ on ? { ...item, connect: { ...item.connect, ...on } } : item
468
+ );
468
469
  }
469
470
  }
470
471
  let connected;
@@ -551,12 +552,11 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
551
552
  }
552
553
  obj[primaryKeys[i]] = id;
553
554
  }
554
- if (obj) {
555
- await _queryUpdate(
556
- query.findBy(obj),
557
- upsert.update
558
- );
559
- } else {
555
+ const count = obj ? await _queryUpdate(
556
+ query.findBy(obj),
557
+ upsert.update
558
+ ) : 0;
559
+ if (!count) {
560
560
  const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
561
561
  const result = await _queryCreate(
562
562
  query.select(...primaryKeys),
@@ -647,10 +647,13 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
647
647
  const { through, source } = relation.options;
648
648
  const throughRelation = getThroughRelation(table, through);
649
649
  const sourceRelation = getSourceRelation(throughRelation, source);
650
+ const sourceRelationQuery = sourceRelation.query.as(
651
+ relationName
652
+ );
650
653
  const sourceQuery = sourceRelation.joinQuery(
651
- sourceRelation.query,
654
+ sourceRelationQuery,
652
655
  throughRelation.query
653
- ).as(relationName);
656
+ );
654
657
  const whereExistsCallback = () => sourceQuery;
655
658
  const reverseJoin2 = (baseQuery, joiningQuery) => {
656
659
  return joinHasThrough(
@@ -682,6 +685,11 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
682
685
  }
683
686
  const primaryKeys = relation.options.columns;
684
687
  const foreignKeys = relation.options.references;
688
+ const { on } = relation.options;
689
+ if (on) {
690
+ _queryWhere(query, [on]);
691
+ _queryDefaults(query, on);
692
+ }
685
693
  addAutoForeignKey(
686
694
  tableConfig,
687
695
  query,
@@ -690,7 +698,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
690
698
  foreignKeys,
691
699
  relation.options
692
700
  );
693
- const state = { query, primaryKeys, foreignKeys };
701
+ const state = { query, primaryKeys, foreignKeys, on };
694
702
  const len = primaryKeys.length;
695
703
  const reversedOn = {};
696
704
  for (let i = 0; i < len; i++) {
@@ -713,7 +721,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
713
721
  for (let i = 0; i < len; i++) {
714
722
  values[foreignKeys[i]] = params[primaryKeys[i]];
715
723
  }
716
- return _queryDefaults(query.where(values), values);
724
+ return _queryDefaults(query.where(values), { ...on, ...values });
717
725
  },
718
726
  virtualColumn: new HasOneVirtualColumn(
719
727
  defaultSchemaConfig,
@@ -837,7 +845,9 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
837
845
  } else if (params.update) {
838
846
  await _queryUpdate(currentRelationsQuery, params.update);
839
847
  } else if (params.delete) {
840
- await _queryDelete(currentRelationsQuery);
848
+ const q = _queryDelete(currentRelationsQuery);
849
+ q.q.returnType = "value";
850
+ await q;
841
851
  } else if (params.upsert) {
842
852
  const { update, create } = params.upsert;
843
853
  currentRelationsQuery.q.select = foreignKeys;
@@ -946,6 +956,11 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
946
956
  }
947
957
  const primaryKeys = relation.options.columns;
948
958
  const foreignKeys = relation.options.references;
959
+ const { on } = relation.options;
960
+ if (on) {
961
+ _queryWhere(query, [on]);
962
+ _queryDefaults(query, on);
963
+ }
949
964
  addAutoForeignKey(
950
965
  tableConfig,
951
966
  query,
@@ -954,7 +969,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
954
969
  foreignKeys,
955
970
  relation.options
956
971
  );
957
- const state = { query, primaryKeys, foreignKeys };
972
+ const state = { query, primaryKeys, foreignKeys, on };
958
973
  const len = primaryKeys.length;
959
974
  const reversedOn = {};
960
975
  for (let i = 0; i < len; i++) {
@@ -977,7 +992,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
977
992
  for (let i = 0; i < len; i++) {
978
993
  values[foreignKeys[i]] = params[primaryKeys[i]];
979
994
  }
980
- return _queryDefaults(query.where(values), values);
995
+ return _queryDefaults(query.where(values), { ...on, ...values });
981
996
  },
982
997
  virtualColumn: new HasManyVirtualColumn(
983
998
  defaultSchemaConfig,
@@ -1033,7 +1048,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1033
1048
  obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
1034
1049
  }
1035
1050
  items[i] = _queryUpdateOrThrow(
1036
- t.orWhere(...connect),
1051
+ t.where({ OR: connect }),
1037
1052
  obj
1038
1053
  );
1039
1054
  }
@@ -1144,7 +1159,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1144
1159
  }
1145
1160
  const relatedWheres = toArray(params.add);
1146
1161
  const count = await _queryUpdate(
1147
- t.orWhere(...relatedWheres),
1162
+ t.where({ OR: relatedWheres }),
1148
1163
  obj
1149
1164
  );
1150
1165
  if (count < relatedWheres.length) {
@@ -1246,6 +1261,11 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1246
1261
  const joinTable = options.through.table;
1247
1262
  const throughForeignKeys = options.through.columns;
1248
1263
  const throughPrimaryKeys = options.through.references;
1264
+ const { on } = options;
1265
+ if (on) {
1266
+ _queryWhere(query, [on]);
1267
+ _queryDefaults(query, on);
1268
+ }
1249
1269
  const { snakeCase } = table.internal;
1250
1270
  const foreignKeysFull = foreignKeys.map((key, i) => {
1251
1271
  if (snakeCase) key = foreignKeys[i] = toSnakeCase(key);
@@ -1308,7 +1328,8 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1308
1328
  foreignKeysFull,
1309
1329
  throughForeignKeysFull,
1310
1330
  throughPrimaryKeysFull,
1311
- primaryKeysShape
1331
+ primaryKeysShape,
1332
+ on
1312
1333
  };
1313
1334
  const joinQuery = (joiningQuery, tableAs, foreignAs, joinedShapes) => {
1314
1335
  const cloned = joiningQuery.clone();
@@ -1348,20 +1369,21 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1348
1369
  return {
1349
1370
  returns: "many",
1350
1371
  queryRelated(params) {
1351
- return query.whereExists(subQuery, (q) => {
1352
- q = q.clone();
1372
+ const q = query.whereExists(subQuery, (q2) => {
1373
+ q2 = q2.clone();
1353
1374
  const where = {};
1354
1375
  for (let i = 0; i < len; i++) {
1355
1376
  where[foreignKeysFull[i]] = params[primaryKeys[i]];
1356
1377
  }
1357
1378
  for (let i = 0; i < throughLen; i++) {
1358
- _queryJoinOn(q, [
1379
+ _queryJoinOn(q2, [
1359
1380
  throughForeignKeysFull[i],
1360
1381
  throughPrimaryKeysFull[i]
1361
1382
  ]);
1362
1383
  }
1363
- return _queryWhere(q, [where]);
1384
+ return _queryWhere(q2, [where]);
1364
1385
  });
1386
+ return on ? _queryDefaults(q, on) : q;
1365
1387
  },
1366
1388
  virtualColumn: new HasAndBelongsToManyVirtualColumn(
1367
1389
  subQuery,
@@ -1436,6 +1458,19 @@ const queryJoinTable = (state, data, conditions) => {
1436
1458
  }
1437
1459
  ]);
1438
1460
  }
1461
+ if (state.on) {
1462
+ _queryWhereExists(t, state.relatedTableQuery, [
1463
+ (q) => {
1464
+ for (let i = 0; i < state.throughPrimaryKeys.length; i++) {
1465
+ _queryJoinOn(q, [
1466
+ state.throughPrimaryKeysFull[i],
1467
+ state.throughForeignKeysFull[i]
1468
+ ]);
1469
+ }
1470
+ return q;
1471
+ }
1472
+ ]);
1473
+ }
1439
1474
  return t;
1440
1475
  };
1441
1476
  const conditionsToWhereArg = (conditions) => Array.isArray(conditions) ? { OR: conditions } : conditions;
@@ -1663,7 +1698,7 @@ const nestedUpdate = (state) => {
1663
1698
  try {
1664
1699
  const count = await state.joinTableQuery.insertManyFrom(
1665
1700
  _querySelect(
1666
- state.relatedTableQuery.orWhere(...relatedWheres),
1701
+ state.relatedTableQuery.whereOneOf(...relatedWheres),
1667
1702
  [
1668
1703
  Object.fromEntries([
1669
1704
  ...state.primaryKeys.map((key, i) => [
@@ -1690,7 +1725,7 @@ const nestedUpdate = (state) => {
1690
1725
  if (count < data.length * relatedWheres.length) {
1691
1726
  throw new OrchidOrmInternalError(
1692
1727
  query,
1693
- `Expected to find at least ${relatedWheres.length} record(s) based on \`connect\` conditions, but found ${count / data.length}`
1728
+ `Expected to find at least ${relatedWheres.length} record(s) based on \`add\` conditions, but found ${count / data.length}`
1694
1729
  );
1695
1730
  }
1696
1731
  } catch (err) {
@@ -1832,7 +1867,7 @@ const delayRelation = (delayedRelations, table, relationName, data) => {
1832
1867
  const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations) => {
1833
1868
  const baseQuery = Object.create(otherDbTable);
1834
1869
  baseQuery.baseQuery = baseQuery;
1835
- const query = (relation.options.scope ? relation.options.scope(baseQuery) : baseQuery).as(relationName);
1870
+ const query = baseQuery.as(relationName);
1836
1871
  const definedAs = query.definedAs;
1837
1872
  if (!definedAs) {
1838
1873
  throw new Error(