pqb 0.25.0 → 0.25.1

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.js CHANGED
@@ -98,6 +98,9 @@ function raw(...args) {
98
98
  return orchidCore.isTemplateLiteralArgs(args) ? new RawSQL(args) : typeof args[0] === "function" ? new DynamicRawSQL(args[0]) : new RawSQL(args[0].raw, args[0].values);
99
99
  }
100
100
  const countSelect = [new RawSQL("count(*)")];
101
+ function sqlQueryArgsToExpression(args) {
102
+ return Array.isArray(args[0]) ? new RawSQL(args) : args[0];
103
+ }
101
104
 
102
105
  var __defProp$g = Object.defineProperty;
103
106
  var __defProps$a = Object.defineProperties;
@@ -2110,31 +2113,28 @@ class AsMethods {
2110
2113
  }
2111
2114
  }
2112
2115
 
2113
- function queryFrom(self, args) {
2114
- if (Array.isArray(args[0])) {
2115
- return queryFrom(self, [
2116
- new RawSQL(args)
2117
- ]);
2118
- }
2116
+ function queryFrom(self, arg, options) {
2119
2117
  const data = self.q;
2120
- if (typeof args[0] === "string") {
2121
- data.as || (data.as = args[0]);
2122
- } else if (!orchidCore.isExpression(args[0])) {
2123
- const q = args[0];
2118
+ if (typeof arg === "string") {
2119
+ data.as || (data.as = arg);
2120
+ } else if (!orchidCore.isExpression(arg)) {
2121
+ const q = arg;
2124
2122
  data.as || (data.as = q.q.as || q.table || "t");
2125
- data.shape = getShapeFromSelect(
2126
- args[0],
2127
- true
2128
- );
2123
+ data.shape = getShapeFromSelect(arg, true);
2129
2124
  data.parsers = q.q.parsers;
2130
2125
  } else {
2131
2126
  data.as || (data.as = "t");
2132
2127
  }
2133
- const options = args[1];
2134
2128
  if (options == null ? void 0 : options.only) {
2135
2129
  data.fromOnly = options.only;
2136
2130
  }
2137
- data.from = args[0];
2131
+ data.from = arg;
2132
+ return self;
2133
+ }
2134
+ function queryFromSql(self, args) {
2135
+ const data = self.q;
2136
+ data.as || (data.as = "t");
2137
+ data.from = sqlQueryArgsToExpression(args);
2138
2138
  return self;
2139
2139
  }
2140
2140
  class From {
@@ -2145,13 +2145,6 @@ class From {
2145
2145
  * // accepts sub-query:
2146
2146
  * db.table.from(Otherdb.table.select('foo', 'bar'));
2147
2147
  *
2148
- * // accepts raw sql by template literal:
2149
- * const value = 123;
2150
- * db.table.from`value = ${value}`;
2151
- *
2152
- * // accepts raw sql:
2153
- * db.table.from(db.table.sql`value = ${value}`);
2154
- *
2155
2148
  * // accepts alias of `WITH` expression:
2156
2149
  * q.with('foo', Otherdb.table.select('id', 'name')).from('foo');
2157
2150
  * ```
@@ -2164,10 +2157,29 @@ class From {
2164
2157
  * });
2165
2158
  * ```
2166
2159
  *
2167
- * @param args - query, raw SQL, name of CTE table, or a template string
2160
+ * @param arg - query or name of CTE table
2161
+ * @param options - { only: true } for SQL `ONLY` keyword
2168
2162
  */
2169
- from(...args) {
2163
+ from(arg, options) {
2170
2164
  return queryFrom(
2165
+ this.clone(),
2166
+ arg,
2167
+ options
2168
+ );
2169
+ }
2170
+ /**
2171
+ * Set the `FROM` value with custom SQL:
2172
+ *
2173
+ * ```ts
2174
+ * const value = 123;
2175
+ * db.table.from`value = ${value}`;
2176
+ * db.table.from(db.table.sql`value = ${value}`);
2177
+ * ```
2178
+ *
2179
+ * @param args - SQL expression
2180
+ */
2181
+ fromSql(...args) {
2182
+ return queryFromSql(
2171
2183
  this.clone(),
2172
2184
  args
2173
2185
  );
@@ -2175,7 +2187,7 @@ class From {
2175
2187
  }
2176
2188
 
2177
2189
  function queryWrap(self, query, as = "t") {
2178
- return _queryAs(queryFrom(query, [self]), as);
2190
+ return _queryAs(queryFrom(query, self), as);
2179
2191
  }
2180
2192
 
2181
2193
  function queryJson(self, coalesce) {
@@ -6167,12 +6179,6 @@ class Having {
6167
6179
  * // HAVING count(*) >= 10
6168
6180
  * ```
6169
6181
  *
6170
- * Alternatively, it accepts a raw SQL template:
6171
- *
6172
- * ```ts
6173
- * db.table.having`count(*) >= ${10}`;
6174
- * ```
6175
- *
6176
6182
  * Multiple having conditions will be combined with `AND`:
6177
6183
  *
6178
6184
  * ```ts
@@ -6218,11 +6224,23 @@ class Having {
6218
6224
  return pushQueryValue(
6219
6225
  q,
6220
6226
  "having",
6221
- "raw" in args[0] ? args : args.map(
6227
+ args.map(
6222
6228
  (arg) => arg(q).q.expr
6223
6229
  )
6224
6230
  );
6225
6231
  }
6232
+ /**
6233
+ * Provide SQL expression for the `HAVING` SQL statement:
6234
+ *
6235
+ * ```ts
6236
+ * db.table.having`count(*) >= ${10}`;
6237
+ * ```
6238
+ *
6239
+ * @param args - SQL expression
6240
+ */
6241
+ havingSql(...args) {
6242
+ return pushQueryValue(this.clone(), "having", args);
6243
+ }
6226
6244
  }
6227
6245
 
6228
6246
  const before = (q, key, cb) => pushQueryValue(q, `before${key}`, cb);
@@ -6475,29 +6493,29 @@ class QueryBase {
6475
6493
  }
6476
6494
 
6477
6495
  const _queryWhere = (q, args) => {
6478
- if (Array.isArray(args[0])) {
6479
- return pushQueryValue(
6480
- q,
6481
- "and",
6482
- new RawSQL(args)
6483
- );
6484
- }
6485
6496
  return pushQueryArray(
6486
6497
  q,
6487
6498
  "and",
6488
6499
  args
6489
6500
  );
6490
6501
  };
6502
+ const _queryWhereSql = (q, args) => {
6503
+ return pushQueryValue(
6504
+ q,
6505
+ "and",
6506
+ sqlQueryArgsToExpression(args)
6507
+ );
6508
+ };
6491
6509
  const _queryWhereNot = (q, args) => {
6492
- if (Array.isArray(args[0])) {
6493
- return pushQueryValue(q, "and", {
6494
- NOT: new RawSQL(args)
6495
- });
6496
- }
6497
6510
  return pushQueryValue(q, "and", {
6498
6511
  NOT: args
6499
6512
  });
6500
6513
  };
6514
+ const _queryWhereNotSql = (q, args) => {
6515
+ return pushQueryValue(q, "and", {
6516
+ NOT: sqlQueryArgsToExpression(args)
6517
+ });
6518
+ };
6501
6519
  const _queryOr = (q, args) => {
6502
6520
  return pushQueryArray(
6503
6521
  q,
@@ -6580,7 +6598,7 @@ class Where {
6580
6598
  * },
6581
6599
  *
6582
6600
  * // where column equals to raw SQL
6583
- * column: db.table.sql`raw expression`,
6601
+ * column: db.table.sql`sql expression`,
6584
6602
  * });
6585
6603
  * ```
6586
6604
  *
@@ -6629,9 +6647,6 @@ class Where {
6629
6647
  * `where` supports raw SQL:
6630
6648
  *
6631
6649
  * ```ts
6632
- * db.table.where`a = b`;
6633
- *
6634
- * // or
6635
6650
  * db.table.where(db.table.sql`a = b`);
6636
6651
  *
6637
6652
  * // or
@@ -6956,6 +6971,29 @@ class Where {
6956
6971
  args
6957
6972
  );
6958
6973
  }
6974
+ /**
6975
+ * Use a custom SQL expression in `WHERE` statement:
6976
+ *
6977
+ * ```ts
6978
+ * db.table.where`a = b`;
6979
+ *
6980
+ * // or
6981
+ * db.table.where(db.table.sql`a = b`);
6982
+ *
6983
+ * // or
6984
+ * import { raw } from 'orchid-orm';
6985
+ *
6986
+ * db.table.where(raw`a = b`);
6987
+ * ```
6988
+ *
6989
+ * @param args - SQL expression
6990
+ */
6991
+ whereSql(...args) {
6992
+ return _queryWhereSql(
6993
+ this.clone(),
6994
+ args
6995
+ );
6996
+ }
6959
6997
  /**
6960
6998
  * `whereNot` takes the same argument as `where`,
6961
6999
  * multiple conditions are combined with `AND`,
@@ -6977,6 +7015,18 @@ class Where {
6977
7015
  args
6978
7016
  );
6979
7017
  }
7018
+ /**
7019
+ * `whereNot` version accepting SQL expression:
7020
+ *
7021
+ * ```ts
7022
+ * db.table.whereNot`sql expression`
7023
+ * ```
7024
+ *
7025
+ * @param args - SQL expression
7026
+ */
7027
+ whereNotSql(...args) {
7028
+ return _queryWhereNotSql(this.clone(), args);
7029
+ }
6980
7030
  /**
6981
7031
  * `orWhere` is accepting the same arguments as {@link where}, joining arguments with `OR`.
6982
7032
  *
@@ -9014,12 +9064,10 @@ class Update {
9014
9064
  * @param args - raw SQL via a template string or by using a `sql` method
9015
9065
  */
9016
9066
  updateRaw(...args) {
9017
- const q = this.clone();
9018
- if (Array.isArray(args[0])) {
9019
- const sql = new RawSQL(args);
9020
- return _queryUpdateRaw(q, sql);
9021
- }
9022
- return _queryUpdateRaw(q, args[0]);
9067
+ return _queryUpdateRaw(
9068
+ this.clone(),
9069
+ sqlQueryArgsToExpression(args)
9070
+ );
9023
9071
  }
9024
9072
  /**
9025
9073
  * To make sure that at least one row was updated use `updateOrThrow`:
@@ -10061,7 +10109,7 @@ class QueryMethods {
10061
10109
  * The `find` method is available only for tables which has exactly one primary key.
10062
10110
  * And also it can accept raw SQL template literal, then the primary key is not required.
10063
10111
  *
10064
- * Find record by id, throw [NotFoundError](/guide/error-handling.html) if not found:
10112
+ * Finds a record by id, throws {@link NotFoundError} if not found:
10065
10113
  *
10066
10114
  * ```ts
10067
10115
  * await db.table.find(1);
@@ -10074,13 +10122,9 @@ class QueryMethods {
10074
10122
  * `;
10075
10123
  * ```
10076
10124
  *
10077
- * @param args - primary key value to find by, or a raw SQL
10125
+ * @param value - primary key value to find by
10078
10126
  */
10079
- find(...args) {
10080
- let [value] = args;
10081
- if (Array.isArray(value)) {
10082
- value = new RawSQL(args);
10083
- }
10127
+ find(value) {
10084
10128
  const q = this.clone();
10085
10129
  if (value === null || value === void 0) {
10086
10130
  throw new OrchidOrmInternalError(
@@ -10097,22 +10141,54 @@ class QueryMethods {
10097
10141
  );
10098
10142
  }
10099
10143
  /**
10100
- * Find a single record by the primary key (id), adds `LIMIT 1`, can accept a raw SQL.
10144
+ * Finds a single record with a given SQL, throws {@link NotFoundError} if not found:
10145
+ *
10146
+ * ```ts
10147
+ * await db.user.find`
10148
+ * age = ${age} AND
10149
+ * name = ${name}
10150
+ * `;
10151
+ * ```
10152
+ *
10153
+ * @param args - SQL expression
10154
+ */
10155
+ findBySql(...args) {
10156
+ const q = this.clone();
10157
+ return _queryTake(_queryWhereSql(q, args));
10158
+ }
10159
+ /**
10160
+ * Find a single record by the primary key (id), adds `LIMIT 1`.
10101
10161
  * Returns `undefined` when not found.
10102
10162
  *
10103
10163
  * ```ts
10104
10164
  * const result: TableType | undefined = await db.table.find(123);
10105
10165
  * ```
10106
10166
  *
10107
- * @param args - primary key value to find by, or a raw SQL
10167
+ * @param value - primary key value to find by, or a raw SQL
10108
10168
  */
10109
- findOptional(...args) {
10169
+ findOptional(value) {
10170
+ return _queryTakeOptional(this.find(value));
10171
+ }
10172
+ /**
10173
+ * Finds a single record with a given SQL.
10174
+ * Returns `undefined` when not found.
10175
+ *
10176
+ * ```ts
10177
+ * await db.user.find`
10178
+ * age = ${age} AND
10179
+ * name = ${name}
10180
+ * `;
10181
+ * ```
10182
+ *
10183
+ * @param args - SQL expression
10184
+ */
10185
+ findBySqlOptional(...args) {
10110
10186
  return _queryTakeOptional(
10111
- this.find(...args)
10187
+ this.findBySql(...args)
10112
10188
  );
10113
10189
  }
10114
10190
  /**
10115
- * The same as `where(conditions).take()`, it will filter records and add a `LIMIT 1`.
10191
+ * The same as `where(conditions).take()`, takes the same arguments as {@link Where.where}, it will filter records and add a `LIMIT 1`.
10116
10192
  * Throws `NotFoundError` if not found.
10117
10193
  *
10118
10194
  * ```ts
@@ -10246,7 +10322,7 @@ class QueryMethods {
10246
10322
  /**
10247
10323
  * Adds an order by clause to the query.
10248
10324
  *
10249
- * Takes one or more arguments, each argument can be a column name, an object, or a raw expression.
10325
+ * Takes one or more arguments, each argument can be a column name or an object.
10250
10326
  *
10251
10327
  * ```ts
10252
10328
  * db.table.order('id', 'name'); // ASC by default
@@ -10258,11 +10334,6 @@ class QueryMethods {
10258
10334
  * name: 'ASC NULLS FIRST',
10259
10335
  * age: 'DESC NULLS LAST',
10260
10336
  * });
10261
- *
10262
- * // order by raw SQL expression:
10263
- * db.table.order`raw sql`;
10264
- * // or
10265
- * db.table.order(db.table.sql`raw sql`);
10266
10337
  * ```
10267
10338
  *
10268
10339
  * `order` can refer to the values returned from `select` sub-queries (unlike `where` which cannot).
@@ -10281,22 +10352,35 @@ class QueryMethods {
10281
10352
  * });
10282
10353
  * ```
10283
10354
  *
10284
- * @param args - column name(s), raw SQL, or an object with column names and sort directions.
10355
+ * @param args - column name(s) or an object with column names and sort directions.
10285
10356
  */
10286
10357
  order(...args) {
10287
- if (Array.isArray(args[0])) {
10288
- return pushQueryValue(
10289
- this.clone(),
10290
- "order",
10291
- new RawSQL(args)
10292
- );
10293
- }
10294
10358
  return pushQueryArray(
10295
10359
  this.clone(),
10296
10360
  "order",
10297
10361
  args
10298
10362
  );
10299
10363
  }
10364
+ /**
10365
+ * Order by SQL expression
10366
+ *
10367
+ * Order by raw SQL expression.
10368
+ *
10369
+ * ```ts
10370
+ * db.table.order`raw sql`;
10371
+ * // or
10372
+ * db.table.order(db.table.sql`raw sql`);
10373
+ * ```
10374
+ *
10375
+ * @param args - SQL expression
10376
+ */
10377
+ orderSql(...args) {
10378
+ return pushQueryValue(
10379
+ this.clone(),
10380
+ "order",
10381
+ sqlQueryArgsToExpression(args)
10382
+ );
10383
+ }
10300
10384
  /**
10301
10385
  * Adds a limit clause to the query.
10302
10386
  *
@@ -11144,6 +11228,8 @@ exports._queryUpdateRaw = _queryUpdateRaw;
11144
11228
  exports._queryWhere = _queryWhere;
11145
11229
  exports._queryWhereIn = _queryWhereIn;
11146
11230
  exports._queryWhereNot = _queryWhereNot;
11231
+ exports._queryWhereNotSql = _queryWhereNotSql;
11232
+ exports._queryWhereSql = _queryWhereSql;
11147
11233
  exports.addComputedColumns = addComputedColumns;
11148
11234
  exports.addParserForRawExpression = addParserForRawExpression;
11149
11235
  exports.addParserForSelectItem = addParserForSelectItem;
@@ -11197,6 +11283,7 @@ exports.pushQueryOn = pushQueryOn;
11197
11283
  exports.pushQueryOrOn = pushQueryOrOn;
11198
11284
  exports.pushQueryValue = pushQueryValue;
11199
11285
  exports.queryFrom = queryFrom;
11286
+ exports.queryFromSql = queryFromSql;
11200
11287
  exports.queryJson = queryJson;
11201
11288
  exports.queryMethodByReturnType = queryMethodByReturnType;
11202
11289
  exports.queryTypeWithLimitOne = queryTypeWithLimitOne;
@@ -11212,6 +11299,7 @@ exports.setParserForSelectedString = setParserForSelectedString;
11212
11299
  exports.setQueryObjectValue = setQueryObjectValue;
11213
11300
  exports.setQueryOperators = setQueryOperators;
11214
11301
  exports.simplifyColumnDefault = simplifyColumnDefault;
11302
+ exports.sqlQueryArgsToExpression = sqlQueryArgsToExpression;
11215
11303
  exports.templateLiteralToSQL = templateLiteralToSQL;
11216
11304
  exports.testTransaction = testTransaction;
11217
11305
  exports.throwIfNoWhere = throwIfNoWhere;