pqb 0.25.0 → 0.26.0

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;
@@ -828,7 +831,7 @@ class JSONColumn extends ColumnType {
828
831
  JSONColumn.prototype.encodeFn = JSON.stringify;
829
832
  class JSONTextColumn extends ColumnType {
830
833
  constructor(schema) {
831
- super(schema, schema.stringSchema);
834
+ super(schema, schema.stringSchema());
832
835
  this.dataType = "json";
833
836
  this.operators = Operators.text;
834
837
  }
@@ -1480,7 +1483,12 @@ const dateTimeEncode = (input) => {
1480
1483
  const skipDateMethodsFromToCode = { encodeFn: dateTimeEncode };
1481
1484
  class DateBaseColumn extends ColumnType {
1482
1485
  constructor(schema) {
1483
- super(schema, schema.stringNumberDate);
1486
+ super(
1487
+ schema,
1488
+ schema.stringNumberDate(),
1489
+ schema.stringSchema(),
1490
+ schema.stringNumberDate()
1491
+ );
1484
1492
  this.operators = Operators.date;
1485
1493
  this.encodeFn = dateTimeEncode;
1486
1494
  this.asNumber = schema.dateAsNumber;
@@ -1554,7 +1562,7 @@ class TimestampTZColumn extends DateTimeTzBaseClass {
1554
1562
  }
1555
1563
  class TimeColumn extends ColumnType {
1556
1564
  constructor(schema, dateTimePrecision) {
1557
- super(schema, schema.stringSchema);
1565
+ super(schema, schema.stringSchema());
1558
1566
  this.dataType = "time";
1559
1567
  this.operators = Operators.time;
1560
1568
  this.data.dateTimePrecision = dateTimePrecision;
@@ -1572,7 +1580,7 @@ class TimeColumn extends ColumnType {
1572
1580
  }
1573
1581
  class IntervalColumn extends ColumnType {
1574
1582
  constructor(schema, fields, precision) {
1575
- super(schema, schema.timeInterval);
1583
+ super(schema, schema.timeInterval());
1576
1584
  this.dataType = "interval";
1577
1585
  this.operators = Operators.date;
1578
1586
  this.data.fields = fields;
@@ -1745,10 +1753,20 @@ const defaultSchemaConfig = {
1745
1753
  array(item) {
1746
1754
  return new ArrayColumn(defaultSchemaConfig, item, void 0);
1747
1755
  },
1756
+ boolean: orchidCore.noop,
1757
+ buffer: orchidCore.noop,
1758
+ unknown: orchidCore.noop,
1759
+ never: orchidCore.noop,
1760
+ stringSchema: orchidCore.noop,
1748
1761
  stringMin: orchidCore.noop,
1749
1762
  stringMax: orchidCore.noop,
1750
1763
  stringMinMax: orchidCore.noop,
1764
+ number: orchidCore.noop,
1765
+ int: orchidCore.noop,
1766
+ stringNumberDate: orchidCore.noop,
1767
+ timeInterval: orchidCore.noop,
1751
1768
  bit: orchidCore.noop,
1769
+ uuid: orchidCore.noop,
1752
1770
  nullable() {
1753
1771
  return orchidCore.setColumnData(this, "isNullable", true);
1754
1772
  },
@@ -2110,31 +2128,28 @@ class AsMethods {
2110
2128
  }
2111
2129
  }
2112
2130
 
2113
- function queryFrom(self, args) {
2114
- if (Array.isArray(args[0])) {
2115
- return queryFrom(self, [
2116
- new RawSQL(args)
2117
- ]);
2118
- }
2131
+ function queryFrom(self, arg, options) {
2119
2132
  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];
2133
+ if (typeof arg === "string") {
2134
+ data.as || (data.as = arg);
2135
+ } else if (!orchidCore.isExpression(arg)) {
2136
+ const q = arg;
2124
2137
  data.as || (data.as = q.q.as || q.table || "t");
2125
- data.shape = getShapeFromSelect(
2126
- args[0],
2127
- true
2128
- );
2138
+ data.shape = getShapeFromSelect(arg, true);
2129
2139
  data.parsers = q.q.parsers;
2130
2140
  } else {
2131
2141
  data.as || (data.as = "t");
2132
2142
  }
2133
- const options = args[1];
2134
2143
  if (options == null ? void 0 : options.only) {
2135
2144
  data.fromOnly = options.only;
2136
2145
  }
2137
- data.from = args[0];
2146
+ data.from = arg;
2147
+ return self;
2148
+ }
2149
+ function queryFromSql(self, args) {
2150
+ const data = self.q;
2151
+ data.as || (data.as = "t");
2152
+ data.from = sqlQueryArgsToExpression(args);
2138
2153
  return self;
2139
2154
  }
2140
2155
  class From {
@@ -2145,13 +2160,6 @@ class From {
2145
2160
  * // accepts sub-query:
2146
2161
  * db.table.from(Otherdb.table.select('foo', 'bar'));
2147
2162
  *
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
2163
  * // accepts alias of `WITH` expression:
2156
2164
  * q.with('foo', Otherdb.table.select('id', 'name')).from('foo');
2157
2165
  * ```
@@ -2164,10 +2172,29 @@ class From {
2164
2172
  * });
2165
2173
  * ```
2166
2174
  *
2167
- * @param args - query, raw SQL, name of CTE table, or a template string
2175
+ * @param arg - query or name of CTE table
2176
+ * @param options - { only: true } for SQL `ONLY` keyword
2168
2177
  */
2169
- from(...args) {
2178
+ from(arg, options) {
2170
2179
  return queryFrom(
2180
+ this.clone(),
2181
+ arg,
2182
+ options
2183
+ );
2184
+ }
2185
+ /**
2186
+ * Set the `FROM` value with custom SQL:
2187
+ *
2188
+ * ```ts
2189
+ * const value = 123;
2190
+ * db.table.from`value = ${value}`;
2191
+ * db.table.from(db.table.sql`value = ${value}`);
2192
+ * ```
2193
+ *
2194
+ * @param args - SQL expression
2195
+ */
2196
+ fromSql(...args) {
2197
+ return queryFromSql(
2171
2198
  this.clone(),
2172
2199
  args
2173
2200
  );
@@ -2175,7 +2202,7 @@ class From {
2175
2202
  }
2176
2203
 
2177
2204
  function queryWrap(self, query, as = "t") {
2178
- return _queryAs(queryFrom(query, [self]), as);
2205
+ return _queryAs(queryFrom(query, self), as);
2179
2206
  }
2180
2207
 
2181
2208
  function queryJson(self, coalesce) {
@@ -3514,19 +3541,19 @@ class NumberBaseColumn extends ColumnType {
3514
3541
  }
3515
3542
  class IntegerBaseColumn extends NumberBaseColumn {
3516
3543
  constructor(schema) {
3517
- super(schema, schema.int);
3544
+ super(schema, schema.int());
3518
3545
  this.data.int = true;
3519
3546
  }
3520
3547
  }
3521
3548
  class NumberAsStringBaseColumn extends ColumnType {
3522
3549
  constructor(schema) {
3523
- super(schema, schema.stringSchema);
3550
+ super(schema, schema.stringSchema());
3524
3551
  this.operators = Operators.number;
3525
3552
  }
3526
3553
  }
3527
3554
  class DecimalColumn extends ColumnType {
3528
3555
  constructor(schema, numericPrecision, numericScale) {
3529
- super(schema, schema.stringSchema);
3556
+ super(schema, schema.stringSchema());
3530
3557
  this.operators = Operators.number;
3531
3558
  this.dataType = "decimal";
3532
3559
  this.data.numericPrecision = numericPrecision;
@@ -3599,7 +3626,7 @@ class BigIntColumn extends NumberAsStringBaseColumn {
3599
3626
  }
3600
3627
  class RealColumn extends NumberBaseColumn {
3601
3628
  constructor(schema) {
3602
- super(schema, schema.number);
3629
+ super(schema, schema.number());
3603
3630
  this.dataType = "real";
3604
3631
  this.parseItem = parseFloat;
3605
3632
  }
@@ -3685,14 +3712,17 @@ var __spreadValues$9 = (a, b) => {
3685
3712
  };
3686
3713
  var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
3687
3714
  class TextBaseColumn extends ColumnType {
3688
- constructor(schema, schemaType = schema.stringSchema) {
3715
+ constructor(schema, schemaType = schema.stringSchema()) {
3689
3716
  super(schema, schemaType);
3690
3717
  this.operators = Operators.text;
3691
3718
  }
3692
3719
  }
3693
3720
  class LimitedTextBaseColumn extends TextBaseColumn {
3694
3721
  constructor(schema, limit) {
3695
- super(schema, limit ? schema.stringMax(limit) : schema.stringSchema);
3722
+ super(
3723
+ schema,
3724
+ limit ? schema.stringMax(limit) : schema.stringSchema()
3725
+ );
3696
3726
  this.data.maxChars = limit;
3697
3727
  }
3698
3728
  toSQL() {
@@ -3775,7 +3805,7 @@ const textColumnToCode = (column, t) => {
3775
3805
  `${column.dataType}(${args})${orchidCore.stringDataToCode(data)}`
3776
3806
  );
3777
3807
  };
3778
- const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema;
3808
+ const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema();
3779
3809
  class TextColumn extends TextBaseColumn {
3780
3810
  constructor(schema, min, max) {
3781
3811
  super(schema, minMaxToSchema(schema, min, max));
@@ -3788,7 +3818,7 @@ class TextColumn extends TextBaseColumn {
3788
3818
  }
3789
3819
  class ByteaColumn extends ColumnType {
3790
3820
  constructor(schema) {
3791
- super(schema, schema.buffer);
3821
+ super(schema, schema.buffer());
3792
3822
  this.dataType = "bytea";
3793
3823
  this.operators = Operators.text;
3794
3824
  }
@@ -3798,7 +3828,7 @@ class ByteaColumn extends ColumnType {
3798
3828
  }
3799
3829
  class PointColumn extends ColumnType {
3800
3830
  constructor(schema) {
3801
- super(schema, schema.stringSchema);
3831
+ super(schema, schema.stringSchema());
3802
3832
  this.dataType = "point";
3803
3833
  this.operators = Operators.text;
3804
3834
  }
@@ -3808,7 +3838,7 @@ class PointColumn extends ColumnType {
3808
3838
  }
3809
3839
  class LineColumn extends ColumnType {
3810
3840
  constructor(schema) {
3811
- super(schema, schema.stringSchema);
3841
+ super(schema, schema.stringSchema());
3812
3842
  this.dataType = "line";
3813
3843
  this.operators = Operators.text;
3814
3844
  }
@@ -3818,7 +3848,7 @@ class LineColumn extends ColumnType {
3818
3848
  }
3819
3849
  class LsegColumn extends ColumnType {
3820
3850
  constructor(schema) {
3821
- super(schema, schema.stringSchema);
3851
+ super(schema, schema.stringSchema());
3822
3852
  this.dataType = "lseg";
3823
3853
  this.operators = Operators.text;
3824
3854
  }
@@ -3828,7 +3858,7 @@ class LsegColumn extends ColumnType {
3828
3858
  }
3829
3859
  class BoxColumn extends ColumnType {
3830
3860
  constructor(schema) {
3831
- super(schema, schema.stringSchema);
3861
+ super(schema, schema.stringSchema());
3832
3862
  this.dataType = "box";
3833
3863
  this.operators = Operators.text;
3834
3864
  }
@@ -3838,7 +3868,7 @@ class BoxColumn extends ColumnType {
3838
3868
  }
3839
3869
  class PathColumn extends ColumnType {
3840
3870
  constructor(schema) {
3841
- super(schema, schema.stringSchema);
3871
+ super(schema, schema.stringSchema());
3842
3872
  this.dataType = "path";
3843
3873
  this.operators = Operators.text;
3844
3874
  }
@@ -3848,7 +3878,7 @@ class PathColumn extends ColumnType {
3848
3878
  }
3849
3879
  class PolygonColumn extends ColumnType {
3850
3880
  constructor(schema) {
3851
- super(schema, schema.stringSchema);
3881
+ super(schema, schema.stringSchema());
3852
3882
  this.dataType = "polygon";
3853
3883
  this.operators = Operators.text;
3854
3884
  }
@@ -3858,7 +3888,7 @@ class PolygonColumn extends ColumnType {
3858
3888
  }
3859
3889
  class CircleColumn extends ColumnType {
3860
3890
  constructor(schema) {
3861
- super(schema, schema.stringSchema);
3891
+ super(schema, schema.stringSchema());
3862
3892
  this.dataType = "circle";
3863
3893
  this.operators = Operators.text;
3864
3894
  }
@@ -3868,7 +3898,7 @@ class CircleColumn extends ColumnType {
3868
3898
  }
3869
3899
  class MoneyColumn extends NumberBaseColumn {
3870
3900
  constructor(schema) {
3871
- super(schema, schema.stringSchema);
3901
+ super(schema, schema.stringSchema());
3872
3902
  this.dataType = "money";
3873
3903
  this.parseFn = Object.assign(
3874
3904
  function(input) {
@@ -3885,7 +3915,7 @@ class MoneyColumn extends NumberBaseColumn {
3885
3915
  }
3886
3916
  class CidrColumn extends ColumnType {
3887
3917
  constructor(schema) {
3888
- super(schema, schema.stringSchema);
3918
+ super(schema, schema.stringSchema());
3889
3919
  this.dataType = "cidr";
3890
3920
  this.operators = Operators.text;
3891
3921
  }
@@ -3895,7 +3925,7 @@ class CidrColumn extends ColumnType {
3895
3925
  }
3896
3926
  class InetColumn extends ColumnType {
3897
3927
  constructor(schema) {
3898
- super(schema, schema.stringSchema);
3928
+ super(schema, schema.stringSchema());
3899
3929
  this.dataType = "inet";
3900
3930
  this.operators = Operators.text;
3901
3931
  }
@@ -3905,7 +3935,7 @@ class InetColumn extends ColumnType {
3905
3935
  }
3906
3936
  class MacAddrColumn extends ColumnType {
3907
3937
  constructor(schema) {
3908
- super(schema, schema.stringSchema);
3938
+ super(schema, schema.stringSchema());
3909
3939
  this.dataType = "macaddr";
3910
3940
  this.operators = Operators.text;
3911
3941
  }
@@ -3915,7 +3945,7 @@ class MacAddrColumn extends ColumnType {
3915
3945
  }
3916
3946
  class MacAddr8Column extends ColumnType {
3917
3947
  constructor(schema) {
3918
- super(schema, schema.stringSchema);
3948
+ super(schema, schema.stringSchema());
3919
3949
  this.dataType = "macaddr8";
3920
3950
  this.operators = Operators.text;
3921
3951
  }
@@ -3961,7 +3991,7 @@ class BitVaryingColumn extends ColumnType {
3961
3991
  }
3962
3992
  class TsVectorColumn extends ColumnType {
3963
3993
  constructor(schema, defaultLanguage = orchidCore.getDefaultLanguage()) {
3964
- super(schema, schema.stringSchema);
3994
+ super(schema, schema.stringSchema());
3965
3995
  this.defaultLanguage = defaultLanguage;
3966
3996
  this.dataType = "tsvector";
3967
3997
  this.operators = Operators.text;
@@ -4015,7 +4045,7 @@ class TsVectorColumn extends ColumnType {
4015
4045
  }
4016
4046
  class TsQueryColumn extends ColumnType {
4017
4047
  constructor(schema) {
4018
- super(schema, schema.stringSchema);
4048
+ super(schema, schema.stringSchema());
4019
4049
  this.dataType = "tsquery";
4020
4050
  this.operators = Operators.text;
4021
4051
  }
@@ -4027,7 +4057,7 @@ const uuidDefaultSQL = "gen_random_uuid()";
4027
4057
  const uuidDefault = new RawSQL(uuidDefaultSQL);
4028
4058
  class UUIDColumn extends ColumnType {
4029
4059
  constructor(schema) {
4030
- super(schema, schema.uuid);
4060
+ super(schema, schema.uuid());
4031
4061
  this.dataType = "uuid";
4032
4062
  this.operators = Operators.text;
4033
4063
  }
@@ -4050,7 +4080,7 @@ class UUIDColumn extends ColumnType {
4050
4080
  }
4051
4081
  class XMLColumn extends ColumnType {
4052
4082
  constructor(schema) {
4053
- super(schema, schema.stringSchema);
4083
+ super(schema, schema.stringSchema());
4054
4084
  this.dataType = "xml";
4055
4085
  this.operators = Operators.text;
4056
4086
  }
@@ -4071,7 +4101,7 @@ class CitextColumn extends TextBaseColumn {
4071
4101
 
4072
4102
  class BooleanColumn extends ColumnType {
4073
4103
  constructor(schema) {
4074
- super(schema, schema.boolean);
4104
+ super(schema, schema.boolean());
4075
4105
  this.dataType = "boolean";
4076
4106
  this.operators = Operators.boolean;
4077
4107
  this.parseItem = (input) => input[0] === "t";
@@ -4083,7 +4113,12 @@ class BooleanColumn extends ColumnType {
4083
4113
 
4084
4114
  class CustomTypeColumn extends ColumnType {
4085
4115
  constructor(schema, dataType) {
4086
- super(schema, schema.unknown, schema.unknown, schema.unknown);
4116
+ super(
4117
+ schema,
4118
+ schema.unknown(),
4119
+ schema.unknown(),
4120
+ schema.unknown()
4121
+ );
4087
4122
  this.dataType = dataType;
4088
4123
  this.operators = Operators.any;
4089
4124
  this.data.isOfCustomType = true;
@@ -4332,7 +4367,7 @@ const makeColumnTypes = (schema) => {
4332
4367
  RawSQL.prototype.columnTypes = makeColumnTypes;
4333
4368
 
4334
4369
  class VirtualColumn extends ColumnType {
4335
- constructor(schema, inputSchema = schema.never) {
4370
+ constructor(schema, inputSchema = schema.never()) {
4336
4371
  super(schema, inputSchema);
4337
4372
  this.dataType = "";
4338
4373
  this.operators = Operators.any;
@@ -4344,7 +4379,7 @@ class VirtualColumn extends ColumnType {
4344
4379
 
4345
4380
  class UnknownColumn extends VirtualColumn {
4346
4381
  constructor(schema) {
4347
- super(schema, schema.unknown);
4382
+ super(schema, schema.unknown());
4348
4383
  }
4349
4384
  }
4350
4385
  RawSQL.prototype._type = new UnknownColumn(defaultSchemaConfig);
@@ -6167,12 +6202,6 @@ class Having {
6167
6202
  * // HAVING count(*) >= 10
6168
6203
  * ```
6169
6204
  *
6170
- * Alternatively, it accepts a raw SQL template:
6171
- *
6172
- * ```ts
6173
- * db.table.having`count(*) >= ${10}`;
6174
- * ```
6175
- *
6176
6205
  * Multiple having conditions will be combined with `AND`:
6177
6206
  *
6178
6207
  * ```ts
@@ -6218,11 +6247,23 @@ class Having {
6218
6247
  return pushQueryValue(
6219
6248
  q,
6220
6249
  "having",
6221
- "raw" in args[0] ? args : args.map(
6250
+ args.map(
6222
6251
  (arg) => arg(q).q.expr
6223
6252
  )
6224
6253
  );
6225
6254
  }
6255
+ /**
6256
+ * Provide SQL expression for the `HAVING` SQL statement:
6257
+ *
6258
+ * ```ts
6259
+ * db.table.having`count(*) >= ${10}`;
6260
+ * ```
6261
+ *
6262
+ * @param args - SQL expression
6263
+ */
6264
+ havingSql(...args) {
6265
+ return pushQueryValue(this.clone(), "having", args);
6266
+ }
6226
6267
  }
6227
6268
 
6228
6269
  const before = (q, key, cb) => pushQueryValue(q, `before${key}`, cb);
@@ -6475,29 +6516,29 @@ class QueryBase {
6475
6516
  }
6476
6517
 
6477
6518
  const _queryWhere = (q, args) => {
6478
- if (Array.isArray(args[0])) {
6479
- return pushQueryValue(
6480
- q,
6481
- "and",
6482
- new RawSQL(args)
6483
- );
6484
- }
6485
6519
  return pushQueryArray(
6486
6520
  q,
6487
6521
  "and",
6488
6522
  args
6489
6523
  );
6490
6524
  };
6525
+ const _queryWhereSql = (q, args) => {
6526
+ return pushQueryValue(
6527
+ q,
6528
+ "and",
6529
+ sqlQueryArgsToExpression(args)
6530
+ );
6531
+ };
6491
6532
  const _queryWhereNot = (q, args) => {
6492
- if (Array.isArray(args[0])) {
6493
- return pushQueryValue(q, "and", {
6494
- NOT: new RawSQL(args)
6495
- });
6496
- }
6497
6533
  return pushQueryValue(q, "and", {
6498
6534
  NOT: args
6499
6535
  });
6500
6536
  };
6537
+ const _queryWhereNotSql = (q, args) => {
6538
+ return pushQueryValue(q, "and", {
6539
+ NOT: sqlQueryArgsToExpression(args)
6540
+ });
6541
+ };
6501
6542
  const _queryOr = (q, args) => {
6502
6543
  return pushQueryArray(
6503
6544
  q,
@@ -6580,7 +6621,7 @@ class Where {
6580
6621
  * },
6581
6622
  *
6582
6623
  * // where column equals to raw SQL
6583
- * column: db.table.sql`raw expression`,
6624
+ * column: db.table.sql`sql expression`,
6584
6625
  * });
6585
6626
  * ```
6586
6627
  *
@@ -6629,9 +6670,6 @@ class Where {
6629
6670
  * `where` supports raw SQL:
6630
6671
  *
6631
6672
  * ```ts
6632
- * db.table.where`a = b`;
6633
- *
6634
- * // or
6635
6673
  * db.table.where(db.table.sql`a = b`);
6636
6674
  *
6637
6675
  * // or
@@ -6956,6 +6994,29 @@ class Where {
6956
6994
  args
6957
6995
  );
6958
6996
  }
6997
+ /**
6998
+ * Use a custom SQL expression in `WHERE` statement:
6999
+ *
7000
+ * ```ts
7001
+ * db.table.where`a = b`;
7002
+ *
7003
+ * // or
7004
+ * db.table.where(db.table.sql`a = b`);
7005
+ *
7006
+ * // or
7007
+ * import { raw } from 'orchid-orm';
7008
+ *
7009
+ * db.table.where(raw`a = b`);
7010
+ * ```
7011
+ *
7012
+ * @param args - SQL expression
7013
+ */
7014
+ whereSql(...args) {
7015
+ return _queryWhereSql(
7016
+ this.clone(),
7017
+ args
7018
+ );
7019
+ }
6959
7020
  /**
6960
7021
  * `whereNot` takes the same argument as `where`,
6961
7022
  * multiple conditions are combined with `AND`,
@@ -6977,6 +7038,18 @@ class Where {
6977
7038
  args
6978
7039
  );
6979
7040
  }
7041
+ /**
7042
+ * `whereNot` version accepting SQL expression:
7043
+ *
7044
+ * ```ts
7045
+ * db.table.whereNot`sql expression`
7046
+ * ```
7047
+ *
7048
+ * @param args - SQL expression
7049
+ */
7050
+ whereNotSql(...args) {
7051
+ return _queryWhereNotSql(this.clone(), args);
7052
+ }
6980
7053
  /**
6981
7054
  * `orWhere` is accepting the same arguments as {@link where}, joining arguments with `OR`.
6982
7055
  *
@@ -9014,12 +9087,10 @@ class Update {
9014
9087
  * @param args - raw SQL via a template string or by using a `sql` method
9015
9088
  */
9016
9089
  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]);
9090
+ return _queryUpdateRaw(
9091
+ this.clone(),
9092
+ sqlQueryArgsToExpression(args)
9093
+ );
9023
9094
  }
9024
9095
  /**
9025
9096
  * To make sure that at least one row was updated use `updateOrThrow`:
@@ -10061,7 +10132,7 @@ class QueryMethods {
10061
10132
  * The `find` method is available only for tables which has exactly one primary key.
10062
10133
  * And also it can accept raw SQL template literal, then the primary key is not required.
10063
10134
  *
10064
- * Find record by id, throw [NotFoundError](/guide/error-handling.html) if not found:
10135
+ * Finds a record by id, throws {@link NotFoundError} if not found:
10065
10136
  *
10066
10137
  * ```ts
10067
10138
  * await db.table.find(1);
@@ -10074,13 +10145,9 @@ class QueryMethods {
10074
10145
  * `;
10075
10146
  * ```
10076
10147
  *
10077
- * @param args - primary key value to find by, or a raw SQL
10148
+ * @param value - primary key value to find by
10078
10149
  */
10079
- find(...args) {
10080
- let [value] = args;
10081
- if (Array.isArray(value)) {
10082
- value = new RawSQL(args);
10083
- }
10150
+ find(value) {
10084
10151
  const q = this.clone();
10085
10152
  if (value === null || value === void 0) {
10086
10153
  throw new OrchidOrmInternalError(
@@ -10097,22 +10164,54 @@ class QueryMethods {
10097
10164
  );
10098
10165
  }
10099
10166
  /**
10100
- * Find a single record by the primary key (id), adds `LIMIT 1`, can accept a raw SQL.
10167
+ * Finds a single record with a given SQL, throws {@link NotFoundError} if not found:
10168
+ *
10169
+ * ```ts
10170
+ * await db.user.find`
10171
+ * age = ${age} AND
10172
+ * name = ${name}
10173
+ * `;
10174
+ * ```
10175
+ *
10176
+ * @param args - SQL expression
10177
+ */
10178
+ findBySql(...args) {
10179
+ const q = this.clone();
10180
+ return _queryTake(_queryWhereSql(q, args));
10181
+ }
10182
+ /**
10183
+ * Find a single record by the primary key (id), adds `LIMIT 1`.
10101
10184
  * Returns `undefined` when not found.
10102
10185
  *
10103
10186
  * ```ts
10104
10187
  * const result: TableType | undefined = await db.table.find(123);
10105
10188
  * ```
10106
10189
  *
10107
- * @param args - primary key value to find by, or a raw SQL
10190
+ * @param value - primary key value to find by, or a raw SQL
10108
10191
  */
10109
- findOptional(...args) {
10192
+ findOptional(value) {
10193
+ return _queryTakeOptional(this.find(value));
10194
+ }
10195
+ /**
10196
+ * Finds a single record with a given SQL.
10197
+ * Returns `undefined` when not found.
10198
+ *
10199
+ * ```ts
10200
+ * await db.user.find`
10201
+ * age = ${age} AND
10202
+ * name = ${name}
10203
+ * `;
10204
+ * ```
10205
+ *
10206
+ * @param args - SQL expression
10207
+ */
10208
+ findBySqlOptional(...args) {
10110
10209
  return _queryTakeOptional(
10111
- this.find(...args)
10210
+ this.findBySql(...args)
10112
10211
  );
10113
10212
  }
10114
10213
  /**
10115
- * The same as `where(conditions).take()`, it will filter records and add a `LIMIT 1`.
10214
+ * The same as `where(conditions).take()`, takes the same arguments as {@link Where.where}, it will filter records and add a `LIMIT 1`.
10116
10215
  * Throws `NotFoundError` if not found.
10117
10216
  *
10118
10217
  * ```ts
@@ -10246,7 +10345,7 @@ class QueryMethods {
10246
10345
  /**
10247
10346
  * Adds an order by clause to the query.
10248
10347
  *
10249
- * Takes one or more arguments, each argument can be a column name, an object, or a raw expression.
10348
+ * Takes one or more arguments, each argument can be a column name or an object.
10250
10349
  *
10251
10350
  * ```ts
10252
10351
  * db.table.order('id', 'name'); // ASC by default
@@ -10258,11 +10357,6 @@ class QueryMethods {
10258
10357
  * name: 'ASC NULLS FIRST',
10259
10358
  * age: 'DESC NULLS LAST',
10260
10359
  * });
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
10360
  * ```
10267
10361
  *
10268
10362
  * `order` can refer to the values returned from `select` sub-queries (unlike `where` which cannot).
@@ -10281,22 +10375,35 @@ class QueryMethods {
10281
10375
  * });
10282
10376
  * ```
10283
10377
  *
10284
- * @param args - column name(s), raw SQL, or an object with column names and sort directions.
10378
+ * @param args - column name(s) or an object with column names and sort directions.
10285
10379
  */
10286
10380
  order(...args) {
10287
- if (Array.isArray(args[0])) {
10288
- return pushQueryValue(
10289
- this.clone(),
10290
- "order",
10291
- new RawSQL(args)
10292
- );
10293
- }
10294
10381
  return pushQueryArray(
10295
10382
  this.clone(),
10296
10383
  "order",
10297
10384
  args
10298
10385
  );
10299
10386
  }
10387
+ /**
10388
+ * Order by SQL expression
10389
+ *
10390
+ * Order by raw SQL expression.
10391
+ *
10392
+ * ```ts
10393
+ * db.table.order`raw sql`;
10394
+ * // or
10395
+ * db.table.order(db.table.sql`raw sql`);
10396
+ * ```
10397
+ *
10398
+ * @param args - SQL expression
10399
+ */
10400
+ orderSql(...args) {
10401
+ return pushQueryValue(
10402
+ this.clone(),
10403
+ "order",
10404
+ sqlQueryArgsToExpression(args)
10405
+ );
10406
+ }
10300
10407
  /**
10301
10408
  * Adds a limit clause to the query.
10302
10409
  *
@@ -11144,6 +11251,8 @@ exports._queryUpdateRaw = _queryUpdateRaw;
11144
11251
  exports._queryWhere = _queryWhere;
11145
11252
  exports._queryWhereIn = _queryWhereIn;
11146
11253
  exports._queryWhereNot = _queryWhereNot;
11254
+ exports._queryWhereNotSql = _queryWhereNotSql;
11255
+ exports._queryWhereSql = _queryWhereSql;
11147
11256
  exports.addComputedColumns = addComputedColumns;
11148
11257
  exports.addParserForRawExpression = addParserForRawExpression;
11149
11258
  exports.addParserForSelectItem = addParserForSelectItem;
@@ -11197,6 +11306,7 @@ exports.pushQueryOn = pushQueryOn;
11197
11306
  exports.pushQueryOrOn = pushQueryOrOn;
11198
11307
  exports.pushQueryValue = pushQueryValue;
11199
11308
  exports.queryFrom = queryFrom;
11309
+ exports.queryFromSql = queryFromSql;
11200
11310
  exports.queryJson = queryJson;
11201
11311
  exports.queryMethodByReturnType = queryMethodByReturnType;
11202
11312
  exports.queryTypeWithLimitOne = queryTypeWithLimitOne;
@@ -11212,6 +11322,7 @@ exports.setParserForSelectedString = setParserForSelectedString;
11212
11322
  exports.setQueryObjectValue = setQueryObjectValue;
11213
11323
  exports.setQueryOperators = setQueryOperators;
11214
11324
  exports.simplifyColumnDefault = simplifyColumnDefault;
11325
+ exports.sqlQueryArgsToExpression = sqlQueryArgsToExpression;
11215
11326
  exports.templateLiteralToSQL = templateLiteralToSQL;
11216
11327
  exports.testTransaction = testTransaction;
11217
11328
  exports.throwIfNoWhere = throwIfNoWhere;