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.mjs CHANGED
@@ -654,6 +654,7 @@ const columnCode = (type, t, code, data = type.data, skip) => {
654
654
  return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
655
655
  };
656
656
 
657
+ const joinStatementsSet = /* @__PURE__ */ new Set();
657
658
  function simpleColumnToSQL(ctx, key, column, quotedAs) {
658
659
  if (!column)
659
660
  return `"${key}"`;
@@ -829,7 +830,7 @@ class JSONColumn extends ColumnType {
829
830
  JSONColumn.prototype.encodeFn = JSON.stringify;
830
831
  class JSONTextColumn extends ColumnType {
831
832
  constructor(schema) {
832
- super(schema, schema.stringSchema);
833
+ super(schema, schema.stringSchema());
833
834
  this.dataType = "json";
834
835
  this.operators = Operators.text;
835
836
  }
@@ -989,6 +990,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
989
990
  });
990
991
  } else if (key === "EXISTS") {
991
992
  const joinItems = Array.isArray(value[0]) ? value : [value];
993
+ joinStatementsSet.clear();
992
994
  for (const args of joinItems) {
993
995
  const { target, conditions } = processJoinItem(
994
996
  ctx,
@@ -997,7 +999,11 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
997
999
  args,
998
1000
  quotedAs
999
1001
  );
1000
- ands.push(`EXISTS (SELECT 1 FROM ${target} WHERE ${conditions})`);
1002
+ const sql = `EXISTS (SELECT 1 FROM ${target} WHERE ${conditions})`;
1003
+ if (!joinStatementsSet.has(sql)) {
1004
+ joinStatementsSet.add(sql);
1005
+ ands.push(sql);
1006
+ }
1001
1007
  }
1002
1008
  } else if (key === "SEARCH") {
1003
1009
  const search = value;
@@ -1314,15 +1320,15 @@ const getObjectOrRawConditions = (ctx, query, data, quotedAs, joinAs, joinShape)
1314
1320
  };
1315
1321
  const pushJoinSql = (ctx, table, query, quotedAs) => {
1316
1322
  var _a;
1323
+ joinStatementsSet.clear();
1317
1324
  for (const item of query.join) {
1325
+ let sql;
1318
1326
  if (Array.isArray(item)) {
1319
1327
  const q = item[1];
1320
1328
  const { aliasValue } = ctx;
1321
1329
  ctx.aliasValue = true;
1322
1330
  const as = item[2];
1323
- ctx.sql.push(
1324
- `${item[0]} LATERAL (${q.toSQL(ctx).text}) "${((_a = query.joinOverrides) == null ? void 0 : _a[as]) || as}" ON true`
1325
- );
1331
+ sql = `${item[0]} LATERAL (${q.toSQL(ctx).text}) "${((_a = query.joinOverrides) == null ? void 0 : _a[as]) || as}" ON true`;
1326
1332
  ctx.aliasValue = aliasValue;
1327
1333
  } else {
1328
1334
  const { target, conditions } = processJoinItem(
@@ -1332,9 +1338,11 @@ const pushJoinSql = (ctx, table, query, quotedAs) => {
1332
1338
  item,
1333
1339
  quotedAs
1334
1340
  );
1335
- ctx.sql.push(item.type, target);
1336
- if (conditions)
1337
- ctx.sql.push("ON", conditions);
1341
+ sql = conditions ? `${item.type} ${target} ON ${conditions}` : `${item.type} ${target} ON true`;
1342
+ }
1343
+ if (!joinStatementsSet.has(sql)) {
1344
+ joinStatementsSet.add(sql);
1345
+ ctx.sql.push(sql);
1338
1346
  }
1339
1347
  }
1340
1348
  };
@@ -1481,7 +1489,12 @@ const dateTimeEncode = (input) => {
1481
1489
  const skipDateMethodsFromToCode = { encodeFn: dateTimeEncode };
1482
1490
  class DateBaseColumn extends ColumnType {
1483
1491
  constructor(schema) {
1484
- super(schema, schema.stringNumberDate);
1492
+ super(
1493
+ schema,
1494
+ schema.stringNumberDate(),
1495
+ schema.stringSchema(),
1496
+ schema.stringNumberDate()
1497
+ );
1485
1498
  this.operators = Operators.date;
1486
1499
  this.encodeFn = dateTimeEncode;
1487
1500
  this.asNumber = schema.dateAsNumber;
@@ -1555,7 +1568,7 @@ class TimestampTZColumn extends DateTimeTzBaseClass {
1555
1568
  }
1556
1569
  class TimeColumn extends ColumnType {
1557
1570
  constructor(schema, dateTimePrecision) {
1558
- super(schema, schema.stringSchema);
1571
+ super(schema, schema.stringSchema());
1559
1572
  this.dataType = "time";
1560
1573
  this.operators = Operators.time;
1561
1574
  this.data.dateTimePrecision = dateTimePrecision;
@@ -1573,7 +1586,7 @@ class TimeColumn extends ColumnType {
1573
1586
  }
1574
1587
  class IntervalColumn extends ColumnType {
1575
1588
  constructor(schema, fields, precision) {
1576
- super(schema, schema.timeInterval);
1589
+ super(schema, schema.timeInterval());
1577
1590
  this.dataType = "interval";
1578
1591
  this.operators = Operators.date;
1579
1592
  this.data.fields = fields;
@@ -1746,10 +1759,20 @@ const defaultSchemaConfig = {
1746
1759
  array(item) {
1747
1760
  return new ArrayColumn(defaultSchemaConfig, item, void 0);
1748
1761
  },
1762
+ boolean: noop,
1763
+ buffer: noop,
1764
+ unknown: noop,
1765
+ never: noop,
1766
+ stringSchema: noop,
1749
1767
  stringMin: noop,
1750
1768
  stringMax: noop,
1751
1769
  stringMinMax: noop,
1770
+ number: noop,
1771
+ int: noop,
1772
+ stringNumberDate: noop,
1773
+ timeInterval: noop,
1752
1774
  bit: noop,
1775
+ uuid: noop,
1753
1776
  nullable() {
1754
1777
  return setColumnData(this, "isNullable", true);
1755
1778
  },
@@ -2904,9 +2927,15 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
2904
2927
  let conditions;
2905
2928
  if ((_a = query.join) == null ? void 0 : _a.length) {
2906
2929
  const items = [];
2930
+ joinStatementsSet.clear();
2907
2931
  for (const item of query.join) {
2908
2932
  if (!Array.isArray(item)) {
2909
- items.push(processJoinItem(ctx, table, query, item, quotedAs));
2933
+ const join = processJoinItem(ctx, table, query, item, quotedAs);
2934
+ const key = `${join.target}${join.conditions}`;
2935
+ if (!joinStatementsSet.has(key)) {
2936
+ joinStatementsSet.add(key);
2937
+ items.push(join);
2938
+ }
2910
2939
  }
2911
2940
  }
2912
2941
  if (items.length) {
@@ -3524,19 +3553,19 @@ class NumberBaseColumn extends ColumnType {
3524
3553
  }
3525
3554
  class IntegerBaseColumn extends NumberBaseColumn {
3526
3555
  constructor(schema) {
3527
- super(schema, schema.int);
3556
+ super(schema, schema.int());
3528
3557
  this.data.int = true;
3529
3558
  }
3530
3559
  }
3531
3560
  class NumberAsStringBaseColumn extends ColumnType {
3532
3561
  constructor(schema) {
3533
- super(schema, schema.stringSchema);
3562
+ super(schema, schema.stringSchema());
3534
3563
  this.operators = Operators.number;
3535
3564
  }
3536
3565
  }
3537
3566
  class DecimalColumn extends ColumnType {
3538
3567
  constructor(schema, numericPrecision, numericScale) {
3539
- super(schema, schema.stringSchema);
3568
+ super(schema, schema.stringSchema());
3540
3569
  this.operators = Operators.number;
3541
3570
  this.dataType = "decimal";
3542
3571
  this.data.numericPrecision = numericPrecision;
@@ -3609,7 +3638,7 @@ class BigIntColumn extends NumberAsStringBaseColumn {
3609
3638
  }
3610
3639
  class RealColumn extends NumberBaseColumn {
3611
3640
  constructor(schema) {
3612
- super(schema, schema.number);
3641
+ super(schema, schema.number());
3613
3642
  this.dataType = "real";
3614
3643
  this.parseItem = parseFloat;
3615
3644
  }
@@ -3695,14 +3724,17 @@ var __spreadValues$9 = (a, b) => {
3695
3724
  };
3696
3725
  var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
3697
3726
  class TextBaseColumn extends ColumnType {
3698
- constructor(schema, schemaType = schema.stringSchema) {
3727
+ constructor(schema, schemaType = schema.stringSchema()) {
3699
3728
  super(schema, schemaType);
3700
3729
  this.operators = Operators.text;
3701
3730
  }
3702
3731
  }
3703
3732
  class LimitedTextBaseColumn extends TextBaseColumn {
3704
3733
  constructor(schema, limit) {
3705
- super(schema, limit ? schema.stringMax(limit) : schema.stringSchema);
3734
+ super(
3735
+ schema,
3736
+ limit ? schema.stringMax(limit) : schema.stringSchema()
3737
+ );
3706
3738
  this.data.maxChars = limit;
3707
3739
  }
3708
3740
  toSQL() {
@@ -3785,7 +3817,7 @@ const textColumnToCode = (column, t) => {
3785
3817
  `${column.dataType}(${args})${stringDataToCode(data)}`
3786
3818
  );
3787
3819
  };
3788
- const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema;
3820
+ const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema();
3789
3821
  class TextColumn extends TextBaseColumn {
3790
3822
  constructor(schema, min, max) {
3791
3823
  super(schema, minMaxToSchema(schema, min, max));
@@ -3798,7 +3830,7 @@ class TextColumn extends TextBaseColumn {
3798
3830
  }
3799
3831
  class ByteaColumn extends ColumnType {
3800
3832
  constructor(schema) {
3801
- super(schema, schema.buffer);
3833
+ super(schema, schema.buffer());
3802
3834
  this.dataType = "bytea";
3803
3835
  this.operators = Operators.text;
3804
3836
  }
@@ -3808,7 +3840,7 @@ class ByteaColumn extends ColumnType {
3808
3840
  }
3809
3841
  class PointColumn extends ColumnType {
3810
3842
  constructor(schema) {
3811
- super(schema, schema.stringSchema);
3843
+ super(schema, schema.stringSchema());
3812
3844
  this.dataType = "point";
3813
3845
  this.operators = Operators.text;
3814
3846
  }
@@ -3818,7 +3850,7 @@ class PointColumn extends ColumnType {
3818
3850
  }
3819
3851
  class LineColumn extends ColumnType {
3820
3852
  constructor(schema) {
3821
- super(schema, schema.stringSchema);
3853
+ super(schema, schema.stringSchema());
3822
3854
  this.dataType = "line";
3823
3855
  this.operators = Operators.text;
3824
3856
  }
@@ -3828,7 +3860,7 @@ class LineColumn extends ColumnType {
3828
3860
  }
3829
3861
  class LsegColumn extends ColumnType {
3830
3862
  constructor(schema) {
3831
- super(schema, schema.stringSchema);
3863
+ super(schema, schema.stringSchema());
3832
3864
  this.dataType = "lseg";
3833
3865
  this.operators = Operators.text;
3834
3866
  }
@@ -3838,7 +3870,7 @@ class LsegColumn extends ColumnType {
3838
3870
  }
3839
3871
  class BoxColumn extends ColumnType {
3840
3872
  constructor(schema) {
3841
- super(schema, schema.stringSchema);
3873
+ super(schema, schema.stringSchema());
3842
3874
  this.dataType = "box";
3843
3875
  this.operators = Operators.text;
3844
3876
  }
@@ -3848,7 +3880,7 @@ class BoxColumn extends ColumnType {
3848
3880
  }
3849
3881
  class PathColumn extends ColumnType {
3850
3882
  constructor(schema) {
3851
- super(schema, schema.stringSchema);
3883
+ super(schema, schema.stringSchema());
3852
3884
  this.dataType = "path";
3853
3885
  this.operators = Operators.text;
3854
3886
  }
@@ -3858,7 +3890,7 @@ class PathColumn extends ColumnType {
3858
3890
  }
3859
3891
  class PolygonColumn extends ColumnType {
3860
3892
  constructor(schema) {
3861
- super(schema, schema.stringSchema);
3893
+ super(schema, schema.stringSchema());
3862
3894
  this.dataType = "polygon";
3863
3895
  this.operators = Operators.text;
3864
3896
  }
@@ -3868,7 +3900,7 @@ class PolygonColumn extends ColumnType {
3868
3900
  }
3869
3901
  class CircleColumn extends ColumnType {
3870
3902
  constructor(schema) {
3871
- super(schema, schema.stringSchema);
3903
+ super(schema, schema.stringSchema());
3872
3904
  this.dataType = "circle";
3873
3905
  this.operators = Operators.text;
3874
3906
  }
@@ -3878,7 +3910,7 @@ class CircleColumn extends ColumnType {
3878
3910
  }
3879
3911
  class MoneyColumn extends NumberBaseColumn {
3880
3912
  constructor(schema) {
3881
- super(schema, schema.stringSchema);
3913
+ super(schema, schema.stringSchema());
3882
3914
  this.dataType = "money";
3883
3915
  this.parseFn = Object.assign(
3884
3916
  function(input) {
@@ -3895,7 +3927,7 @@ class MoneyColumn extends NumberBaseColumn {
3895
3927
  }
3896
3928
  class CidrColumn extends ColumnType {
3897
3929
  constructor(schema) {
3898
- super(schema, schema.stringSchema);
3930
+ super(schema, schema.stringSchema());
3899
3931
  this.dataType = "cidr";
3900
3932
  this.operators = Operators.text;
3901
3933
  }
@@ -3905,7 +3937,7 @@ class CidrColumn extends ColumnType {
3905
3937
  }
3906
3938
  class InetColumn extends ColumnType {
3907
3939
  constructor(schema) {
3908
- super(schema, schema.stringSchema);
3940
+ super(schema, schema.stringSchema());
3909
3941
  this.dataType = "inet";
3910
3942
  this.operators = Operators.text;
3911
3943
  }
@@ -3915,7 +3947,7 @@ class InetColumn extends ColumnType {
3915
3947
  }
3916
3948
  class MacAddrColumn extends ColumnType {
3917
3949
  constructor(schema) {
3918
- super(schema, schema.stringSchema);
3950
+ super(schema, schema.stringSchema());
3919
3951
  this.dataType = "macaddr";
3920
3952
  this.operators = Operators.text;
3921
3953
  }
@@ -3925,7 +3957,7 @@ class MacAddrColumn extends ColumnType {
3925
3957
  }
3926
3958
  class MacAddr8Column extends ColumnType {
3927
3959
  constructor(schema) {
3928
- super(schema, schema.stringSchema);
3960
+ super(schema, schema.stringSchema());
3929
3961
  this.dataType = "macaddr8";
3930
3962
  this.operators = Operators.text;
3931
3963
  }
@@ -3971,7 +4003,7 @@ class BitVaryingColumn extends ColumnType {
3971
4003
  }
3972
4004
  class TsVectorColumn extends ColumnType {
3973
4005
  constructor(schema, defaultLanguage = getDefaultLanguage()) {
3974
- super(schema, schema.stringSchema);
4006
+ super(schema, schema.stringSchema());
3975
4007
  this.defaultLanguage = defaultLanguage;
3976
4008
  this.dataType = "tsvector";
3977
4009
  this.operators = Operators.text;
@@ -4025,7 +4057,7 @@ class TsVectorColumn extends ColumnType {
4025
4057
  }
4026
4058
  class TsQueryColumn extends ColumnType {
4027
4059
  constructor(schema) {
4028
- super(schema, schema.stringSchema);
4060
+ super(schema, schema.stringSchema());
4029
4061
  this.dataType = "tsquery";
4030
4062
  this.operators = Operators.text;
4031
4063
  }
@@ -4037,7 +4069,7 @@ const uuidDefaultSQL = "gen_random_uuid()";
4037
4069
  const uuidDefault = new RawSQL(uuidDefaultSQL);
4038
4070
  class UUIDColumn extends ColumnType {
4039
4071
  constructor(schema) {
4040
- super(schema, schema.uuid);
4072
+ super(schema, schema.uuid());
4041
4073
  this.dataType = "uuid";
4042
4074
  this.operators = Operators.text;
4043
4075
  }
@@ -4060,7 +4092,7 @@ class UUIDColumn extends ColumnType {
4060
4092
  }
4061
4093
  class XMLColumn extends ColumnType {
4062
4094
  constructor(schema) {
4063
- super(schema, schema.stringSchema);
4095
+ super(schema, schema.stringSchema());
4064
4096
  this.dataType = "xml";
4065
4097
  this.operators = Operators.text;
4066
4098
  }
@@ -4081,7 +4113,7 @@ class CitextColumn extends TextBaseColumn {
4081
4113
 
4082
4114
  class BooleanColumn extends ColumnType {
4083
4115
  constructor(schema) {
4084
- super(schema, schema.boolean);
4116
+ super(schema, schema.boolean());
4085
4117
  this.dataType = "boolean";
4086
4118
  this.operators = Operators.boolean;
4087
4119
  this.parseItem = (input) => input[0] === "t";
@@ -4093,7 +4125,12 @@ class BooleanColumn extends ColumnType {
4093
4125
 
4094
4126
  class CustomTypeColumn extends ColumnType {
4095
4127
  constructor(schema, dataType) {
4096
- super(schema, schema.unknown, schema.unknown, schema.unknown);
4128
+ super(
4129
+ schema,
4130
+ schema.unknown(),
4131
+ schema.unknown(),
4132
+ schema.unknown()
4133
+ );
4097
4134
  this.dataType = dataType;
4098
4135
  this.operators = Operators.any;
4099
4136
  this.data.isOfCustomType = true;
@@ -4342,7 +4379,7 @@ const makeColumnTypes = (schema) => {
4342
4379
  RawSQL.prototype.columnTypes = makeColumnTypes;
4343
4380
 
4344
4381
  class VirtualColumn extends ColumnType {
4345
- constructor(schema, inputSchema = schema.never) {
4382
+ constructor(schema, inputSchema = schema.never()) {
4346
4383
  super(schema, inputSchema);
4347
4384
  this.dataType = "";
4348
4385
  this.operators = Operators.any;
@@ -4354,7 +4391,7 @@ class VirtualColumn extends ColumnType {
4354
4391
 
4355
4392
  class UnknownColumn extends VirtualColumn {
4356
4393
  constructor(schema) {
4357
- super(schema, schema.unknown);
4394
+ super(schema, schema.unknown());
4358
4395
  }
4359
4396
  }
4360
4397
  RawSQL.prototype._type = new UnknownColumn(defaultSchemaConfig);
@@ -7332,6 +7369,35 @@ class Join {
7332
7369
  *
7333
7370
  * When no matching record is found, it will skip records of the main table.
7334
7371
  *
7372
+ * When joining the same table with the same condition more than once, duplicated joins will be ignored:
7373
+ *
7374
+ * ```ts
7375
+ * // joining a relation
7376
+ * db.post.join('comments').join('comments');
7377
+ *
7378
+ * // joining a table with a condition
7379
+ * db.post
7380
+ * .join('comments', 'comments.postId', 'post.id')
7381
+ * .join('comments', 'comments.postId', 'post.id');
7382
+ * ```
7383
+ *
7384
+ * Both queries will produce SQL with only 1 join
7385
+ *
7386
+ * ```sql
7387
+ * SELECT * FROM post JOIN comments ON comments.postId = post.id
7388
+ * ```
7389
+ *
7390
+ * However, this is only possible if the join has no dynamic values:
7391
+ *
7392
+ * ```ts
7393
+ * db.post
7394
+ * .join('comments', (q) => q.where({ rating: { gt: 5 } }))
7395
+ * .join('comments', (q) => q.where({ rating: { gt: 5 } }));
7396
+ * ```
7397
+ *
7398
+ * Both joins above have the same `{ gt: 5 }`, but still, the `5` is a dynamic value and in this case joins will be duplicated,
7399
+ * resulting in a database error.
7400
+ *
7335
7401
  * ### join relation
7336
7402
  *
7337
7403
  * When relations are defined between the tables, you can join them by a relation name.