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.d.ts CHANGED
@@ -553,8 +553,9 @@ declare class NotFoundError extends OrchidOrmError {
553
553
  constructor(query: Query, message?: string);
554
554
  }
555
555
  declare class OrchidOrmInternalError extends Error {
556
- query: Query;
556
+ #private;
557
557
  constructor(query: Query, message?: string);
558
+ get query(): Query;
558
559
  }
559
560
  type QueryErrorName = 'parseComplete' | 'bindComplete' | 'closeComplete' | 'noData' | 'portalSuspended' | 'replicationStart' | 'emptyQuery' | 'copyDone' | 'copyData' | 'rowDescription' | 'parameterDescription' | 'parameterStatus' | 'backendKeyData' | 'notification' | 'readyForQuery' | 'commandComplete' | 'dataRow' | 'copyInResponse' | 'copyOutResponse' | 'authenticationOk' | 'authenticationMD5Password' | 'authenticationCleartextPassword' | 'authenticationSASL' | 'authenticationSASLContinue' | 'authenticationSASLFinal' | 'error' | 'notice';
560
561
  declare abstract class QueryError<T extends PickQueryShape = PickQueryShape> extends OrchidOrmInternalError {
@@ -587,7 +588,6 @@ declare class MoreThanOneRowError extends OrchidOrmInternalError {
587
588
  constructor(query: Query, message?: string);
588
589
  }
589
590
  declare class UnhandledTypeError extends OrchidOrmInternalError {
590
- query: Query;
591
591
  constructor(query: Query, value: never);
592
592
  }
593
593
 
@@ -2683,7 +2683,7 @@ declare class ArrayColumn<Schema extends ColumnTypeSchemaArg, Item extends Array
2683
2683
  constructor(schema: Schema, item: Item, inputType: InputType, outputType?: OutputType, queryType?: QueryType);
2684
2684
  toSQL(): string;
2685
2685
  toCode(this: ArrayColumn<ColumnSchemaConfig, ArrayColumnValue, unknown, unknown, unknown>, ctx: ColumnToCodeCtx, key: string): Code;
2686
- parseFn: ((input: unknown) => unknown[]) & {
2686
+ parseFn: ((source: string) => unknown[]) & {
2687
2687
  hideFromCode: boolean;
2688
2688
  };
2689
2689
  }
@@ -7893,7 +7893,7 @@ declare class BooleanColumn<Schema extends ColumnSchemaConfig> extends ColumnTyp
7893
7893
  operators: OperatorsBoolean;
7894
7894
  constructor(schema: Schema);
7895
7895
  toCode(ctx: ColumnToCodeCtx, key: string): Code;
7896
- parseItem: (input: string) => boolean;
7896
+ parseItem(input: string): boolean;
7897
7897
  }
7898
7898
 
7899
7899
  declare class CustomTypeColumn<Schema extends ColumnSchemaConfig> extends ColumnType<Schema, unknown, ReturnType<Schema['unknown']>, typeof Operators.any> {
package/dist/index.js CHANGED
@@ -1826,12 +1826,14 @@ class BooleanColumn extends ColumnType {
1826
1826
  super(schema, schema.boolean());
1827
1827
  this.dataType = "bool";
1828
1828
  this.operators = Operators.boolean;
1829
- this.parseItem = (input) => input[0] === "t";
1830
1829
  this.data.alias = "boolean";
1831
1830
  }
1832
1831
  toCode(ctx, key) {
1833
1832
  return columnCode(this, ctx, key, "boolean()");
1834
1833
  }
1834
+ parseItem(input) {
1835
+ return input[0] === "t";
1836
+ }
1835
1837
  }
1836
1838
 
1837
1839
  const encodeFn = (x) => x === null ? x : JSON.stringify(x);
@@ -1866,15 +1868,22 @@ const queryTypeWithLimitOne = {
1866
1868
  };
1867
1869
  const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
1868
1870
 
1869
- function simpleColumnToSQL(ctx, key, column, quotedAs) {
1871
+ const applySqlComputed = (ctx, q, computed, as, quotedAs) => {
1872
+ var _a;
1873
+ const parser = computed.result.value.parseFn;
1874
+ if (parser)
1875
+ ((_a = q.parsers) != null ? _a : q.parsers = {})[as] = parser;
1876
+ return computed.toSQL(ctx, quotedAs);
1877
+ };
1878
+ function simpleColumnToSQL(ctx, q, key, column, quotedAs) {
1870
1879
  if (!column)
1871
1880
  return `"${key}"`;
1872
1881
  const { data } = column;
1873
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1882
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1874
1883
  }
1875
- function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1884
+ function simpleExistingColumnToSQL(ctx, q, key, column, quotedAs) {
1876
1885
  const { data } = column;
1877
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1886
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1878
1887
  }
1879
1888
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1880
1889
  var _a;
@@ -1893,7 +1902,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1893
1902
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1894
1903
  return `"${column}".r`;
1895
1904
  }
1896
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1905
+ return simpleColumnToSQL(ctx, data, column, shape[column], quotedAs);
1897
1906
  };
1898
1907
  const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1899
1908
  var _a;
@@ -1908,12 +1917,12 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1908
1917
  for (const s of data.select) {
1909
1918
  if (typeof s === "object" && "selectAs" in s) {
1910
1919
  if (column in s.selectAs) {
1911
- return simpleColumnToSQL(ctx, column, data.shape[column]);
1920
+ return simpleColumnToSQL(ctx, data, column, data.shape[column]);
1912
1921
  }
1913
1922
  }
1914
1923
  }
1915
1924
  }
1916
- return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1925
+ return simpleColumnToSQL(ctx, data, column, data.shape[column], quotedAs);
1917
1926
  }
1918
1927
  };
1919
1928
  const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
@@ -1931,17 +1940,30 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1931
1940
  return `"${tableName}"."${col.data.name}"`;
1932
1941
  }
1933
1942
  if (col.data.computed) {
1934
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1943
+ return applySqlComputed(ctx, data, col.data.computed, column, quoted);
1935
1944
  }
1936
1945
  return `"${tableName}"."${key}"`;
1937
1946
  }
1938
1947
  return `"${tableName}"."${key}"`;
1939
1948
  };
1940
- const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select) => {
1949
+ const columnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1950
+ const index = column.indexOf(".");
1951
+ return index !== -1 ? tableColumnToSqlWithAs(
1952
+ ctx,
1953
+ data,
1954
+ column,
1955
+ column.slice(0, index),
1956
+ column.slice(index + 1),
1957
+ as,
1958
+ quotedAs,
1959
+ select
1960
+ ) : ownColumnToSqlWithAs(ctx, data, column, as, quotedAs, select);
1961
+ };
1962
+ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, select) => {
1941
1963
  var _a, _b, _c;
1942
1964
  if (key === "*") {
1943
1965
  if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1944
- return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1966
+ return select ? `row_to_json("${table}".*) "${as}"` : `"${table}".r "${as}"`;
1945
1967
  }
1946
1968
  return column;
1947
1969
  }
@@ -1950,29 +1972,41 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select)
1950
1972
  const col = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1951
1973
  if (col) {
1952
1974
  if (col.data.name && col.data.name !== key) {
1953
- return `"${tableName}"."${col.data.name}" "${key}"`;
1975
+ return `"${tableName}"."${col.data.name}" "${as}"`;
1954
1976
  }
1955
1977
  if (col.data.computed) {
1956
- return `${col.data.computed.toSQL(ctx, quoted)} "${key}"`;
1978
+ return `${applySqlComputed(
1979
+ ctx,
1980
+ data,
1981
+ col.data.computed,
1982
+ as,
1983
+ quoted
1984
+ )} "${as}"`;
1957
1985
  }
1958
1986
  }
1959
- return `"${tableName}"."${key}"`;
1987
+ return `"${tableName}"."${key}"${key === as ? "" : ` "${as}"`}`;
1960
1988
  };
1961
- const ownColumnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1989
+ const ownColumnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1962
1990
  var _a;
1963
1991
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1964
- return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
1992
+ return select ? `row_to_json("${column}".*) "${as}"` : `"${column}".r "${as}"`;
1965
1993
  }
1966
1994
  const col = data.shape[column];
1967
1995
  if (col) {
1968
1996
  if (col.data.name && col.data.name !== column) {
1969
- return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
1997
+ return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}"${col.data.name === as ? "" : ` "${as}"`}`;
1970
1998
  }
1971
1999
  if (col.data.computed) {
1972
- return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
2000
+ return `${applySqlComputed(
2001
+ ctx,
2002
+ data,
2003
+ col.data.computed,
2004
+ as,
2005
+ quotedAs
2006
+ )} "${as}"`;
1973
2007
  }
1974
2008
  }
1975
- return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
2009
+ return `${quotedAs ? `${quotedAs}.` : ""}"${column}"${column === as ? "" : ` "${as}"`}`;
1976
2010
  };
1977
2011
  const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
1978
2012
  return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
@@ -1991,6 +2025,25 @@ const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
1991
2025
  }
1992
2026
  };
1993
2027
 
2028
+ var __accessCheck = (obj, member, msg) => {
2029
+ if (!member.has(obj))
2030
+ throw TypeError("Cannot " + msg);
2031
+ };
2032
+ var __privateGet = (obj, member, getter) => {
2033
+ __accessCheck(obj, member, "read from private field");
2034
+ return getter ? getter.call(obj) : member.get(obj);
2035
+ };
2036
+ var __privateAdd = (obj, member, value) => {
2037
+ if (member.has(obj))
2038
+ throw TypeError("Cannot add the same private member more than once");
2039
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
2040
+ };
2041
+ var __privateSet = (obj, member, value, setter) => {
2042
+ __accessCheck(obj, member, "write to private field");
2043
+ setter ? setter.call(obj, value) : member.set(obj, value);
2044
+ return value;
2045
+ };
2046
+ var _query;
1994
2047
  class OrchidOrmError extends Error {
1995
2048
  }
1996
2049
  class NotFoundError extends OrchidOrmError {
@@ -2002,9 +2055,14 @@ class NotFoundError extends OrchidOrmError {
2002
2055
  class OrchidOrmInternalError extends Error {
2003
2056
  constructor(query, message) {
2004
2057
  super(message);
2005
- this.query = query;
2058
+ __privateAdd(this, _query, void 0);
2059
+ __privateSet(this, _query, query);
2060
+ }
2061
+ get query() {
2062
+ return __privateGet(this, _query);
2006
2063
  }
2007
2064
  }
2065
+ _query = new WeakMap();
2008
2066
  class QueryError extends OrchidOrmInternalError {
2009
2067
  get isUnique() {
2010
2068
  return this.code === "23505";
@@ -2034,7 +2092,6 @@ class MoreThanOneRowError extends OrchidOrmInternalError {
2034
2092
  class UnhandledTypeError extends OrchidOrmInternalError {
2035
2093
  constructor(query, value) {
2036
2094
  super(query, `Unhandled type: ${JSON.stringify(value)} received`);
2037
- this.query = query;
2038
2095
  }
2039
2096
  }
2040
2097
 
@@ -2281,7 +2338,13 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2281
2338
  let column = query.shape[key];
2282
2339
  let quotedColumn;
2283
2340
  if (column) {
2284
- quotedColumn = simpleExistingColumnToSQL(ctx, key, column, quotedAs);
2341
+ quotedColumn = simpleExistingColumnToSQL(
2342
+ ctx,
2343
+ query,
2344
+ key,
2345
+ column,
2346
+ quotedAs
2347
+ );
2285
2348
  } else if (!column) {
2286
2349
  const index = key.indexOf(".");
2287
2350
  if (index !== -1) {
@@ -2289,7 +2352,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2289
2352
  const quoted = `"${table2}"`;
2290
2353
  const name = key.slice(index + 1);
2291
2354
  column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
2292
- quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
2355
+ quotedColumn = simpleColumnToSQL(ctx, query, name, column, quoted);
2293
2356
  } else {
2294
2357
  column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
2295
2358
  quotedColumn = `"${key}".r`;
@@ -2950,16 +3013,9 @@ class ArrayColumn extends ColumnType {
2950
3013
  this.dataType = "array";
2951
3014
  this.operators = Operators.array;
2952
3015
  this.parseFn = Object.assign(
2953
- (input) => {
3016
+ (source) => {
2954
3017
  const entries = [];
2955
- parseArray(
2956
- input,
2957
- 0,
2958
- input.length,
2959
- entries,
2960
- false,
2961
- this.data.item
2962
- );
3018
+ parsePostgresArray(source, entries, this.data.item.parseItem);
2963
3019
  return entries;
2964
3020
  },
2965
3021
  {
@@ -2991,70 +3047,60 @@ class ArrayColumn extends ColumnType {
2991
3047
  return columnCode(this, ctx, key, code);
2992
3048
  }
2993
3049
  }
2994
- const parseArray = (input, pos, len, entries, nested, item) => {
2995
- if (input[0] === "[") {
2996
- while (pos < len) {
2997
- let char = input[pos++];
2998
- if (char === "\\") {
2999
- char = input[pos++];
3050
+ const parsePostgresArray = (source, entries, transform) => {
3051
+ let pos = 0;
3052
+ if (source[0] === "[") {
3053
+ pos = source.indexOf("=") + 1;
3054
+ if (!pos)
3055
+ pos = source.length;
3056
+ }
3057
+ if (source[pos] === "{")
3058
+ pos++;
3059
+ let recorded = "";
3060
+ while (pos < source.length) {
3061
+ const character = source[pos++];
3062
+ if (character === "{") {
3063
+ const innerEntries = [];
3064
+ entries.push(innerEntries);
3065
+ pos += parsePostgresArray(source.slice(pos - 1), innerEntries, transform) - 1;
3066
+ } else if (character === "}") {
3067
+ if (recorded) {
3068
+ entries.push(
3069
+ recorded === "NULL" ? null : transform ? transform(recorded) : recorded
3070
+ );
3000
3071
  }
3001
- if (char === "=")
3002
- break;
3003
- }
3004
- }
3005
- let quote = false;
3006
- let start = pos;
3007
- while (pos < len) {
3008
- let char = input[pos++];
3009
- const escaped = char === "\\";
3010
- if (escaped) {
3011
- char = input[pos++];
3012
- }
3013
- if (char === '"' && !escaped) {
3014
- if (quote) {
3015
- pushEntry(input, start, pos, entries, item);
3016
- } else {
3017
- start = pos;
3018
- }
3019
- quote = !quote;
3020
- } else if (char === "," && !quote) {
3021
- if (start !== pos) {
3022
- pushEntry(input, start, pos, entries, item);
3023
- }
3024
- start = pos;
3025
- } else if (char === "{" && !quote) {
3026
- let array;
3027
- let nestedItem = item;
3028
- if (nested) {
3029
- array = [];
3030
- entries.push(array);
3031
- if ("item" in item.data) {
3032
- nestedItem = item.data.item;
3072
+ return pos;
3073
+ } else if (character === '"') {
3074
+ let esc = false;
3075
+ let rec = "";
3076
+ while (pos < source.length) {
3077
+ let char;
3078
+ while ((char = source[pos++]) === "\\") {
3079
+ if (!(esc = !esc))
3080
+ rec += "\\";
3033
3081
  }
3034
- } else {
3035
- array = entries;
3036
- }
3037
- pos = parseArray(input, pos, len, array, true, nestedItem);
3038
- start = pos + 1;
3039
- } else if (char === "}" && !quote) {
3040
- if (start !== pos) {
3041
- pushEntry(input, start, pos, entries, item);
3082
+ if (esc) {
3083
+ esc = false;
3084
+ } else if (char === '"') {
3085
+ break;
3086
+ }
3087
+ rec += char;
3088
+ }
3089
+ entries.push(transform ? transform(rec) : rec);
3090
+ recorded = "";
3091
+ } else if (character === ",") {
3092
+ if (recorded) {
3093
+ entries.push(
3094
+ recorded === "NULL" ? null : transform ? transform(recorded) : recorded
3095
+ );
3096
+ recorded = "";
3042
3097
  }
3043
- start = pos + 1;
3044
- break;
3098
+ } else {
3099
+ recorded += character;
3045
3100
  }
3046
3101
  }
3047
3102
  return pos;
3048
3103
  };
3049
- const pushEntry = (input, start, pos, entries, item) => {
3050
- let entry = input.slice(start, pos - 1);
3051
- if (entry === "NULL") {
3052
- entry = null;
3053
- } else if (item.parseItem) {
3054
- entry = item.parseItem(entry);
3055
- }
3056
- entries.push(entry);
3057
- };
3058
3104
 
3059
3105
  const parseDateToNumber = (value) => value ? Date.parse(value) : value;
3060
3106
  const parseDateToDate = (value) => value ? new Date(value) : value;
@@ -4841,13 +4887,21 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4841
4887
  item,
4842
4888
  tableName,
4843
4889
  key,
4890
+ key === "*" ? tableName : key,
4844
4891
  quotedAs,
4845
4892
  true
4846
4893
  );
4847
4894
  } else {
4848
4895
  if (hookSelect == null ? void 0 : hookSelect.get(item))
4849
4896
  (selected != null ? selected : selected = {})[item] = quotedAs;
4850
- sql = ownColumnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4897
+ sql = ownColumnToSqlWithAs(
4898
+ ctx,
4899
+ table.q,
4900
+ item,
4901
+ item,
4902
+ quotedAs,
4903
+ true
4904
+ );
4851
4905
  }
4852
4906
  }
4853
4907
  list.push(sql);
@@ -4866,14 +4920,14 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4866
4920
  }
4867
4921
  } else if (value) {
4868
4922
  list.push(
4869
- `${columnToSql(
4923
+ columnToSqlWithAs(
4870
4924
  ctx,
4871
4925
  table.q,
4872
- table.q.shape,
4873
4926
  value,
4927
+ as,
4874
4928
  quotedAs,
4875
4929
  true
4876
- )} "${as}"`
4930
+ )
4877
4931
  );
4878
4932
  }
4879
4933
  }
@@ -4902,7 +4956,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4902
4956
  quotedTable = quotedAs;
4903
4957
  columnName = select;
4904
4958
  col = query.shape[select];
4905
- sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4959
+ sql = simpleColumnToSQL(ctx, query, select, col, quotedAs);
4906
4960
  }
4907
4961
  if (selected == null ? void 0 : selected[columnName]) {
4908
4962
  if ((selected == null ? void 0 : selected[columnName]) === quotedTable) {
@@ -11371,6 +11425,8 @@ class ColumnRefExpression extends orchidCore.Expression {
11371
11425
  makeSQL(ctx, quotedAs) {
11372
11426
  return simpleExistingColumnToSQL(
11373
11427
  ctx,
11428
+ // it's for parsers for computed SQL. In the column ref case, parsers should be set when selecting the column ref.
11429
+ {},
11374
11430
  this.name,
11375
11431
  this.result.value,
11376
11432
  quotedAs