pqb 0.13.2 → 0.13.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.d.ts CHANGED
@@ -849,8 +849,8 @@ declare abstract class Where extends QueryBase {
849
849
  _or<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
850
850
  orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
851
851
  _orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
852
- whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): T;
853
- whereIn<T extends Where>(this: T, arg: WhereInArg<T>): T;
852
+ whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
853
+ whereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
854
854
  _whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
855
855
  _whereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
856
856
  orWhereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
@@ -1903,10 +1903,10 @@ declare class Update {
1903
1903
  * You need to provide `.where`, `.findBy`, or `.find` conditions before calling `.update`.
1904
1904
  * To ensure that the whole table won't be updated by accident, updating without where conditions will result in TypeScript and runtime errors.
1905
1905
  *
1906
- * If you need to update ALL records, use `where` method without arguments:
1906
+ * Use `all()` to update ALL records without conditions:
1907
1907
  *
1908
1908
  * ```ts
1909
- * await db.table.where().update({ name: 'new name' });
1909
+ * await db.table.all().update({ name: 'new name' });
1910
1910
  * ```
1911
1911
  *
1912
1912
  * If `.select` and `.where` were specified before the update it will return an array of updated records.
@@ -2088,10 +2088,10 @@ declare class Delete {
2088
2088
  * Need to provide `.where`, `.findBy`, or `.find` conditions before calling `.delete`.
2089
2089
  * To prevent accidental deletion of all records, deleting without where will result in TypeScript and a runtime error.
2090
2090
  *
2091
- * To delete all records without conditions add an empty `where`:
2091
+ * Use `all()` to delete ALL records without conditions:
2092
2092
  *
2093
2093
  * ```ts
2094
- * await db.table.where().delete();
2094
+ * await db.table.all().delete();
2095
2095
  * ```
2096
2096
  *
2097
2097
  * ```ts
@@ -2121,7 +2121,7 @@ declare class Delete {
2121
2121
  *
2122
2122
  * ```ts
2123
2123
  * // delete all users who have corresponding profile records:
2124
- * db.table.join(Profile, 'profile.userId', 'user.id').where().delete();
2124
+ * db.table.join(Profile, 'profile.userId', 'user.id').all().delete();
2125
2125
  * ```
2126
2126
  */
2127
2127
  delete<T extends Query>(this: T, ..._args: DeleteArgs<T>): DeleteResult<T>;
@@ -2867,8 +2867,6 @@ type QueryHelper<T extends Query, Args extends unknown[], Result> = <Q extends {
2867
2867
  interface QueryMethods extends Omit<AsMethods, 'result'>, Aggregate, Select, From, Join, With, Union, Omit<JsonModifiers, 'result'>, JsonMethods, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Omit<Where, 'result'>, Clear, Having, Window, Then, QueryLog, Omit<QueryHooks, 'result'>, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawSqlMethods, CopyMethods, TransformMethods {
2868
2868
  }
2869
2869
  declare class QueryMethods {
2870
- windows: EmptyObject;
2871
- baseQuery: Query;
2872
2870
  /**
2873
2871
  * `.all` is a default behavior, that returns an array of objects:
2874
2872
  *
@@ -2940,6 +2938,31 @@ declare class QueryMethods {
2940
2938
  */
2941
2939
  exec<T extends Query>(this: T): SetQueryReturnsVoid<T>;
2942
2940
  _exec<T extends Query>(this: T): SetQueryReturnsVoid<T>;
2941
+ /**
2942
+ * Call `toSql` on a query to get an object with a `text` SQL string and a `values` array of binding values:
2943
+ *
2944
+ * ```ts
2945
+ * const sql = db.table.select('id', 'name').where({ name: 'name' }).toSql();
2946
+ *
2947
+ * expect(sql.text).toBe(
2948
+ * 'SELECT "table"."id", "table"."name" FROM "table" WHERE "table"."name" = $1',
2949
+ * );
2950
+ * expect(sql.values).toEqual(['name']);
2951
+ * ```
2952
+ *
2953
+ * `toSql` is called internally when awaiting a query.
2954
+ *
2955
+ * It is caching the result. Not mutating query methods are resetting the cache, but need to be careful with mutating methods that start with `_` - they won't reset the cache, which may lead to unwanted results.
2956
+ *
2957
+ * `toSql` optionally accepts such parameters:
2958
+ *
2959
+ * ```ts
2960
+ * type ToSqlOptions = {
2961
+ * clearCache?: true;
2962
+ * values?: [];
2963
+ * };
2964
+ * ```
2965
+ */
2943
2966
  toSql(this: Query, options?: ToSqlOptions): Sql;
2944
2967
  /**
2945
2968
  * Adds a `DISTINCT` keyword to `SELECT`:
@@ -3388,7 +3411,7 @@ type SetSelect<T extends Pick<Query, 'result' | 'meta'>, Result extends ColumnsS
3388
3411
  type SetQueryReturns<T extends Query, R extends QueryReturnType, Data = GetQueryResult<R, T['result']>> = {
3389
3412
  [K in keyof T]: K extends 'returnType' ? R : K extends 'then' ? QueryThen<Data> : K extends 'catch' ? QueryCatch<Data> : T[K];
3390
3413
  };
3391
- type SetQueryReturnsAll<T extends Query> = SetQueryReturns<T, 'all'>;
3414
+ type SetQueryReturnsAll<T extends Query> = SetQueryReturns<WhereResult<T>, 'all'>;
3392
3415
  type SetQueryReturnsOneOptional<T extends Query> = SetQueryReturns<T, 'one'>;
3393
3416
  type SetQueryReturnsOne<T extends Query> = SetQueryReturns<T, 'oneOrThrow'>;
3394
3417
  type SetQueryReturnsRows<T extends Query> = SetQueryReturns<T, 'rows'>;
package/dist/index.js CHANGED
@@ -2029,8 +2029,9 @@ const pushWhereToSql = (sql, ctx, table, query, quotedAs, not) => {
2029
2029
  }
2030
2030
  };
2031
2031
  const whereToSql = (ctx, table, query, quotedAs, not) => {
2032
+ var _a;
2032
2033
  if (query.or) {
2033
- const ors = query.and ? [query.and, ...query.or] : query.or;
2034
+ const ors = ((_a = query.and) == null ? void 0 : _a.length) ? [query.and, ...query.or] : query.or;
2034
2035
  return ors.map((and) => processAnds(and, ctx, table, query, quotedAs, not)).join(" OR ");
2035
2036
  } else if (query.and) {
2036
2037
  return processAnds(query.and, ctx, table, query, quotedAs, not);
@@ -2603,7 +2604,8 @@ const pushWithSql = (ctx, withData) => {
2603
2604
  };
2604
2605
 
2605
2606
  const checkIfASimpleQuery = (q) => {
2606
- if (q.query.returnType && q.query.returnType !== "all" || q.internal.columnsForSelectAll)
2607
+ var _a, _b;
2608
+ if (q.query.returnType && q.query.returnType !== "all" || q.internal.columnsForSelectAll || ((_a = q.query.and) == null ? void 0 : _a.length) || ((_b = q.query.or) == null ? void 0 : _b.length))
2607
2609
  return false;
2608
2610
  const keys = Object.keys(q.query);
2609
2611
  return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
@@ -2612,8 +2614,6 @@ const queryKeysOfNotSimpleQuery = [
2612
2614
  "with",
2613
2615
  "as",
2614
2616
  "from",
2615
- "and",
2616
- "or",
2617
2617
  "select",
2618
2618
  "distinct",
2619
2619
  "fromOnly",
@@ -4943,10 +4943,10 @@ class Delete {
4943
4943
  * Need to provide `.where`, `.findBy`, or `.find` conditions before calling `.delete`.
4944
4944
  * To prevent accidental deletion of all records, deleting without where will result in TypeScript and a runtime error.
4945
4945
  *
4946
- * To delete all records without conditions add an empty `where`:
4946
+ * Use `all()` to delete ALL records without conditions:
4947
4947
  *
4948
4948
  * ```ts
4949
- * await db.table.where().delete();
4949
+ * await db.table.all().delete();
4950
4950
  * ```
4951
4951
  *
4952
4952
  * ```ts
@@ -4976,7 +4976,7 @@ class Delete {
4976
4976
  *
4977
4977
  * ```ts
4978
4978
  * // delete all users who have corresponding profile records:
4979
- * db.table.join(Profile, 'profile.userId', 'user.id').where().delete();
4979
+ * db.table.join(Profile, 'profile.userId', 'user.id').all().delete();
4980
4980
  * ```
4981
4981
  */
4982
4982
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -7111,10 +7111,10 @@ class Update {
7111
7111
  * You need to provide `.where`, `.findBy`, or `.find` conditions before calling `.update`.
7112
7112
  * To ensure that the whole table won't be updated by accident, updating without where conditions will result in TypeScript and runtime errors.
7113
7113
  *
7114
- * If you need to update ALL records, use `where` method without arguments:
7114
+ * Use `all()` to update ALL records without conditions:
7115
7115
  *
7116
7116
  * ```ts
7117
- * await db.table.where().update({ name: 'new name' });
7117
+ * await db.table.all().update({ name: 'new name' });
7118
7118
  * ```
7119
7119
  *
7120
7120
  * If `.select` and `.where` were specified before the update it will return an array of updated records.
@@ -7907,7 +7907,9 @@ class QueryMethods {
7907
7907
  return this.clone()._all();
7908
7908
  }
7909
7909
  _all() {
7910
+ var _a, _b;
7910
7911
  this.query.returnType = "all";
7912
+ (_b = (_a = this.query).and) != null ? _b : _a.and = [];
7911
7913
  return this;
7912
7914
  }
7913
7915
  /**
@@ -7997,6 +7999,31 @@ class QueryMethods {
7997
7999
  this.query.returnType = "void";
7998
8000
  return this;
7999
8001
  }
8002
+ /**
8003
+ * Call `toSql` on a query to get an object with a `text` SQL string and a `values` array of binding values:
8004
+ *
8005
+ * ```ts
8006
+ * const sql = db.table.select('id', 'name').where({ name: 'name' }).toSql();
8007
+ *
8008
+ * expect(sql.text).toBe(
8009
+ * 'SELECT "table"."id", "table"."name" FROM "table" WHERE "table"."name" = $1',
8010
+ * );
8011
+ * expect(sql.values).toEqual(['name']);
8012
+ * ```
8013
+ *
8014
+ * `toSql` is called internally when awaiting a query.
8015
+ *
8016
+ * It is caching the result. Not mutating query methods are resetting the cache, but need to be careful with mutating methods that start with `_` - they won't reset the cache, which may lead to unwanted results.
8017
+ *
8018
+ * `toSql` optionally accepts such parameters:
8019
+ *
8020
+ * ```ts
8021
+ * type ToSqlOptions = {
8022
+ * clearCache?: true;
8023
+ * values?: [];
8024
+ * };
8025
+ * ```
8026
+ */
8000
8027
  toSql(options) {
8001
8028
  return toSql(this, options);
8002
8029
  }