orchid-orm 1.20.0 → 1.20.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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { makeColumnTypes, defaultSchemaConfig, QueryHooks, getColumnTypes, getQueryAs, setQueryObjectValue, pushQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, toSQLCacheKey, OrchidOrmInternalError, NotFoundError, Adapter, Db, anyShape, addComputedColumns, getClonedQueryData } from 'pqb';
1
+ import { makeColumnTypes, defaultSchemaConfig, QueryHooks, getColumnTypes, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValue, pushQueryOn, VirtualColumn, pushQueryValue, _queryCreateMany, isQueryReturnsAll, _queryHookBeforeUpdate, _queryFindBy, _queryCreate, _queryRows, _queryUpdate, _queryDelete, _queryDefaults, _queryUpdateOrThrow, _queryWhere, toSQLCacheKey, _queryJoinOn, OrchidOrmInternalError, _queryCreateFrom, NotFoundError, _queryFindByOptional, _querySelect, _queryTake, _queryTakeOptional, _queryAll, Adapter, Db, anyShape, addComputedColumns, getClonedQueryData } from 'pqb';
2
2
  export * from 'pqb';
3
3
  import { getStackTrace, applyMixins, getCallerFilePath, snakeCaseKey, toSnakeCase, emptyArray, toArray } from 'orchid-core';
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
@@ -149,7 +149,8 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, neste
149
149
  q.q.wrapInTransaction = true;
150
150
  const relationData = [values];
151
151
  store.hasRelation[key] = relationData;
152
- q._afterCreate(
152
+ _queryHookAfterCreate(
153
+ q,
153
154
  primaryKeys,
154
155
  (rows, q2) => nestedInsert(
155
156
  q2,
@@ -166,7 +167,7 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
166
167
  return;
167
168
  selectIfNotSelected(q, primaryKeys);
168
169
  q.q.wrapInTransaction = true;
169
- q._afterUpdate(q.primaryKeys, (rows, q2) => {
170
+ _queryHookAfterUpdate(q, q.primaryKeys, (rows, q2) => {
170
171
  return nestedUpdate(
171
172
  q2,
172
173
  rows,
@@ -358,7 +359,7 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
358
359
  for (let i = 0, len = items.length; i < len; i++) {
359
360
  items[i] = "create" in items[i] ? items[i].create : items[i].connectOrCreate.create;
360
361
  }
361
- created = await t.select(...primaryKeys)._createMany(items);
362
+ created = await _queryCreateMany(t.select(...primaryKeys), items);
362
363
  } else {
363
364
  created = emptyArray;
364
365
  }
@@ -392,7 +393,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
392
393
  throw new Error("`upsert` option is not allowed in a batch update");
393
394
  }
394
395
  let idsForDelete;
395
- q._beforeUpdate(async (q2) => {
396
+ _queryHookBeforeUpdate(q, async (q2) => {
396
397
  if (params.disconnect) {
397
398
  for (const key of foreignKeys) {
398
399
  update[key] = null;
@@ -410,7 +411,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
410
411
  }
411
412
  }
412
413
  if (loadPrimaryKeys) {
413
- const record = await query.select(...loadPrimaryKeys)._findBy(params.set);
414
+ const record = await _queryFindBy(query.select(...loadPrimaryKeys), [
415
+ params.set
416
+ ]);
414
417
  for (let i = 0, len2 = loadPrimaryKeys.length; i < len2; i++) {
415
418
  update[loadForeignKeys[i]] = record[loadPrimaryKeys[i]];
416
419
  }
@@ -418,7 +421,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
418
421
  } else if (params.create) {
419
422
  const q3 = query.clone();
420
423
  q3.q.select = primaryKeys;
421
- const record = await q3._create(params.create);
424
+ const record = await _queryCreate(q3, params.create);
422
425
  for (let i = 0; i < len; i++) {
423
426
  update[foreignKeys[i]] = record[primaryKeys[i]];
424
427
  }
@@ -426,7 +429,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
426
429
  const selectQuery = q2.clone();
427
430
  selectQuery.q.type = void 0;
428
431
  selectQuery.q.distinct = emptyArray;
429
- idsForDelete = await selectQuery._rows();
432
+ idsForDelete = await _queryRows(selectQuery);
430
433
  for (const foreignKey of foreignKeys) {
431
434
  update[foreignKey] = null;
432
435
  }
@@ -450,17 +453,20 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
450
453
  obj[primaryKeys[i]] = id;
451
454
  }
452
455
  if (obj) {
453
- await query.findBy(obj)._update(upsert.update);
456
+ await _queryUpdate(
457
+ query.findBy(obj),
458
+ upsert.update
459
+ );
454
460
  } else {
455
461
  const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
456
- const result = await query.select(...primaryKeys)._create(data);
462
+ const result = await _queryCreate(query.select(...primaryKeys), data);
457
463
  for (let i = 0; i < len; i++) {
458
464
  ((_a2 = state.updateData) != null ? _a2 : state.updateData = {})[foreignKeys[i]] = result[primaryKeys[i]];
459
465
  }
460
466
  }
461
467
  });
462
468
  } else if (params.delete || params.update) {
463
- q._afterUpdate([], async (data) => {
469
+ _queryHookAfterUpdate(q, [], async (data) => {
464
470
  let ids;
465
471
  if (params.delete) {
466
472
  ids = idsForDelete;
@@ -488,11 +494,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
488
494
  ids
489
495
  );
490
496
  if (params.delete) {
491
- await t._delete();
497
+ await _queryDelete(t);
492
498
  } else {
493
- await t._update(
494
- params.update
495
- );
499
+ await _queryUpdate(t, params.update);
496
500
  }
497
501
  });
498
502
  }
@@ -612,7 +616,7 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
612
616
  for (let i = 0; i < len; i++) {
613
617
  values[foreignKeys[i]] = params[primaryKeys[i]];
614
618
  }
615
- return query.where(values)._defaults(values);
619
+ return _queryDefaults(query.where(values), values);
616
620
  },
617
621
  virtualColumn: new HasOneVirtualColumn(
618
622
  defaultSchemaConfig,
@@ -652,9 +656,15 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
652
656
  for (let i2 = 0; i2 < len; i2++) {
653
657
  data2[foreignKeys[i2]] = selfData[primaryKeys[i2]];
654
658
  }
655
- items[i] = "connect" in item ? t.where(item.connect)._updateOrThrow(data2) : t.where(
656
- item.connectOrCreate.where
657
- )._update(data2);
659
+ items[i] = "connect" in item ? _queryUpdateOrThrow(
660
+ t.where(item.connect),
661
+ data2
662
+ ) : _queryUpdate(
663
+ t.where(
664
+ item.connectOrCreate.where
665
+ ),
666
+ data2
667
+ );
658
668
  }
659
669
  connected = await Promise.all(items);
660
670
  } else {
@@ -700,7 +710,8 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
700
710
  ids
701
711
  );
702
712
  if (params.create || params.disconnect || params.set) {
703
- await currentRelationsQuery._update(
713
+ await _queryUpdate(
714
+ currentRelationsQuery,
704
715
  setNulls
705
716
  );
706
717
  const record = data[0];
@@ -716,16 +727,25 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
716
727
  for (let i = 0; i < len; i++) {
717
728
  obj[foreignKeys[i]] = record[primaryKeys[i]];
718
729
  }
719
- await t._where(params.set)._update(obj);
730
+ await _queryUpdate(
731
+ _queryWhere(t, [params.set]),
732
+ obj
733
+ );
720
734
  }
721
735
  } else if (params.update) {
722
- await currentRelationsQuery._update(params.update);
736
+ await _queryUpdate(
737
+ currentRelationsQuery,
738
+ params.update
739
+ );
723
740
  } else if (params.delete) {
724
- await currentRelationsQuery._delete();
741
+ await _queryDelete(currentRelationsQuery);
725
742
  } else if (params.upsert) {
726
743
  const { update, create } = params.upsert;
727
744
  currentRelationsQuery.q.select = foreignKeys;
728
- const updatedIds = await currentRelationsQuery._rows()._update(update);
745
+ const updatedIds = await _queryUpdate(
746
+ _queryRows(currentRelationsQuery),
747
+ update
748
+ );
729
749
  if (updatedIds.length < ids.length) {
730
750
  const data2 = typeof create === "function" ? create() : create;
731
751
  await t.createMany(
@@ -864,7 +884,7 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
864
884
  for (let i = 0; i < len; i++) {
865
885
  values[foreignKeys[i]] = params[primaryKeys[i]];
866
886
  }
867
- return query.where(values)._defaults(values);
887
+ return _queryDefaults(query.where(values), values);
868
888
  },
869
889
  virtualColumn: new HasManyVirtualColumn(
870
890
  defaultSchemaConfig,
@@ -913,7 +933,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
913
933
  for (let i2 = 0; i2 < len2; i2++) {
914
934
  obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
915
935
  }
916
- items[i] = t.orWhere(...connect)._updateOrThrow(obj);
936
+ items[i] = _queryUpdateOrThrow(
937
+ t.orWhere(...connect),
938
+ obj
939
+ );
917
940
  }
918
941
  await Promise.all(items);
919
942
  }
@@ -934,7 +957,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
934
957
  obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
935
958
  }
936
959
  queries.push(
937
- t.where(item.where)._update(obj)
960
+ _queryUpdate(
961
+ t.where(item.where),
962
+ obj
963
+ )
938
964
  );
939
965
  }
940
966
  }
@@ -979,7 +1005,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
979
1005
  }
980
1006
  }
981
1007
  }
982
- await t._createMany(records);
1008
+ await _queryCreateMany(t, records);
983
1009
  }
984
1010
  };
985
1011
  };
@@ -1003,24 +1029,30 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1003
1029
  for (const foreignKey of foreignKeys) {
1004
1030
  obj[foreignKey] = null;
1005
1031
  }
1006
- await getWhereForNestedUpdate(
1007
- t,
1008
- data,
1009
- params.disconnect,
1010
- primaryKeys,
1011
- foreignKeys
1012
- )._update(obj);
1032
+ await _queryUpdate(
1033
+ getWhereForNestedUpdate(
1034
+ t,
1035
+ data,
1036
+ params.disconnect,
1037
+ primaryKeys,
1038
+ foreignKeys
1039
+ ),
1040
+ obj
1041
+ );
1013
1042
  if (params.set) {
1014
1043
  delete t.q[toSQLCacheKey];
1015
1044
  const obj2 = {};
1016
1045
  for (let i = 0; i < len; i++) {
1017
1046
  obj2[foreignKeys[i]] = data[0][primaryKeys[i]];
1018
1047
  }
1019
- await t.where(
1020
- Array.isArray(params.set) ? {
1021
- OR: params.set
1022
- } : params.set
1023
- )._update(obj2);
1048
+ await _queryUpdate(
1049
+ t.where(
1050
+ Array.isArray(params.set) ? {
1051
+ OR: params.set
1052
+ } : params.set
1053
+ ),
1054
+ obj2
1055
+ );
1024
1056
  }
1025
1057
  }
1026
1058
  if (params.delete || params.update) {
@@ -1033,9 +1065,9 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1033
1065
  foreignKeys
1034
1066
  );
1035
1067
  if (params.delete) {
1036
- await q._delete();
1068
+ await _queryDelete(q);
1037
1069
  } else if (params.update) {
1038
- await q._update(params.update.data);
1070
+ await _queryUpdate(q, params.update.data);
1039
1071
  }
1040
1072
  }
1041
1073
  };
@@ -1159,13 +1191,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1159
1191
  const joinQuery = (joiningQuery, tableAs, foreignAs) => {
1160
1192
  return joiningQuery.whereExists(subQuery, (q) => {
1161
1193
  for (let i = 0; i < throughLen; i++) {
1162
- q._on(
1194
+ _queryJoinOn(q, [
1163
1195
  throughForeignKeysFull[i],
1164
1196
  `${foreignAs}.${throughPrimaryKeys[i]}`
1165
- );
1197
+ ]);
1166
1198
  }
1167
1199
  for (let i = 0; i < len; i++) {
1168
- q._on(foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`);
1200
+ _queryJoinOn(q, [foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`]);
1169
1201
  }
1170
1202
  return q;
1171
1203
  });
@@ -1192,9 +1224,12 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1192
1224
  where[foreignKeysFull[i]] = params[primaryKeys[i]];
1193
1225
  }
1194
1226
  for (let i = 0; i < throughLen; i++) {
1195
- q._on(throughForeignKeysFull[i], throughPrimaryKeysFull[i]);
1227
+ _queryJoinOn(q, [
1228
+ throughForeignKeysFull[i],
1229
+ throughPrimaryKeysFull[i]
1230
+ ]);
1196
1231
  }
1197
- return q._where(where);
1232
+ return _queryWhere(q, [where]);
1198
1233
  });
1199
1234
  },
1200
1235
  virtualColumn: new HasAndBelongsToManyVirtualColumn(
@@ -1216,7 +1251,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1216
1251
  reverseJoin,
1217
1252
  modifyRelatedQuery(relationQuery) {
1218
1253
  const ref = {};
1219
- relationQuery._afterCreate([], async (result) => {
1254
+ _queryHookAfterCreate(relationQuery, [], async (result) => {
1220
1255
  if (result.length > 1) {
1221
1256
  throw new OrchidOrmInternalError(
1222
1257
  relationQuery,
@@ -1229,7 +1264,11 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1229
1264
  for (let i = 0; i < throughLen; i++) {
1230
1265
  data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
1231
1266
  }
1232
- const createdCount = await subQuery.count()._createFrom(baseQuery2, data);
1267
+ const createdCount = await _queryCreateFrom(
1268
+ subQuery.count(),
1269
+ baseQuery2,
1270
+ data
1271
+ );
1233
1272
  if (createdCount === 0) {
1234
1273
  throw new NotFoundError(baseQuery2);
1235
1274
  }
@@ -1248,12 +1287,17 @@ const queryJoinTable = (state, data, conditions) => {
1248
1287
  }
1249
1288
  });
1250
1289
  if (conditions) {
1251
- t._where({
1252
- IN: {
1253
- columns: state.throughForeignKeys,
1254
- values: state.relatedTableQuery.where(conditionsToWhereArg(conditions))._select(...state.throughPrimaryKeys)
1290
+ _queryWhere(t, [
1291
+ {
1292
+ IN: {
1293
+ columns: state.throughForeignKeys,
1294
+ values: _querySelect(
1295
+ state.relatedTableQuery.where(conditionsToWhereArg(conditions)),
1296
+ state.throughPrimaryKeys
1297
+ )
1298
+ }
1255
1299
  }
1256
- });
1300
+ ]);
1257
1301
  }
1258
1302
  return t;
1259
1303
  };
@@ -1300,9 +1344,7 @@ const nestedInsert = ({
1300
1344
  const queries = [];
1301
1345
  for (const [, { connect }] of items) {
1302
1346
  for (const item of connect) {
1303
- queries.push(
1304
- t.select(...throughPrimaryKeys)._findBy(item)._take()
1305
- );
1347
+ queries.push(_queryFindBy(t.select(...throughPrimaryKeys), [item]));
1306
1348
  }
1307
1349
  }
1308
1350
  connected = await Promise.all(queries);
@@ -1321,7 +1363,7 @@ const nestedInsert = ({
1321
1363
  for (const [, { connectOrCreate }] of items) {
1322
1364
  for (const item of connectOrCreate) {
1323
1365
  queries.push(
1324
- t.select(...throughPrimaryKeys)._findBy(item.where)._takeOptional()
1366
+ _queryFindByOptional(t.select(...throughPrimaryKeys), [item.where])
1325
1367
  );
1326
1368
  }
1327
1369
  }
@@ -1361,7 +1403,10 @@ const nestedInsert = ({
1361
1403
  }
1362
1404
  }
1363
1405
  }
1364
- created = await t.select(...throughPrimaryKeys)._createMany(records2);
1406
+ created = await _queryCreateMany(
1407
+ t.select(...throughPrimaryKeys),
1408
+ records2
1409
+ );
1365
1410
  } else {
1366
1411
  created = [];
1367
1412
  }
@@ -1419,7 +1464,10 @@ const nestedUpdate = (state) => {
1419
1464
  const throughLen = state.throughPrimaryKeys.length;
1420
1465
  return async (_, data, params) => {
1421
1466
  if (params.create) {
1422
- const idsRows = await state.relatedTableQuery.select(...state.throughPrimaryKeys)._rows()._createMany(params.create);
1467
+ const idsRows = await _queryCreateMany(
1468
+ _queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
1469
+ params.create
1470
+ );
1423
1471
  const records = [];
1424
1472
  for (const item of data) {
1425
1473
  const obj = {};
@@ -1437,41 +1485,58 @@ const nestedUpdate = (state) => {
1437
1485
  await state.joinTableQuery.createMany(records);
1438
1486
  }
1439
1487
  if (params.update) {
1440
- await state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
1441
- for (let i = 0; i < throughLen; i++) {
1442
- q._on(
1443
- state.throughForeignKeysFull[i],
1444
- state.throughPrimaryKeysFull[i]
1445
- );
1446
- }
1447
- return q._where({
1448
- IN: {
1449
- columns: state.foreignKeysFull,
1450
- values: data.map(
1451
- (item) => state.primaryKeys.map((key) => item[key])
1452
- )
1453
- }
1454
- });
1455
- })._where(conditionsToWhereArg(params.update.where))._update(params.update.data);
1488
+ await _queryUpdate(
1489
+ _queryWhere(
1490
+ state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
1491
+ for (let i = 0; i < throughLen; i++) {
1492
+ _queryJoinOn(q, [
1493
+ state.throughForeignKeysFull[i],
1494
+ state.throughPrimaryKeysFull[i]
1495
+ ]);
1496
+ }
1497
+ return _queryWhere(q, [
1498
+ {
1499
+ IN: {
1500
+ columns: state.foreignKeysFull,
1501
+ values: data.map(
1502
+ (item) => state.primaryKeys.map((key) => item[key])
1503
+ )
1504
+ }
1505
+ }
1506
+ ]);
1507
+ }),
1508
+ [conditionsToWhereArg(params.update.where)]
1509
+ ),
1510
+ params.update.data
1511
+ );
1456
1512
  }
1457
1513
  if (params.disconnect) {
1458
- await queryJoinTable(state, data, params.disconnect)._delete();
1514
+ await _queryDelete(queryJoinTable(state, data, params.disconnect));
1459
1515
  }
1460
1516
  if (params.delete) {
1461
1517
  const j = queryJoinTable(state, data, params.delete);
1462
- const idsRows = await j._select(...state.throughForeignKeys)._rows()._delete();
1463
- await state.relatedTableQuery.where({
1464
- IN: {
1465
- columns: state.throughPrimaryKeys,
1466
- values: idsRows
1467
- }
1468
- })._delete();
1518
+ const idsRows = await _queryDelete(
1519
+ _queryRows(_querySelect(j, state.throughForeignKeys))
1520
+ );
1521
+ await _queryDelete(
1522
+ state.relatedTableQuery.where({
1523
+ IN: {
1524
+ columns: state.throughPrimaryKeys,
1525
+ values: idsRows
1526
+ }
1527
+ })
1528
+ );
1469
1529
  }
1470
1530
  if (params.set) {
1471
1531
  const j = queryJoinTable(state, data);
1472
- await j._delete();
1532
+ await _queryDelete(j);
1473
1533
  delete j.q[toSQLCacheKey];
1474
- const idsRows = await state.relatedTableQuery.where(conditionsToWhereArg(params.set))._select(...state.throughPrimaryKeys)._rows();
1534
+ const idsRows = await _queryRows(
1535
+ _querySelect(
1536
+ state.relatedTableQuery.where(conditionsToWhereArg(params.set)),
1537
+ state.throughPrimaryKeys
1538
+ )
1539
+ );
1475
1540
  await insertToJoinTable(state, j, data, idsRows);
1476
1541
  }
1477
1542
  };
@@ -1613,9 +1678,9 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1613
1678
  }
1614
1679
  if (data.returns === "one") {
1615
1680
  if (relation.options.required) {
1616
- query._take();
1681
+ _queryTake(query);
1617
1682
  } else {
1618
- query._takeOptional();
1683
+ _queryTakeOptional(query);
1619
1684
  }
1620
1685
  query.q.returnsOne = true;
1621
1686
  }
@@ -1658,11 +1723,13 @@ const makeRelationQuery = (table, relationName, data, q) => {
1658
1723
  query = toTable;
1659
1724
  query.q.isSubQuery = true;
1660
1725
  } else {
1661
- query = toTable._all()._where({
1662
- EXISTS: {
1663
- args: [data.reverseJoin(this, toTable)]
1726
+ query = _queryWhere(_queryAll(toTable), [
1727
+ {
1728
+ EXISTS: {
1729
+ args: [data.reverseJoin(this, toTable)]
1730
+ }
1664
1731
  }
1665
- });
1732
+ ]);
1666
1733
  }
1667
1734
  if (this.q.relChain) {
1668
1735
  query.q.relChain = [...this.q.relChain, this];