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.mjs CHANGED
@@ -96,6 +96,9 @@ function raw(...args) {
96
96
  return isTemplateLiteralArgs(args) ? new RawSQL(args) : typeof args[0] === "function" ? new DynamicRawSQL(args[0]) : new RawSQL(args[0].raw, args[0].values);
97
97
  }
98
98
  const countSelect = [new RawSQL("count(*)")];
99
+ function sqlQueryArgsToExpression(args) {
100
+ return Array.isArray(args[0]) ? new RawSQL(args) : args[0];
101
+ }
99
102
 
100
103
  var __defProp$g = Object.defineProperty;
101
104
  var __defProps$a = Object.defineProperties;
@@ -826,7 +829,7 @@ class JSONColumn extends ColumnType {
826
829
  JSONColumn.prototype.encodeFn = JSON.stringify;
827
830
  class JSONTextColumn extends ColumnType {
828
831
  constructor(schema) {
829
- super(schema, schema.stringSchema);
832
+ super(schema, schema.stringSchema());
830
833
  this.dataType = "json";
831
834
  this.operators = Operators.text;
832
835
  }
@@ -1478,7 +1481,12 @@ const dateTimeEncode = (input) => {
1478
1481
  const skipDateMethodsFromToCode = { encodeFn: dateTimeEncode };
1479
1482
  class DateBaseColumn extends ColumnType {
1480
1483
  constructor(schema) {
1481
- super(schema, schema.stringNumberDate);
1484
+ super(
1485
+ schema,
1486
+ schema.stringNumberDate(),
1487
+ schema.stringSchema(),
1488
+ schema.stringNumberDate()
1489
+ );
1482
1490
  this.operators = Operators.date;
1483
1491
  this.encodeFn = dateTimeEncode;
1484
1492
  this.asNumber = schema.dateAsNumber;
@@ -1552,7 +1560,7 @@ class TimestampTZColumn extends DateTimeTzBaseClass {
1552
1560
  }
1553
1561
  class TimeColumn extends ColumnType {
1554
1562
  constructor(schema, dateTimePrecision) {
1555
- super(schema, schema.stringSchema);
1563
+ super(schema, schema.stringSchema());
1556
1564
  this.dataType = "time";
1557
1565
  this.operators = Operators.time;
1558
1566
  this.data.dateTimePrecision = dateTimePrecision;
@@ -1570,7 +1578,7 @@ class TimeColumn extends ColumnType {
1570
1578
  }
1571
1579
  class IntervalColumn extends ColumnType {
1572
1580
  constructor(schema, fields, precision) {
1573
- super(schema, schema.timeInterval);
1581
+ super(schema, schema.timeInterval());
1574
1582
  this.dataType = "interval";
1575
1583
  this.operators = Operators.date;
1576
1584
  this.data.fields = fields;
@@ -1743,10 +1751,20 @@ const defaultSchemaConfig = {
1743
1751
  array(item) {
1744
1752
  return new ArrayColumn(defaultSchemaConfig, item, void 0);
1745
1753
  },
1754
+ boolean: noop,
1755
+ buffer: noop,
1756
+ unknown: noop,
1757
+ never: noop,
1758
+ stringSchema: noop,
1746
1759
  stringMin: noop,
1747
1760
  stringMax: noop,
1748
1761
  stringMinMax: noop,
1762
+ number: noop,
1763
+ int: noop,
1764
+ stringNumberDate: noop,
1765
+ timeInterval: noop,
1749
1766
  bit: noop,
1767
+ uuid: noop,
1750
1768
  nullable() {
1751
1769
  return setColumnData(this, "isNullable", true);
1752
1770
  },
@@ -2108,31 +2126,28 @@ class AsMethods {
2108
2126
  }
2109
2127
  }
2110
2128
 
2111
- function queryFrom(self, args) {
2112
- if (Array.isArray(args[0])) {
2113
- return queryFrom(self, [
2114
- new RawSQL(args)
2115
- ]);
2116
- }
2129
+ function queryFrom(self, arg, options) {
2117
2130
  const data = self.q;
2118
- if (typeof args[0] === "string") {
2119
- data.as || (data.as = args[0]);
2120
- } else if (!isExpression(args[0])) {
2121
- const q = args[0];
2131
+ if (typeof arg === "string") {
2132
+ data.as || (data.as = arg);
2133
+ } else if (!isExpression(arg)) {
2134
+ const q = arg;
2122
2135
  data.as || (data.as = q.q.as || q.table || "t");
2123
- data.shape = getShapeFromSelect(
2124
- args[0],
2125
- true
2126
- );
2136
+ data.shape = getShapeFromSelect(arg, true);
2127
2137
  data.parsers = q.q.parsers;
2128
2138
  } else {
2129
2139
  data.as || (data.as = "t");
2130
2140
  }
2131
- const options = args[1];
2132
2141
  if (options == null ? void 0 : options.only) {
2133
2142
  data.fromOnly = options.only;
2134
2143
  }
2135
- data.from = args[0];
2144
+ data.from = arg;
2145
+ return self;
2146
+ }
2147
+ function queryFromSql(self, args) {
2148
+ const data = self.q;
2149
+ data.as || (data.as = "t");
2150
+ data.from = sqlQueryArgsToExpression(args);
2136
2151
  return self;
2137
2152
  }
2138
2153
  class From {
@@ -2143,13 +2158,6 @@ class From {
2143
2158
  * // accepts sub-query:
2144
2159
  * db.table.from(Otherdb.table.select('foo', 'bar'));
2145
2160
  *
2146
- * // accepts raw sql by template literal:
2147
- * const value = 123;
2148
- * db.table.from`value = ${value}`;
2149
- *
2150
- * // accepts raw sql:
2151
- * db.table.from(db.table.sql`value = ${value}`);
2152
- *
2153
2161
  * // accepts alias of `WITH` expression:
2154
2162
  * q.with('foo', Otherdb.table.select('id', 'name')).from('foo');
2155
2163
  * ```
@@ -2162,10 +2170,29 @@ class From {
2162
2170
  * });
2163
2171
  * ```
2164
2172
  *
2165
- * @param args - query, raw SQL, name of CTE table, or a template string
2173
+ * @param arg - query or name of CTE table
2174
+ * @param options - { only: true } for SQL `ONLY` keyword
2166
2175
  */
2167
- from(...args) {
2176
+ from(arg, options) {
2168
2177
  return queryFrom(
2178
+ this.clone(),
2179
+ arg,
2180
+ options
2181
+ );
2182
+ }
2183
+ /**
2184
+ * Set the `FROM` value with custom SQL:
2185
+ *
2186
+ * ```ts
2187
+ * const value = 123;
2188
+ * db.table.from`value = ${value}`;
2189
+ * db.table.from(db.table.sql`value = ${value}`);
2190
+ * ```
2191
+ *
2192
+ * @param args - SQL expression
2193
+ */
2194
+ fromSql(...args) {
2195
+ return queryFromSql(
2169
2196
  this.clone(),
2170
2197
  args
2171
2198
  );
@@ -2173,7 +2200,7 @@ class From {
2173
2200
  }
2174
2201
 
2175
2202
  function queryWrap(self, query, as = "t") {
2176
- return _queryAs(queryFrom(query, [self]), as);
2203
+ return _queryAs(queryFrom(query, self), as);
2177
2204
  }
2178
2205
 
2179
2206
  function queryJson(self, coalesce) {
@@ -3512,19 +3539,19 @@ class NumberBaseColumn extends ColumnType {
3512
3539
  }
3513
3540
  class IntegerBaseColumn extends NumberBaseColumn {
3514
3541
  constructor(schema) {
3515
- super(schema, schema.int);
3542
+ super(schema, schema.int());
3516
3543
  this.data.int = true;
3517
3544
  }
3518
3545
  }
3519
3546
  class NumberAsStringBaseColumn extends ColumnType {
3520
3547
  constructor(schema) {
3521
- super(schema, schema.stringSchema);
3548
+ super(schema, schema.stringSchema());
3522
3549
  this.operators = Operators.number;
3523
3550
  }
3524
3551
  }
3525
3552
  class DecimalColumn extends ColumnType {
3526
3553
  constructor(schema, numericPrecision, numericScale) {
3527
- super(schema, schema.stringSchema);
3554
+ super(schema, schema.stringSchema());
3528
3555
  this.operators = Operators.number;
3529
3556
  this.dataType = "decimal";
3530
3557
  this.data.numericPrecision = numericPrecision;
@@ -3597,7 +3624,7 @@ class BigIntColumn extends NumberAsStringBaseColumn {
3597
3624
  }
3598
3625
  class RealColumn extends NumberBaseColumn {
3599
3626
  constructor(schema) {
3600
- super(schema, schema.number);
3627
+ super(schema, schema.number());
3601
3628
  this.dataType = "real";
3602
3629
  this.parseItem = parseFloat;
3603
3630
  }
@@ -3683,14 +3710,17 @@ var __spreadValues$9 = (a, b) => {
3683
3710
  };
3684
3711
  var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
3685
3712
  class TextBaseColumn extends ColumnType {
3686
- constructor(schema, schemaType = schema.stringSchema) {
3713
+ constructor(schema, schemaType = schema.stringSchema()) {
3687
3714
  super(schema, schemaType);
3688
3715
  this.operators = Operators.text;
3689
3716
  }
3690
3717
  }
3691
3718
  class LimitedTextBaseColumn extends TextBaseColumn {
3692
3719
  constructor(schema, limit) {
3693
- super(schema, limit ? schema.stringMax(limit) : schema.stringSchema);
3720
+ super(
3721
+ schema,
3722
+ limit ? schema.stringMax(limit) : schema.stringSchema()
3723
+ );
3694
3724
  this.data.maxChars = limit;
3695
3725
  }
3696
3726
  toSQL() {
@@ -3773,7 +3803,7 @@ const textColumnToCode = (column, t) => {
3773
3803
  `${column.dataType}(${args})${stringDataToCode(data)}`
3774
3804
  );
3775
3805
  };
3776
- const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema;
3806
+ const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema();
3777
3807
  class TextColumn extends TextBaseColumn {
3778
3808
  constructor(schema, min, max) {
3779
3809
  super(schema, minMaxToSchema(schema, min, max));
@@ -3786,7 +3816,7 @@ class TextColumn extends TextBaseColumn {
3786
3816
  }
3787
3817
  class ByteaColumn extends ColumnType {
3788
3818
  constructor(schema) {
3789
- super(schema, schema.buffer);
3819
+ super(schema, schema.buffer());
3790
3820
  this.dataType = "bytea";
3791
3821
  this.operators = Operators.text;
3792
3822
  }
@@ -3796,7 +3826,7 @@ class ByteaColumn extends ColumnType {
3796
3826
  }
3797
3827
  class PointColumn extends ColumnType {
3798
3828
  constructor(schema) {
3799
- super(schema, schema.stringSchema);
3829
+ super(schema, schema.stringSchema());
3800
3830
  this.dataType = "point";
3801
3831
  this.operators = Operators.text;
3802
3832
  }
@@ -3806,7 +3836,7 @@ class PointColumn extends ColumnType {
3806
3836
  }
3807
3837
  class LineColumn extends ColumnType {
3808
3838
  constructor(schema) {
3809
- super(schema, schema.stringSchema);
3839
+ super(schema, schema.stringSchema());
3810
3840
  this.dataType = "line";
3811
3841
  this.operators = Operators.text;
3812
3842
  }
@@ -3816,7 +3846,7 @@ class LineColumn extends ColumnType {
3816
3846
  }
3817
3847
  class LsegColumn extends ColumnType {
3818
3848
  constructor(schema) {
3819
- super(schema, schema.stringSchema);
3849
+ super(schema, schema.stringSchema());
3820
3850
  this.dataType = "lseg";
3821
3851
  this.operators = Operators.text;
3822
3852
  }
@@ -3826,7 +3856,7 @@ class LsegColumn extends ColumnType {
3826
3856
  }
3827
3857
  class BoxColumn extends ColumnType {
3828
3858
  constructor(schema) {
3829
- super(schema, schema.stringSchema);
3859
+ super(schema, schema.stringSchema());
3830
3860
  this.dataType = "box";
3831
3861
  this.operators = Operators.text;
3832
3862
  }
@@ -3836,7 +3866,7 @@ class BoxColumn extends ColumnType {
3836
3866
  }
3837
3867
  class PathColumn extends ColumnType {
3838
3868
  constructor(schema) {
3839
- super(schema, schema.stringSchema);
3869
+ super(schema, schema.stringSchema());
3840
3870
  this.dataType = "path";
3841
3871
  this.operators = Operators.text;
3842
3872
  }
@@ -3846,7 +3876,7 @@ class PathColumn extends ColumnType {
3846
3876
  }
3847
3877
  class PolygonColumn extends ColumnType {
3848
3878
  constructor(schema) {
3849
- super(schema, schema.stringSchema);
3879
+ super(schema, schema.stringSchema());
3850
3880
  this.dataType = "polygon";
3851
3881
  this.operators = Operators.text;
3852
3882
  }
@@ -3856,7 +3886,7 @@ class PolygonColumn extends ColumnType {
3856
3886
  }
3857
3887
  class CircleColumn extends ColumnType {
3858
3888
  constructor(schema) {
3859
- super(schema, schema.stringSchema);
3889
+ super(schema, schema.stringSchema());
3860
3890
  this.dataType = "circle";
3861
3891
  this.operators = Operators.text;
3862
3892
  }
@@ -3866,7 +3896,7 @@ class CircleColumn extends ColumnType {
3866
3896
  }
3867
3897
  class MoneyColumn extends NumberBaseColumn {
3868
3898
  constructor(schema) {
3869
- super(schema, schema.stringSchema);
3899
+ super(schema, schema.stringSchema());
3870
3900
  this.dataType = "money";
3871
3901
  this.parseFn = Object.assign(
3872
3902
  function(input) {
@@ -3883,7 +3913,7 @@ class MoneyColumn extends NumberBaseColumn {
3883
3913
  }
3884
3914
  class CidrColumn extends ColumnType {
3885
3915
  constructor(schema) {
3886
- super(schema, schema.stringSchema);
3916
+ super(schema, schema.stringSchema());
3887
3917
  this.dataType = "cidr";
3888
3918
  this.operators = Operators.text;
3889
3919
  }
@@ -3893,7 +3923,7 @@ class CidrColumn extends ColumnType {
3893
3923
  }
3894
3924
  class InetColumn extends ColumnType {
3895
3925
  constructor(schema) {
3896
- super(schema, schema.stringSchema);
3926
+ super(schema, schema.stringSchema());
3897
3927
  this.dataType = "inet";
3898
3928
  this.operators = Operators.text;
3899
3929
  }
@@ -3903,7 +3933,7 @@ class InetColumn extends ColumnType {
3903
3933
  }
3904
3934
  class MacAddrColumn extends ColumnType {
3905
3935
  constructor(schema) {
3906
- super(schema, schema.stringSchema);
3936
+ super(schema, schema.stringSchema());
3907
3937
  this.dataType = "macaddr";
3908
3938
  this.operators = Operators.text;
3909
3939
  }
@@ -3913,7 +3943,7 @@ class MacAddrColumn extends ColumnType {
3913
3943
  }
3914
3944
  class MacAddr8Column extends ColumnType {
3915
3945
  constructor(schema) {
3916
- super(schema, schema.stringSchema);
3946
+ super(schema, schema.stringSchema());
3917
3947
  this.dataType = "macaddr8";
3918
3948
  this.operators = Operators.text;
3919
3949
  }
@@ -3959,7 +3989,7 @@ class BitVaryingColumn extends ColumnType {
3959
3989
  }
3960
3990
  class TsVectorColumn extends ColumnType {
3961
3991
  constructor(schema, defaultLanguage = getDefaultLanguage()) {
3962
- super(schema, schema.stringSchema);
3992
+ super(schema, schema.stringSchema());
3963
3993
  this.defaultLanguage = defaultLanguage;
3964
3994
  this.dataType = "tsvector";
3965
3995
  this.operators = Operators.text;
@@ -4013,7 +4043,7 @@ class TsVectorColumn extends ColumnType {
4013
4043
  }
4014
4044
  class TsQueryColumn extends ColumnType {
4015
4045
  constructor(schema) {
4016
- super(schema, schema.stringSchema);
4046
+ super(schema, schema.stringSchema());
4017
4047
  this.dataType = "tsquery";
4018
4048
  this.operators = Operators.text;
4019
4049
  }
@@ -4025,7 +4055,7 @@ const uuidDefaultSQL = "gen_random_uuid()";
4025
4055
  const uuidDefault = new RawSQL(uuidDefaultSQL);
4026
4056
  class UUIDColumn extends ColumnType {
4027
4057
  constructor(schema) {
4028
- super(schema, schema.uuid);
4058
+ super(schema, schema.uuid());
4029
4059
  this.dataType = "uuid";
4030
4060
  this.operators = Operators.text;
4031
4061
  }
@@ -4048,7 +4078,7 @@ class UUIDColumn extends ColumnType {
4048
4078
  }
4049
4079
  class XMLColumn extends ColumnType {
4050
4080
  constructor(schema) {
4051
- super(schema, schema.stringSchema);
4081
+ super(schema, schema.stringSchema());
4052
4082
  this.dataType = "xml";
4053
4083
  this.operators = Operators.text;
4054
4084
  }
@@ -4069,7 +4099,7 @@ class CitextColumn extends TextBaseColumn {
4069
4099
 
4070
4100
  class BooleanColumn extends ColumnType {
4071
4101
  constructor(schema) {
4072
- super(schema, schema.boolean);
4102
+ super(schema, schema.boolean());
4073
4103
  this.dataType = "boolean";
4074
4104
  this.operators = Operators.boolean;
4075
4105
  this.parseItem = (input) => input[0] === "t";
@@ -4081,7 +4111,12 @@ class BooleanColumn extends ColumnType {
4081
4111
 
4082
4112
  class CustomTypeColumn extends ColumnType {
4083
4113
  constructor(schema, dataType) {
4084
- super(schema, schema.unknown, schema.unknown, schema.unknown);
4114
+ super(
4115
+ schema,
4116
+ schema.unknown(),
4117
+ schema.unknown(),
4118
+ schema.unknown()
4119
+ );
4085
4120
  this.dataType = dataType;
4086
4121
  this.operators = Operators.any;
4087
4122
  this.data.isOfCustomType = true;
@@ -4330,7 +4365,7 @@ const makeColumnTypes = (schema) => {
4330
4365
  RawSQL.prototype.columnTypes = makeColumnTypes;
4331
4366
 
4332
4367
  class VirtualColumn extends ColumnType {
4333
- constructor(schema, inputSchema = schema.never) {
4368
+ constructor(schema, inputSchema = schema.never()) {
4334
4369
  super(schema, inputSchema);
4335
4370
  this.dataType = "";
4336
4371
  this.operators = Operators.any;
@@ -4342,7 +4377,7 @@ class VirtualColumn extends ColumnType {
4342
4377
 
4343
4378
  class UnknownColumn extends VirtualColumn {
4344
4379
  constructor(schema) {
4345
- super(schema, schema.unknown);
4380
+ super(schema, schema.unknown());
4346
4381
  }
4347
4382
  }
4348
4383
  RawSQL.prototype._type = new UnknownColumn(defaultSchemaConfig);
@@ -6165,12 +6200,6 @@ class Having {
6165
6200
  * // HAVING count(*) >= 10
6166
6201
  * ```
6167
6202
  *
6168
- * Alternatively, it accepts a raw SQL template:
6169
- *
6170
- * ```ts
6171
- * db.table.having`count(*) >= ${10}`;
6172
- * ```
6173
- *
6174
6203
  * Multiple having conditions will be combined with `AND`:
6175
6204
  *
6176
6205
  * ```ts
@@ -6216,11 +6245,23 @@ class Having {
6216
6245
  return pushQueryValue(
6217
6246
  q,
6218
6247
  "having",
6219
- "raw" in args[0] ? args : args.map(
6248
+ args.map(
6220
6249
  (arg) => arg(q).q.expr
6221
6250
  )
6222
6251
  );
6223
6252
  }
6253
+ /**
6254
+ * Provide SQL expression for the `HAVING` SQL statement:
6255
+ *
6256
+ * ```ts
6257
+ * db.table.having`count(*) >= ${10}`;
6258
+ * ```
6259
+ *
6260
+ * @param args - SQL expression
6261
+ */
6262
+ havingSql(...args) {
6263
+ return pushQueryValue(this.clone(), "having", args);
6264
+ }
6224
6265
  }
6225
6266
 
6226
6267
  const before = (q, key, cb) => pushQueryValue(q, `before${key}`, cb);
@@ -6473,29 +6514,29 @@ class QueryBase {
6473
6514
  }
6474
6515
 
6475
6516
  const _queryWhere = (q, args) => {
6476
- if (Array.isArray(args[0])) {
6477
- return pushQueryValue(
6478
- q,
6479
- "and",
6480
- new RawSQL(args)
6481
- );
6482
- }
6483
6517
  return pushQueryArray(
6484
6518
  q,
6485
6519
  "and",
6486
6520
  args
6487
6521
  );
6488
6522
  };
6523
+ const _queryWhereSql = (q, args) => {
6524
+ return pushQueryValue(
6525
+ q,
6526
+ "and",
6527
+ sqlQueryArgsToExpression(args)
6528
+ );
6529
+ };
6489
6530
  const _queryWhereNot = (q, args) => {
6490
- if (Array.isArray(args[0])) {
6491
- return pushQueryValue(q, "and", {
6492
- NOT: new RawSQL(args)
6493
- });
6494
- }
6495
6531
  return pushQueryValue(q, "and", {
6496
6532
  NOT: args
6497
6533
  });
6498
6534
  };
6535
+ const _queryWhereNotSql = (q, args) => {
6536
+ return pushQueryValue(q, "and", {
6537
+ NOT: sqlQueryArgsToExpression(args)
6538
+ });
6539
+ };
6499
6540
  const _queryOr = (q, args) => {
6500
6541
  return pushQueryArray(
6501
6542
  q,
@@ -6578,7 +6619,7 @@ class Where {
6578
6619
  * },
6579
6620
  *
6580
6621
  * // where column equals to raw SQL
6581
- * column: db.table.sql`raw expression`,
6622
+ * column: db.table.sql`sql expression`,
6582
6623
  * });
6583
6624
  * ```
6584
6625
  *
@@ -6627,9 +6668,6 @@ class Where {
6627
6668
  * `where` supports raw SQL:
6628
6669
  *
6629
6670
  * ```ts
6630
- * db.table.where`a = b`;
6631
- *
6632
- * // or
6633
6671
  * db.table.where(db.table.sql`a = b`);
6634
6672
  *
6635
6673
  * // or
@@ -6954,6 +6992,29 @@ class Where {
6954
6992
  args
6955
6993
  );
6956
6994
  }
6995
+ /**
6996
+ * Use a custom SQL expression in `WHERE` statement:
6997
+ *
6998
+ * ```ts
6999
+ * db.table.where`a = b`;
7000
+ *
7001
+ * // or
7002
+ * db.table.where(db.table.sql`a = b`);
7003
+ *
7004
+ * // or
7005
+ * import { raw } from 'orchid-orm';
7006
+ *
7007
+ * db.table.where(raw`a = b`);
7008
+ * ```
7009
+ *
7010
+ * @param args - SQL expression
7011
+ */
7012
+ whereSql(...args) {
7013
+ return _queryWhereSql(
7014
+ this.clone(),
7015
+ args
7016
+ );
7017
+ }
6957
7018
  /**
6958
7019
  * `whereNot` takes the same argument as `where`,
6959
7020
  * multiple conditions are combined with `AND`,
@@ -6975,6 +7036,18 @@ class Where {
6975
7036
  args
6976
7037
  );
6977
7038
  }
7039
+ /**
7040
+ * `whereNot` version accepting SQL expression:
7041
+ *
7042
+ * ```ts
7043
+ * db.table.whereNot`sql expression`
7044
+ * ```
7045
+ *
7046
+ * @param args - SQL expression
7047
+ */
7048
+ whereNotSql(...args) {
7049
+ return _queryWhereNotSql(this.clone(), args);
7050
+ }
6978
7051
  /**
6979
7052
  * `orWhere` is accepting the same arguments as {@link where}, joining arguments with `OR`.
6980
7053
  *
@@ -9012,12 +9085,10 @@ class Update {
9012
9085
  * @param args - raw SQL via a template string or by using a `sql` method
9013
9086
  */
9014
9087
  updateRaw(...args) {
9015
- const q = this.clone();
9016
- if (Array.isArray(args[0])) {
9017
- const sql = new RawSQL(args);
9018
- return _queryUpdateRaw(q, sql);
9019
- }
9020
- return _queryUpdateRaw(q, args[0]);
9088
+ return _queryUpdateRaw(
9089
+ this.clone(),
9090
+ sqlQueryArgsToExpression(args)
9091
+ );
9021
9092
  }
9022
9093
  /**
9023
9094
  * To make sure that at least one row was updated use `updateOrThrow`:
@@ -10059,7 +10130,7 @@ class QueryMethods {
10059
10130
  * The `find` method is available only for tables which has exactly one primary key.
10060
10131
  * And also it can accept raw SQL template literal, then the primary key is not required.
10061
10132
  *
10062
- * Find record by id, throw [NotFoundError](/guide/error-handling.html) if not found:
10133
+ * Finds a record by id, throws {@link NotFoundError} if not found:
10063
10134
  *
10064
10135
  * ```ts
10065
10136
  * await db.table.find(1);
@@ -10072,13 +10143,9 @@ class QueryMethods {
10072
10143
  * `;
10073
10144
  * ```
10074
10145
  *
10075
- * @param args - primary key value to find by, or a raw SQL
10146
+ * @param value - primary key value to find by
10076
10147
  */
10077
- find(...args) {
10078
- let [value] = args;
10079
- if (Array.isArray(value)) {
10080
- value = new RawSQL(args);
10081
- }
10148
+ find(value) {
10082
10149
  const q = this.clone();
10083
10150
  if (value === null || value === void 0) {
10084
10151
  throw new OrchidOrmInternalError(
@@ -10095,22 +10162,54 @@ class QueryMethods {
10095
10162
  );
10096
10163
  }
10097
10164
  /**
10098
- * Find a single record by the primary key (id), adds `LIMIT 1`, can accept a raw SQL.
10165
+ * Finds a single record with a given SQL, throws {@link NotFoundError} if not found:
10166
+ *
10167
+ * ```ts
10168
+ * await db.user.find`
10169
+ * age = ${age} AND
10170
+ * name = ${name}
10171
+ * `;
10172
+ * ```
10173
+ *
10174
+ * @param args - SQL expression
10175
+ */
10176
+ findBySql(...args) {
10177
+ const q = this.clone();
10178
+ return _queryTake(_queryWhereSql(q, args));
10179
+ }
10180
+ /**
10181
+ * Find a single record by the primary key (id), adds `LIMIT 1`.
10099
10182
  * Returns `undefined` when not found.
10100
10183
  *
10101
10184
  * ```ts
10102
10185
  * const result: TableType | undefined = await db.table.find(123);
10103
10186
  * ```
10104
10187
  *
10105
- * @param args - primary key value to find by, or a raw SQL
10188
+ * @param value - primary key value to find by, or a raw SQL
10106
10189
  */
10107
- findOptional(...args) {
10190
+ findOptional(value) {
10191
+ return _queryTakeOptional(this.find(value));
10192
+ }
10193
+ /**
10194
+ * Finds a single record with a given SQL.
10195
+ * Returns `undefined` when not found.
10196
+ *
10197
+ * ```ts
10198
+ * await db.user.find`
10199
+ * age = ${age} AND
10200
+ * name = ${name}
10201
+ * `;
10202
+ * ```
10203
+ *
10204
+ * @param args - SQL expression
10205
+ */
10206
+ findBySqlOptional(...args) {
10108
10207
  return _queryTakeOptional(
10109
- this.find(...args)
10208
+ this.findBySql(...args)
10110
10209
  );
10111
10210
  }
10112
10211
  /**
10113
- * The same as `where(conditions).take()`, it will filter records and add a `LIMIT 1`.
10212
+ * The same as `where(conditions).take()`, takes the same arguments as {@link Where.where}, it will filter records and add a `LIMIT 1`.
10114
10213
  * Throws `NotFoundError` if not found.
10115
10214
  *
10116
10215
  * ```ts
@@ -10244,7 +10343,7 @@ class QueryMethods {
10244
10343
  /**
10245
10344
  * Adds an order by clause to the query.
10246
10345
  *
10247
- * Takes one or more arguments, each argument can be a column name, an object, or a raw expression.
10346
+ * Takes one or more arguments, each argument can be a column name or an object.
10248
10347
  *
10249
10348
  * ```ts
10250
10349
  * db.table.order('id', 'name'); // ASC by default
@@ -10256,11 +10355,6 @@ class QueryMethods {
10256
10355
  * name: 'ASC NULLS FIRST',
10257
10356
  * age: 'DESC NULLS LAST',
10258
10357
  * });
10259
- *
10260
- * // order by raw SQL expression:
10261
- * db.table.order`raw sql`;
10262
- * // or
10263
- * db.table.order(db.table.sql`raw sql`);
10264
10358
  * ```
10265
10359
  *
10266
10360
  * `order` can refer to the values returned from `select` sub-queries (unlike `where` which cannot).
@@ -10279,22 +10373,35 @@ class QueryMethods {
10279
10373
  * });
10280
10374
  * ```
10281
10375
  *
10282
- * @param args - column name(s), raw SQL, or an object with column names and sort directions.
10376
+ * @param args - column name(s) or an object with column names and sort directions.
10283
10377
  */
10284
10378
  order(...args) {
10285
- if (Array.isArray(args[0])) {
10286
- return pushQueryValue(
10287
- this.clone(),
10288
- "order",
10289
- new RawSQL(args)
10290
- );
10291
- }
10292
10379
  return pushQueryArray(
10293
10380
  this.clone(),
10294
10381
  "order",
10295
10382
  args
10296
10383
  );
10297
10384
  }
10385
+ /**
10386
+ * Order by SQL expression
10387
+ *
10388
+ * Order by raw SQL expression.
10389
+ *
10390
+ * ```ts
10391
+ * db.table.order`raw sql`;
10392
+ * // or
10393
+ * db.table.order(db.table.sql`raw sql`);
10394
+ * ```
10395
+ *
10396
+ * @param args - SQL expression
10397
+ */
10398
+ orderSql(...args) {
10399
+ return pushQueryValue(
10400
+ this.clone(),
10401
+ "order",
10402
+ sqlQueryArgsToExpression(args)
10403
+ );
10404
+ }
10298
10405
  /**
10299
10406
  * Adds a limit clause to the query.
10300
10407
  *
@@ -10990,5 +11097,5 @@ function copyTableData(query, arg) {
10990
11097
  return q;
10991
11098
  }
10992
11099
 
10993
- export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, FnExpression, For, From, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, JsonModifiers, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnQueryBuilder, Operators, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL, RawSqlMethods, RealColumn, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransactionAdapter, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, UnhandledTypeError, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WhereQueryBase, With, XMLColumn, _queryAfterSaveCommit, _queryAll, _queryAs, _queryChangeCounter, _queryCreate, _queryCreateFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateManyRaw, _queryCreateRaw, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertManyRaw, _queryInsertRaw, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereIn, _queryWhereNot, addComputedColumns, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, checkIfASimpleQuery, cloneQuery, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, constraintPropsToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, extendQuery, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getConstraintKind, getQueryAs, getShapeFromSelect, getTableData, handleResult, identityToCode, indexToCode, instantiateColumn, isQueryReturnsAll, isSelectingCount, joinSubQuery, logColors, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeExpression, makeFnExpression, makeRegexToFindInSql, makeSQL, newTableData, parseRecord, parseResult, primaryKeyToCode, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryFrom, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, quote, quoteString, raw, referencesArgsToCode, resetTableData, resolveSubQueryCallback, saveSearchAlias, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, templateLiteralToSQL, testTransaction, throwIfNoWhere, toSQL, toSQLCacheKey };
11100
+ export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, FnExpression, For, From, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, JsonModifiers, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnQueryBuilder, Operators, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL, RawSqlMethods, RealColumn, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransactionAdapter, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, UnhandledTypeError, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WhereQueryBase, With, XMLColumn, _queryAfterSaveCommit, _queryAll, _queryAs, _queryChangeCounter, _queryCreate, _queryCreateFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateManyRaw, _queryCreateRaw, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertManyRaw, _queryInsertRaw, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereIn, _queryWhereNot, _queryWhereNotSql, _queryWhereSql, addComputedColumns, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, checkIfASimpleQuery, cloneQuery, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, constraintPropsToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, extendQuery, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getConstraintKind, getQueryAs, getShapeFromSelect, getTableData, handleResult, identityToCode, indexToCode, instantiateColumn, isQueryReturnsAll, isSelectingCount, joinSubQuery, logColors, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeExpression, makeFnExpression, makeRegexToFindInSql, makeSQL, newTableData, parseRecord, parseResult, primaryKeyToCode, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, quote, quoteString, raw, referencesArgsToCode, resetTableData, resolveSubQueryCallback, saveSearchAlias, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlQueryArgsToExpression, templateLiteralToSQL, testTransaction, throwIfNoWhere, toSQL, toSQLCacheKey };
10994
11101
  //# sourceMappingURL=index.mjs.map