pqb 0.42.9 → 0.43.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
@@ -810,8 +810,10 @@ const columnExcludesToCode = (items) => {
810
810
  }
811
811
  return code;
812
812
  };
813
- const columnCheckToCode = (ctx, { sql, name }, columnName) => {
814
- return `.check(${sql.toCode(ctx.t)}${name && name !== `${ctx.table}_${columnName}_check` ? `, { name: '${name}' }` : ""})`;
813
+ const columnCheckToCode = (ctx, checks) => {
814
+ return checks.map(
815
+ ({ sql, name }) => `.check(${sql.toCode(ctx.t)}${name ? `, '${name}'` : ""})`
816
+ ).join("");
815
817
  };
816
818
  const identityToCode = (identity, dataType) => {
817
819
  const code = [];
@@ -895,8 +897,8 @@ const columnCode = (type, ctx, key, code) => {
895
897
  }
896
898
  }
897
899
  if (data.comment) addCode(code, `.comment(${singleQuote(data.comment)})`);
898
- if (data.check) {
899
- addCode(code, columnCheckToCode(ctx, data.check, name));
900
+ if (data.checks) {
901
+ addCode(code, columnCheckToCode(ctx, data.checks));
900
902
  }
901
903
  if (data.errors) {
902
904
  for (const part of columnErrorMessagesToCode(data.errors)) {
@@ -5648,11 +5650,18 @@ function queryJson(self, coalesce) {
5648
5650
  return q;
5649
5651
  }
5650
5652
 
5651
- const pushSelectSql = (ctx, table, query, quotedAs) => {
5652
- const sql = selectToSql(ctx, table, query, quotedAs);
5653
+ const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
5654
+ const sql = selectToSql(
5655
+ ctx,
5656
+ table,
5657
+ query,
5658
+ quotedAs,
5659
+ query.hookSelect,
5660
+ aliases
5661
+ );
5653
5662
  if (sql) ctx.sql.push(sql);
5654
5663
  };
5655
- const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect) => {
5664
+ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, aliases) => {
5656
5665
  let selected;
5657
5666
  const list = [];
5658
5667
  if (query.select) {
@@ -5694,6 +5703,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5694
5703
  }
5695
5704
  }
5696
5705
  list.push(sql);
5706
+ aliases?.push("");
5697
5707
  } else if (item) {
5698
5708
  if ("selectAs" in item) {
5699
5709
  const obj = item.selectAs;
@@ -5703,8 +5713,9 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5703
5713
  if (typeof value === "object") {
5704
5714
  if (isExpression(value)) {
5705
5715
  list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
5716
+ aliases?.push(as);
5706
5717
  } else {
5707
- pushSubQuerySql(ctx, value, as, list, quotedAs);
5718
+ pushSubQuerySql(ctx, value, as, list, quotedAs, aliases);
5708
5719
  }
5709
5720
  } else if (value) {
5710
5721
  list.push(
@@ -5717,10 +5728,12 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5717
5728
  true
5718
5729
  )
5719
5730
  );
5731
+ aliases?.push(as);
5720
5732
  }
5721
5733
  }
5722
5734
  } else {
5723
5735
  list.push(selectedObjectToSQL(ctx, quotedAs, item));
5736
+ aliases?.push("");
5724
5737
  }
5725
5738
  }
5726
5739
  }
@@ -5771,7 +5784,7 @@ function selectedObjectToSQL(ctx, quotedAs, item) {
5771
5784
  const selectAllSql = (query, quotedAs) => {
5772
5785
  return query.join?.length ? query.selectAllColumns?.map((item) => `${quotedAs}.${item}`).join(", ") || `${quotedAs}.*` : query.selectAllColumns?.join(", ") || "*";
5773
5786
  };
5774
- const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5787
+ const pushSubQuerySql = (ctx, query, as, list, quotedAs, aliases) => {
5775
5788
  const { returnType = "all" } = query.q;
5776
5789
  if (isQueryNone(query)) {
5777
5790
  let sql;
@@ -5797,6 +5810,7 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5797
5810
  throw new UnhandledTypeError(query, returnType);
5798
5811
  }
5799
5812
  list.push(`${sql} "${as}"`);
5813
+ aliases?.push(as);
5800
5814
  return;
5801
5815
  }
5802
5816
  if (query.q.joinedForSelect) {
@@ -5821,7 +5835,10 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5821
5835
  default:
5822
5836
  throw new UnhandledTypeError(query, returnType);
5823
5837
  }
5824
- if (sql) list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
5838
+ if (sql) {
5839
+ list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
5840
+ aliases?.push(as);
5841
+ }
5825
5842
  return;
5826
5843
  }
5827
5844
  switch (returnType) {
@@ -6591,7 +6608,8 @@ const makeSQL = (table, options) => {
6591
6608
  if (query.distinct) {
6592
6609
  pushDistinctSql(ctx, table, query.distinct, quotedAs);
6593
6610
  }
6594
- pushSelectSql(ctx, table, query, quotedAs);
6611
+ const aliases = query.group ? [] : void 0;
6612
+ pushSelectSql(ctx, table, query, quotedAs, aliases);
6595
6613
  if (table.table || query.from) {
6596
6614
  pushFromAndAs(ctx, table, query, quotedAs);
6597
6615
  }
@@ -6607,9 +6625,14 @@ const makeSQL = (table, options) => {
6607
6625
  pushWhereStatementSql(ctx, table, query, quotedAs);
6608
6626
  }
6609
6627
  if (query.group) {
6610
- const group = query.group.map(
6611
- (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : maybeSelectedColumnToSql(ctx, table.q, item, quotedAs)
6612
- );
6628
+ const group = query.group.map((item) => {
6629
+ if (isExpression(item)) {
6630
+ return item.toSQL(ctx, quotedAs);
6631
+ } else {
6632
+ const i = aliases.indexOf(item);
6633
+ return i !== -1 ? i + 1 : columnToSql(ctx, table.q, table.shape, item, quotedAs);
6634
+ }
6635
+ });
6613
6636
  sql.push(`GROUP BY ${group.join(", ")}`);
6614
6637
  }
6615
6638
  if (query.having) pushHavingSql(ctx, query, quotedAs);
@@ -11720,6 +11743,10 @@ class QueryMethods {
11720
11743
  * .group('month');
11721
11744
  * ```
11722
11745
  *
11746
+ * Column aliases in `select` take precedence over table columns,
11747
+ * so if in the query above `db.product` had a column `month`,
11748
+ * the query would work in the exact same way, group by would reference the selected `month` expression.
11749
+ *
11723
11750
  * @param columns - column names or a raw SQL
11724
11751
  */
11725
11752
  group(...columns) {