pqb 0.70.0 → 0.71.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
@@ -147,7 +147,7 @@ const toPascalCase = (str) => {
147
147
  * @param str - string to translate
148
148
  */
149
149
  const toSnakeCase = (str) => {
150
- return str.replace(/[A-Z]/g, (a) => `_${a.toLowerCase()}`);
150
+ return str.replace(/[A-Z]/g, (a, i) => `${i ? "_" : ""}${a.toLowerCase()}`);
151
151
  };
152
152
  /**
153
153
  * Compare two values deeply.
@@ -1413,6 +1413,7 @@ const indexInnerToCode = (index, t) => {
1413
1413
  "name",
1414
1414
  "using",
1415
1415
  "nullsNotDistinct",
1416
+ "deferrable",
1416
1417
  "include",
1417
1418
  "with",
1418
1419
  "tablespace",
@@ -1594,6 +1595,7 @@ const columnIndexesToCode = (items) => {
1594
1595
  options.using && `using: ${singleQuote(options.using)},`,
1595
1596
  options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
1596
1597
  options.nullsNotDistinct && `nullsNotDistinct: true,`,
1598
+ options.deferrable && `deferrable: ${singleQuote(options.deferrable)},`,
1597
1599
  options.with && `with: ${singleQuote(options.with)},`,
1598
1600
  options.tablespace && `tablespace: ${singleQuote(options.tablespace)},`,
1599
1601
  options.where && `where: ${singleQuote(options.where)},`
@@ -5596,20 +5598,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, shape, whereExists,
5596
5598
  const args0 = args.length ? args[0] : returnArg;
5597
5599
  if (typeof args0 === "function") {
5598
5600
  let q = first;
5599
- if (q.joinQueryAfterCallback) {
5600
- let base = q.baseQuery;
5601
- if (q.q.as) base = base.as(q.q.as);
5602
- const { q: query } = q.joinQueryAfterCallback(base, joinTo);
5603
- if (query.and || query.or || query.scopes) {
5604
- q = _clone(q);
5605
- if (query.and) pushQueryArrayImmutable(q, "and", query.and);
5606
- if (query.or) pushQueryArrayImmutable(q, "or", query.or);
5607
- if (query.scopes) q.q.scopes = {
5608
- ...q.q.scopes,
5609
- ...query.scopes
5610
- };
5611
- }
5612
- }
5601
+ if (q.joinQueryAfterCallback) q = q.joinQueryAfterCallback(q, joinTo);
5613
5602
  const joinedShapes = {
5614
5603
  ...joinTo.q.joinedShapes,
5615
5604
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -6602,12 +6591,12 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
6602
6591
  ctx.aliasValue = aliasValue;
6603
6592
  } else if ("j" in args) {
6604
6593
  const { j, s, r } = args;
6605
- const tableName = typeof j.q.from === "string" ? j.q.from : j.table;
6606
- const joinTable = requireTableOrStringFrom(j);
6594
+ const joinTable = j.q.nameInDb || requireTableOrStringFrom(j);
6607
6595
  target = quoteFromWithSchema(getQuerySchema$1(j), joinTable);
6608
6596
  const as = j.q.as;
6609
6597
  const joinAs = `"${as}"`;
6610
- if (as !== tableName) target += ` ${joinAs}`;
6598
+ const alias = getQueryRelationAliasForAs(j, as);
6599
+ if (alias) target += ` ${alias}`;
6611
6600
  if (r && s) target = subJoinToSql(ctx, j, `"${joinTable}"`, !forbidLateral, joinAs, true);
6612
6601
  else on = whereToSql(ctx, j, j.q, joinAs);
6613
6602
  } else if ("w" in args) {
@@ -6665,16 +6654,16 @@ const getArgQueryTarget = (ctx, first, lateral, joinSubQuery, cloned) => {
6665
6654
  const quotedFrom = typeof joinQuery.from === "string" ? `"${joinQuery.from}"` : void 0;
6666
6655
  let joinAs = quotedFrom || `"${first.table}"`;
6667
6656
  const qAs = joinQuery.as ? `"${joinQuery.as}"` : void 0;
6668
- const addAs = qAs && qAs !== joinAs;
6657
+ const aliasToAdd = getQueryRelationAliasForAs(first, joinQuery.as);
6669
6658
  if (joinSubQuery) return {
6670
6659
  target: subJoinToSql(ctx, first, joinAs, lateral, qAs, cloned),
6671
- joinAs: addAs ? qAs : joinAs
6660
+ joinAs: qAs || joinAs
6672
6661
  };
6673
6662
  else {
6674
6663
  let target = quotedFrom || quoteTableWithSchema(first);
6675
- if (addAs) {
6676
- joinAs = qAs;
6677
- target += ` ${qAs}`;
6664
+ if (aliasToAdd) {
6665
+ joinAs = aliasToAdd;
6666
+ target += ` ${aliasToAdd}`;
6678
6667
  }
6679
6668
  return {
6680
6669
  target,
@@ -7432,14 +7421,18 @@ const makeInsertSql = (ctx, q, query, quotedAs, isSubSql) => {
7432
7421
  }
7433
7422
  }
7434
7423
  const hasNonSelect = ctx.hasNonSelect;
7424
+ const returningQuotedAs = query.as ? `"${query.as}"` : quotedAs;
7425
+ const insertAlias = getQueryRelationAliasForAs(q, query.as);
7426
+ quotedAs = insertAlias || quotedAs;
7435
7427
  const sqlState = {
7436
7428
  ctx,
7437
7429
  q,
7438
7430
  query,
7439
7431
  quotedAs,
7432
+ returningQuotedAs,
7440
7433
  isSubSql,
7441
7434
  returningPos: 0,
7442
- insertSql: `INSERT INTO ${quoteTableWithSchema(q)}${quotedColumns.length ? "(" + quotedColumns.join(", ") + ")" : ""}`
7435
+ insertSql: `INSERT INTO ${quoteTableWithSchema(q)}${insertAlias ? ` AS ${insertAlias}` : ""}${quotedColumns.length ? "(" + quotedColumns.join(", ") + ")" : ""}`
7443
7436
  };
7444
7437
  ctx.sql.push(null, null);
7445
7438
  const hasOnConflictWhere = pushOnConflictSql(ctx, q, query, quotedAs, columns, quotedColumns, runtimeDefaultColumns);
@@ -7552,7 +7545,7 @@ const applySqlState = (sqlState) => {
7552
7545
  const insertSql = sqlState.selectFromSql ? sqlState.insertSql + sqlState.selectFromSql : sqlState.insertSql;
7553
7546
  const wrapInCte = getShouldWrapMainQueryInCte(ctx, sqlState.query, "insert", sqlState.isSubSql);
7554
7547
  if ("valuesSql" in sqlState) ctx.sql[1] = sqlState.valuesPrepend + sqlState.valuesSql.join(", ") + sqlState.valuesAppend;
7555
- const returning = makeReturningSql(ctx, sqlState.q, sqlState.query, sqlState.quotedAs, sqlState.relationSelectState, "Create", void 0, sqlState.isSubSql || !!ctx.topCtx.cteHooks);
7548
+ const returning = makeReturningSql(ctx, sqlState.q, sqlState.query, sqlState.returningQuotedAs, sqlState.relationSelectState, "Create", void 0, sqlState.isSubSql || !!ctx.topCtx.cteHooks);
7556
7549
  if (returning) ctx.sql[sqlState.returningPos] = "RETURNING " + returning;
7557
7550
  ctx.sql[0] = insertSql;
7558
7551
  if (wrapInCte) wrapMainQueryInCte(ctx, sqlState.query);
@@ -7713,8 +7706,9 @@ const pushUpdateSql = (ctx, query, q, quotedAs, isSubSql) => {
7713
7706
  }
7714
7707
  };
7715
7708
  const pushUpdateSqlWithoutValuesJoinedAs = (ctx, query, q, quotedAs, isSubSql) => {
7716
- const quotedTable = `"${query.table || q.from}"`;
7717
7709
  const from = quoteTableWithSchema(query);
7710
+ const alias = getQueryRelationAliasForAs(query, q.as);
7711
+ quotedAs = alias || quotedAs;
7718
7712
  const set = [];
7719
7713
  const hookSet = q.hookUpdateSet ? Object.fromEntries(q.hookUpdateSet.flatMap((item) => Object.entries(item))) : emptyObject;
7720
7714
  const relationSelectState = newMutativeQueriesSelectRelationsSqlState(query);
@@ -7729,7 +7723,7 @@ const pushUpdateSqlWithoutValuesJoinedAs = (ctx, query, q, quotedAs, isSubSql) =
7729
7723
  if (!set.length) pushSelectForEmptySet(ctx, query, q, quotedAs, from, isSubSql, updateManyValuesSql, relationSelectState);
7730
7724
  else {
7731
7725
  ctx.sql.push(`UPDATE ${from}`);
7732
- if (quotedTable !== quotedAs) ctx.sql.push(quotedAs);
7726
+ if (alias) ctx.sql.push(alias);
7733
7727
  ctx.sql.push("SET", set.join(", "));
7734
7728
  let fromWhereSql;
7735
7729
  if (updateManyValuesSql) {
@@ -7746,6 +7740,8 @@ const pushSelectForEmptySet = (ctx, query, q, quotedAs, from, isSubSql, updateMa
7746
7740
  if (!q.select) q.select = countSelect;
7747
7741
  pushUpdateReturning(ctx, query, q, quotedAs, "SELECT", relationSelectState, isSubSql);
7748
7742
  let fromSql = `FROM ${from}`;
7743
+ const alias = getQueryRelationAliasForAs(query, q.as);
7744
+ if (alias) fromSql += ` ${alias}`;
7749
7745
  if (updateManyValuesSql) fromSql += `, ${updateManyValuesSql}`;
7750
7746
  ctx.sql.push(fromSql);
7751
7747
  pushWhereStatementSql(ctx, query, q, quotedAs);
@@ -7860,7 +7856,8 @@ const throwOnDifferentColumns = (query, keysSet, row, i) => {
7860
7856
  const pushDeleteSql = (ctx, query, q, quotedAs, isSubSql) => {
7861
7857
  const from = quoteTableWithSchema(query);
7862
7858
  ctx.sql.push(`DELETE FROM ${from}`);
7863
- if (q.as && query.table !== q.as) ctx.sql.push(quotedAs);
7859
+ const alias = getQueryRelationAliasForAs(query, q.as);
7860
+ if (alias) ctx.sql.push(alias);
7864
7861
  const relationSelectState = newMutativeQueriesSelectRelationsSqlState(query);
7865
7862
  let returning = makeReturningSql(ctx, query, q, quotedAs, relationSelectState, "Delete", void 0, isSubSql);
7866
7863
  const selectRelations = handleDeleteSelectRelationsSqlState(ctx, query, relationSelectState);
@@ -9445,7 +9442,8 @@ const getFrom = (ctx, query, data, quotedAs) => {
9445
9442
  return fromToSql(ctx, query, data, from, quotedAs);
9446
9443
  }
9447
9444
  let sql = quoteTableWithSchema(query);
9448
- if (data.as && query.table !== data.as) sql += ` ${quotedAs}`;
9445
+ const alias = getQueryRelationAliasForAs(query, data.as);
9446
+ if (alias) sql += ` ${alias}`;
9449
9447
  if (data.only) sql = `ONLY ${sql}`;
9450
9448
  return sql;
9451
9449
  };
@@ -9457,7 +9455,7 @@ const fromToSql = (ctx, query, data, from, quotedAs) => {
9457
9455
  only = from.q.only;
9458
9456
  if (!from.table) sql = `(${moveMutativeQueryToCte(ctx, from)})`;
9459
9457
  else if (!checkIfASimpleQuery(from)) sql = `(${moveMutativeQueryToCte(ctx, from)}) ${quotedAs || `"${getQueryAs(from)}"`}`;
9460
- else sql = quoteTableWithSchema(from);
9458
+ else sql = quoteTableWithSchemaAndAlias(from);
9461
9459
  fromQuery = from;
9462
9460
  }
9463
9461
  else sql = quoteFromWithSchema(getQuerySchema$1(query), from);
@@ -9758,7 +9756,18 @@ const requireTableOrStringFrom = (query) => {
9758
9756
  if (!table) throw new OrchidOrmInternalError(query, "The query object does not have a table and doesn't define a `from` string");
9759
9757
  return table;
9760
9758
  };
9761
- const quoteTableWithSchema = (query) => quoteFromWithSchema(getQuerySchema$1(query), requireTableOrStringFrom(query));
9759
+ const getQueryRelationAlias = (query) => query.table && query.q.nameInDb && query.table !== query.q.nameInDb ? `"${query.table}"` : void 0;
9760
+ const getQueryRelationAliasForAs = (query, as) => {
9761
+ if (!as) return getQueryRelationAlias(query);
9762
+ const nameInDb = query.q.nameInDb || requireTableOrStringFrom(query);
9763
+ return nameInDb && as !== nameInDb ? `"${as}"` : void 0;
9764
+ };
9765
+ const quoteTableWithSchema = (query) => quoteFromWithSchema(getQuerySchema$1(query), query.q.nameInDb || requireTableOrStringFrom(query));
9766
+ const quoteTableWithSchemaAndAlias = (query) => {
9767
+ const table = quoteTableWithSchema(query);
9768
+ const alias = getQueryRelationAlias(query);
9769
+ return alias ? `${table} ${alias}` : table;
9770
+ };
9762
9771
  const quoteFromWithSchema = (schema, table) => {
9763
9772
  const s = typeof schema === "function" ? schema() : schema;
9764
9773
  return s ? `"${s}"."${table}"` : `"${table}"`;
@@ -10026,7 +10035,7 @@ const _joinLateral = (self, type, joinQuery, as, innerJoinLateral) => {
10026
10035
  const joinedAs = getQueryAs(query);
10027
10036
  setObjectValueImmutable(joinQuery.q, "joinedShapes", joinedAs, query.q.selectShape);
10028
10037
  }
10029
- const shape = getShapeFromSelect(joinQuery, true);
10038
+ const shape = joinQuery.table && joinQuery.q.joinedShapes?.[joinQuery.table] || joinQuery.q.joinedShapes?.[joinAs] || getShapeFromSelect(joinQuery, true);
10030
10039
  setObjectValueImmutable(query.q, "joinedShapes", joinAs, shape);
10031
10040
  if (joinValue) setObjectValueImmutable(query.q, "valuesJoinedAs", joinAs, joinValueAs);
10032
10041
  setObjectValueImmutable(query.q, "joinedParsers", joinValueAs || joinAs, getQueryParsers(joinQuery));
@@ -10926,6 +10935,7 @@ const processNestedSelectPathEntry = (batches, path, thisKey, thisReturnType, i,
10926
10935
  key: thisKey
10927
10936
  });
10928
10937
  else {
10938
+ if (!data) return;
10929
10939
  const { key, returnType } = path[++i];
10930
10940
  if (!thisReturnType || thisReturnType === "all") for (const row of data) processNestedSelectPathEntry(batches, path, key, returnType, i, last, row);
10931
10941
  else processNestedSelectPathEntry(batches, path, key, returnType, i, last, data);
@@ -13872,6 +13882,7 @@ const performQuery = async (q, args, method) => {
13872
13882
  throw err;
13873
13883
  }
13874
13884
  };
13885
+ const getTableNameInDb = (table, nameInDb, snakeCase) => table && (nameInDb || (snakeCase ? toSnakeCase(table) : table));
13875
13886
  var Db = class extends QueryMethods {
13876
13887
  constructor(adapterNotInTransaction, qb, table = void 0, shape, columnTypes, asyncStorage, options, tableData = {}, viewData) {
13877
13888
  super();
@@ -13949,6 +13960,7 @@ var Db = class extends QueryMethods {
13949
13960
  this.q = {
13950
13961
  adapter: adapterNotInTransaction,
13951
13962
  selectShape: shape,
13963
+ nameInDb: getTableNameInDb(table, options.nameInDb, snakeCase),
13952
13964
  handleResult,
13953
13965
  logger,
13954
13966
  log: logParamToLogObject(logger, options.log),
@@ -14285,7 +14297,7 @@ const _initQueryBuilder = (adapter, columnTypes, asyncStorage, commonOptions, op
14285
14297
  const makeColumnInfoSql = (query, column) => {
14286
14298
  const values = [];
14287
14299
  const schema = getQuerySchema(query);
14288
- let text = `SELECT * FROM information_schema.columns WHERE table_name = ${addValue(values, query.table)} AND table_catalog = current_database() AND table_schema = ${schema ? addValue(values, schema) : "current_schema()"}`;
14300
+ let text = `SELECT * FROM information_schema.columns WHERE table_name = ${addValue(values, query.q.nameInDb || query.table)} AND table_catalog = current_database() AND table_schema = ${schema ? addValue(values, schema) : "current_schema()"}`;
14289
14301
  if (column) text += ` AND column_name = ${addValue(values, query.shape[column]?.data.name || column)}`;
14290
14302
  return {
14291
14303
  text,