pqb 0.52.1 → 0.52.3

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
@@ -493,7 +493,7 @@ const columnsShapeToCode = (ctx, shape) => {
493
493
  if (name === key) column.data.name = void 0;
494
494
  code.push(
495
495
  ...combineCodeElements([
496
- `${quoteObjectKey(key)}: `,
496
+ `${quoteObjectKey(key, ctx.snakeCase)}: `,
497
497
  ...toArray(shape[key].toCode(ctx, key)),
498
498
  ","
499
499
  ])
@@ -925,20 +925,33 @@ const getSqlText = (sql) => {
925
925
  };
926
926
 
927
927
  class CustomTypeColumn extends ColumnType {
928
- constructor(schema, dataType, extension) {
928
+ constructor(schema, typeName, typeSchema, extension) {
929
929
  super(
930
930
  schema,
931
931
  schema.unknown(),
932
932
  schema.unknown(),
933
933
  schema.unknown()
934
934
  );
935
- this.dataType = dataType;
935
+ this.typeName = typeName;
936
+ this.typeSchema = typeSchema;
936
937
  this.operators = Operators.any;
938
+ this.dataType = typeSchema ? typeSchema + "." + typeName : typeName;
937
939
  this.data.isOfCustomType = true;
938
940
  this.data.extension = extension;
939
941
  }
940
942
  toCode(ctx, key) {
941
- return columnCode(this, ctx, key, `type(${singleQuote(this.dataType)})`);
943
+ const {
944
+ dataType,
945
+ data: { typmod }
946
+ } = this;
947
+ return columnCode(
948
+ this,
949
+ ctx,
950
+ key,
951
+ `type(${singleQuote(
952
+ (dataType.startsWith(ctx.currentSchema) ? dataType.slice(ctx.currentSchema.length + 1) : dataType) + (typmod !== void 0 && typmod !== -1 && !dataType.includes("(") ? `(${typmod})` : "")
953
+ )})`
954
+ );
942
955
  }
943
956
  as(column) {
944
957
  const c = setColumnData(
@@ -2131,7 +2144,7 @@ class TsVectorColumn extends ColumnType {
2131
2144
  const pairs = [];
2132
2145
  for (const key in target) {
2133
2146
  pairs.push(
2134
- `${quoteObjectKey(key)}: '${target[key]}'`
2147
+ `${quoteObjectKey(key, false)}: '${target[key]}'`
2135
2148
  );
2136
2149
  }
2137
2150
  code += `{ ${pairs.join(", ")} }`;
@@ -2643,8 +2656,8 @@ const _join = (query, require, type, first, args) => {
2643
2656
  joinKey = first;
2644
2657
  const relation = query.relations[joinKey];
2645
2658
  if (relation) {
2646
- shape = getShapeFromSelect(relation.relationConfig.query);
2647
- const r = relation.relationConfig.query;
2659
+ shape = getShapeFromSelect(relation.query);
2660
+ const r = relation.query;
2648
2661
  parsers = r.q.parsers;
2649
2662
  batchParsers = r.q.batchParsers;
2650
2663
  computeds = r.q.computeds;
@@ -2733,7 +2746,7 @@ const _joinLateralProcessArg = (q, arg, cb) => {
2733
2746
  if (typeof arg === "string") {
2734
2747
  relation = q.relations[arg];
2735
2748
  if (relation) {
2736
- arg = _clone(relation.relationConfig.query);
2749
+ arg = _clone(relation.query);
2737
2750
  } else {
2738
2751
  const w = q.q.withShapes?.[arg];
2739
2752
  if (w) {
@@ -2755,14 +2768,14 @@ const _joinLateralProcessArg = (q, arg, cb) => {
2755
2768
  cb
2756
2769
  );
2757
2770
  if (relation) {
2758
- result = relation.relationConfig.joinQuery(
2771
+ result = relation.joinQuery(
2759
2772
  result,
2760
2773
  q
2761
2774
  );
2762
2775
  }
2763
2776
  return result;
2764
2777
  };
2765
- const _joinLateral = (self, type, arg, as) => {
2778
+ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
2766
2779
  const q = self;
2767
2780
  arg.q.joinTo = q;
2768
2781
  const joinedAs = getQueryAs(q);
@@ -2783,7 +2796,10 @@ const _joinLateral = (self, type, arg, as) => {
2783
2796
  }
2784
2797
  as || (as = getQueryAs(arg));
2785
2798
  setObjectValueImmutable(q.q, "joinedComputeds", as, arg.q.computeds);
2786
- pushQueryValueImmutable(q, "join", [type, arg, as]);
2799
+ pushQueryValueImmutable(q, "join", {
2800
+ type: `${type} LATERAL`,
2801
+ args: { l: arg, a: as, i: innerJoinLateral }
2802
+ });
2787
2803
  return q;
2788
2804
  };
2789
2805
 
@@ -3557,7 +3573,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3557
3573
  if (query.transform) {
3558
3574
  result = applyTransforms(query, returnType, query.transform, result);
3559
3575
  }
3560
- return resolve?.(result);
3576
+ return resolve ? resolve(result) : result;
3561
3577
  } catch (err) {
3562
3578
  let error;
3563
3579
  if (err instanceof pg.DatabaseError) {
@@ -4235,10 +4251,12 @@ const processSelectArg = (q, as, arg, columnAs) => {
4235
4251
  joinQuery = true;
4236
4252
  value = value.joinQuery(value, q);
4237
4253
  let query;
4238
- const returnType = value.q.returnType;
4254
+ const { returnType, innerJoinLateral } = value.q;
4239
4255
  if (!returnType || returnType === "all") {
4240
4256
  query = value.json(false);
4241
- value.q.coalesceValue = emptyArrSQL;
4257
+ if (!innerJoinLateral) {
4258
+ value.q.coalesceValue = emptyArrSQL;
4259
+ }
4242
4260
  } else if (returnType === "pluck") {
4243
4261
  query = value.q.select ? value.wrap(cloneQueryBaseUnscoped(value)).jsonAgg(value.q.select[0]) : value.json(false);
4244
4262
  value.q.coalesceValue = emptyArrSQL;
@@ -4265,9 +4283,12 @@ const processSelectArg = (q, as, arg, columnAs) => {
4265
4283
  }
4266
4284
  _joinLateral(
4267
4285
  q,
4268
- value.q.innerJoinLateral ? "JOIN" : "LEFT JOIN",
4286
+ innerJoinLateral ? "JOIN" : "LEFT JOIN",
4269
4287
  query,
4270
- key
4288
+ key,
4289
+ // no need for `ON p.r IS NOT NULL` check when joining a single record,
4290
+ // `JOIN` will handle it on itself.
4291
+ innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow"
4271
4292
  );
4272
4293
  }
4273
4294
  }
@@ -6034,7 +6055,13 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
6034
6055
  const processJoinItem = (ctx, table, query, args, quotedAs) => {
6035
6056
  let target;
6036
6057
  let on;
6037
- if ("j" in args) {
6058
+ if ("l" in args) {
6059
+ const { aliasValue } = ctx;
6060
+ ctx.aliasValue = true;
6061
+ target = `(${getSqlText(args.l.toSQL(ctx))}) "${query.aliases?.[args.a] || args.a}"`;
6062
+ on = `${args.i ? `"${args.a}".r IS NOT NULL` : "true"}`;
6063
+ ctx.aliasValue = aliasValue;
6064
+ } else if ("j" in args) {
6038
6065
  const { j, s, r } = args;
6039
6066
  const tableName = typeof j.q.from === "string" ? j.q.from : j.table;
6040
6067
  const quotedTable = quoteSchemaAndTable(j.q.schema, tableName);
@@ -6204,24 +6231,14 @@ const getObjectOrRawConditions = (ctx, query, data, quotedAs, joinAs, joinShape)
6204
6231
  const pushJoinSql = (ctx, table, query, quotedAs) => {
6205
6232
  const joinSet = query.join.length > 1 ? /* @__PURE__ */ new Set() : null;
6206
6233
  for (const item of query.join) {
6207
- let sql;
6208
- if (Array.isArray(item)) {
6209
- const q = item[1];
6210
- const { aliasValue } = ctx;
6211
- ctx.aliasValue = true;
6212
- const as = item[2];
6213
- sql = `${item[0]} LATERAL (${getSqlText(q.toSQL(ctx))}) "${query.aliases?.[as] || as}" ON true`;
6214
- ctx.aliasValue = aliasValue;
6215
- } else {
6216
- const { target, on = "true" } = processJoinItem(
6217
- ctx,
6218
- table,
6219
- query,
6220
- item.args,
6221
- quotedAs
6222
- );
6223
- sql = `${item.type} ${target} ON ${on}`;
6224
- }
6234
+ const { target, on = "true" } = processJoinItem(
6235
+ ctx,
6236
+ table,
6237
+ query,
6238
+ item.args,
6239
+ quotedAs
6240
+ );
6241
+ const sql = `${item.type} ${target} ON ${on}`;
6225
6242
  if (joinSet) {
6226
6243
  if (joinSet.has(sql)) continue;
6227
6244
  joinSet.add(sql);
@@ -6259,7 +6276,7 @@ const getIsJoinSubQuery = (query) => {
6259
6276
  const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
6260
6277
  if (typeof first === "string") {
6261
6278
  if (first in joinTo.relations) {
6262
- const { query: toQuery, joinQuery } = joinTo.relations[first].relationConfig;
6279
+ const { query: toQuery, joinQuery } = joinTo.relations[first];
6263
6280
  const j = joinQuery(toQuery, joinTo);
6264
6281
  if (typeof args[0] === "function") {
6265
6282
  const r = args[0](
@@ -6359,7 +6376,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
6359
6376
  };
6360
6377
  const preprocessJoinArg = (q, arg) => {
6361
6378
  if (typeof arg !== "function") return arg;
6362
- arg = arg(q.relations);
6379
+ arg = arg(
6380
+ q.relationQueries
6381
+ );
6363
6382
  arg.joinQueryAfterCallback = arg.joinQuery;
6364
6383
  return arg;
6365
6384
  };
@@ -7179,9 +7198,9 @@ const _chain = (fromQuery, toQuery, rel) => {
7179
7198
  q.returnType = q.returnsOne = q.limit = void 0;
7180
7199
  }
7181
7200
  if (self.q.relChain) {
7182
- q.relChain = [...self.q.relChain, self];
7201
+ q.relChain = [...self.q.relChain, { query: self, rel }];
7183
7202
  } else {
7184
- q.relChain = [self];
7203
+ q.relChain = [{ query: self, rel }];
7185
7204
  }
7186
7205
  const aliases = self.q.as ? { ...self.q.aliases } : { ...self.q.aliases, [self.table]: self.table };
7187
7206
  const relAliases = q.aliases;
@@ -7218,7 +7237,7 @@ const resolveSubQueryCallbackV2 = (q, cb) => {
7218
7237
  for (const key in relations) {
7219
7238
  Object.defineProperty(base, key, {
7220
7239
  get() {
7221
- const rel = relations[key].relationConfig;
7240
+ const rel = relations[key];
7222
7241
  const relQuery = _clone(rel.query);
7223
7242
  relQuery.q.withShapes = this.q.withShapes;
7224
7243
  return _chain(this, relQuery, rel);
@@ -7238,11 +7257,8 @@ const resolveSubQueryCallbackV2 = (q, cb) => {
7238
7257
  return cb(arg);
7239
7258
  };
7240
7259
  const joinSubQuery = (q, sub) => {
7241
- if (!("relationConfig" in sub)) return sub;
7242
- return sub.relationConfig.joinQuery(
7243
- sub,
7244
- q
7245
- );
7260
+ if (!("joinQuery" in sub)) return sub;
7261
+ return sub.joinQuery(sub, q);
7246
7262
  };
7247
7263
 
7248
7264
  const defaultSrid = 4326;
@@ -12656,12 +12672,10 @@ class QueryMethods {
12656
12672
  return condition ? fn(this) : this;
12657
12673
  }
12658
12674
  queryRelated(relName, params) {
12659
- return this.relations[relName].relationConfig.queryRelated(
12660
- params
12661
- );
12675
+ return this.relations[relName].queryRelated(params);
12662
12676
  }
12663
12677
  chain(relName) {
12664
- const rel = this.relations[relName].relationConfig;
12678
+ const rel = this.relations[relName];
12665
12679
  return _chain(this, _clone(rel.query), rel);
12666
12680
  }
12667
12681
  }
@@ -12820,6 +12834,7 @@ class Db extends QueryMethods {
12820
12834
  };
12821
12835
  this.baseQuery = this;
12822
12836
  this.relations = {};
12837
+ this.relationQueries = {};
12823
12838
  const logger = options.logger || console;
12824
12839
  const parsers = {};
12825
12840
  let hasParsers = false;