pqb 0.56.6 → 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.js CHANGED
@@ -970,7 +970,7 @@ class EnumColumn extends ColumnType {
970
970
  super(schema, schemaType);
971
971
  this.enumName = enumName;
972
972
  this.options = options;
973
- this.operators = Operators.any;
973
+ this.operators = Operators.ordinalText;
974
974
  this.dataType = "enum";
975
975
  this.inputSchema = this.outputSchema = this.querySchema = schemaType;
976
976
  }
@@ -1071,8 +1071,8 @@ const makeVarArg = (_op) => {
1071
1071
  };
1072
1072
  const quoteValue = (arg, ctx, quotedAs, IN) => {
1073
1073
  if (arg && typeof arg === "object") {
1074
- if (IN && Array.isArray(arg)) {
1075
- return `(${arg.map((value) => orchidCore.addValue(ctx.values, value)).join(", ")})`;
1074
+ if (IN && orchidCore.isIterable(arg)) {
1075
+ return `(${(Array.isArray(arg) ? arg : [...arg]).map((value) => orchidCore.addValue(ctx.values, value)).join(", ")})`;
1076
1076
  }
1077
1077
  if (orchidCore.isExpression(arg)) {
1078
1078
  return arg.toSQL(ctx, quotedAs);
@@ -1113,16 +1113,7 @@ const base = {
1113
1113
  (key, value, ctx, quotedAs) => `NOT ${key} IN ${quoteValue(value, ctx, quotedAs, true)}`
1114
1114
  )
1115
1115
  };
1116
- const boolean = {
1117
- ...base,
1118
- and: make(
1119
- (key, value, ctx, quotedAs) => `${key} AND ${value.q.expr.toSQL(ctx, quotedAs)}`
1120
- ),
1121
- or: make(
1122
- (key, value, ctx, quotedAs) => `(${key}) OR (${value.q.expr.toSQL(ctx, quotedAs)})`
1123
- )
1124
- };
1125
- const numeric = {
1116
+ const ord = {
1126
1117
  ...base,
1127
1118
  lt: make(
1128
1119
  (key, value, ctx, quotedAs) => `${key} < ${quoteValue(value, ctx, quotedAs)}`
@@ -1144,6 +1135,15 @@ const numeric = {
1144
1135
  )}`
1145
1136
  )
1146
1137
  };
1138
+ const boolean = {
1139
+ ...ord,
1140
+ and: make(
1141
+ (key, value, ctx, quotedAs) => `${key} AND ${value.q.expr.toSQL(ctx, quotedAs)}`
1142
+ ),
1143
+ or: make(
1144
+ (key, value, ctx, quotedAs) => `(${key}) OR (${value.q.expr.toSQL(ctx, quotedAs)})`
1145
+ )
1146
+ };
1147
1147
  const text = {
1148
1148
  ...base,
1149
1149
  contains: make(
@@ -1165,6 +1165,10 @@ const text = {
1165
1165
  (key, value, ctx, quotedAs) => `${key} LIKE '%' || ${quoteLikeValue(value, ctx, quotedAs)}`
1166
1166
  )
1167
1167
  };
1168
+ const ordinalText = {
1169
+ ...ord,
1170
+ ...text
1171
+ };
1168
1172
  const encodeJsonPath = (ctx, path) => orchidCore.addValue(ctx.values, `{${Array.isArray(path) ? path.join(", ") : path}}`);
1169
1173
  const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${orchidCore.addValue(ctx.values, path)}${options?.vars ? `, ${orchidCore.addValue(ctx.values, JSON.stringify(options.vars))}${options.silent ? ", true" : ""}` : options?.silent ? ", NULL, true" : ""})`;
1170
1174
  const quoteJsonValue = (arg, ctx, quotedAs, IN) => {
@@ -1193,6 +1197,7 @@ const serializeJsonValue = (arg, ctx, quotedAs) => {
1193
1197
  return orchidCore.addValue(ctx.values, JSON.stringify(arg));
1194
1198
  };
1195
1199
  const json = {
1200
+ ...ord,
1196
1201
  equals: make(
1197
1202
  (key, value, ctx, quotedAs) => value === null ? `nullif(${key}, 'null'::jsonb) IS NULL` : `${key} = ${quoteJsonValue(value, ctx, quotedAs)}`
1198
1203
  ),
@@ -1259,7 +1264,7 @@ const json = {
1259
1264
  )
1260
1265
  };
1261
1266
  const array = {
1262
- ...base,
1267
+ ...ord,
1263
1268
  has: make(
1264
1269
  (key, value, ctx, quotedAs) => `${quoteValue(value, ctx, quotedAs)} = ANY(${key})`
1265
1270
  ),
@@ -1277,7 +1282,7 @@ const array = {
1277
1282
  return typeof value === "number" ? `${expr} = ${quoteValue(value, ctx, quotedAs)}` : Object.keys(value).map(
1278
1283
  (key2) => (
1279
1284
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1280
- numeric[key2]._op(expr, value[key2], ctx, quotedAs)
1285
+ ord[key2]._op(expr, value[key2], ctx, quotedAs)
1281
1286
  )
1282
1287
  ).join(" AND ");
1283
1288
  })
@@ -1285,9 +1290,10 @@ const array = {
1285
1290
  const Operators = {
1286
1291
  any: base,
1287
1292
  boolean,
1288
- number: numeric,
1289
- date: numeric,
1290
- time: numeric,
1293
+ ordinalText,
1294
+ number: ord,
1295
+ date: ord,
1296
+ time: ord,
1291
1297
  text,
1292
1298
  json,
1293
1299
  array
@@ -1757,6 +1763,9 @@ const defaultSchemaConfig = {
1757
1763
  asType() {
1758
1764
  return this;
1759
1765
  },
1766
+ narrowType() {
1767
+ return this;
1768
+ },
1760
1769
  dateAsNumber() {
1761
1770
  return this.parse(Date.parse);
1762
1771
  },
@@ -1822,6 +1831,7 @@ class LimitedTextBaseColumn extends TextBaseColumn {
1822
1831
  schema,
1823
1832
  limit !== void 0 ? schema.stringMax(limit) : schema.stringSchema()
1824
1833
  );
1834
+ this.operators = Operators.ordinalText;
1825
1835
  this.data.maxChars = limit;
1826
1836
  }
1827
1837
  toSQL() {
@@ -1888,6 +1898,7 @@ class TextColumn extends TextBaseColumn {
1888
1898
  constructor(schema) {
1889
1899
  super(schema, schema.stringSchema());
1890
1900
  this.dataType = "text";
1901
+ this.operators = Operators.ordinalText;
1891
1902
  }
1892
1903
  static get instance() {
1893
1904
  return this._instance ?? (this._instance = new TextColumn(defaultSchemaConfig));
@@ -1901,7 +1912,7 @@ class ByteaColumn extends ColumnType {
1901
1912
  constructor(schema) {
1902
1913
  super(schema, schema.buffer());
1903
1914
  this.dataType = "bytea";
1904
- this.operators = Operators.text;
1915
+ this.operators = Operators.ordinalText;
1905
1916
  setColumnDefaultParse(this, byteaParse);
1906
1917
  }
1907
1918
  toCode(ctx, key) {
@@ -2011,7 +2022,7 @@ class InetColumn extends ColumnType {
2011
2022
  constructor(schema) {
2012
2023
  super(schema, schema.stringSchema());
2013
2024
  this.dataType = "inet";
2014
- this.operators = Operators.text;
2025
+ this.operators = Operators.ordinalText;
2015
2026
  }
2016
2027
  toCode(ctx, key) {
2017
2028
  return columnCode(this, ctx, key, `inet()`);
@@ -2021,7 +2032,7 @@ class MacAddrColumn extends ColumnType {
2021
2032
  constructor(schema) {
2022
2033
  super(schema, schema.stringSchema());
2023
2034
  this.dataType = "macaddr";
2024
- this.operators = Operators.text;
2035
+ this.operators = Operators.ordinalText;
2025
2036
  }
2026
2037
  toCode(ctx, key) {
2027
2038
  return columnCode(this, ctx, key, `macaddr()`);
@@ -2031,7 +2042,7 @@ class MacAddr8Column extends ColumnType {
2031
2042
  constructor(schema) {
2032
2043
  super(schema, schema.stringSchema());
2033
2044
  this.dataType = "macaddr8";
2034
- this.operators = Operators.text;
2045
+ this.operators = Operators.ordinalText;
2035
2046
  }
2036
2047
  toCode(ctx, key) {
2037
2048
  return columnCode(this, ctx, key, `macaddr8()`);
@@ -2041,7 +2052,7 @@ class BitColumn extends ColumnType {
2041
2052
  constructor(schema, length) {
2042
2053
  super(schema, schema.bit(length));
2043
2054
  this.dataType = "bit";
2044
- this.operators = Operators.text;
2055
+ this.operators = Operators.ordinalText;
2045
2056
  this.data.length = length;
2046
2057
  }
2047
2058
  toCode(ctx, key) {
@@ -2059,7 +2070,7 @@ class BitVaryingColumn extends ColumnType {
2059
2070
  constructor(schema, length) {
2060
2071
  super(schema, schema.bit(length));
2061
2072
  this.dataType = "varbit";
2062
- this.operators = Operators.text;
2073
+ this.operators = Operators.ordinalText;
2063
2074
  this.data.length = length;
2064
2075
  this.data.alias = "bitVarying";
2065
2076
  }
@@ -2079,7 +2090,7 @@ class TsVectorColumn extends ColumnType {
2079
2090
  super(schema, schema.stringSchema());
2080
2091
  this.defaultLanguage = defaultLanguage;
2081
2092
  this.dataType = "tsvector";
2082
- this.operators = Operators.text;
2093
+ this.operators = Operators.ordinalText;
2083
2094
  }
2084
2095
  toCode(ctx, key) {
2085
2096
  return columnCode(this, ctx, key, `tsvector()`);
@@ -2171,7 +2182,7 @@ class TsQueryColumn extends ColumnType {
2171
2182
  constructor(schema) {
2172
2183
  super(schema, schema.stringSchema());
2173
2184
  this.dataType = "tsquery";
2174
- this.operators = Operators.text;
2185
+ this.operators = Operators.ordinalText;
2175
2186
  }
2176
2187
  toCode(ctx, key) {
2177
2188
  return columnCode(this, ctx, key, `tsquery()`);
@@ -2183,7 +2194,7 @@ class UUIDColumn extends ColumnType {
2183
2194
  constructor(schema) {
2184
2195
  super(schema, schema.uuid());
2185
2196
  this.dataType = "uuid";
2186
- this.operators = Operators.text;
2197
+ this.operators = Operators.ordinalText;
2187
2198
  this.data.defaultDefault = uuidDefault;
2188
2199
  }
2189
2200
  /**
@@ -2215,6 +2226,7 @@ class CitextColumn extends TextBaseColumn {
2215
2226
  constructor(schema) {
2216
2227
  super(schema, schema.stringSchema());
2217
2228
  this.dataType = "citext";
2229
+ this.operators = Operators.ordinalText;
2218
2230
  this.data.extension = "citext";
2219
2231
  }
2220
2232
  toCode(ctx, key) {
@@ -3430,6 +3442,7 @@ const _queryOrNot = (q, args) => {
3430
3442
  const _queryWhereIn = (q, and, arg, values, not) => {
3431
3443
  let item;
3432
3444
  if (values) {
3445
+ if (orchidCore.isIterable(values)) values = [...values];
3433
3446
  if ("length" in values && !values.length) {
3434
3447
  return _queryNone(q);
3435
3448
  }
@@ -3447,7 +3460,7 @@ const _queryWhereIn = (q, and, arg, values, not) => {
3447
3460
  item = {};
3448
3461
  for (const key in arg) {
3449
3462
  const values2 = arg[key];
3450
- if ("length" in values2 && !values2.length) {
3463
+ if ("length" in values2 && !values2.length || "size" in values2 && !values2.size) {
3451
3464
  return _queryNone(q);
3452
3465
  }
3453
3466
  item[key] = { in: values2 };
@@ -3990,8 +4003,10 @@ class Where {
3990
4003
  *
3991
4004
  * ```ts
3992
4005
  * db.table.whereIn('column', [1, 2, 3]);
4006
+ * db.table.whereIn('column', new Set([1, 2, 3]));
3993
4007
  * // the same as:
3994
- * db.table.where({ column: [1, 2, 3] });
4008
+ * db.table.where({ column: { in: [1, 2, 3] } });
4009
+ * db.table.where({ column: { in: new Set([1, 2, 3]) } });
3995
4010
  * ```
3996
4011
  *
3997
4012
  * `whereIn` can support a tuple of columns, that's what the `in` operator cannot support:
@@ -4022,9 +4037,9 @@ class Where {
4022
4037
  *
4023
4038
  * ```ts
4024
4039
  * // following queries resolves into `none`:
4025
- * db.table.where('id', [])
4026
- * db.table.where(['id', 'name'], [])
4027
- * db.table.where({ id: [] })
4040
+ * db.table.whereIn('id', [])
4041
+ * db.table.whereIn(['id', 'name'], [])
4042
+ * db.table.whereIn({ id: [] })
4028
4043
  * ```
4029
4044
  */
4030
4045
  whereIn(...args) {
@@ -11783,7 +11798,7 @@ class QueryMap {
11783
11798
  *
11784
11799
  * ```ts
11785
11800
  * // add a `titleLength` to every post
11786
- * const posts = await db.post.limit(10).map((post) => ({
11801
+ * const posts = await db.post.limit(10).map((post, i) => ({
11787
11802
  * ...post,
11788
11803
  * titleLength: post.title.length,
11789
11804
  * }));
@@ -11791,7 +11806,7 @@ class QueryMap {
11791
11806
  * posts[0].titleLength; // number
11792
11807
  *
11793
11808
  * // using the exact same `map` function to transform a single post
11794
- * const singlePost = await db.post.find(id).map((post) => ({
11809
+ * const singlePost = await db.post.find(id).map((post, i) => ({
11795
11810
  * ...post,
11796
11811
  * titleLength: post.title.length,
11797
11812
  * }));
@@ -11801,7 +11816,7 @@ class QueryMap {
11801
11816
  * // can be used in sub-queries
11802
11817
  * const postsWithComments = await db.post.select('title', {
11803
11818
  * comments: (q) =>
11804
- * q.comments.map((comment) => ({
11819
+ * q.comments.map((comment, i) => ({
11805
11820
  * ...comment,
11806
11821
  * truncatedContent: comment.content.slice(0, 100),
11807
11822
  * })),
@@ -11811,10 +11826,12 @@ class QueryMap {
11811
11826
  * ```
11812
11827
  *
11813
11828
  * @param fn - function to transform an individual record
11829
+ * @param thisArg - same as in the native array map
11814
11830
  */
11815
- map(fn) {
11831
+ map(fn, thisArg) {
11816
11832
  return orchidCore.pushQueryValueImmutable(_clone(this), "transform", {
11817
- map: fn
11833
+ map: fn,
11834
+ thisArg
11818
11835
  });
11819
11836
  }
11820
11837
  }