pqb 0.30.1 → 0.30.2

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
@@ -796,15 +796,14 @@ declare class Join {
796
796
  * profile: (q) => q.profile,
797
797
  * });
798
798
  *
799
- * // select posts with counts of comments, order by comments count
799
+ * // select posts with counts of comments, filter and order by comments count
800
800
  * // result type is Array<Post & { commentsCount: number }>
801
801
  * await db.post
802
802
  * .select('*', {
803
803
  * commentsCount: (q) => q.comments.count(),
804
804
  * })
805
- * .order({
806
- * commentsCount: 'DESC',
807
- * });
805
+ * .where({ commentsCount: { gt: 10 } })
806
+ * .order({ commentsCount: 'DESC' });
808
807
  *
809
808
  * // select authors with array of their book titles
810
809
  * // result type is Array<Author & { books: string[] }>
@@ -4809,19 +4808,6 @@ declare class MergeQueryMethods {
4809
4808
  merge<T extends Query, Q extends Query>(this: T, q: Q): MergeQuery<T, Q>;
4810
4809
  }
4811
4810
 
4812
- declare const queryMethodByReturnType: {
4813
- [K in QueryReturnType]: 'query' | 'arrays';
4814
- };
4815
- type Resolve = (result: any) => any;
4816
- type Reject = (error: any) => any;
4817
- declare class Then {
4818
- then: (this: Query, resolve?: Resolve, reject?: Reject) => Promise<unknown>;
4819
- catch(this: Query, fn: (reason: any) => unknown): Promise<unknown>;
4820
- }
4821
- declare const handleResult: CommonQueryData['handleResult'];
4822
- declare const parseResult: (q: Query, parsers: ColumnsParsers | undefined, returnType: QueryReturnType | undefined, result: QueryResult, isSubQuery?: boolean) => unknown;
4823
- declare const parseRecord: (parsers: ColumnsParsers, row: any) => any;
4824
-
4825
4811
  interface SelectSelf {
4826
4812
  shape: QueryColumns;
4827
4813
  relations: RelationsBase;
@@ -4869,12 +4855,20 @@ type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey
4869
4855
  } & (T['meta']['hasSelect'] extends true ? Omit<T['result'], Columns[number]> : unknown)>> : T[K];
4870
4856
  } & QueryMetaHasSelect;
4871
4857
  type SelectAsSelectable<Arg> = {
4872
- [K in keyof Arg]: Arg[keyof Arg] extends (q: never) => {
4858
+ [K in keyof Arg]: Arg[K] extends (q: never) => {
4859
+ returnType: 'value' | 'valueOrThrow';
4873
4860
  result: QueryColumns;
4874
4861
  } ? {
4875
- [C in keyof ReturnType<Arg[keyof Arg]>['result'] & string as `${K & string}.${C}`]: {
4862
+ [P in K]: {
4863
+ as: K;
4864
+ column: ReturnType<Arg[K]>['result']['value'];
4865
+ };
4866
+ } : Arg[K] extends (q: never) => {
4867
+ result: QueryColumns;
4868
+ } ? {
4869
+ [C in keyof ReturnType<Arg[K]>['result'] & string as `${K & string}.${C}`]: {
4876
4870
  as: C;
4877
- column: ReturnType<Arg[keyof Arg]>['result'][C];
4871
+ column: ReturnType<Arg[K]>['result'][C];
4878
4872
  };
4879
4873
  } : never;
4880
4874
  }[keyof Arg];
@@ -6156,6 +6150,11 @@ type QueryHelper<T extends PickQueryMetaShape, Args extends unknown[], Result> =
6156
6150
  result: Result;
6157
6151
  };
6158
6152
  type QueryHelperResult<T extends QueryHelper<Query, unknown[], unknown>> = T['result'];
6153
+ type NarrowTypeResult<T extends PickQueryMetaResultReturnType, Narrow> = {
6154
+ [K in keyof T['result']]: K extends keyof Narrow ? {
6155
+ [P in keyof T['result'][K]]: P extends 'outputType' ? Narrow[K] extends T['result'][K]['outputType'] ? Narrow[K] : `narrowType() error: passed type does not exist in '${K & string}'s type union` : T['result'][K][P];
6156
+ } : T['result'][K];
6157
+ };
6159
6158
  declare class ColumnRefExpression<T extends QueryColumn> extends Expression<T> {
6160
6159
  name: string;
6161
6160
  result: {
@@ -6182,7 +6181,7 @@ declare const _queryExec: <T extends Query>(q: T) => never;
6182
6181
  declare const _queryFindBy: <T extends QueryBase<EmptyObject>>(q: T, args: WhereArg<T>[]) => SetQueryReturnsOne<WhereResult<T>>;
6183
6182
  declare const _queryFindByOptional: <T extends QueryBase<EmptyObject>>(q: T, args: WhereArg<T>[]) => SetQueryReturnsOneOptional<WhereResult<T>>;
6184
6183
  declare const _queryRows: <T extends Query>(q: T) => SetQueryReturnsRows<T>;
6185
- interface QueryMethods<ColumnTypes> extends AsMethods, AggregateMethods, Select, From, Join, With, Union, JsonModifiers, JsonMethods, Create, Update, Delete, Transaction, For, Where, SearchMethods, Clear, Having, Then, QueryLog, QueryHooks, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawSqlMethods<ColumnTypes>, TransformMethods, ScopeMethods, SoftDeleteMethods {
6184
+ interface QueryMethods<ColumnTypes> extends AsMethods, AggregateMethods, Select, From, Join, With, Union, JsonModifiers, JsonMethods, Create, Update, Delete, Transaction, For, Where, SearchMethods, Clear, Having, QueryLog, QueryHooks, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawSqlMethods<ColumnTypes>, TransformMethods, ScopeMethods, SoftDeleteMethods {
6186
6185
  }
6187
6186
  declare class QueryMethods<ColumnTypes> {
6188
6187
  /**
@@ -6698,7 +6697,45 @@ declare class QueryMethods<ColumnTypes> {
6698
6697
  * @param arg - any available column name, such as of a joined table
6699
6698
  */
6700
6699
  ref<T extends PickQueryMeta, K extends keyof T['meta']['selectable'] & string>(this: T, arg: K): RefExpression<T['meta']['selectable'][K]['column']> & T['meta']['selectable'][K]['column']['operators'];
6700
+ /**
6701
+ * Narrows a part of the query output type.
6702
+ * Use with caution, type-safety isn't guaranteed with it.
6703
+ * This is similar so using `as` keyword from TypeScript, except that it applies only to a part of the result.
6704
+ *
6705
+ * The syntax `()<{ ... }>()` is enforced by internal limitations.
6706
+ *
6707
+ * ```ts
6708
+ * const rows = db.table
6709
+ * // filter out records where the `nullableColumn` is null
6710
+ * .where({ nullableColumn: { not: null } });
6711
+ * // narrows only a specified column, the rest of result is unchanged
6712
+ * .narrowType()<{ nullableColumn: string }>()
6713
+ *
6714
+ * // the column had type `string | null`, now it is `string`
6715
+ * rows[0].nullableColumn
6716
+ *
6717
+ * // imagine that table has a enum column kind with variants 'first' | 'second'
6718
+ * // and a boolean `approved`
6719
+ * db.table
6720
+ * .where({ kind: 'first', approved: true })
6721
+ * // after applying such `where`, it's safe to narrow the type to receive the literal values
6722
+ * .narrowType()<{ kind: 'first', approved: true }>();
6723
+ * ```
6724
+ */
6725
+ narrowType<T extends PickQueryMetaResultReturnType>(this: T): <Narrow>() => {
6726
+ [K in keyof T]: K extends 'result' ? NarrowTypeResult<T, Narrow> : K extends 'then' ? QueryThen<GetQueryResult<T, NarrowTypeResult<T, Narrow>>> : T[K];
6727
+ };
6728
+ }
6729
+
6730
+ declare const queryMethodByReturnType: {
6731
+ [K in QueryReturnType]: 'query' | 'arrays';
6732
+ };
6733
+ declare class Then {
6734
+ catch(this: Query, fn: (reason: any) => unknown): Promise<unknown>;
6701
6735
  }
6736
+ declare const handleResult: CommonQueryData['handleResult'];
6737
+ declare const parseResult: (q: Query, parsers: ColumnsParsers | undefined, returnType: QueryReturnType | undefined, result: QueryResult, isSubQuery?: boolean) => unknown;
6738
+ declare const parseRecord: (parsers: ColumnsParsers, row: any) => any;
6702
6739
 
6703
6740
  declare function queryJson<T>(self: T, coalesce?: boolean): SetQueryReturnsColumnOptional<T, QueryColumn<string>>;
6704
6741
 
@@ -6842,8 +6879,8 @@ declare const queryTypeWithLimitOne: {
6842
6879
  void: true | undefined;
6843
6880
  };
6844
6881
  declare const isQueryReturnsAll: (q: Query) => boolean;
6845
- type QueryReturnsAll<T extends QueryReturnType> = (QueryReturnType extends T ? 'all' : T) extends 'all' ? true : false;
6846
- type GetQueryResult<T extends PickQueryReturnType, Result extends QueryColumns> = QueryReturnsAll<T['returnType']> extends true ? ColumnShapeOutput<Result>[] : T['returnType'] extends 'one' ? ColumnShapeOutput<Result> | undefined : T['returnType'] extends 'oneOrThrow' ? ColumnShapeOutput<Result> : T['returnType'] extends 'value' ? Result['value']['outputType'] | undefined : T['returnType'] extends 'valueOrThrow' ? Result['value']['outputType'] : T['returnType'] extends 'rows' ? ColumnShapeOutput<Result>[keyof Result][][] : T['returnType'] extends 'pluck' ? Result['pluck']['outputType'][] : T['returnType'] extends 'rowCount' ? number : T['returnType'] extends 'void' ? void : never;
6882
+ type QueryReturnsAll<T extends QueryReturnType> = QueryReturnType extends T ? true : T extends 'all' ? true : false;
6883
+ type GetQueryResult<T extends PickQueryReturnType, Result extends QueryColumns> = QueryReturnsAll<T['returnType']> extends true ? ColumnShapeOutput<Result>[] : T['returnType'] extends 'one' ? ColumnShapeOutput<Result> | undefined : T['returnType'] extends 'oneOrThrow' ? ColumnShapeOutput<Result> : T['returnType'] extends 'value' ? Result['value']['outputType'] | undefined : T['returnType'] extends 'valueOrThrow' ? Result['value']['outputType'] : T['returnType'] extends 'rows' ? ColumnShapeOutput<Result>[keyof Result][][] : T['returnType'] extends 'pluck' ? Result['pluck']['outputType'][] : T['returnType'] extends 'rowCount' ? number : void;
6847
6884
  type AddQuerySelect<T extends PickQueryMetaResultReturnType, Result extends QueryColumns> = {
6848
6885
  [K in keyof T]: K extends 'result' ? {
6849
6886
  [K in (T['meta']['hasSelect'] extends true ? keyof T['result'] : never) | keyof Result]: K extends keyof Result ? Result[K] : K extends keyof T['result'] ? T['result'][K] : never;
package/dist/index.js CHANGED
@@ -2021,7 +2021,7 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
2021
2021
  return parens && ands.length > 1 ? `(${sql})` : sql;
2022
2022
  };
2023
2023
  const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2024
- var _a, _b;
2024
+ var _a, _b, _c, _d;
2025
2025
  if (typeof data === "function") {
2026
2026
  const qb = Object.create(table);
2027
2027
  qb.q = getClonedQueryData(query);
@@ -2183,7 +2183,8 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2183
2183
  column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
2184
2184
  quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
2185
2185
  } else {
2186
- quotedColumn = void 0;
2186
+ column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
2187
+ quotedColumn = `"${key}".r`;
2187
2188
  }
2188
2189
  if (!column || !quotedColumn) {
2189
2190
  throw new Error(`Unknown column ${key} provided to condition`);
@@ -3456,6 +3457,8 @@ const getShapeFromSelect = (q, isSubQuery) => {
3456
3457
  }
3457
3458
  }
3458
3459
  }
3460
+ } else if (orchidCore.isExpression(item)) {
3461
+ result.value = item.result.value;
3459
3462
  }
3460
3463
  }
3461
3464
  }
@@ -7190,15 +7193,14 @@ class Join {
7190
7193
  * profile: (q) => q.profile,
7191
7194
  * });
7192
7195
  *
7193
- * // select posts with counts of comments, order by comments count
7196
+ * // select posts with counts of comments, filter and order by comments count
7194
7197
  * // result type is Array<Post & { commentsCount: number }>
7195
7198
  * await db.post
7196
7199
  * .select('*', {
7197
7200
  * commentsCount: (q) => q.comments.count(),
7198
7201
  * })
7199
- * .order({
7200
- * commentsCount: 'DESC',
7201
- * });
7202
+ * .where({ commentsCount: { gt: 10 } })
7203
+ * .order({ commentsCount: 'DESC' });
7202
7204
  *
7203
7205
  * // select authors with array of their book titles
7204
7206
  * // result type is Array<Author & { books: string[] }>
@@ -11066,6 +11068,34 @@ class QueryMethods {
11066
11068
  }
11067
11069
  return new RefExpression(column, q.q, arg);
11068
11070
  }
11071
+ /**
11072
+ * Narrows a part of the query output type.
11073
+ * Use with caution, type-safety isn't guaranteed with it.
11074
+ * This is similar so using `as` keyword from TypeScript, except that it applies only to a part of the result.
11075
+ *
11076
+ * The syntax `()<{ ... }>()` is enforced by internal limitations.
11077
+ *
11078
+ * ```ts
11079
+ * const rows = db.table
11080
+ * // filter out records where the `nullableColumn` is null
11081
+ * .where({ nullableColumn: { not: null } });
11082
+ * // narrows only a specified column, the rest of result is unchanged
11083
+ * .narrowType()<{ nullableColumn: string }>()
11084
+ *
11085
+ * // the column had type `string | null`, now it is `string`
11086
+ * rows[0].nullableColumn
11087
+ *
11088
+ * // imagine that table has a enum column kind with variants 'first' | 'second'
11089
+ * // and a boolean `approved`
11090
+ * db.table
11091
+ * .where({ kind: 'first', approved: true })
11092
+ * // after applying such `where`, it's safe to narrow the type to receive the literal values
11093
+ * .narrowType()<{ kind: 'first', approved: true }>();
11094
+ * ```
11095
+ */
11096
+ narrowType() {
11097
+ return () => this;
11098
+ }
11069
11099
  }
11070
11100
  orchidCore.applyMixins(QueryMethods, [
11071
11101
  QueryBase,