pqb 0.56.7 → 0.56.8

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
@@ -1,4 +1,4 @@
1
- import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, templateLiteralSQLToCode, quoteObjectKey, toArray, emptyArray, singleQuote, addCode, singleQuoteArray, objectHasValues, toSnakeCase, columnDefaultArgumentToCode, columnErrorMessagesToCode, setObjectValueImmutable, getValueKey, addValue, isExpression, dateDataToCode, joinTruthy, arrayDataToCode, numberDataToCode, noop, stringDataToCode, getDefaultLanguage, setDefaultNowFn, setDefaultLanguage, setCurrentColumnName, timestampHelpers, _getQueryAliasOrName, _getQueryOuterAliases, returnArg, pushQueryValueImmutable, NotFoundError, OrchidOrmInternalError, _setSubQueryAliases, _applyRelationAliases, isRelationQuery, getFreeAlias, _checkIfAliased, logColors, OrchidOrmError, applyTransforms, callWithThis, requirePrimaryKeys, pick, _setQueryAs, _copyQueryAliasToQuery, setParserToQuery, newDelayedRelationSelect, pushOrNewArray, getPrimaryKeys, setDelayedRelation, UnhandledTypeError, isRawSQL, pushOrNewArrayToObjectImmutable, _getQueryFreeAlias, _setQueryAlias, QueryHookUtils, MoreThanOneRowError, isObjectEmpty, ValExpression, applyMixins, _getQueryAs, QueryError, snakeCaseKey } from 'orchid-core';
1
+ import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, templateLiteralSQLToCode, quoteObjectKey, toArray, emptyArray, singleQuote, addCode, singleQuoteArray, objectHasValues, toSnakeCase, columnDefaultArgumentToCode, columnErrorMessagesToCode, setObjectValueImmutable, getValueKey, addValue, isIterable, isExpression, dateDataToCode, joinTruthy, arrayDataToCode, numberDataToCode, noop, stringDataToCode, getDefaultLanguage, setDefaultNowFn, setDefaultLanguage, setCurrentColumnName, timestampHelpers, _getQueryAliasOrName, _getQueryOuterAliases, returnArg, pushQueryValueImmutable, NotFoundError, OrchidOrmInternalError, _setSubQueryAliases, _applyRelationAliases, isRelationQuery, getFreeAlias, _checkIfAliased, logColors, OrchidOrmError, applyTransforms, callWithThis, requirePrimaryKeys, pick, _setQueryAs, _copyQueryAliasToQuery, setParserToQuery, newDelayedRelationSelect, pushOrNewArray, getPrimaryKeys, setDelayedRelation, UnhandledTypeError, isRawSQL, pushOrNewArrayToObjectImmutable, _getQueryFreeAlias, _setQueryAlias, QueryHookUtils, MoreThanOneRowError, isObjectEmpty, ValExpression, applyMixins, _getQueryAs, QueryError, snakeCaseKey } from 'orchid-core';
2
2
  import { inspect } from 'node:util';
3
3
  import { AsyncLocalStorage } from 'node:async_hooks';
4
4
  import { templateLiteralToSQL as templateLiteralToSQL$1 } from 'pqb';
@@ -968,7 +968,7 @@ class EnumColumn extends ColumnType {
968
968
  super(schema, schemaType);
969
969
  this.enumName = enumName;
970
970
  this.options = options;
971
- this.operators = Operators.any;
971
+ this.operators = Operators.ordinalText;
972
972
  this.dataType = "enum";
973
973
  this.inputSchema = this.outputSchema = this.querySchema = schemaType;
974
974
  }
@@ -1069,8 +1069,8 @@ const makeVarArg = (_op) => {
1069
1069
  };
1070
1070
  const quoteValue = (arg, ctx, quotedAs, IN) => {
1071
1071
  if (arg && typeof arg === "object") {
1072
- if (IN && Array.isArray(arg)) {
1073
- return `(${arg.map((value) => addValue(ctx.values, value)).join(", ")})`;
1072
+ if (IN && isIterable(arg)) {
1073
+ return `(${(Array.isArray(arg) ? arg : [...arg]).map((value) => addValue(ctx.values, value)).join(", ")})`;
1074
1074
  }
1075
1075
  if (isExpression(arg)) {
1076
1076
  return arg.toSQL(ctx, quotedAs);
@@ -1111,16 +1111,7 @@ const base = {
1111
1111
  (key, value, ctx, quotedAs) => `NOT ${key} IN ${quoteValue(value, ctx, quotedAs, true)}`
1112
1112
  )
1113
1113
  };
1114
- const boolean = {
1115
- ...base,
1116
- and: make(
1117
- (key, value, ctx, quotedAs) => `${key} AND ${value.q.expr.toSQL(ctx, quotedAs)}`
1118
- ),
1119
- or: make(
1120
- (key, value, ctx, quotedAs) => `(${key}) OR (${value.q.expr.toSQL(ctx, quotedAs)})`
1121
- )
1122
- };
1123
- const numeric = {
1114
+ const ord = {
1124
1115
  ...base,
1125
1116
  lt: make(
1126
1117
  (key, value, ctx, quotedAs) => `${key} < ${quoteValue(value, ctx, quotedAs)}`
@@ -1142,6 +1133,15 @@ const numeric = {
1142
1133
  )}`
1143
1134
  )
1144
1135
  };
1136
+ const boolean = {
1137
+ ...ord,
1138
+ and: make(
1139
+ (key, value, ctx, quotedAs) => `${key} AND ${value.q.expr.toSQL(ctx, quotedAs)}`
1140
+ ),
1141
+ or: make(
1142
+ (key, value, ctx, quotedAs) => `(${key}) OR (${value.q.expr.toSQL(ctx, quotedAs)})`
1143
+ )
1144
+ };
1145
1145
  const text = {
1146
1146
  ...base,
1147
1147
  contains: make(
@@ -1163,6 +1163,10 @@ const text = {
1163
1163
  (key, value, ctx, quotedAs) => `${key} LIKE '%' || ${quoteLikeValue(value, ctx, quotedAs)}`
1164
1164
  )
1165
1165
  };
1166
+ const ordinalText = {
1167
+ ...ord,
1168
+ ...text
1169
+ };
1166
1170
  const encodeJsonPath = (ctx, path) => addValue(ctx.values, `{${Array.isArray(path) ? path.join(", ") : path}}`);
1167
1171
  const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${addValue(ctx.values, path)}${options?.vars ? `, ${addValue(ctx.values, JSON.stringify(options.vars))}${options.silent ? ", true" : ""}` : options?.silent ? ", NULL, true" : ""})`;
1168
1172
  const quoteJsonValue = (arg, ctx, quotedAs, IN) => {
@@ -1191,6 +1195,7 @@ const serializeJsonValue = (arg, ctx, quotedAs) => {
1191
1195
  return addValue(ctx.values, JSON.stringify(arg));
1192
1196
  };
1193
1197
  const json = {
1198
+ ...ord,
1194
1199
  equals: make(
1195
1200
  (key, value, ctx, quotedAs) => value === null ? `nullif(${key}, 'null'::jsonb) IS NULL` : `${key} = ${quoteJsonValue(value, ctx, quotedAs)}`
1196
1201
  ),
@@ -1257,7 +1262,7 @@ const json = {
1257
1262
  )
1258
1263
  };
1259
1264
  const array = {
1260
- ...base,
1265
+ ...ord,
1261
1266
  has: make(
1262
1267
  (key, value, ctx, quotedAs) => `${quoteValue(value, ctx, quotedAs)} = ANY(${key})`
1263
1268
  ),
@@ -1275,7 +1280,7 @@ const array = {
1275
1280
  return typeof value === "number" ? `${expr} = ${quoteValue(value, ctx, quotedAs)}` : Object.keys(value).map(
1276
1281
  (key2) => (
1277
1282
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1278
- numeric[key2]._op(expr, value[key2], ctx, quotedAs)
1283
+ ord[key2]._op(expr, value[key2], ctx, quotedAs)
1279
1284
  )
1280
1285
  ).join(" AND ");
1281
1286
  })
@@ -1283,9 +1288,10 @@ const array = {
1283
1288
  const Operators = {
1284
1289
  any: base,
1285
1290
  boolean,
1286
- number: numeric,
1287
- date: numeric,
1288
- time: numeric,
1291
+ ordinalText,
1292
+ number: ord,
1293
+ date: ord,
1294
+ time: ord,
1289
1295
  text,
1290
1296
  json,
1291
1297
  array
@@ -1755,6 +1761,9 @@ const defaultSchemaConfig = {
1755
1761
  asType() {
1756
1762
  return this;
1757
1763
  },
1764
+ narrowType() {
1765
+ return this;
1766
+ },
1758
1767
  dateAsNumber() {
1759
1768
  return this.parse(Date.parse);
1760
1769
  },
@@ -1820,6 +1829,7 @@ class LimitedTextBaseColumn extends TextBaseColumn {
1820
1829
  schema,
1821
1830
  limit !== void 0 ? schema.stringMax(limit) : schema.stringSchema()
1822
1831
  );
1832
+ this.operators = Operators.ordinalText;
1823
1833
  this.data.maxChars = limit;
1824
1834
  }
1825
1835
  toSQL() {
@@ -1886,6 +1896,7 @@ class TextColumn extends TextBaseColumn {
1886
1896
  constructor(schema) {
1887
1897
  super(schema, schema.stringSchema());
1888
1898
  this.dataType = "text";
1899
+ this.operators = Operators.ordinalText;
1889
1900
  }
1890
1901
  static get instance() {
1891
1902
  return this._instance ?? (this._instance = new TextColumn(defaultSchemaConfig));
@@ -1899,7 +1910,7 @@ class ByteaColumn extends ColumnType {
1899
1910
  constructor(schema) {
1900
1911
  super(schema, schema.buffer());
1901
1912
  this.dataType = "bytea";
1902
- this.operators = Operators.text;
1913
+ this.operators = Operators.ordinalText;
1903
1914
  setColumnDefaultParse(this, byteaParse);
1904
1915
  }
1905
1916
  toCode(ctx, key) {
@@ -2009,7 +2020,7 @@ class InetColumn extends ColumnType {
2009
2020
  constructor(schema) {
2010
2021
  super(schema, schema.stringSchema());
2011
2022
  this.dataType = "inet";
2012
- this.operators = Operators.text;
2023
+ this.operators = Operators.ordinalText;
2013
2024
  }
2014
2025
  toCode(ctx, key) {
2015
2026
  return columnCode(this, ctx, key, `inet()`);
@@ -2019,7 +2030,7 @@ class MacAddrColumn extends ColumnType {
2019
2030
  constructor(schema) {
2020
2031
  super(schema, schema.stringSchema());
2021
2032
  this.dataType = "macaddr";
2022
- this.operators = Operators.text;
2033
+ this.operators = Operators.ordinalText;
2023
2034
  }
2024
2035
  toCode(ctx, key) {
2025
2036
  return columnCode(this, ctx, key, `macaddr()`);
@@ -2029,7 +2040,7 @@ class MacAddr8Column extends ColumnType {
2029
2040
  constructor(schema) {
2030
2041
  super(schema, schema.stringSchema());
2031
2042
  this.dataType = "macaddr8";
2032
- this.operators = Operators.text;
2043
+ this.operators = Operators.ordinalText;
2033
2044
  }
2034
2045
  toCode(ctx, key) {
2035
2046
  return columnCode(this, ctx, key, `macaddr8()`);
@@ -2039,7 +2050,7 @@ class BitColumn extends ColumnType {
2039
2050
  constructor(schema, length) {
2040
2051
  super(schema, schema.bit(length));
2041
2052
  this.dataType = "bit";
2042
- this.operators = Operators.text;
2053
+ this.operators = Operators.ordinalText;
2043
2054
  this.data.length = length;
2044
2055
  }
2045
2056
  toCode(ctx, key) {
@@ -2057,7 +2068,7 @@ class BitVaryingColumn extends ColumnType {
2057
2068
  constructor(schema, length) {
2058
2069
  super(schema, schema.bit(length));
2059
2070
  this.dataType = "varbit";
2060
- this.operators = Operators.text;
2071
+ this.operators = Operators.ordinalText;
2061
2072
  this.data.length = length;
2062
2073
  this.data.alias = "bitVarying";
2063
2074
  }
@@ -2077,7 +2088,7 @@ class TsVectorColumn extends ColumnType {
2077
2088
  super(schema, schema.stringSchema());
2078
2089
  this.defaultLanguage = defaultLanguage;
2079
2090
  this.dataType = "tsvector";
2080
- this.operators = Operators.text;
2091
+ this.operators = Operators.ordinalText;
2081
2092
  }
2082
2093
  toCode(ctx, key) {
2083
2094
  return columnCode(this, ctx, key, `tsvector()`);
@@ -2169,7 +2180,7 @@ class TsQueryColumn extends ColumnType {
2169
2180
  constructor(schema) {
2170
2181
  super(schema, schema.stringSchema());
2171
2182
  this.dataType = "tsquery";
2172
- this.operators = Operators.text;
2183
+ this.operators = Operators.ordinalText;
2173
2184
  }
2174
2185
  toCode(ctx, key) {
2175
2186
  return columnCode(this, ctx, key, `tsquery()`);
@@ -2181,7 +2192,7 @@ class UUIDColumn extends ColumnType {
2181
2192
  constructor(schema) {
2182
2193
  super(schema, schema.uuid());
2183
2194
  this.dataType = "uuid";
2184
- this.operators = Operators.text;
2195
+ this.operators = Operators.ordinalText;
2185
2196
  this.data.defaultDefault = uuidDefault;
2186
2197
  }
2187
2198
  /**
@@ -2213,6 +2224,7 @@ class CitextColumn extends TextBaseColumn {
2213
2224
  constructor(schema) {
2214
2225
  super(schema, schema.stringSchema());
2215
2226
  this.dataType = "citext";
2227
+ this.operators = Operators.ordinalText;
2216
2228
  this.data.extension = "citext";
2217
2229
  }
2218
2230
  toCode(ctx, key) {
@@ -3428,6 +3440,7 @@ const _queryOrNot = (q, args) => {
3428
3440
  const _queryWhereIn = (q, and, arg, values, not) => {
3429
3441
  let item;
3430
3442
  if (values) {
3443
+ if (isIterable(values)) values = [...values];
3431
3444
  if ("length" in values && !values.length) {
3432
3445
  return _queryNone(q);
3433
3446
  }
@@ -3445,7 +3458,7 @@ const _queryWhereIn = (q, and, arg, values, not) => {
3445
3458
  item = {};
3446
3459
  for (const key in arg) {
3447
3460
  const values2 = arg[key];
3448
- if ("length" in values2 && !values2.length) {
3461
+ if ("length" in values2 && !values2.length || "size" in values2 && !values2.size) {
3449
3462
  return _queryNone(q);
3450
3463
  }
3451
3464
  item[key] = { in: values2 };
@@ -3988,8 +4001,10 @@ class Where {
3988
4001
  *
3989
4002
  * ```ts
3990
4003
  * db.table.whereIn('column', [1, 2, 3]);
4004
+ * db.table.whereIn('column', new Set([1, 2, 3]));
3991
4005
  * // the same as:
3992
- * db.table.where({ column: [1, 2, 3] });
4006
+ * db.table.where({ column: { in: [1, 2, 3] } });
4007
+ * db.table.where({ column: { in: new Set([1, 2, 3]) } });
3993
4008
  * ```
3994
4009
  *
3995
4010
  * `whereIn` can support a tuple of columns, that's what the `in` operator cannot support:
@@ -4020,9 +4035,9 @@ class Where {
4020
4035
  *
4021
4036
  * ```ts
4022
4037
  * // following queries resolves into `none`:
4023
- * db.table.where('id', [])
4024
- * db.table.where(['id', 'name'], [])
4025
- * db.table.where({ id: [] })
4038
+ * db.table.whereIn('id', [])
4039
+ * db.table.whereIn(['id', 'name'], [])
4040
+ * db.table.whereIn({ id: [] })
4026
4041
  * ```
4027
4042
  */
4028
4043
  whereIn(...args) {
@@ -11781,7 +11796,7 @@ class QueryMap {
11781
11796
  *
11782
11797
  * ```ts
11783
11798
  * // add a `titleLength` to every post
11784
- * const posts = await db.post.limit(10).map((post) => ({
11799
+ * const posts = await db.post.limit(10).map((post, i) => ({
11785
11800
  * ...post,
11786
11801
  * titleLength: post.title.length,
11787
11802
  * }));
@@ -11789,7 +11804,7 @@ class QueryMap {
11789
11804
  * posts[0].titleLength; // number
11790
11805
  *
11791
11806
  * // using the exact same `map` function to transform a single post
11792
- * const singlePost = await db.post.find(id).map((post) => ({
11807
+ * const singlePost = await db.post.find(id).map((post, i) => ({
11793
11808
  * ...post,
11794
11809
  * titleLength: post.title.length,
11795
11810
  * }));
@@ -11799,7 +11814,7 @@ class QueryMap {
11799
11814
  * // can be used in sub-queries
11800
11815
  * const postsWithComments = await db.post.select('title', {
11801
11816
  * comments: (q) =>
11802
- * q.comments.map((comment) => ({
11817
+ * q.comments.map((comment, i) => ({
11803
11818
  * ...comment,
11804
11819
  * truncatedContent: comment.content.slice(0, 100),
11805
11820
  * })),
@@ -11809,10 +11824,12 @@ class QueryMap {
11809
11824
  * ```
11810
11825
  *
11811
11826
  * @param fn - function to transform an individual record
11827
+ * @param thisArg - same as in the native array map
11812
11828
  */
11813
- map(fn) {
11829
+ map(fn, thisArg) {
11814
11830
  return pushQueryValueImmutable(_clone(this), "transform", {
11815
- map: fn
11831
+ map: fn,
11832
+ thisArg
11816
11833
  });
11817
11834
  }
11818
11835
  }