pqb 0.9.25 → 0.9.27

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
@@ -467,7 +467,7 @@ declare type JoinItem = {
467
467
  type: string;
468
468
  args: [relation: string] | [
469
469
  arg: string | QueryWithTable,
470
- conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase)
470
+ conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase) | true
471
471
  ] | [
472
472
  arg: string | QueryWithTable,
473
473
  leftColumn: string | RawExpression,
@@ -792,7 +792,7 @@ declare type JoinArgs<T extends QueryBase, Q extends Query = Query, R extends ke
792
792
  leftColumn: WithSelectable<T, W> | RawExpression,
793
793
  op: string,
794
794
  rightColumn: Selectable<T> | RawExpression
795
- ];
795
+ ] | [query: Q, conditions: true];
796
796
  declare type JoinResult<T extends Query, Args extends JoinArgs<T>, A extends Query | keyof T['relations'] = Args[0]> = AddQueryJoinedTable<T, A extends Query ? A : T['relations'] extends Record<string, Relation> ? A extends keyof T['relations'] ? T['relations'][A]['table'] : A extends keyof T['withData'] ? T['withData'][A] extends WithDataItem ? {
797
797
  table: T['withData'][A]['table'];
798
798
  result: T['withData'][A]['shape'];
@@ -4422,6 +4422,7 @@ declare function timestamps<T extends ColumnType>(this: {
4422
4422
  };
4423
4423
  declare type DefaultColumnTypes = typeof columnTypes;
4424
4424
  declare const columnTypes: {
4425
+ raw: (sql: string, values?: false | Record<string, unknown> | undefined) => RawExpression<ColumnTypeBase<unknown, BaseOperators, unknown, ColumnDataBase>>;
4425
4426
  smallint: () => SmallIntColumn;
4426
4427
  integer: () => IntegerColumn;
4427
4428
  bigint: () => BigIntColumn;
package/dist/index.js CHANGED
@@ -285,13 +285,17 @@ const processJoinItem = (ctx, table, args, quotedAs) => {
285
285
  const query = first.query;
286
286
  const quotedFrom = typeof query.from === "string" ? q(query.from) : void 0;
287
287
  target = quotedFrom || quoteSchemaAndTable(query.schema, first.table);
288
+ const subQuery = first.toSql({
289
+ values: ctx.values
290
+ });
288
291
  let joinAs = quotedFrom || q(first.table);
289
- if (query.as) {
290
- const quoted = q(query.as);
291
- if (quoted !== joinAs) {
292
- joinAs = quoted;
293
- target += ` AS ${quoted}`;
294
- }
292
+ const qAs = query.as ? q(query.as) : void 0;
293
+ const addAs = qAs && qAs !== joinAs;
294
+ if (subQuery.text !== `SELECT * FROM ${target}${addAs ? ` AS ${qAs}` : ""}`) {
295
+ target = `(${subQuery.text}) ${qAs || joinAs}`;
296
+ } else if (addAs) {
297
+ joinAs = qAs;
298
+ target += ` AS ${qAs}`;
295
299
  }
296
300
  conditions = processArgs(args, ctx, table, first, joinAs, quotedAs);
297
301
  const whereSql = whereToSql(ctx, table, query, joinAs);
@@ -340,7 +344,9 @@ const getConditionsFor3Or4LengthItem = (target, values, quotedAs, args) => {
340
344
  return `${typeof leftColumn === "string" ? quoteFullColumn(leftColumn, target) : getRaw(leftColumn, values)} ${op} ${typeof rightColumn === "string" ? quoteFullColumn(rightColumn, quotedAs) : getRaw(rightColumn, values)}`;
341
345
  };
342
346
  const getObjectOrRawConditions = (data, values, quotedAs, joinAs) => {
343
- if (isRaw(data)) {
347
+ if (data === true) {
348
+ return "true";
349
+ } else if (isRaw(data)) {
344
350
  return getRaw(data, values);
345
351
  } else {
346
352
  const pairs = [];
@@ -395,6 +401,7 @@ const processAnds = (and, ctx, table, quotedAs, not) => {
395
401
  return ands.join(" AND ");
396
402
  };
397
403
  const processWhere = (ands, ctx, table, data, quotedAs, not) => {
404
+ var _a, _b;
398
405
  const prefix = not ? "NOT " : "";
399
406
  if (typeof data === "function") {
400
407
  const qb = data(new ctx.whereQueryBuilder(table, table.query.shape));
@@ -489,9 +496,23 @@ const processWhere = (ands, ctx, table, data, quotedAs, not) => {
489
496
  )}`
490
497
  );
491
498
  } else {
492
- const column = table.query.shape[key];
493
- if (!column) {
494
- throw new Error(`Unknown column ${key} provided to condition`);
499
+ let column = table.query.shape[key];
500
+ let quotedColumn;
501
+ if (column) {
502
+ quotedColumn = qc(key, quotedAs);
503
+ } else if (!column) {
504
+ const index = key.indexOf(".");
505
+ if (index !== -1) {
506
+ const joinedTable = key.slice(0, index);
507
+ const joinedColumn = key.slice(index + 1);
508
+ column = (_b = (_a = table.query.joinedShapes) == null ? void 0 : _a[joinedTable]) == null ? void 0 : _b[joinedColumn];
509
+ quotedColumn = qc(joinedColumn, q(joinedTable));
510
+ } else {
511
+ quotedColumn = void 0;
512
+ }
513
+ if (!column || !quotedColumn) {
514
+ throw new Error(`Unknown column ${key} provided to condition`);
515
+ }
495
516
  }
496
517
  for (const op in value) {
497
518
  const operator = column.operators[op];
@@ -500,7 +521,7 @@ const processWhere = (ands, ctx, table, data, quotedAs, not) => {
500
521
  }
501
522
  ands.push(
502
523
  `${prefix}${operator(
503
- qc(key, quotedAs),
524
+ quotedColumn,
504
525
  value[op],
505
526
  ctx.values
506
527
  )}`
@@ -4001,6 +4022,7 @@ const checkIfDataHasUpdatedAt = (data) => {
4001
4022
  });
4002
4023
  };
4003
4024
  const columnTypes = {
4025
+ raw,
4004
4026
  smallint: () => new SmallIntColumn(),
4005
4027
  integer: () => new IntegerColumn(),
4006
4028
  bigint: () => new BigIntColumn(),
@@ -5878,6 +5900,9 @@ class OnQueryBuilder extends WhereQueryBuilder {
5878
5900
  constructor(q, shape, joinTo) {
5879
5901
  super(q, shape);
5880
5902
  this.joinTo = joinTo;
5903
+ this.query.joinedShapes = {
5904
+ [joinTo.query.as || joinTo.table]: joinTo.shape
5905
+ };
5881
5906
  }
5882
5907
  on(...args) {
5883
5908
  return this.clone()._on(...args);