pqb 0.10.34 → 0.10.35

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
@@ -531,7 +531,6 @@ declare type WhereArg<T extends QueryBase> = (Omit<{
531
531
  columns: (keyof T['selectable'])[];
532
532
  values: unknown[][] | Query | RawExpression;
533
533
  }>;
534
- EXISTS?: MaybeArray<[JoinFirstArg<T>, ...JoinArgs<T, Query>] | [JoinFirstArg<T>, JoinCallback<T, JoinFirstArg<T>>]>;
535
534
  }) | QueryBase | RawExpression | ((q: WhereQueryBuilder<T>) => WhereQueryBuilder);
536
535
  declare type WhereInColumn<T extends QueryBase> = keyof T['selectable'] | [keyof T['selectable'], ...(keyof T['selectable'])[]];
537
536
  declare type WhereInValues<T extends QueryBase, Column extends WhereInColumn<T>> = Column extends keyof T['selectable'] ? T['selectable'][Column]['column']['type'][] | Query | RawExpression : ({
@@ -593,9 +592,9 @@ declare abstract class Where implements QueryBase {
593
592
  orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
594
593
  _orWhereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
595
594
  _orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
596
- whereExists<T extends Where, Arg extends JoinFirstArg<T>, Args extends JoinArgs<T, Arg>>(this: T, arg: Arg, ...args: Args): WhereResult<T>;
595
+ whereExists<T extends Where, Arg extends JoinFirstArg<T>>(this: T, arg: Arg, ...args: JoinArgs<T, Arg>): WhereResult<T>;
597
596
  whereExists<T extends Where, Arg extends JoinFirstArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
598
- _whereExists<T extends Where, Arg extends JoinFirstArg<T>, Args extends JoinArgs<T, Arg>>(this: T, arg: Arg, ...args: Args): WhereResult<T>;
597
+ _whereExists<T extends Where, Arg extends JoinFirstArg<T>>(this: T, arg: Arg, ...args: JoinArgs<T, Arg>): WhereResult<T>;
599
598
  _whereExists<T extends Where, Arg extends JoinFirstArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
600
599
  orWhereExists<T extends Where, Arg extends JoinFirstArg<T>, Args extends JoinArgs<T, Arg>>(this: T, arg: Arg, ...args: Args): WhereResult<T>;
601
600
  orWhereExists<T extends Where, Arg extends JoinFirstArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
@@ -624,16 +623,17 @@ declare class WhereQueryBuilder<Q extends QueryBase = QueryBase> extends Where i
624
623
  declare type WithSelectable<T extends QueryBase, W extends keyof T['withData']> = T['withData'][W] extends WithDataItem ? StringKey<keyof T['withData'][W]['shape']> | `${T['withData'][W]['table']}.${StringKey<keyof T['withData'][W]['shape']>}` : never;
625
624
  declare type JoinFirstArg<T extends QueryBase> = Query | keyof T['relations'] | keyof T['withData'];
626
625
  declare type JoinArgs<T extends QueryBase, Arg extends JoinFirstArg<T>> = Arg extends Query ? JoinQueryArgs<T, Arg> : Arg extends keyof T['relations'] ? EmptyTuple : Arg extends keyof T['withData'] ? JoinWithArgs<T, Arg> : never;
627
- declare type JoinQueryArgs<T extends QueryBase, Q extends Query, JoinSelectable extends PropertyKey = keyof Q['result'] | `${AliasOrTable<Q>}.${StringKey<keyof Q['result']>}`> = [
628
- conditions: Record<JoinSelectable, Selectable<T> | RawExpression> | RawExpression
626
+ declare type JoinSelectable<Q extends Query> = keyof Q['result'] | `${AliasOrTable<Q>}.${StringKey<keyof Q['result']>}`;
627
+ declare type JoinQueryArgs<T extends QueryBase, Q extends Query> = [
628
+ conditions: Record<JoinSelectable<Q>, Selectable<T> | RawExpression> | RawExpression | true
629
629
  ] | [
630
- leftColumn: JoinSelectable | RawExpression,
630
+ leftColumn: JoinSelectable<Q> | RawExpression,
631
631
  rightColumn: Selectable<T> | RawExpression
632
632
  ] | [
633
- leftColumn: JoinSelectable | RawExpression,
633
+ leftColumn: JoinSelectable<Q> | RawExpression,
634
634
  op: string,
635
635
  rightColumn: Selectable<T> | RawExpression
636
- ] | [conditions: true];
636
+ ];
637
637
  declare type JoinWithArgs<T extends QueryBase, W extends keyof T['withData']> = [
638
638
  conditions: Record<WithSelectable<T, W>, Selectable<T> | RawExpression> | RawExpression
639
639
  ] | [
@@ -1315,8 +1315,10 @@ declare type WindowArgDeclaration<T extends Query = Query> = {
1315
1315
  order?: OrderArg<T>;
1316
1316
  };
1317
1317
  declare type WindowResult<T extends Query, W extends WindowArg<T>> = SetQueryWindows<T, Record<keyof W, true>>;
1318
- declare type OrderArg<T extends Query> = keyof T['selectable'] | {
1319
- [K in Selectable<T>]?: SortDir;
1318
+ declare type OrderArg<T extends Query, Key extends PropertyKey = keyof T['selectable'] | {
1319
+ [K in keyof T['result']]: T['result'][K]['dataType'] extends 'array' | 'object' ? never : K;
1320
+ }[keyof T['result']]> = Key | {
1321
+ [K in Key]?: SortDir;
1320
1322
  } | RawExpression;
1321
1323
  interface QueryMethods extends Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Where, Clear, Having, Window, Then, QueryLog, QueryCallbacks, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawMethods, CopyMethods {
1322
1324
  }
package/dist/index.js CHANGED
@@ -173,7 +173,7 @@ const getJoinedColumnName = (data, shape, table, key, isOwnColumn) => {
173
173
  return (_c = (isOwnColumn ? shape[key] : void 0) || ((_b = (_a = data.joinedShapes) == null ? void 0 : _a[table]) == null ? void 0 : _b[key])) == null ? void 0 : _c.data.name;
174
174
  };
175
175
  const revealColumnToSql = (data, shape, column, quotedAs) => {
176
- var _a, _b;
176
+ var _a;
177
177
  const index = column.indexOf(".");
178
178
  if (index !== -1) {
179
179
  const table = column.slice(0, index);
@@ -182,10 +182,10 @@ const revealColumnToSql = (data, shape, column, quotedAs) => {
182
182
  return `${quoted}.${q(
183
183
  getJoinedColumnName(data, shape, table, key, quoted === quotedAs) || key
184
184
  )}`;
185
- } else if (quotedAs) {
186
- return `${quotedAs}.${q(((_a = shape[column]) == null ? void 0 : _a.data.name) || column)}`;
185
+ } else if (quotedAs && shape[column]) {
186
+ return `${quotedAs}.${q(shape[column].data.name || column)}`;
187
187
  } else {
188
- return q(((_b = shape[column]) == null ? void 0 : _b.data.name) || column);
188
+ return q(((_a = shape[column]) == null ? void 0 : _a.data.name) || column);
189
189
  }
190
190
  };
191
191
  const revealColumnToSqlWithAs = (data, column, quotedAs) => {
@@ -1683,7 +1683,7 @@ const processJoinItem = (ctx, table, query, item, quotedAs) => {
1683
1683
  let joinAs = quotedFrom || q(first.table);
1684
1684
  const qAs = joinQuery.as ? q(joinQuery.as) : void 0;
1685
1685
  const addAs = qAs && qAs !== joinAs;
1686
- let joinedShape;
1686
+ const joinedShape = first.shape;
1687
1687
  if (item.isSubQuery) {
1688
1688
  const subQuery = first.toSql({
1689
1689
  values: ctx.values
@@ -1691,9 +1691,7 @@ const processJoinItem = (ctx, table, query, item, quotedAs) => {
1691
1691
  target = `(${subQuery.text}) ${qAs || joinAs}`;
1692
1692
  if (addAs)
1693
1693
  joinAs = qAs;
1694
- joinedShape = orchidCore.emptyObject;
1695
1694
  } else {
1696
- joinedShape = first.shape;
1697
1695
  if (addAs) {
1698
1696
  joinAs = qAs;
1699
1697
  target += ` AS ${qAs}`;
@@ -1970,22 +1968,20 @@ const processWhere = (ands, ctx, table, query, data, quotedAs, not) => {
1970
1968
  });
1971
1969
  } else if (key === "EXISTS") {
1972
1970
  const joinItems = Array.isArray(value[0]) ? value : [value];
1973
- joinItems.forEach((args) => {
1974
- const q2 = args[0];
1975
- const { target, conditions } = processJoinItem(
1976
- ctx,
1977
- table,
1978
- query,
1979
- {
1971
+ joinItems.forEach(
1972
+ (args) => {
1973
+ const { target, conditions } = processJoinItem(
1974
+ ctx,
1975
+ table,
1976
+ query,
1980
1977
  args,
1981
- isSubQuery: typeof q2 === "object" && getIsJoinSubQuery(q2.query, q2.baseQuery.query)
1982
- },
1983
- quotedAs
1984
- );
1985
- ands.push(
1986
- `${prefix}EXISTS (SELECT 1 FROM ${target} WHERE ${conditions} LIMIT 1)`
1987
- );
1988
- });
1978
+ quotedAs
1979
+ );
1980
+ ands.push(
1981
+ `${prefix}EXISTS (SELECT 1 FROM ${target} WHERE ${conditions} LIMIT 1)`
1982
+ );
1983
+ }
1984
+ );
1989
1985
  } else if (typeof value === "object" && value !== null) {
1990
1986
  if (orchidCore.isRaw(value)) {
1991
1987
  ands.push(
@@ -5031,6 +5027,25 @@ const addWhereIn = (q, and, arg, values, not) => {
5031
5027
  }
5032
5028
  return q;
5033
5029
  };
5030
+ const existsArgs = (args) => {
5031
+ const q = args[0];
5032
+ let isSubQuery;
5033
+ if (typeof q === "object") {
5034
+ isSubQuery = getIsJoinSubQuery(q.query, q.baseQuery.query);
5035
+ if (isSubQuery) {
5036
+ args[0] = q.clone();
5037
+ args[0].shape = getShapeFromSelect(q, true);
5038
+ }
5039
+ } else {
5040
+ isSubQuery = false;
5041
+ }
5042
+ return {
5043
+ EXISTS: {
5044
+ args,
5045
+ isSubQuery
5046
+ }
5047
+ };
5048
+ };
5034
5049
  class Where {
5035
5050
  constructor() {
5036
5051
  this.query = {};
@@ -5105,25 +5120,25 @@ class Where {
5105
5120
  return this.clone()._whereExists(arg, ...args);
5106
5121
  }
5107
5122
  _whereExists(...args) {
5108
- return this._where({ EXISTS: args });
5123
+ return this._where(existsArgs(args));
5109
5124
  }
5110
5125
  orWhereExists(arg, ...args) {
5111
5126
  return this.clone()._orWhereExists(arg, ...args);
5112
5127
  }
5113
5128
  _orWhereExists(...args) {
5114
- return this._or({ EXISTS: args });
5129
+ return this._or(existsArgs(args));
5115
5130
  }
5116
5131
  whereNotExists(arg, ...args) {
5117
5132
  return this.clone()._whereNotExists(arg, ...args);
5118
5133
  }
5119
5134
  _whereNotExists(...args) {
5120
- return this._whereNot({ EXISTS: args });
5135
+ return this._whereNot(existsArgs(args));
5121
5136
  }
5122
5137
  orWhereNotExists(arg, ...args) {
5123
5138
  return this.clone()._orWhereNotExists(arg, ...args);
5124
5139
  }
5125
5140
  _orWhereNotExists(...args) {
5126
- return this._orNot({ EXISTS: args });
5141
+ return this._orNot(existsArgs(args));
5127
5142
  }
5128
5143
  }
5129
5144
  const emptyInternal = {
@@ -5165,6 +5180,10 @@ const _join = (q, type, args) => {
5165
5180
  if (joinKey) {
5166
5181
  shape = getShapeFromSelect(first, isSubQuery);
5167
5182
  parsers = first.query.parsers;
5183
+ if (isSubQuery) {
5184
+ args[0] = first.clone();
5185
+ args[0].shape = shape;
5186
+ }
5168
5187
  }
5169
5188
  } else {
5170
5189
  joinKey = first;