pqb 0.39.0 → 0.39.2

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
@@ -1824,12 +1824,14 @@ class BooleanColumn extends ColumnType {
1824
1824
  super(schema, schema.boolean());
1825
1825
  this.dataType = "bool";
1826
1826
  this.operators = Operators.boolean;
1827
- this.parseItem = (input) => input[0] === "t";
1828
1827
  this.data.alias = "boolean";
1829
1828
  }
1830
1829
  toCode(ctx, key) {
1831
1830
  return columnCode(this, ctx, key, "boolean()");
1832
1831
  }
1832
+ parseItem(input) {
1833
+ return input[0] === "t";
1834
+ }
1833
1835
  }
1834
1836
 
1835
1837
  const encodeFn = (x) => x === null ? x : JSON.stringify(x);
@@ -1864,15 +1866,22 @@ const queryTypeWithLimitOne = {
1864
1866
  };
1865
1867
  const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
1866
1868
 
1867
- function simpleColumnToSQL(ctx, key, column, quotedAs) {
1869
+ const applySqlComputed = (ctx, q, computed, as, quotedAs) => {
1870
+ var _a;
1871
+ const parser = computed.result.value.parseFn;
1872
+ if (parser)
1873
+ ((_a = q.parsers) != null ? _a : q.parsers = {})[as] = parser;
1874
+ return computed.toSQL(ctx, quotedAs);
1875
+ };
1876
+ function simpleColumnToSQL(ctx, q, key, column, quotedAs) {
1868
1877
  if (!column)
1869
1878
  return `"${key}"`;
1870
1879
  const { data } = column;
1871
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1880
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1872
1881
  }
1873
- function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1882
+ function simpleExistingColumnToSQL(ctx, q, key, column, quotedAs) {
1874
1883
  const { data } = column;
1875
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1884
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1876
1885
  }
1877
1886
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1878
1887
  var _a;
@@ -1891,7 +1900,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1891
1900
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1892
1901
  return `"${column}".r`;
1893
1902
  }
1894
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1903
+ return simpleColumnToSQL(ctx, data, column, shape[column], quotedAs);
1895
1904
  };
1896
1905
  const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1897
1906
  var _a;
@@ -1906,12 +1915,12 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1906
1915
  for (const s of data.select) {
1907
1916
  if (typeof s === "object" && "selectAs" in s) {
1908
1917
  if (column in s.selectAs) {
1909
- return simpleColumnToSQL(ctx, column, data.shape[column]);
1918
+ return simpleColumnToSQL(ctx, data, column, data.shape[column]);
1910
1919
  }
1911
1920
  }
1912
1921
  }
1913
1922
  }
1914
- return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1923
+ return simpleColumnToSQL(ctx, data, column, data.shape[column], quotedAs);
1915
1924
  }
1916
1925
  };
1917
1926
  const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
@@ -1929,17 +1938,30 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1929
1938
  return `"${tableName}"."${col.data.name}"`;
1930
1939
  }
1931
1940
  if (col.data.computed) {
1932
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1941
+ return applySqlComputed(ctx, data, col.data.computed, column, quoted);
1933
1942
  }
1934
1943
  return `"${tableName}"."${key}"`;
1935
1944
  }
1936
1945
  return `"${tableName}"."${key}"`;
1937
1946
  };
1938
- const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select) => {
1947
+ const columnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1948
+ const index = column.indexOf(".");
1949
+ return index !== -1 ? tableColumnToSqlWithAs(
1950
+ ctx,
1951
+ data,
1952
+ column,
1953
+ column.slice(0, index),
1954
+ column.slice(index + 1),
1955
+ as,
1956
+ quotedAs,
1957
+ select
1958
+ ) : ownColumnToSqlWithAs(ctx, data, column, as, quotedAs, select);
1959
+ };
1960
+ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, select) => {
1939
1961
  var _a, _b, _c;
1940
1962
  if (key === "*") {
1941
1963
  if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1942
- return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1964
+ return select ? `row_to_json("${table}".*) "${as}"` : `"${table}".r "${as}"`;
1943
1965
  }
1944
1966
  return column;
1945
1967
  }
@@ -1948,29 +1970,41 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select)
1948
1970
  const col = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1949
1971
  if (col) {
1950
1972
  if (col.data.name && col.data.name !== key) {
1951
- return `"${tableName}"."${col.data.name}" "${key}"`;
1973
+ return `"${tableName}"."${col.data.name}" "${as}"`;
1952
1974
  }
1953
1975
  if (col.data.computed) {
1954
- return `${col.data.computed.toSQL(ctx, quoted)} "${key}"`;
1976
+ return `${applySqlComputed(
1977
+ ctx,
1978
+ data,
1979
+ col.data.computed,
1980
+ as,
1981
+ quoted
1982
+ )} "${as}"`;
1955
1983
  }
1956
1984
  }
1957
- return `"${tableName}"."${key}"`;
1985
+ return `"${tableName}"."${key}"${key === as ? "" : ` "${as}"`}`;
1958
1986
  };
1959
- const ownColumnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1987
+ const ownColumnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1960
1988
  var _a;
1961
1989
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1962
- return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
1990
+ return select ? `row_to_json("${column}".*) "${as}"` : `"${column}".r "${as}"`;
1963
1991
  }
1964
1992
  const col = data.shape[column];
1965
1993
  if (col) {
1966
1994
  if (col.data.name && col.data.name !== column) {
1967
- return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
1995
+ return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}"${col.data.name === as ? "" : ` "${as}"`}`;
1968
1996
  }
1969
1997
  if (col.data.computed) {
1970
- return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
1998
+ return `${applySqlComputed(
1999
+ ctx,
2000
+ data,
2001
+ col.data.computed,
2002
+ as,
2003
+ quotedAs
2004
+ )} "${as}"`;
1971
2005
  }
1972
2006
  }
1973
- return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
2007
+ return `${quotedAs ? `${quotedAs}.` : ""}"${column}"${column === as ? "" : ` "${as}"`}`;
1974
2008
  };
1975
2009
  const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
1976
2010
  return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
@@ -1989,6 +2023,25 @@ const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
1989
2023
  }
1990
2024
  };
1991
2025
 
2026
+ var __accessCheck = (obj, member, msg) => {
2027
+ if (!member.has(obj))
2028
+ throw TypeError("Cannot " + msg);
2029
+ };
2030
+ var __privateGet = (obj, member, getter) => {
2031
+ __accessCheck(obj, member, "read from private field");
2032
+ return getter ? getter.call(obj) : member.get(obj);
2033
+ };
2034
+ var __privateAdd = (obj, member, value) => {
2035
+ if (member.has(obj))
2036
+ throw TypeError("Cannot add the same private member more than once");
2037
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
2038
+ };
2039
+ var __privateSet = (obj, member, value, setter) => {
2040
+ __accessCheck(obj, member, "write to private field");
2041
+ setter ? setter.call(obj, value) : member.set(obj, value);
2042
+ return value;
2043
+ };
2044
+ var _query;
1992
2045
  class OrchidOrmError extends Error {
1993
2046
  }
1994
2047
  class NotFoundError extends OrchidOrmError {
@@ -2000,9 +2053,14 @@ class NotFoundError extends OrchidOrmError {
2000
2053
  class OrchidOrmInternalError extends Error {
2001
2054
  constructor(query, message) {
2002
2055
  super(message);
2003
- this.query = query;
2056
+ __privateAdd(this, _query, void 0);
2057
+ __privateSet(this, _query, query);
2058
+ }
2059
+ get query() {
2060
+ return __privateGet(this, _query);
2004
2061
  }
2005
2062
  }
2063
+ _query = new WeakMap();
2006
2064
  class QueryError extends OrchidOrmInternalError {
2007
2065
  get isUnique() {
2008
2066
  return this.code === "23505";
@@ -2032,7 +2090,6 @@ class MoreThanOneRowError extends OrchidOrmInternalError {
2032
2090
  class UnhandledTypeError extends OrchidOrmInternalError {
2033
2091
  constructor(query, value) {
2034
2092
  super(query, `Unhandled type: ${JSON.stringify(value)} received`);
2035
- this.query = query;
2036
2093
  }
2037
2094
  }
2038
2095
 
@@ -2279,7 +2336,13 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2279
2336
  let column = query.shape[key];
2280
2337
  let quotedColumn;
2281
2338
  if (column) {
2282
- quotedColumn = simpleExistingColumnToSQL(ctx, key, column, quotedAs);
2339
+ quotedColumn = simpleExistingColumnToSQL(
2340
+ ctx,
2341
+ query,
2342
+ key,
2343
+ column,
2344
+ quotedAs
2345
+ );
2283
2346
  } else if (!column) {
2284
2347
  const index = key.indexOf(".");
2285
2348
  if (index !== -1) {
@@ -2287,7 +2350,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2287
2350
  const quoted = `"${table2}"`;
2288
2351
  const name = key.slice(index + 1);
2289
2352
  column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
2290
- quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
2353
+ quotedColumn = simpleColumnToSQL(ctx, query, name, column, quoted);
2291
2354
  } else {
2292
2355
  column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
2293
2356
  quotedColumn = `"${key}".r`;
@@ -2948,16 +3011,9 @@ class ArrayColumn extends ColumnType {
2948
3011
  this.dataType = "array";
2949
3012
  this.operators = Operators.array;
2950
3013
  this.parseFn = Object.assign(
2951
- (input) => {
3014
+ (source) => {
2952
3015
  const entries = [];
2953
- parseArray(
2954
- input,
2955
- 0,
2956
- input.length,
2957
- entries,
2958
- false,
2959
- this.data.item
2960
- );
3016
+ parsePostgresArray(source, entries, this.data.item.parseItem);
2961
3017
  return entries;
2962
3018
  },
2963
3019
  {
@@ -2989,70 +3045,60 @@ class ArrayColumn extends ColumnType {
2989
3045
  return columnCode(this, ctx, key, code);
2990
3046
  }
2991
3047
  }
2992
- const parseArray = (input, pos, len, entries, nested, item) => {
2993
- if (input[0] === "[") {
2994
- while (pos < len) {
2995
- let char = input[pos++];
2996
- if (char === "\\") {
2997
- char = input[pos++];
3048
+ const parsePostgresArray = (source, entries, transform) => {
3049
+ let pos = 0;
3050
+ if (source[0] === "[") {
3051
+ pos = source.indexOf("=") + 1;
3052
+ if (!pos)
3053
+ pos = source.length;
3054
+ }
3055
+ if (source[pos] === "{")
3056
+ pos++;
3057
+ let recorded = "";
3058
+ while (pos < source.length) {
3059
+ const character = source[pos++];
3060
+ if (character === "{") {
3061
+ const innerEntries = [];
3062
+ entries.push(innerEntries);
3063
+ pos += parsePostgresArray(source.slice(pos - 1), innerEntries, transform) - 1;
3064
+ } else if (character === "}") {
3065
+ if (recorded) {
3066
+ entries.push(
3067
+ recorded === "NULL" ? null : transform ? transform(recorded) : recorded
3068
+ );
2998
3069
  }
2999
- if (char === "=")
3000
- break;
3001
- }
3002
- }
3003
- let quote = false;
3004
- let start = pos;
3005
- while (pos < len) {
3006
- let char = input[pos++];
3007
- const escaped = char === "\\";
3008
- if (escaped) {
3009
- char = input[pos++];
3010
- }
3011
- if (char === '"' && !escaped) {
3012
- if (quote) {
3013
- pushEntry(input, start, pos, entries, item);
3014
- } else {
3015
- start = pos;
3016
- }
3017
- quote = !quote;
3018
- } else if (char === "," && !quote) {
3019
- if (start !== pos) {
3020
- pushEntry(input, start, pos, entries, item);
3021
- }
3022
- start = pos;
3023
- } else if (char === "{" && !quote) {
3024
- let array;
3025
- let nestedItem = item;
3026
- if (nested) {
3027
- array = [];
3028
- entries.push(array);
3029
- if ("item" in item.data) {
3030
- nestedItem = item.data.item;
3070
+ return pos;
3071
+ } else if (character === '"') {
3072
+ let esc = false;
3073
+ let rec = "";
3074
+ while (pos < source.length) {
3075
+ let char;
3076
+ while ((char = source[pos++]) === "\\") {
3077
+ if (!(esc = !esc))
3078
+ rec += "\\";
3031
3079
  }
3032
- } else {
3033
- array = entries;
3034
- }
3035
- pos = parseArray(input, pos, len, array, true, nestedItem);
3036
- start = pos + 1;
3037
- } else if (char === "}" && !quote) {
3038
- if (start !== pos) {
3039
- pushEntry(input, start, pos, entries, item);
3080
+ if (esc) {
3081
+ esc = false;
3082
+ } else if (char === '"') {
3083
+ break;
3084
+ }
3085
+ rec += char;
3086
+ }
3087
+ entries.push(transform ? transform(rec) : rec);
3088
+ recorded = "";
3089
+ } else if (character === ",") {
3090
+ if (recorded) {
3091
+ entries.push(
3092
+ recorded === "NULL" ? null : transform ? transform(recorded) : recorded
3093
+ );
3094
+ recorded = "";
3040
3095
  }
3041
- start = pos + 1;
3042
- break;
3096
+ } else {
3097
+ recorded += character;
3043
3098
  }
3044
3099
  }
3045
3100
  return pos;
3046
3101
  };
3047
- const pushEntry = (input, start, pos, entries, item) => {
3048
- let entry = input.slice(start, pos - 1);
3049
- if (entry === "NULL") {
3050
- entry = null;
3051
- } else if (item.parseItem) {
3052
- entry = item.parseItem(entry);
3053
- }
3054
- entries.push(entry);
3055
- };
3056
3102
 
3057
3103
  const parseDateToNumber = (value) => value ? Date.parse(value) : value;
3058
3104
  const parseDateToDate = (value) => value ? new Date(value) : value;
@@ -4839,13 +4885,21 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4839
4885
  item,
4840
4886
  tableName,
4841
4887
  key,
4888
+ key === "*" ? tableName : key,
4842
4889
  quotedAs,
4843
4890
  true
4844
4891
  );
4845
4892
  } else {
4846
4893
  if (hookSelect == null ? void 0 : hookSelect.get(item))
4847
4894
  (selected != null ? selected : selected = {})[item] = quotedAs;
4848
- sql = ownColumnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4895
+ sql = ownColumnToSqlWithAs(
4896
+ ctx,
4897
+ table.q,
4898
+ item,
4899
+ item,
4900
+ quotedAs,
4901
+ true
4902
+ );
4849
4903
  }
4850
4904
  }
4851
4905
  list.push(sql);
@@ -4864,14 +4918,14 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4864
4918
  }
4865
4919
  } else if (value) {
4866
4920
  list.push(
4867
- `${columnToSql(
4921
+ columnToSqlWithAs(
4868
4922
  ctx,
4869
4923
  table.q,
4870
- table.q.shape,
4871
4924
  value,
4925
+ as,
4872
4926
  quotedAs,
4873
4927
  true
4874
- )} "${as}"`
4928
+ )
4875
4929
  );
4876
4930
  }
4877
4931
  }
@@ -4900,7 +4954,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4900
4954
  quotedTable = quotedAs;
4901
4955
  columnName = select;
4902
4956
  col = query.shape[select];
4903
- sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4957
+ sql = simpleColumnToSQL(ctx, query, select, col, quotedAs);
4904
4958
  }
4905
4959
  if (selected == null ? void 0 : selected[columnName]) {
4906
4960
  if ((selected == null ? void 0 : selected[columnName]) === quotedTable) {
@@ -11369,6 +11423,8 @@ class ColumnRefExpression extends Expression {
11369
11423
  makeSQL(ctx, quotedAs) {
11370
11424
  return simpleExistingColumnToSQL(
11371
11425
  ctx,
11426
+ // it's for parsers for computed SQL. In the column ref case, parsers should be set when selecting the column ref.
11427
+ {},
11372
11428
  this.name,
11373
11429
  this.result.value,
11374
11430
  quotedAs