orchid-orm 1.78.1 → 1.78.3

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
@@ -343,11 +343,11 @@ const createTableFactory = (options) => {
343
343
  id,
344
344
  table,
345
345
  nameInDb,
346
- schema: tableOptions?.schema,
347
- noPrimaryKey: tableOptions?.noPrimaryKey,
346
+ schema: tableOptions?.schema ?? options?.schema,
347
+ noPrimaryKey: tableOptions?.noPrimaryKey ?? options?.noPrimaryKey,
348
348
  snakeCase,
349
349
  comment: tableOptions?.comment,
350
- generatorIgnore: tableOptions?.generatorIgnore,
350
+ generatorIgnore: tableOptions?.generatorIgnore ?? options?.generatorIgnore,
351
351
  readOnly: tableOptions?.readOnly,
352
352
  columns: {
353
353
  shape,
@@ -460,6 +460,10 @@ const createTableFactory = (options) => {
460
460
  defineTable.nowSQL = options?.nowSQL;
461
461
  defineTable.snakeCase = options?.snakeCase;
462
462
  defineTable.language = options?.language;
463
+ defineTable.extend = (extendedOptions) => createTableFactory({
464
+ ...options,
465
+ ...extendedOptions
466
+ }).defineTable;
463
467
  const defineView = ((name, optionsOrT, maybeT) => {
464
468
  const viewOptions = typeof optionsOrT === "function" ? void 0 : optionsOrT;
465
469
  const materialized = viewOptions?.materialized;
@@ -587,6 +591,12 @@ const makeNestedUpdateUpsertData = (upsert, setIds) => ({
587
591
  ...setIds
588
592
  }
589
593
  });
594
+ const queryUnionAll = async (queries, indexAs) => {
595
+ const records = await queries[0].unionAll(...queries.slice(1));
596
+ const result = Array(queries.length);
597
+ for (const record of records) result[record[indexAs]] = record;
598
+ return result;
599
+ };
590
600
  const makeRawSqlPlaceholders = (length) => {
591
601
  return Array.from({ length }, () => new RawSql(""));
592
602
  };
@@ -1460,22 +1470,23 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1460
1470
  let connected;
1461
1471
  if (items.length) {
1462
1472
  const queries = [];
1473
+ const indexAs = getFreeAlias(t.shape, "i");
1463
1474
  for (let i = 0, len = items.length; i < len; i++) {
1464
1475
  const [selfData, { connectOrCreate }] = items[i];
1465
1476
  for (const item of connectOrCreate) {
1466
1477
  const obj = {};
1467
1478
  for (let i = 0; i < len; i++) obj[foreignKeys[i]] = selfData[primaryKeys[i]];
1468
- queries.push(_queryUpdate(t.where(item.where), obj));
1479
+ queries.push(_querySelect(_queryUpdate(t.where(item.where), obj), [{ [indexAs]: new RawSql(String(queries.length)) }]));
1469
1480
  }
1470
1481
  }
1471
- connected = await Promise.all(queries);
1482
+ connected = await queryUnionAll(queries, indexAs);
1472
1483
  } else connected = [];
1473
1484
  let connectedI = 0;
1474
1485
  items.length = 0;
1475
1486
  for (const item of data) if (item[1].connectOrCreate) {
1476
1487
  const length = item[1].connectOrCreate.length;
1477
1488
  connectedI += length;
1478
- for (let i = length; i > 0; i--) if (connected[connectedI - i] === 0) {
1489
+ for (let i = length; i > 0; i--) if (!connected[connectedI - i]) {
1479
1490
  items.push(item);
1480
1491
  break;
1481
1492
  }
@@ -1491,7 +1502,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1491
1502
  ...obj
1492
1503
  });
1493
1504
  if (connectOrCreate) {
1494
- for (const item of connectOrCreate) if (connected[connectedI++] === 0) records.push({
1505
+ for (const item of connectOrCreate) if (!connected[connectedI++]) records.push({
1495
1506
  ...item.create,
1496
1507
  ...obj
1497
1508
  });
@@ -1499,23 +1510,20 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1499
1510
  }
1500
1511
  await _queryCreateMany(t, records);
1501
1512
  }
1502
- items.length = 0;
1503
- for (const item of data) if (item[1].upsert) items.push(item);
1504
- if (items.length) {
1505
- const queries = [];
1506
- for (const [selfData, { upsert }] of items) {
1507
- const foreignKeyValues = {};
1508
- for (let i = 0; i < len; i++) foreignKeyValues[foreignKeys[i]] = selfData[primaryKeys[i]];
1509
- for (const item of toArray(upsert)) queries.push(_queryUpsert(t.clone().where(item.findBy), {
1510
- update: {
1511
- ...item.update,
1512
- ...foreignKeyValues
1513
- },
1514
- create: makeNestedUpdateUpsertData(item, foreignKeyValues).create
1515
- }));
1516
- }
1517
- await Promise.all(queries);
1513
+ const queries = [];
1514
+ for (const [selfData, { upsert }] of data) {
1515
+ if (!upsert) continue;
1516
+ const obj = {};
1517
+ for (let i = 0; i < len; i++) obj[foreignKeys[i]] = selfData[primaryKeys[i]];
1518
+ for (const item of toArray(upsert)) queries.push(_queryUpsert(t.clone().where(item.findBy), {
1519
+ update: {
1520
+ ...item.update,
1521
+ ...obj
1522
+ },
1523
+ create: makeNestedUpdateUpsertData(item, obj).create
1524
+ }));
1518
1525
  }
1526
+ if (queries.length) await queries[0].unionAll(...queries.slice(1));
1519
1527
  });
1520
1528
  };
1521
1529
  const hasManyCreate = (key, state, nestedInsertFn, self, ctx, items, rowIndexes, count) => {
@@ -1960,16 +1968,18 @@ const nestedInsert = ({ relatedTableQuery, joinTableQuery, primaryKeys, foreignK
1960
1968
  let connected;
1961
1969
  if (items.length) {
1962
1970
  const queries = [];
1963
- for (const [, { connect }] of items) for (const item of connect) queries.push(_queryFindBy(t.select(...throughPrimaryKeys), item));
1964
- connected = await Promise.all(queries);
1971
+ const indexAs = getFreeAlias(t.shape, "i");
1972
+ for (const [, { connect }] of items) for (const item of connect) queries.push(_queryFindBy(t.select(...throughPrimaryKeys, { [indexAs]: new RawSql(String(queries.length)) }), item));
1973
+ connected = await queryUnionAll(queries, indexAs);
1965
1974
  } else connected = [];
1966
1975
  items.length = 0;
1967
1976
  for (const item of data) if (item[1].connectOrCreate) items.push(item);
1968
1977
  let connectOrCreated;
1969
1978
  if (items.length) {
1970
1979
  const queries = [];
1971
- for (const [, { connectOrCreate }] of items) for (const item of connectOrCreate) queries.push(_queryFindByOptional(t.select(...throughPrimaryKeys), item.where));
1972
- connectOrCreated = await Promise.all(queries);
1980
+ const indexAs = getFreeAlias(t.shape, "i");
1981
+ for (const [, { connectOrCreate }] of items) for (const item of connectOrCreate) queries.push(_queryFindByOptional(t.select(...throughPrimaryKeys, { [indexAs]: new RawSql(String(queries.length)) }), item.where));
1982
+ connectOrCreated = await queryUnionAll(queries, indexAs);
1973
1983
  } else connectOrCreated = [];
1974
1984
  let connectOrCreateI = 0;
1975
1985
  items.length = 0;
@@ -2755,6 +2765,7 @@ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPr
2755
2765
  $withRecursive: qb.withRecursive.bind(qb),
2756
2766
  $withSql: qb.withSql.bind(qb),
2757
2767
  $from: qb.from.bind(qb),
2768
+ $select: ((selection) => qb.select(selection).take()),
2758
2769
  $close: adapter.close.bind(adapter),
2759
2770
  $withOptions: qb.withOptions.bind(qb)
2760
2771
  };