pqb 0.25.1 → 0.26.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
@@ -656,6 +656,7 @@ const columnCode = (type, t, code, data = type.data, skip) => {
656
656
  return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
657
657
  };
658
658
 
659
+ const joinStatementsSet = /* @__PURE__ */ new Set();
659
660
  function simpleColumnToSQL(ctx, key, column, quotedAs) {
660
661
  if (!column)
661
662
  return `"${key}"`;
@@ -831,7 +832,7 @@ class JSONColumn extends ColumnType {
831
832
  JSONColumn.prototype.encodeFn = JSON.stringify;
832
833
  class JSONTextColumn extends ColumnType {
833
834
  constructor(schema) {
834
- super(schema, schema.stringSchema);
835
+ super(schema, schema.stringSchema());
835
836
  this.dataType = "json";
836
837
  this.operators = Operators.text;
837
838
  }
@@ -991,6 +992,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
991
992
  });
992
993
  } else if (key === "EXISTS") {
993
994
  const joinItems = Array.isArray(value[0]) ? value : [value];
995
+ joinStatementsSet.clear();
994
996
  for (const args of joinItems) {
995
997
  const { target, conditions } = processJoinItem(
996
998
  ctx,
@@ -999,7 +1001,11 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
999
1001
  args,
1000
1002
  quotedAs
1001
1003
  );
1002
- ands.push(`EXISTS (SELECT 1 FROM ${target} WHERE ${conditions})`);
1004
+ const sql = `EXISTS (SELECT 1 FROM ${target} WHERE ${conditions})`;
1005
+ if (!joinStatementsSet.has(sql)) {
1006
+ joinStatementsSet.add(sql);
1007
+ ands.push(sql);
1008
+ }
1003
1009
  }
1004
1010
  } else if (key === "SEARCH") {
1005
1011
  const search = value;
@@ -1316,15 +1322,15 @@ const getObjectOrRawConditions = (ctx, query, data, quotedAs, joinAs, joinShape)
1316
1322
  };
1317
1323
  const pushJoinSql = (ctx, table, query, quotedAs) => {
1318
1324
  var _a;
1325
+ joinStatementsSet.clear();
1319
1326
  for (const item of query.join) {
1327
+ let sql;
1320
1328
  if (Array.isArray(item)) {
1321
1329
  const q = item[1];
1322
1330
  const { aliasValue } = ctx;
1323
1331
  ctx.aliasValue = true;
1324
1332
  const as = item[2];
1325
- ctx.sql.push(
1326
- `${item[0]} LATERAL (${q.toSQL(ctx).text}) "${((_a = query.joinOverrides) == null ? void 0 : _a[as]) || as}" ON true`
1327
- );
1333
+ sql = `${item[0]} LATERAL (${q.toSQL(ctx).text}) "${((_a = query.joinOverrides) == null ? void 0 : _a[as]) || as}" ON true`;
1328
1334
  ctx.aliasValue = aliasValue;
1329
1335
  } else {
1330
1336
  const { target, conditions } = processJoinItem(
@@ -1334,9 +1340,11 @@ const pushJoinSql = (ctx, table, query, quotedAs) => {
1334
1340
  item,
1335
1341
  quotedAs
1336
1342
  );
1337
- ctx.sql.push(item.type, target);
1338
- if (conditions)
1339
- ctx.sql.push("ON", conditions);
1343
+ sql = conditions ? `${item.type} ${target} ON ${conditions}` : `${item.type} ${target} ON true`;
1344
+ }
1345
+ if (!joinStatementsSet.has(sql)) {
1346
+ joinStatementsSet.add(sql);
1347
+ ctx.sql.push(sql);
1340
1348
  }
1341
1349
  }
1342
1350
  };
@@ -1483,7 +1491,12 @@ const dateTimeEncode = (input) => {
1483
1491
  const skipDateMethodsFromToCode = { encodeFn: dateTimeEncode };
1484
1492
  class DateBaseColumn extends ColumnType {
1485
1493
  constructor(schema) {
1486
- super(schema, schema.stringNumberDate);
1494
+ super(
1495
+ schema,
1496
+ schema.stringNumberDate(),
1497
+ schema.stringSchema(),
1498
+ schema.stringNumberDate()
1499
+ );
1487
1500
  this.operators = Operators.date;
1488
1501
  this.encodeFn = dateTimeEncode;
1489
1502
  this.asNumber = schema.dateAsNumber;
@@ -1557,7 +1570,7 @@ class TimestampTZColumn extends DateTimeTzBaseClass {
1557
1570
  }
1558
1571
  class TimeColumn extends ColumnType {
1559
1572
  constructor(schema, dateTimePrecision) {
1560
- super(schema, schema.stringSchema);
1573
+ super(schema, schema.stringSchema());
1561
1574
  this.dataType = "time";
1562
1575
  this.operators = Operators.time;
1563
1576
  this.data.dateTimePrecision = dateTimePrecision;
@@ -1575,7 +1588,7 @@ class TimeColumn extends ColumnType {
1575
1588
  }
1576
1589
  class IntervalColumn extends ColumnType {
1577
1590
  constructor(schema, fields, precision) {
1578
- super(schema, schema.timeInterval);
1591
+ super(schema, schema.timeInterval());
1579
1592
  this.dataType = "interval";
1580
1593
  this.operators = Operators.date;
1581
1594
  this.data.fields = fields;
@@ -1748,10 +1761,20 @@ const defaultSchemaConfig = {
1748
1761
  array(item) {
1749
1762
  return new ArrayColumn(defaultSchemaConfig, item, void 0);
1750
1763
  },
1764
+ boolean: orchidCore.noop,
1765
+ buffer: orchidCore.noop,
1766
+ unknown: orchidCore.noop,
1767
+ never: orchidCore.noop,
1768
+ stringSchema: orchidCore.noop,
1751
1769
  stringMin: orchidCore.noop,
1752
1770
  stringMax: orchidCore.noop,
1753
1771
  stringMinMax: orchidCore.noop,
1772
+ number: orchidCore.noop,
1773
+ int: orchidCore.noop,
1774
+ stringNumberDate: orchidCore.noop,
1775
+ timeInterval: orchidCore.noop,
1754
1776
  bit: orchidCore.noop,
1777
+ uuid: orchidCore.noop,
1755
1778
  nullable() {
1756
1779
  return orchidCore.setColumnData(this, "isNullable", true);
1757
1780
  },
@@ -2906,9 +2929,15 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
2906
2929
  let conditions;
2907
2930
  if ((_a = query.join) == null ? void 0 : _a.length) {
2908
2931
  const items = [];
2932
+ joinStatementsSet.clear();
2909
2933
  for (const item of query.join) {
2910
2934
  if (!Array.isArray(item)) {
2911
- items.push(processJoinItem(ctx, table, query, item, quotedAs));
2935
+ const join = processJoinItem(ctx, table, query, item, quotedAs);
2936
+ const key = `${join.target}${join.conditions}`;
2937
+ if (!joinStatementsSet.has(key)) {
2938
+ joinStatementsSet.add(key);
2939
+ items.push(join);
2940
+ }
2912
2941
  }
2913
2942
  }
2914
2943
  if (items.length) {
@@ -3526,19 +3555,19 @@ class NumberBaseColumn extends ColumnType {
3526
3555
  }
3527
3556
  class IntegerBaseColumn extends NumberBaseColumn {
3528
3557
  constructor(schema) {
3529
- super(schema, schema.int);
3558
+ super(schema, schema.int());
3530
3559
  this.data.int = true;
3531
3560
  }
3532
3561
  }
3533
3562
  class NumberAsStringBaseColumn extends ColumnType {
3534
3563
  constructor(schema) {
3535
- super(schema, schema.stringSchema);
3564
+ super(schema, schema.stringSchema());
3536
3565
  this.operators = Operators.number;
3537
3566
  }
3538
3567
  }
3539
3568
  class DecimalColumn extends ColumnType {
3540
3569
  constructor(schema, numericPrecision, numericScale) {
3541
- super(schema, schema.stringSchema);
3570
+ super(schema, schema.stringSchema());
3542
3571
  this.operators = Operators.number;
3543
3572
  this.dataType = "decimal";
3544
3573
  this.data.numericPrecision = numericPrecision;
@@ -3611,7 +3640,7 @@ class BigIntColumn extends NumberAsStringBaseColumn {
3611
3640
  }
3612
3641
  class RealColumn extends NumberBaseColumn {
3613
3642
  constructor(schema) {
3614
- super(schema, schema.number);
3643
+ super(schema, schema.number());
3615
3644
  this.dataType = "real";
3616
3645
  this.parseItem = parseFloat;
3617
3646
  }
@@ -3697,14 +3726,17 @@ var __spreadValues$9 = (a, b) => {
3697
3726
  };
3698
3727
  var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
3699
3728
  class TextBaseColumn extends ColumnType {
3700
- constructor(schema, schemaType = schema.stringSchema) {
3729
+ constructor(schema, schemaType = schema.stringSchema()) {
3701
3730
  super(schema, schemaType);
3702
3731
  this.operators = Operators.text;
3703
3732
  }
3704
3733
  }
3705
3734
  class LimitedTextBaseColumn extends TextBaseColumn {
3706
3735
  constructor(schema, limit) {
3707
- super(schema, limit ? schema.stringMax(limit) : schema.stringSchema);
3736
+ super(
3737
+ schema,
3738
+ limit ? schema.stringMax(limit) : schema.stringSchema()
3739
+ );
3708
3740
  this.data.maxChars = limit;
3709
3741
  }
3710
3742
  toSQL() {
@@ -3787,7 +3819,7 @@ const textColumnToCode = (column, t) => {
3787
3819
  `${column.dataType}(${args})${orchidCore.stringDataToCode(data)}`
3788
3820
  );
3789
3821
  };
3790
- const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema;
3822
+ const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema();
3791
3823
  class TextColumn extends TextBaseColumn {
3792
3824
  constructor(schema, min, max) {
3793
3825
  super(schema, minMaxToSchema(schema, min, max));
@@ -3800,7 +3832,7 @@ class TextColumn extends TextBaseColumn {
3800
3832
  }
3801
3833
  class ByteaColumn extends ColumnType {
3802
3834
  constructor(schema) {
3803
- super(schema, schema.buffer);
3835
+ super(schema, schema.buffer());
3804
3836
  this.dataType = "bytea";
3805
3837
  this.operators = Operators.text;
3806
3838
  }
@@ -3810,7 +3842,7 @@ class ByteaColumn extends ColumnType {
3810
3842
  }
3811
3843
  class PointColumn extends ColumnType {
3812
3844
  constructor(schema) {
3813
- super(schema, schema.stringSchema);
3845
+ super(schema, schema.stringSchema());
3814
3846
  this.dataType = "point";
3815
3847
  this.operators = Operators.text;
3816
3848
  }
@@ -3820,7 +3852,7 @@ class PointColumn extends ColumnType {
3820
3852
  }
3821
3853
  class LineColumn extends ColumnType {
3822
3854
  constructor(schema) {
3823
- super(schema, schema.stringSchema);
3855
+ super(schema, schema.stringSchema());
3824
3856
  this.dataType = "line";
3825
3857
  this.operators = Operators.text;
3826
3858
  }
@@ -3830,7 +3862,7 @@ class LineColumn extends ColumnType {
3830
3862
  }
3831
3863
  class LsegColumn extends ColumnType {
3832
3864
  constructor(schema) {
3833
- super(schema, schema.stringSchema);
3865
+ super(schema, schema.stringSchema());
3834
3866
  this.dataType = "lseg";
3835
3867
  this.operators = Operators.text;
3836
3868
  }
@@ -3840,7 +3872,7 @@ class LsegColumn extends ColumnType {
3840
3872
  }
3841
3873
  class BoxColumn extends ColumnType {
3842
3874
  constructor(schema) {
3843
- super(schema, schema.stringSchema);
3875
+ super(schema, schema.stringSchema());
3844
3876
  this.dataType = "box";
3845
3877
  this.operators = Operators.text;
3846
3878
  }
@@ -3850,7 +3882,7 @@ class BoxColumn extends ColumnType {
3850
3882
  }
3851
3883
  class PathColumn extends ColumnType {
3852
3884
  constructor(schema) {
3853
- super(schema, schema.stringSchema);
3885
+ super(schema, schema.stringSchema());
3854
3886
  this.dataType = "path";
3855
3887
  this.operators = Operators.text;
3856
3888
  }
@@ -3860,7 +3892,7 @@ class PathColumn extends ColumnType {
3860
3892
  }
3861
3893
  class PolygonColumn extends ColumnType {
3862
3894
  constructor(schema) {
3863
- super(schema, schema.stringSchema);
3895
+ super(schema, schema.stringSchema());
3864
3896
  this.dataType = "polygon";
3865
3897
  this.operators = Operators.text;
3866
3898
  }
@@ -3870,7 +3902,7 @@ class PolygonColumn extends ColumnType {
3870
3902
  }
3871
3903
  class CircleColumn extends ColumnType {
3872
3904
  constructor(schema) {
3873
- super(schema, schema.stringSchema);
3905
+ super(schema, schema.stringSchema());
3874
3906
  this.dataType = "circle";
3875
3907
  this.operators = Operators.text;
3876
3908
  }
@@ -3880,7 +3912,7 @@ class CircleColumn extends ColumnType {
3880
3912
  }
3881
3913
  class MoneyColumn extends NumberBaseColumn {
3882
3914
  constructor(schema) {
3883
- super(schema, schema.stringSchema);
3915
+ super(schema, schema.stringSchema());
3884
3916
  this.dataType = "money";
3885
3917
  this.parseFn = Object.assign(
3886
3918
  function(input) {
@@ -3897,7 +3929,7 @@ class MoneyColumn extends NumberBaseColumn {
3897
3929
  }
3898
3930
  class CidrColumn extends ColumnType {
3899
3931
  constructor(schema) {
3900
- super(schema, schema.stringSchema);
3932
+ super(schema, schema.stringSchema());
3901
3933
  this.dataType = "cidr";
3902
3934
  this.operators = Operators.text;
3903
3935
  }
@@ -3907,7 +3939,7 @@ class CidrColumn extends ColumnType {
3907
3939
  }
3908
3940
  class InetColumn extends ColumnType {
3909
3941
  constructor(schema) {
3910
- super(schema, schema.stringSchema);
3942
+ super(schema, schema.stringSchema());
3911
3943
  this.dataType = "inet";
3912
3944
  this.operators = Operators.text;
3913
3945
  }
@@ -3917,7 +3949,7 @@ class InetColumn extends ColumnType {
3917
3949
  }
3918
3950
  class MacAddrColumn extends ColumnType {
3919
3951
  constructor(schema) {
3920
- super(schema, schema.stringSchema);
3952
+ super(schema, schema.stringSchema());
3921
3953
  this.dataType = "macaddr";
3922
3954
  this.operators = Operators.text;
3923
3955
  }
@@ -3927,7 +3959,7 @@ class MacAddrColumn extends ColumnType {
3927
3959
  }
3928
3960
  class MacAddr8Column extends ColumnType {
3929
3961
  constructor(schema) {
3930
- super(schema, schema.stringSchema);
3962
+ super(schema, schema.stringSchema());
3931
3963
  this.dataType = "macaddr8";
3932
3964
  this.operators = Operators.text;
3933
3965
  }
@@ -3973,7 +4005,7 @@ class BitVaryingColumn extends ColumnType {
3973
4005
  }
3974
4006
  class TsVectorColumn extends ColumnType {
3975
4007
  constructor(schema, defaultLanguage = orchidCore.getDefaultLanguage()) {
3976
- super(schema, schema.stringSchema);
4008
+ super(schema, schema.stringSchema());
3977
4009
  this.defaultLanguage = defaultLanguage;
3978
4010
  this.dataType = "tsvector";
3979
4011
  this.operators = Operators.text;
@@ -4027,7 +4059,7 @@ class TsVectorColumn extends ColumnType {
4027
4059
  }
4028
4060
  class TsQueryColumn extends ColumnType {
4029
4061
  constructor(schema) {
4030
- super(schema, schema.stringSchema);
4062
+ super(schema, schema.stringSchema());
4031
4063
  this.dataType = "tsquery";
4032
4064
  this.operators = Operators.text;
4033
4065
  }
@@ -4039,7 +4071,7 @@ const uuidDefaultSQL = "gen_random_uuid()";
4039
4071
  const uuidDefault = new RawSQL(uuidDefaultSQL);
4040
4072
  class UUIDColumn extends ColumnType {
4041
4073
  constructor(schema) {
4042
- super(schema, schema.uuid);
4074
+ super(schema, schema.uuid());
4043
4075
  this.dataType = "uuid";
4044
4076
  this.operators = Operators.text;
4045
4077
  }
@@ -4062,7 +4094,7 @@ class UUIDColumn extends ColumnType {
4062
4094
  }
4063
4095
  class XMLColumn extends ColumnType {
4064
4096
  constructor(schema) {
4065
- super(schema, schema.stringSchema);
4097
+ super(schema, schema.stringSchema());
4066
4098
  this.dataType = "xml";
4067
4099
  this.operators = Operators.text;
4068
4100
  }
@@ -4083,7 +4115,7 @@ class CitextColumn extends TextBaseColumn {
4083
4115
 
4084
4116
  class BooleanColumn extends ColumnType {
4085
4117
  constructor(schema) {
4086
- super(schema, schema.boolean);
4118
+ super(schema, schema.boolean());
4087
4119
  this.dataType = "boolean";
4088
4120
  this.operators = Operators.boolean;
4089
4121
  this.parseItem = (input) => input[0] === "t";
@@ -4095,7 +4127,12 @@ class BooleanColumn extends ColumnType {
4095
4127
 
4096
4128
  class CustomTypeColumn extends ColumnType {
4097
4129
  constructor(schema, dataType) {
4098
- super(schema, schema.unknown, schema.unknown, schema.unknown);
4130
+ super(
4131
+ schema,
4132
+ schema.unknown(),
4133
+ schema.unknown(),
4134
+ schema.unknown()
4135
+ );
4099
4136
  this.dataType = dataType;
4100
4137
  this.operators = Operators.any;
4101
4138
  this.data.isOfCustomType = true;
@@ -4344,7 +4381,7 @@ const makeColumnTypes = (schema) => {
4344
4381
  RawSQL.prototype.columnTypes = makeColumnTypes;
4345
4382
 
4346
4383
  class VirtualColumn extends ColumnType {
4347
- constructor(schema, inputSchema = schema.never) {
4384
+ constructor(schema, inputSchema = schema.never()) {
4348
4385
  super(schema, inputSchema);
4349
4386
  this.dataType = "";
4350
4387
  this.operators = Operators.any;
@@ -4356,7 +4393,7 @@ class VirtualColumn extends ColumnType {
4356
4393
 
4357
4394
  class UnknownColumn extends VirtualColumn {
4358
4395
  constructor(schema) {
4359
- super(schema, schema.unknown);
4396
+ super(schema, schema.unknown());
4360
4397
  }
4361
4398
  }
4362
4399
  RawSQL.prototype._type = new UnknownColumn(defaultSchemaConfig);
@@ -7334,6 +7371,35 @@ class Join {
7334
7371
  *
7335
7372
  * When no matching record is found, it will skip records of the main table.
7336
7373
  *
7374
+ * When joining the same table with the same condition more than once, duplicated joins will be ignored:
7375
+ *
7376
+ * ```ts
7377
+ * // joining a relation
7378
+ * db.post.join('comments').join('comments');
7379
+ *
7380
+ * // joining a table with a condition
7381
+ * db.post
7382
+ * .join('comments', 'comments.postId', 'post.id')
7383
+ * .join('comments', 'comments.postId', 'post.id');
7384
+ * ```
7385
+ *
7386
+ * Both queries will produce SQL with only 1 join
7387
+ *
7388
+ * ```sql
7389
+ * SELECT * FROM post JOIN comments ON comments.postId = post.id
7390
+ * ```
7391
+ *
7392
+ * However, this is only possible if the join has no dynamic values:
7393
+ *
7394
+ * ```ts
7395
+ * db.post
7396
+ * .join('comments', (q) => q.where({ rating: { gt: 5 } }))
7397
+ * .join('comments', (q) => q.where({ rating: { gt: 5 } }));
7398
+ * ```
7399
+ *
7400
+ * Both joins above have the same `{ gt: 5 }`, but still, the `5` is a dynamic value and in this case joins will be duplicated,
7401
+ * resulting in a database error.
7402
+ *
7337
7403
  * ### join relation
7338
7404
  *
7339
7405
  * When relations are defined between the tables, you can join them by a relation name.