drizzle-orm 0.28.0-1f79521 → 0.28.0-5dd9190

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.
Files changed (130) hide show
  1. package/{alias-d10e3a09.cjs → alias-3f5ac719.cjs} +380 -179
  2. package/alias-3f5ac719.cjs.map +1 -0
  3. package/{alias-90302e28.mjs → alias-7164ff43.mjs} +302 -126
  4. package/alias-7164ff43.mjs.map +1 -0
  5. package/aws-data-api/pg/index.cjs +1 -1
  6. package/aws-data-api/pg/index.d.ts +4 -4
  7. package/aws-data-api/pg/index.mjs +2 -2
  8. package/aws-data-api/pg/migrator.d.ts +4 -4
  9. package/better-sqlite3/index.cjs +8 -8
  10. package/better-sqlite3/index.cjs.map +1 -1
  11. package/better-sqlite3/index.d.ts +5 -5
  12. package/better-sqlite3/index.mjs +2 -2
  13. package/better-sqlite3/migrator.d.ts +5 -5
  14. package/bun-sqlite/index.cjs +8 -8
  15. package/bun-sqlite/index.cjs.map +1 -1
  16. package/bun-sqlite/index.d.ts +5 -5
  17. package/bun-sqlite/index.mjs +2 -2
  18. package/bun-sqlite/migrator.d.ts +5 -5
  19. package/column.d-9d2f4045.d.ts +1151 -0
  20. package/d1/index.cjs +8 -8
  21. package/d1/index.cjs.map +1 -1
  22. package/d1/index.d.ts +5 -5
  23. package/d1/index.mjs +2 -2
  24. package/d1/migrator.d.ts +5 -5
  25. package/{driver.d-fa11e40f.d.ts → driver.d-055d4782.d.ts} +2 -2
  26. package/{driver.d-75ef8cb0.d.ts → driver.d-20707624.d.ts} +2 -2
  27. package/{driver.d-c77c3397.d.ts → driver.d-ab0f126f.d.ts} +2 -2
  28. package/{driver.d-03bef305.d.ts → driver.d-b8dd753f.d.ts} +2 -2
  29. package/{driver.d-dc08faa4.d.ts → driver.d-c4dd56f7.d.ts} +2 -2
  30. package/{driver.d-1f88d173.d.ts → driver.d-eb654ed5.d.ts} +2 -2
  31. package/index.cjs +3 -3
  32. package/index.d.ts +4 -4
  33. package/index.mjs +2 -2
  34. package/knex/index.d.ts +1 -1
  35. package/kysely/index.d.ts +1 -1
  36. package/libsql/index.cjs +8 -8
  37. package/libsql/index.cjs.map +1 -1
  38. package/libsql/index.d.ts +5 -5
  39. package/libsql/index.mjs +2 -2
  40. package/libsql/migrator.d.ts +5 -5
  41. package/mysql-core/index.cjs +67 -21
  42. package/mysql-core/index.cjs.map +1 -1
  43. package/mysql-core/index.d.ts +185 -344
  44. package/mysql-core/index.mjs +64 -23
  45. package/mysql-core/index.mjs.map +1 -1
  46. package/mysql2/index.cjs +14 -9
  47. package/mysql2/index.cjs.map +1 -1
  48. package/mysql2/index.d.ts +18 -9
  49. package/mysql2/index.mjs +15 -9
  50. package/mysql2/index.mjs.map +1 -1
  51. package/mysql2/migrator.d.ts +4 -4
  52. package/neon-http/index.cjs +144 -0
  53. package/neon-http/index.cjs.map +1 -0
  54. package/neon-http/index.d.ts +68 -0
  55. package/neon-http/index.mjs +149 -0
  56. package/neon-http/index.mjs.map +1 -0
  57. package/neon-http/migrator.cjs +46 -0
  58. package/neon-http/migrator.cjs.map +1 -0
  59. package/neon-http/migrator.d.ts +19 -0
  60. package/neon-http/migrator.mjs +44 -0
  61. package/neon-http/migrator.mjs.map +1 -0
  62. package/neon-serverless/index.cjs +1 -1
  63. package/neon-serverless/index.d.ts +4 -4
  64. package/neon-serverless/index.mjs +2 -2
  65. package/neon-serverless/migrator.d.ts +4 -4
  66. package/node-postgres/index.cjs +2 -1
  67. package/node-postgres/index.cjs.map +1 -1
  68. package/node-postgres/index.d.ts +4 -4
  69. package/node-postgres/index.mjs +3 -2
  70. package/node-postgres/index.mjs.map +1 -1
  71. package/node-postgres/migrator.d.ts +4 -4
  72. package/package.json +16 -4
  73. package/pg-core/index.cjs +6 -1
  74. package/pg-core/index.cjs.map +1 -1
  75. package/pg-core/index.d.ts +208 -373
  76. package/pg-core/index.mjs +48 -18
  77. package/pg-core/index.mjs.map +1 -1
  78. package/planetscale-serverless/index.cjs +6 -3
  79. package/planetscale-serverless/index.cjs.map +1 -1
  80. package/planetscale-serverless/index.d.ts +6 -5
  81. package/planetscale-serverless/index.mjs +6 -3
  82. package/planetscale-serverless/index.mjs.map +1 -1
  83. package/planetscale-serverless/migrator.d.ts +4 -4
  84. package/postgres-js/index.cjs +1 -1
  85. package/postgres-js/index.d.ts +4 -4
  86. package/postgres-js/index.mjs +2 -2
  87. package/postgres-js/migrator.d.ts +4 -4
  88. package/{query-promise.d-f1d00de0.d.ts → query-promise.d-bc96befc.d.ts} +48 -45
  89. package/{db.d-764a7144.d.ts → select.types.d-570dd25a.d.ts} +281 -441
  90. package/{select.types.d-e607c32f.d.ts → select.types.d-73209a67.d.ts} +58 -230
  91. package/{select.types.d-2c3ec4bd.d.ts → select.types.d-a3c4c974.d.ts} +20 -19
  92. package/{db.d-6bc873d8.d.ts → select.types.d-d0a10728.d.ts} +472 -704
  93. package/{session-e6ed6b82.mjs → session-0c131cde.mjs} +2 -2
  94. package/{session-e6ed6b82.mjs.map → session-0c131cde.mjs.map} +1 -1
  95. package/{session-2000845e.mjs → session-16f863cd.mjs} +96 -65
  96. package/session-16f863cd.mjs.map +1 -0
  97. package/{session-d4be5b4b.cjs → session-805d2876.cjs} +330 -76
  98. package/session-805d2876.cjs.map +1 -0
  99. package/{session-daa36f74.mjs → session-ac71392b.mjs} +324 -67
  100. package/session-ac71392b.mjs.map +1 -0
  101. package/{unique-constraint-d11fc52e.cjs → session-e9306785.cjs} +208 -188
  102. package/session-e9306785.cjs.map +1 -0
  103. package/sql-js/index.cjs +8 -8
  104. package/sql-js/index.cjs.map +1 -1
  105. package/sql-js/index.d.ts +5 -5
  106. package/sql-js/index.mjs +2 -2
  107. package/sql-js/migrator.d.ts +5 -5
  108. package/sqlite-core/index.cjs +94 -87
  109. package/sqlite-core/index.cjs.map +1 -1
  110. package/sqlite-core/index.d.ts +84 -165
  111. package/sqlite-core/index.mjs +34 -39
  112. package/sqlite-core/index.mjs.map +1 -1
  113. package/sqlite-proxy/index.cjs +8 -8
  114. package/sqlite-proxy/index.cjs.map +1 -1
  115. package/sqlite-proxy/index.d.ts +6 -6
  116. package/sqlite-proxy/index.mjs +2 -2
  117. package/sqlite-proxy/migrator.cjs +1 -1
  118. package/sqlite-proxy/migrator.d.ts +5 -5
  119. package/sqlite-proxy/migrator.mjs +1 -1
  120. package/vercel-postgres/index.cjs +1 -1
  121. package/vercel-postgres/index.d.ts +4 -4
  122. package/vercel-postgres/index.mjs +2 -2
  123. package/vercel-postgres/migrator.d.ts +4 -4
  124. package/alias-90302e28.mjs.map +0 -1
  125. package/alias-d10e3a09.cjs.map +0 -1
  126. package/column.d-61b0c252.d.ts +0 -552
  127. package/session-2000845e.mjs.map +0 -1
  128. package/session-d4be5b4b.cjs.map +0 -1
  129. package/session-daa36f74.mjs.map +0 -1
  130. package/unique-constraint-d11fc52e.cjs.map +0 -1
@@ -25,7 +25,7 @@ function is(value, type) {
25
25
  return false;
26
26
  }
27
27
 
28
- var _a$U;
28
+ var _a$V;
29
29
  /*
30
30
  `Column` only accepts a full `ColumnConfig` as its generic.
31
31
  To infer parts of the config, use `AnyColumn` that accepts a partial config.
@@ -34,12 +34,18 @@ var _a$U;
34
34
  class Column {
35
35
  constructor(table, config) {
36
36
  this.table = table;
37
+ this.enumValues = undefined;
37
38
  this.config = config;
38
39
  this.name = config.name;
39
40
  this.notNull = config.notNull;
40
41
  this.default = config.default;
41
42
  this.hasDefault = config.hasDefault;
42
43
  this.primary = config.primaryKey;
44
+ this.isUnique = config.isUnique;
45
+ this.uniqueName = config.uniqueName;
46
+ this.uniqueType = config.uniqueType;
47
+ this.dataType = config.dataType;
48
+ this.columnType = config.columnType;
43
49
  }
44
50
  mapFromDriverValue(value) {
45
51
  return value;
@@ -48,10 +54,10 @@ class Column {
48
54
  return value;
49
55
  }
50
56
  }
51
- _a$U = entityKind;
52
- Column[_a$U] = 'Column';
57
+ _a$V = entityKind;
58
+ Column[_a$V] = 'Column';
53
59
 
54
- var _a$T, _b$J, _c$d, _d$7;
60
+ var _a$U, _b$K, _c$e, _d$7;
55
61
  /** @internal */
56
62
  const TableName = Symbol.for('drizzle:Name');
57
63
  /** @internal */
@@ -70,17 +76,20 @@ const IsDrizzleTable = Symbol.for('drizzle:IsDrizzleTable');
70
76
  class Table {
71
77
  constructor(name, schema, baseName) {
72
78
  /** @internal */
73
- this[_b$J] = false;
79
+ this[_b$K] = false;
74
80
  /** @internal */
75
- this[_c$d] = undefined;
81
+ this[_c$e] = undefined;
76
82
  this[_d$7] = true;
77
83
  this[TableName] = this[OriginalName] = name;
78
84
  this[Schema] = schema;
79
85
  this[BaseName] = baseName;
80
86
  }
87
+ getSQL() {
88
+ return new SQL([this]);
89
+ }
81
90
  }
82
- _a$T = entityKind, _b$J = IsAlias, _c$d = ExtraConfigBuilder, _d$7 = IsDrizzleTable;
83
- Table[_a$T] = 'Table';
91
+ _a$U = entityKind, _b$K = IsAlias, _c$e = ExtraConfigBuilder, _d$7 = IsDrizzleTable;
92
+ Table[_a$U] = 'Table';
84
93
  /** @internal */
85
94
  Table.Symbol = {
86
95
  Name: TableName,
@@ -98,7 +107,7 @@ function getTableName(table) {
98
107
  return table[TableName];
99
108
  }
100
109
 
101
- var _a$S, _b$I;
110
+ var _a$T, _b$J;
102
111
  class CheckBuilder {
103
112
  constructor(name, value) {
104
113
  this.name = name;
@@ -109,8 +118,8 @@ class CheckBuilder {
109
118
  return new Check(table, this);
110
119
  }
111
120
  }
112
- _a$S = entityKind;
113
- CheckBuilder[_a$S] = 'PgCheckBuilder';
121
+ _a$T = entityKind;
122
+ CheckBuilder[_a$T] = 'PgCheckBuilder';
114
123
  class Check {
115
124
  constructor(table, builder) {
116
125
  this.table = table;
@@ -118,26 +127,26 @@ class Check {
118
127
  this.value = builder.value;
119
128
  }
120
129
  }
121
- _b$I = entityKind;
122
- Check[_b$I] = 'PgCheck';
130
+ _b$J = entityKind;
131
+ Check[_b$J] = 'PgCheck';
123
132
  function check(name, value) {
124
133
  return new CheckBuilder(name, value);
125
134
  }
126
135
 
127
- var _a$R, _b$H, _c$c;
136
+ var _a$S, _b$I, _c$d;
128
137
  /** @internal */
129
138
  const InlineForeignKeys = Symbol.for('drizzle:PgInlineForeignKeys');
130
139
  class PgTable extends Table {
131
140
  constructor() {
132
141
  super(...arguments);
133
142
  /**@internal */
134
- this[_b$H] = [];
143
+ this[_b$I] = [];
135
144
  /** @internal */
136
- this[_c$c] = undefined;
145
+ this[_c$d] = undefined;
137
146
  }
138
147
  }
139
- _a$R = entityKind, _b$H = InlineForeignKeys, _c$c = Table.Symbol.ExtraConfigBuilder;
140
- PgTable[_a$R] = 'PgTable';
148
+ _a$S = entityKind, _b$I = InlineForeignKeys, _c$d = Table.Symbol.ExtraConfigBuilder;
149
+ PgTable[_a$S] = 'PgTable';
141
150
  /** @internal */
142
151
  PgTable.Symbol = Object.assign({}, Table.Symbol, {
143
152
  InlineForeignKeys: InlineForeignKeys,
@@ -166,7 +175,7 @@ function pgTableCreator(customizeTableName) {
166
175
  };
167
176
  }
168
177
 
169
- var _a$Q;
178
+ var _a$R;
170
179
  const ViewBaseConfig = Symbol.for('drizzle:ViewBaseConfig');
171
180
  class View {
172
181
  constructor({ name, schema, selectedFields, query }) {
@@ -180,11 +189,14 @@ class View {
180
189
  isAlias: false,
181
190
  };
182
191
  }
192
+ getSQL() {
193
+ return new SQL([this]);
194
+ }
183
195
  }
184
- _a$Q = entityKind;
185
- View[_a$Q] = 'View';
196
+ _a$R = entityKind;
197
+ View[_a$R] = 'View';
186
198
 
187
- var _a$P, _b$G;
199
+ var _a$Q, _b$H;
188
200
  class ForeignKeyBuilder {
189
201
  constructor(config, actions) {
190
202
  /** @internal */
@@ -213,8 +225,8 @@ class ForeignKeyBuilder {
213
225
  return new ForeignKey(table, this);
214
226
  }
215
227
  }
216
- _a$P = entityKind;
217
- ForeignKeyBuilder[_a$P] = 'PgForeignKeyBuilder';
228
+ _a$Q = entityKind;
229
+ ForeignKeyBuilder[_a$Q] = 'PgForeignKeyBuilder';
218
230
  class ForeignKey {
219
231
  constructor(table, builder) {
220
232
  this.table = table;
@@ -235,8 +247,8 @@ class ForeignKey {
235
247
  return `${chunks.join('_')}_fk`;
236
248
  }
237
249
  }
238
- _b$G = entityKind;
239
- ForeignKey[_b$G] = 'PgForeignKey';
250
+ _b$H = entityKind;
251
+ ForeignKey[_b$H] = 'PgForeignKey';
240
252
  function foreignKey(config) {
241
253
  function mappedConfig() {
242
254
  const { columns, foreignColumns } = config;
@@ -248,7 +260,7 @@ function foreignKey(config) {
248
260
  return new ForeignKeyBuilder(mappedConfig);
249
261
  }
250
262
 
251
- var _a$O, _b$F, _c$b;
263
+ var _a$P, _b$G, _c$c;
252
264
  class IndexBuilderOn {
253
265
  constructor(unique, name) {
254
266
  this.unique = unique;
@@ -261,8 +273,8 @@ class IndexBuilderOn {
261
273
  return new IndexBuilder(columns, this.unique, true, this.name);
262
274
  }
263
275
  }
264
- _a$O = entityKind;
265
- IndexBuilderOn[_a$O] = 'PgIndexBuilderOn';
276
+ _a$P = entityKind;
277
+ IndexBuilderOn[_a$P] = 'PgIndexBuilderOn';
266
278
  class IndexBuilder {
267
279
  constructor(columns, unique, only, name) {
268
280
  this.config = {
@@ -305,15 +317,15 @@ class IndexBuilder {
305
317
  return new Index(this.config, table);
306
318
  }
307
319
  }
308
- _b$F = entityKind;
309
- IndexBuilder[_b$F] = 'PgIndexBuilder';
320
+ _b$G = entityKind;
321
+ IndexBuilder[_b$G] = 'PgIndexBuilder';
310
322
  class Index {
311
323
  constructor(config, table) {
312
324
  this.config = { ...config, table };
313
325
  }
314
326
  }
315
- _c$b = entityKind;
316
- Index[_c$b] = 'PgIndex';
327
+ _c$c = entityKind;
328
+ Index[_c$c] = 'PgIndex';
317
329
  function index(name) {
318
330
  return new IndexBuilderOn(false, name);
319
331
  }
@@ -321,7 +333,7 @@ function uniqueIndex(name) {
321
333
  return new IndexBuilderOn(true, name);
322
334
  }
323
335
 
324
- var _a$N, _b$E;
336
+ var _a$O, _b$F;
325
337
  function primaryKey(...columns) {
326
338
  return new PrimaryKeyBuilder(columns);
327
339
  }
@@ -334,8 +346,8 @@ class PrimaryKeyBuilder {
334
346
  return new PrimaryKey(table, this.columns);
335
347
  }
336
348
  }
337
- _a$N = entityKind;
338
- PrimaryKeyBuilder[_a$N] = 'PgPrimaryKeyBuilder';
349
+ _a$O = entityKind;
350
+ PrimaryKeyBuilder[_a$O] = 'PgPrimaryKeyBuilder';
339
351
  class PrimaryKey {
340
352
  constructor(table, columns) {
341
353
  this.table = table;
@@ -345,8 +357,58 @@ class PrimaryKey {
345
357
  return `${this.table[PgTable.Symbol.Name]}_${this.columns.map((column) => column.name).join('_')}_pk`;
346
358
  }
347
359
  }
360
+ _b$F = entityKind;
361
+ PrimaryKey[_b$F] = 'PgPrimaryKey';
362
+
363
+ var _a$N, _b$E, _c$b;
364
+ function unique(name) {
365
+ return new UniqueOnConstraintBuilder(name);
366
+ }
367
+ function uniqueKeyName(table, columns) {
368
+ return `${table[PgTable.Symbol.Name]}_${columns.join('_')}_unique`;
369
+ }
370
+ class UniqueConstraintBuilder {
371
+ constructor(columns, name) {
372
+ this.name = name;
373
+ /** @internal */
374
+ this.nullsNotDistinctConfig = false;
375
+ this.columns = columns;
376
+ }
377
+ nullsNotDistinct() {
378
+ this.nullsNotDistinctConfig = true;
379
+ return this;
380
+ }
381
+ /** @internal */
382
+ build(table) {
383
+ return new UniqueConstraint(table, this.columns, this.nullsNotDistinctConfig, this.name);
384
+ }
385
+ }
386
+ _a$N = entityKind;
387
+ UniqueConstraintBuilder[_a$N] = 'PgUniqueConstraintBuilder';
388
+ class UniqueOnConstraintBuilder {
389
+ constructor(name) {
390
+ this.name = name;
391
+ }
392
+ on(...columns) {
393
+ return new UniqueConstraintBuilder(columns, this.name);
394
+ }
395
+ }
348
396
  _b$E = entityKind;
349
- PrimaryKey[_b$E] = 'PgPrimaryKey';
397
+ UniqueOnConstraintBuilder[_b$E] = 'PgUniqueOnConstraintBuilder';
398
+ class UniqueConstraint {
399
+ constructor(table, columns, nullsNotDistinct, name) {
400
+ this.table = table;
401
+ this.nullsNotDistinct = false;
402
+ this.columns = columns;
403
+ this.name = name ?? uniqueKeyName(this.table, this.columns.map((column) => column.name));
404
+ this.nullsNotDistinct = nullsNotDistinct;
405
+ }
406
+ getName() {
407
+ return this.name;
408
+ }
409
+ }
410
+ _c$b = entityKind;
411
+ UniqueConstraint[_c$b] = 'PgUniqueConstraint';
350
412
 
351
413
  var _a$M, _b$D, _c$a;
352
414
  const SubqueryConfig = Symbol.for('drizzle:SubqueryConfig');
@@ -359,6 +421,9 @@ class Subquery {
359
421
  isWith,
360
422
  };
361
423
  }
424
+ getSQL() {
425
+ return new SQL([this]);
426
+ }
362
427
  }
363
428
  _a$M = entityKind;
364
429
  Subquery[_a$M] = 'Subquery';
@@ -938,12 +1003,9 @@ class PgDialect {
938
1003
  return sql `${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`;
939
1004
  }
940
1005
  buildInsertQuery({ table, values, onConflict, returning }) {
941
- const isSingleValue = values.length === 1;
942
1006
  const valuesSqlList = [];
943
1007
  const columns = table[Table.Symbol.Columns];
944
- const colEntries = isSingleValue
945
- ? Object.keys(values[0]).map((fieldName) => [fieldName, columns[fieldName]])
946
- : Object.entries(columns);
1008
+ const colEntries = Object.entries(columns);
947
1009
  const insertOrder = colEntries.map(([, column]) => sql.identifier(column.name));
948
1010
  for (const [valueIndex, value] of values.entries()) {
949
1011
  const valueList = [];
@@ -1502,10 +1564,10 @@ class PgDialect {
1502
1564
  }
1503
1565
  else {
1504
1566
  const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
1505
- const aliasedRelations = Object.fromEntries(Object.entries(tableConfig.relations).map(([key, value]) => [key, aliasedRelation(value, tableAlias)]));
1506
- const aliasedFields = Object.assign({}, aliasedColumns, aliasedRelations);
1507
1567
  if (config.where) {
1508
- const whereSql = typeof config.where === 'function' ? config.where(aliasedFields, operators) : config.where;
1568
+ const whereSql = typeof config.where === 'function'
1569
+ ? config.where(aliasedColumns, getOperators())
1570
+ : config.where;
1509
1571
  where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
1510
1572
  }
1511
1573
  const fieldsSelection = [];
@@ -1549,7 +1611,7 @@ class PgDialect {
1549
1611
  // Figure out which extras to select
1550
1612
  if (config.extras) {
1551
1613
  extras = typeof config.extras === 'function'
1552
- ? config.extras(aliasedFields, { sql })
1614
+ ? config.extras(aliasedColumns, { sql })
1553
1615
  : config.extras;
1554
1616
  for (const [tsKey, value] of Object.entries(extras)) {
1555
1617
  fieldsSelection.push({
@@ -1571,7 +1633,7 @@ class PgDialect {
1571
1633
  });
1572
1634
  }
1573
1635
  let orderByOrig = typeof config.orderBy === 'function'
1574
- ? config.orderBy(aliasedFields, orderByOperators)
1636
+ ? config.orderBy(aliasedColumns, getOrderByOperators())
1575
1637
  : config.orderBy ?? [];
1576
1638
  if (!Array.isArray(orderByOrig)) {
1577
1639
  orderByOrig = [orderByOrig];
@@ -1597,7 +1659,11 @@ class PgDialect {
1597
1659
  tableNamesMap,
1598
1660
  table: fullSchema[relationTableTsName],
1599
1661
  tableConfig: schema[relationTableTsName],
1600
- queryConfig: selectedRelationConfigValue,
1662
+ queryConfig: is(relation, One)
1663
+ ? (selectedRelationConfigValue === true
1664
+ ? { limit: 1 }
1665
+ : { ...selectedRelationConfigValue, limit: 1 })
1666
+ : selectedRelationConfigValue,
1601
1667
  tableAlias: relationTableAlias,
1602
1668
  joinOn,
1603
1669
  nestedQueryRelation: relation,
@@ -1624,7 +1690,6 @@ class PgDialect {
1624
1690
  throw new DrizzleError(`No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")`);
1625
1691
  }
1626
1692
  let result;
1627
- const needsSubquery = where || limit !== undefined || offset !== undefined || orderBy.length > 0;
1628
1693
  where = and(joinOn, where);
1629
1694
  if (nestedQueryRelation) {
1630
1695
  let field = sql `json_build_array(${sql.join(selection.map(({ field, tsKey, isJson }) => isJson
@@ -1644,6 +1709,7 @@ class PgDialect {
1644
1709
  relationTableTsKey: tableConfig.tsName,
1645
1710
  selection,
1646
1711
  }];
1712
+ const needsSubquery = limit !== undefined || offset !== undefined || orderBy.length > 0;
1647
1713
  if (needsSubquery) {
1648
1714
  result = this.buildSelectQuery({
1649
1715
  table: aliasedTable(table, tableAlias),
@@ -1680,57 +1746,19 @@ class PgDialect {
1680
1746
  });
1681
1747
  }
1682
1748
  else {
1683
- if (needsSubquery) {
1684
- const nestedSelection = selection.filter(({ field }) => !is(field, Column));
1685
- nestedSelection.splice(0, 0, {
1686
- dbKey: '*',
1687
- tsKey: '*',
1688
- field: sql `${sql.identifier(tableAlias)}.*`,
1689
- isJson: false,
1690
- relationTableTsKey: tableConfig.tsName,
1691
- selection: [],
1692
- });
1693
- result = this.buildSelectQuery({
1694
- table: aliasedTable(table, tableAlias),
1695
- fields: {},
1696
- fieldsFlat: nestedSelection.map(({ field }) => ({
1697
- path: [],
1698
- field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field,
1699
- })),
1700
- joins,
1701
- });
1702
- result = this.buildSelectQuery({
1703
- table: new Subquery(result, {}, tableAlias),
1704
- fields: {},
1705
- fieldsFlat: selection.map(({ field }) => ({
1706
- path: [],
1707
- field: is(field, Column)
1708
- ? sql `${sql.identifier(field.name)}`
1709
- : is(field, SQL.Aliased)
1710
- ? sql `${sql.identifier(field.fieldAlias)}`
1711
- : field,
1712
- })),
1713
- where,
1714
- limit,
1715
- offset,
1716
- orderBy,
1717
- });
1718
- }
1719
- else {
1720
- result = this.buildSelectQuery({
1721
- table: aliasedTable(table, tableAlias),
1722
- fields: {},
1723
- fieldsFlat: selection.map(({ field }) => ({
1724
- path: [],
1725
- field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field,
1726
- })),
1727
- joins,
1728
- where,
1729
- limit,
1730
- offset,
1731
- orderBy,
1732
- });
1733
- }
1749
+ result = this.buildSelectQuery({
1750
+ table: aliasedTable(table, tableAlias),
1751
+ fields: {},
1752
+ fieldsFlat: selection.map(({ field }) => ({
1753
+ path: [],
1754
+ field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field,
1755
+ })),
1756
+ joins,
1757
+ where,
1758
+ limit,
1759
+ offset,
1760
+ orderBy,
1761
+ });
1734
1762
  }
1735
1763
  return {
1736
1764
  tableTsKey: tableConfig.tsName,
@@ -2456,6 +2484,7 @@ function getTableConfig(table) {
2456
2484
  const checks = [];
2457
2485
  const primaryKeys = [];
2458
2486
  const foreignKeys = Object.values(table[PgTable.Symbol.InlineForeignKeys]);
2487
+ const uniqueConstraints = [];
2459
2488
  const name = table[Table.Symbol.Name];
2460
2489
  const schema = table[Table.Symbol.Schema];
2461
2490
  const extraConfigBuilder = table[PgTable.Symbol.ExtraConfigBuilder];
@@ -2468,6 +2497,9 @@ function getTableConfig(table) {
2468
2497
  else if (is(builder, CheckBuilder)) {
2469
2498
  checks.push(builder.build(table));
2470
2499
  }
2500
+ else if (is(builder, UniqueConstraintBuilder)) {
2501
+ uniqueConstraints.push(builder.build(table));
2502
+ }
2471
2503
  else if (is(builder, PrimaryKeyBuilder)) {
2472
2504
  primaryKeys.push(builder.build(table));
2473
2505
  }
@@ -2482,6 +2514,7 @@ function getTableConfig(table) {
2482
2514
  foreignKeys,
2483
2515
  checks,
2484
2516
  primaryKeys,
2517
+ uniqueConstraints,
2485
2518
  name,
2486
2519
  schema,
2487
2520
  };
@@ -2576,12 +2609,18 @@ function makePgArray(array) {
2576
2609
  var _a$A;
2577
2610
  // To understand how to use `ColumnBuilder` and `AnyColumnBuilder`, see `Column` and `AnyColumn` documentation.
2578
2611
  class ColumnBuilder {
2579
- constructor(name) {
2612
+ constructor(name, dataType, columnType) {
2580
2613
  this.config = {
2581
2614
  name,
2582
2615
  notNull: false,
2583
2616
  default: undefined,
2617
+ hasDefault: false,
2584
2618
  primaryKey: false,
2619
+ isUnique: false,
2620
+ uniqueName: undefined,
2621
+ uniqueType: undefined,
2622
+ dataType,
2623
+ columnType,
2585
2624
  };
2586
2625
  }
2587
2626
  $type() {
@@ -2618,10 +2657,16 @@ class PgColumnBuilder extends ColumnBuilder {
2618
2657
  this.foreignKeyConfigs.push({ ref, actions });
2619
2658
  return this;
2620
2659
  }
2660
+ unique(name, config) {
2661
+ this.config.isUnique = true;
2662
+ this.config.uniqueName = name;
2663
+ this.config.uniqueType = config?.nulls;
2664
+ return this;
2665
+ }
2621
2666
  /** @internal */
2622
2667
  buildForeignKeys(column, table) {
2623
2668
  return this.foreignKeyConfigs.map(({ ref, actions }) => {
2624
- return ((ref, actions) => {
2669
+ return iife((ref, actions) => {
2625
2670
  const builder = new ForeignKeyBuilder(() => {
2626
2671
  const foreignColumn = ref();
2627
2672
  return { columns: [column], foreignColumns: [foreignColumn] };
@@ -2633,7 +2678,7 @@ class PgColumnBuilder extends ColumnBuilder {
2633
2678
  builder.onDelete(actions.onDelete);
2634
2679
  }
2635
2680
  return builder.build(table);
2636
- })(ref, actions);
2681
+ }, ref, actions);
2637
2682
  });
2638
2683
  }
2639
2684
  }
@@ -2641,6 +2686,13 @@ _a$z = entityKind;
2641
2686
  PgColumnBuilder[_a$z] = 'PgColumnBuilder';
2642
2687
  // To understand how to use `PgColumn` and `AnyPgColumn`, see `Column` and `AnyColumn` documentation.
2643
2688
  class PgColumn extends Column {
2689
+ constructor(table, config) {
2690
+ if (!config.uniqueName) {
2691
+ config.uniqueName = uniqueKeyName(table, [config.name]);
2692
+ }
2693
+ super(table, config);
2694
+ this.table = table;
2695
+ }
2644
2696
  }
2645
2697
  _b$w = entityKind;
2646
2698
  PgColumn[_b$w] = 'PgColumn';
@@ -2648,7 +2700,7 @@ PgColumn[_b$w] = 'PgColumn';
2648
2700
  var _a$y, _b$v;
2649
2701
  class PgArrayBuilder extends PgColumnBuilder {
2650
2702
  constructor(name, baseBuilder, size) {
2651
- super(name);
2703
+ super(name, 'array', 'PgArray');
2652
2704
  this.config.baseBuilder = baseBuilder;
2653
2705
  this.config.size = size;
2654
2706
  }
@@ -2693,6 +2745,9 @@ PgArray[_b$v] = 'PgArray';
2693
2745
 
2694
2746
  var _a$x, _b$u, _c$7, _d$5;
2695
2747
  class PgBigInt53Builder extends PgColumnBuilder {
2748
+ constructor(name) {
2749
+ super(name, 'number', 'PgBigInt53');
2750
+ }
2696
2751
  /** @internal */
2697
2752
  build(table) {
2698
2753
  return new PgBigInt53(table, this.config);
@@ -2714,6 +2769,9 @@ class PgBigInt53 extends PgColumn {
2714
2769
  _b$u = entityKind;
2715
2770
  PgBigInt53[_b$u] = 'PgBigInt53';
2716
2771
  class PgBigInt64Builder extends PgColumnBuilder {
2772
+ constructor(name) {
2773
+ super(name, 'bigint', 'PgBigInt64');
2774
+ }
2717
2775
  /** @internal */
2718
2776
  build(table) {
2719
2777
  return new PgBigInt64(table, this.config);
@@ -2742,7 +2800,7 @@ function bigint(name, config) {
2742
2800
  var _a$w, _b$t, _c$6, _d$4;
2743
2801
  class PgBigSerial53Builder extends PgColumnBuilder {
2744
2802
  constructor(name) {
2745
- super(name);
2803
+ super(name, 'number', 'PgBigSerial53');
2746
2804
  this.config.hasDefault = true;
2747
2805
  this.config.notNull = true;
2748
2806
  }
@@ -2768,7 +2826,7 @@ _b$t = entityKind;
2768
2826
  PgBigSerial53[_b$t] = 'PgBigSerial53';
2769
2827
  class PgBigSerial64Builder extends PgColumnBuilder {
2770
2828
  constructor(name) {
2771
- super(name);
2829
+ super(name, 'bigint', 'PgBigSerial64');
2772
2830
  this.config.hasDefault = true;
2773
2831
  }
2774
2832
  /** @internal */
@@ -2798,6 +2856,9 @@ function bigserial(name, { mode }) {
2798
2856
 
2799
2857
  var _a$v, _b$s;
2800
2858
  class PgBooleanBuilder extends PgColumnBuilder {
2859
+ constructor(name) {
2860
+ super(name, 'boolean', 'PgBoolean');
2861
+ }
2801
2862
  /** @internal */
2802
2863
  build(table) {
2803
2864
  return new PgBoolean(table, this.config);
@@ -2819,9 +2880,9 @@ function boolean(name) {
2819
2880
  var _a$u, _b$r;
2820
2881
  class PgCharBuilder extends PgColumnBuilder {
2821
2882
  constructor(name, config) {
2822
- super(name);
2883
+ super(name, 'string', 'PgChar');
2823
2884
  this.config.length = config.length;
2824
- this.config.enumValues = (config.enum ?? []);
2885
+ this.config.enumValues = config.enum;
2825
2886
  }
2826
2887
  /** @internal */
2827
2888
  build(table) {
@@ -2848,6 +2909,9 @@ function char(name, config = {}) {
2848
2909
 
2849
2910
  var _a$t, _b$q;
2850
2911
  class PgCidrBuilder extends PgColumnBuilder {
2912
+ constructor(name) {
2913
+ super(name, 'string', 'PgCidr');
2914
+ }
2851
2915
  /** @internal */
2852
2916
  build(table) {
2853
2917
  return new PgCidr(table, this.config);
@@ -2869,7 +2933,7 @@ function cidr(name) {
2869
2933
  var _a$s, _b$p;
2870
2934
  class PgCustomColumnBuilder extends PgColumnBuilder {
2871
2935
  constructor(name, fieldConfig, customTypeParams) {
2872
- super(name);
2936
+ super(name, 'custom', 'PgCustomColumn');
2873
2937
  this.config.fieldConfig = fieldConfig;
2874
2938
  this.config.customTypeParams = customTypeParams;
2875
2939
  }
@@ -2919,6 +2983,9 @@ PgDateColumnBaseBuilder[_a$r] = 'PgDateColumnBaseBuilder';
2919
2983
 
2920
2984
  var _a$q, _b$o, _c$5, _d$3;
2921
2985
  class PgDateBuilder extends PgDateColumnBaseBuilder {
2986
+ constructor(name) {
2987
+ super(name, 'date', 'PgDate');
2988
+ }
2922
2989
  /** @internal */
2923
2990
  build(table) {
2924
2991
  return new PgDate(table, this.config);
@@ -2940,6 +3007,9 @@ class PgDate extends PgColumn {
2940
3007
  _b$o = entityKind;
2941
3008
  PgDate[_b$o] = 'PgDate';
2942
3009
  class PgDateStringBuilder extends PgDateColumnBaseBuilder {
3010
+ constructor(name) {
3011
+ super(name, 'string', 'PgDateString');
3012
+ }
2943
3013
  /** @internal */
2944
3014
  build(table) {
2945
3015
  return new PgDateString(table, this.config);
@@ -2963,6 +3033,9 @@ function date(name, config) {
2963
3033
 
2964
3034
  var _a$p, _b$n;
2965
3035
  class PgDoublePrecisionBuilder extends PgColumnBuilder {
3036
+ constructor(name) {
3037
+ super(name, 'number', 'PgDoublePrecision');
3038
+ }
2966
3039
  /** @internal */
2967
3040
  build(table) {
2968
3041
  return new PgDoublePrecision(table, this.config);
@@ -2990,11 +3063,11 @@ function doublePrecision(name) {
2990
3063
  var _a$o, _b$m;
2991
3064
  const isPgEnumSym = Symbol.for('drizzle:isPgEnum');
2992
3065
  function isPgEnum(obj) {
2993
- return !!obj && typeof obj === 'function' && isPgEnumSym in obj;
3066
+ return !!obj && typeof obj === 'function' && isPgEnumSym in obj && obj[isPgEnumSym] === true;
2994
3067
  }
2995
3068
  class PgEnumColumnBuilder extends PgColumnBuilder {
2996
3069
  constructor(name, enumInstance) {
2997
- super(name);
3070
+ super(name, 'string', 'PgEnumColumn');
2998
3071
  this.config.enum = enumInstance;
2999
3072
  }
3000
3073
  /** @internal */
@@ -3029,6 +3102,9 @@ function pgEnum(enumName, values) {
3029
3102
 
3030
3103
  var _a$n, _b$l;
3031
3104
  class PgInetBuilder extends PgColumnBuilder {
3105
+ constructor(name) {
3106
+ super(name, 'string', 'PgInet');
3107
+ }
3032
3108
  /** @internal */
3033
3109
  build(table) {
3034
3110
  return new PgInet(table, this.config);
@@ -3049,6 +3125,9 @@ function inet(name) {
3049
3125
 
3050
3126
  var _a$m, _b$k;
3051
3127
  class PgIntegerBuilder extends PgColumnBuilder {
3128
+ constructor(name) {
3129
+ super(name, 'number', 'PgInteger');
3130
+ }
3052
3131
  /** @internal */
3053
3132
  build(table) {
3054
3133
  return new PgInteger(table, this.config);
@@ -3076,7 +3155,7 @@ function integer(name) {
3076
3155
  var _a$l, _b$j;
3077
3156
  class PgIntervalBuilder extends PgColumnBuilder {
3078
3157
  constructor(name, intervalConfig) {
3079
- super(name);
3158
+ super(name, 'string', 'PgInterval');
3080
3159
  this.config.intervalConfig = intervalConfig;
3081
3160
  }
3082
3161
  /** @internal */
@@ -3106,6 +3185,9 @@ function interval(name, config = {}) {
3106
3185
 
3107
3186
  var _a$k, _b$i;
3108
3187
  class PgJsonBuilder extends PgColumnBuilder {
3188
+ constructor(name) {
3189
+ super(name, 'json', 'PgJson');
3190
+ }
3109
3191
  /** @internal */
3110
3192
  build(table) {
3111
3193
  return new PgJson(table, this.config);
@@ -3143,6 +3225,9 @@ function json(name) {
3143
3225
 
3144
3226
  var _a$j, _b$h;
3145
3227
  class PgJsonbBuilder extends PgColumnBuilder {
3228
+ constructor(name) {
3229
+ super(name, 'json', 'PgJsonb');
3230
+ }
3146
3231
  /** @internal */
3147
3232
  build(table) {
3148
3233
  return new PgJsonb(table, this.config);
@@ -3180,6 +3265,9 @@ function jsonb(name) {
3180
3265
 
3181
3266
  var _a$i, _b$g;
3182
3267
  class PgMacaddrBuilder extends PgColumnBuilder {
3268
+ constructor(name) {
3269
+ super(name, 'string', 'PgMacaddr');
3270
+ }
3183
3271
  /** @internal */
3184
3272
  build(table) {
3185
3273
  return new PgMacaddr(table, this.config);
@@ -3200,6 +3288,9 @@ function macaddr(name) {
3200
3288
 
3201
3289
  var _a$h, _b$f;
3202
3290
  class PgMacaddr8Builder extends PgColumnBuilder {
3291
+ constructor(name) {
3292
+ super(name, 'string', 'PgMacaddr8');
3293
+ }
3203
3294
  /** @internal */
3204
3295
  build(table) {
3205
3296
  return new PgMacaddr8(table, this.config);
@@ -3221,7 +3312,7 @@ function macaddr8(name) {
3221
3312
  var _a$g, _b$e;
3222
3313
  class PgNumericBuilder extends PgColumnBuilder {
3223
3314
  constructor(name, precision, scale) {
3224
- super(name);
3315
+ super(name, 'string', 'PgNumeric');
3225
3316
  this.config.precision = precision;
3226
3317
  this.config.scale = scale;
3227
3318
  }
@@ -3260,7 +3351,7 @@ const decimal = numeric;
3260
3351
  var _a$f, _b$d;
3261
3352
  class PgRealBuilder extends PgColumnBuilder {
3262
3353
  constructor(name, length) {
3263
- super(name);
3354
+ super(name, 'number', 'PgReal');
3264
3355
  this.config.length = length;
3265
3356
  }
3266
3357
  /** @internal */
@@ -3293,7 +3384,7 @@ function real(name) {
3293
3384
  var _a$e, _b$c;
3294
3385
  class PgSerialBuilder extends PgColumnBuilder {
3295
3386
  constructor(name) {
3296
- super(name);
3387
+ super(name, 'number', 'PgSerial');
3297
3388
  this.config.hasDefault = true;
3298
3389
  this.config.notNull = true;
3299
3390
  }
@@ -3317,6 +3408,9 @@ function serial(name) {
3317
3408
 
3318
3409
  var _a$d, _b$b;
3319
3410
  class PgSmallIntBuilder extends PgColumnBuilder {
3411
+ constructor(name) {
3412
+ super(name, 'number', 'PgSmallInt');
3413
+ }
3320
3414
  /** @internal */
3321
3415
  build(table) {
3322
3416
  return new PgSmallInt(table, this.config);
@@ -3347,7 +3441,7 @@ function smallint(name) {
3347
3441
  var _a$c, _b$a;
3348
3442
  class PgSmallSerialBuilder extends PgColumnBuilder {
3349
3443
  constructor(name) {
3350
- super(name);
3444
+ super(name, 'number', 'PgSmallSerial');
3351
3445
  this.config.hasDefault = true;
3352
3446
  this.config.notNull = true;
3353
3447
  }
@@ -3372,8 +3466,8 @@ function smallserial(name) {
3372
3466
  var _a$b, _b$9;
3373
3467
  class PgTextBuilder extends PgColumnBuilder {
3374
3468
  constructor(name, config) {
3375
- super(name);
3376
- this.config.enumValues = (config.enum ?? []);
3469
+ super(name, 'string', 'PgText');
3470
+ this.config.enumValues = config.enum;
3377
3471
  }
3378
3472
  /** @internal */
3379
3473
  build(table) {
@@ -3400,7 +3494,7 @@ function text(name, config = {}) {
3400
3494
  var _a$a, _b$8;
3401
3495
  class PgTimeBuilder extends PgDateColumnBaseBuilder {
3402
3496
  constructor(name, withTimezone, precision) {
3403
- super(name);
3497
+ super(name, 'string', 'PgTime');
3404
3498
  this.withTimezone = withTimezone;
3405
3499
  this.precision = precision;
3406
3500
  this.config.withTimezone = withTimezone;
@@ -3433,7 +3527,7 @@ function time(name, config = {}) {
3433
3527
  var _a$9, _b$7, _c$4, _d$2;
3434
3528
  class PgTimestampBuilder extends PgDateColumnBaseBuilder {
3435
3529
  constructor(name, withTimezone, precision) {
3436
- super(name);
3530
+ super(name, 'date', 'PgTimestamp');
3437
3531
  this.config.withTimezone = withTimezone;
3438
3532
  this.config.precision = precision;
3439
3533
  }
@@ -3465,7 +3559,7 @@ _b$7 = entityKind;
3465
3559
  PgTimestamp[_b$7] = 'PgTimestamp';
3466
3560
  class PgTimestampStringBuilder extends PgDateColumnBaseBuilder {
3467
3561
  constructor(name, withTimezone, precision) {
3468
- super(name);
3562
+ super(name, 'string', 'PgTimestampString');
3469
3563
  this.config.withTimezone = withTimezone;
3470
3564
  this.config.precision = precision;
3471
3565
  }
@@ -3498,6 +3592,9 @@ function timestamp(name, config = {}) {
3498
3592
 
3499
3593
  var _a$8, _b$6;
3500
3594
  class PgUUIDBuilder extends PgColumnBuilder {
3595
+ constructor(name) {
3596
+ super(name, 'string', 'PgUUID');
3597
+ }
3501
3598
  /**
3502
3599
  * Adds `default gen_random_uuid()` to the column definition.
3503
3600
  */
@@ -3525,9 +3622,9 @@ function uuid(name) {
3525
3622
  var _a$7, _b$5;
3526
3623
  class PgVarcharBuilder extends PgColumnBuilder {
3527
3624
  constructor(name, config) {
3528
- super(name);
3625
+ super(name, 'string', 'PgVarchar');
3529
3626
  this.config.length = config.length;
3530
- this.config.enumValues = (config.enum ?? []);
3627
+ this.config.enumValues = config.enum;
3531
3628
  }
3532
3629
  /** @internal */
3533
3630
  build(table) {
@@ -3850,17 +3947,21 @@ class Many extends Relation {
3850
3947
  }
3851
3948
  _d$1 = entityKind;
3852
3949
  Many[_d$1] = 'Many';
3853
- const operators = {
3854
- sql,
3855
- eq,
3856
- and,
3857
- or,
3858
- };
3859
- const orderByOperators = {
3860
- sql,
3861
- asc,
3862
- desc,
3863
- };
3950
+ function getOperators() {
3951
+ return {
3952
+ sql,
3953
+ eq,
3954
+ and,
3955
+ or,
3956
+ };
3957
+ }
3958
+ function getOrderByOperators() {
3959
+ return {
3960
+ sql,
3961
+ asc,
3962
+ desc,
3963
+ };
3964
+ }
3864
3965
  function extractTablesRelationalConfig(schema, configHelpers) {
3865
3966
  if (Object.keys(schema).length === 1 && 'default' in schema && !is(schema['default'], Table)) {
3866
3967
  schema = schema['default'];
@@ -3946,8 +4047,8 @@ function normalizeRelation(schema, tableNamesMap, relation) {
3946
4047
  if (!referencedTableTsName) {
3947
4048
  throw new Error(`Table "${relation.referencedTable[Table.Symbol.Name]}" not found in schema`);
3948
4049
  }
3949
- const referencedTableFields = schema[referencedTableTsName];
3950
- if (!referencedTableFields) {
4050
+ const referencedTableConfig = schema[referencedTableTsName];
4051
+ if (!referencedTableConfig) {
3951
4052
  throw new Error(`Table "${referencedTableTsName}" not found in schema`);
3952
4053
  }
3953
4054
  const sourceTable = relation.sourceTable;
@@ -3956,7 +4057,7 @@ function normalizeRelation(schema, tableNamesMap, relation) {
3956
4057
  throw new Error(`Table "${sourceTable[Table.Symbol.Name]}" not found in schema`);
3957
4058
  }
3958
4059
  const reverseRelations = [];
3959
- for (const referencedTableRelation of Object.values(referencedTableFields.relations)) {
4060
+ for (const referencedTableRelation of Object.values(referencedTableConfig.relations)) {
3960
4061
  if ((relation.relationName && relation !== referencedTableRelation
3961
4062
  && referencedTableRelation.relationName === relation.relationName)
3962
4063
  || (!relation.relationName && referencedTableRelation.referencedTable === relation.sourceTable)) {
@@ -4020,31 +4121,57 @@ function bindIfParam(value, column) {
4020
4121
  }
4021
4122
  return value;
4022
4123
  }
4023
- function eq(left, right) {
4124
+ /**
4125
+ * Test that two values are equal.
4126
+ *
4127
+ * Remember that the SQL standard dictates that
4128
+ * two NULL values are not equal, so if you want to test
4129
+ * whether a value is null, you may want to use
4130
+ * `isNull` instead.
4131
+ *
4132
+ * ## Examples
4133
+ *
4134
+ * ```ts
4135
+ * // Select cars made by Ford
4136
+ * db.select().from(cars)
4137
+ * .where(eq(cars.make, 'Ford'))
4138
+ * ```
4139
+ *
4140
+ * @see isNull for a way to test equality to NULL.
4141
+ */
4142
+ const eq = (left, right) => {
4024
4143
  return sql `${left} = ${bindIfParam(right, left)}`;
4025
- }
4026
- function ne(left, right) {
4144
+ };
4145
+ /**
4146
+ * Test that two values are not equal.
4147
+ *
4148
+ * Remember that the SQL standard dictates that
4149
+ * two NULL values are not equal, so if you want to test
4150
+ * whether a value is not null, you may want to use
4151
+ * `isNotNull` instead.
4152
+ *
4153
+ * ## Examples
4154
+ *
4155
+ * ```ts
4156
+ * // Select cars not made by Ford
4157
+ * db.select().from(cars)
4158
+ * .where(ne(cars.make, 'Ford'))
4159
+ * ```
4160
+ *
4161
+ * @see isNotNull for a way to test whether a value is not null.
4162
+ */
4163
+ const ne = (left, right) => {
4027
4164
  return sql `${left} <> ${bindIfParam(right, left)}`;
4028
- }
4165
+ };
4029
4166
  function and(...unfilteredConditions) {
4030
4167
  const conditions = unfilteredConditions.filter((c) => c !== undefined);
4031
4168
  if (conditions.length === 0) {
4032
4169
  return undefined;
4033
4170
  }
4034
4171
  if (conditions.length === 1) {
4035
- return conditions[0];
4172
+ return new SQL(conditions);
4036
4173
  }
4037
- const chunks = [sql.raw('(')];
4038
- for (const [index, condition] of conditions.entries()) {
4039
- if (index === 0) {
4040
- chunks.push(condition);
4041
- }
4042
- else {
4043
- chunks.push(sql ` and `, condition);
4044
- }
4045
- }
4046
- chunks.push(sql `)`);
4047
- return sql.join(chunks);
4174
+ return new SQL([new StringChunk('('), sql.join(conditions, new StringChunk(' and ')), new StringChunk(')')]);
4048
4175
  }
4049
4176
  function or(...unfilteredConditions) {
4050
4177
  const conditions = unfilteredConditions.filter((c) => c !== undefined);
@@ -4052,19 +4179,9 @@ function or(...unfilteredConditions) {
4052
4179
  return undefined;
4053
4180
  }
4054
4181
  if (conditions.length === 1) {
4055
- return conditions[0];
4056
- }
4057
- const chunks = [sql.raw('(')];
4058
- for (const [index, condition] of conditions.entries()) {
4059
- if (index === 0) {
4060
- chunks.push(condition);
4061
- }
4062
- else {
4063
- chunks.push(sql ` or `, condition);
4064
- }
4182
+ return new SQL(conditions);
4065
4183
  }
4066
- chunks.push(sql `)`);
4067
- return sql.join(chunks);
4184
+ return new SQL([new StringChunk('('), sql.join(conditions, new StringChunk(' or ')), new StringChunk(')')]);
4068
4185
  }
4069
4186
  /**
4070
4187
  * Negate the meaning of an expression using the `not` keyword.
@@ -4080,18 +4197,76 @@ function or(...unfilteredConditions) {
4080
4197
  function not(condition) {
4081
4198
  return sql `not ${condition}`;
4082
4199
  }
4083
- function gt(left, right) {
4200
+ /**
4201
+ * Test that the first expression passed is greater than
4202
+ * the second expression.
4203
+ *
4204
+ * ## Examples
4205
+ *
4206
+ * ```ts
4207
+ * // Select cars made after 2000.
4208
+ * db.select().from(cars)
4209
+ * .where(gt(cars.year, 2000))
4210
+ * ```
4211
+ *
4212
+ * @see gte for greater-than-or-equal
4213
+ */
4214
+ const gt = (left, right) => {
4084
4215
  return sql `${left} > ${bindIfParam(right, left)}`;
4085
- }
4086
- function gte(left, right) {
4216
+ };
4217
+ /**
4218
+ * Test that the first expression passed is greater than
4219
+ * or equal to the second expression. Use `gt` to
4220
+ * test whether an expression is strictly greater
4221
+ * than another.
4222
+ *
4223
+ * ## Examples
4224
+ *
4225
+ * ```ts
4226
+ * // Select cars made on or after 2000.
4227
+ * db.select().from(cars)
4228
+ * .where(gte(cars.year, 2000))
4229
+ * ```
4230
+ *
4231
+ * @see gt for a strictly greater-than condition
4232
+ */
4233
+ const gte = (left, right) => {
4087
4234
  return sql `${left} >= ${bindIfParam(right, left)}`;
4088
- }
4089
- function lt(left, right) {
4235
+ };
4236
+ /**
4237
+ * Test that the first expression passed is less than
4238
+ * the second expression.
4239
+ *
4240
+ * ## Examples
4241
+ *
4242
+ * ```ts
4243
+ * // Select cars made before 2000.
4244
+ * db.select().from(cars)
4245
+ * .where(lt(cars.year, 2000))
4246
+ * ```
4247
+ *
4248
+ * @see lte for greater-than-or-equal
4249
+ */
4250
+ const lt = (left, right) => {
4090
4251
  return sql `${left} < ${bindIfParam(right, left)}`;
4091
- }
4092
- function lte(left, right) {
4252
+ };
4253
+ /**
4254
+ * Test that the first expression passed is less than
4255
+ * or equal to the second expression.
4256
+ *
4257
+ * ## Examples
4258
+ *
4259
+ * ```ts
4260
+ * // Select cars made before 2000.
4261
+ * db.select().from(cars)
4262
+ * .where(lte(cars.year, 2000))
4263
+ * ```
4264
+ *
4265
+ * @see lt for a strictly less-than condition
4266
+ */
4267
+ const lte = (left, right) => {
4093
4268
  return sql `${left} <= ${bindIfParam(right, left)}`;
4094
- }
4269
+ };
4095
4270
  function inArray(column, values) {
4096
4271
  if (Array.isArray(values)) {
4097
4272
  if (values.length === 0) {
@@ -4129,8 +4304,8 @@ function notInArray(column, values) {
4129
4304
  *
4130
4305
  * @see isNotNull for the inverse of this test
4131
4306
  */
4132
- function isNull(column) {
4133
- return sql `${column} is null`;
4307
+ function isNull(value) {
4308
+ return sql `${value} is null`;
4134
4309
  }
4135
4310
  /**
4136
4311
  * Test whether an expression is not NULL. By the SQL standard,
@@ -4148,8 +4323,8 @@ function isNull(column) {
4148
4323
  *
4149
4324
  * @see isNull for the inverse of this test
4150
4325
  */
4151
- function isNotNull(column) {
4152
- return sql `${column} is not null`;
4326
+ function isNotNull(value) {
4327
+ return sql `${value} is not null`;
4153
4328
  }
4154
4329
  /**
4155
4330
  * Test whether a subquery evaluates to have any rows.
@@ -4359,6 +4534,9 @@ class StringChunk {
4359
4534
  constructor(value) {
4360
4535
  this.value = Array.isArray(value) ? value : [value];
4361
4536
  }
4537
+ getSQL() {
4538
+ return new SQL([this]);
4539
+ }
4362
4540
  }
4363
4541
  _b$1 = entityKind;
4364
4542
  StringChunk[_b$1] = 'StringChunk';
@@ -4453,6 +4631,9 @@ class SQL {
4453
4631
  }
4454
4632
  return { sql: escapeParam(paramStartIndex.value++, mappedValue), params: [mappedValue], typings };
4455
4633
  }
4634
+ if (is(chunk, Placeholder)) {
4635
+ return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk] };
4636
+ }
4456
4637
  if (is(chunk, SQL.Aliased) && chunk.fieldAlias !== undefined) {
4457
4638
  return { sql: escapeName(chunk.fieldAlias), params: [] };
4458
4639
  }
@@ -4467,6 +4648,8 @@ class SQL {
4467
4648
  new Name(chunk[SubqueryConfig].alias),
4468
4649
  ], config);
4469
4650
  }
4651
+ // if (is(chunk, Placeholder)) {
4652
+ // return {sql: escapeParam}
4470
4653
  if (isSQLWrapper(chunk)) {
4471
4654
  return this.buildQueryFromSourceParams([
4472
4655
  new StringChunk('('),
@@ -4534,6 +4717,9 @@ class Name {
4534
4717
  constructor(value) {
4535
4718
  this.value = value;
4536
4719
  }
4720
+ getSQL() {
4721
+ return new SQL([this]);
4722
+ }
4537
4723
  }
4538
4724
  _d = entityKind;
4539
4725
  Name[_d] = 'Name';
@@ -4568,6 +4754,9 @@ class Param {
4568
4754
  this.value = value;
4569
4755
  this.encoder = encoder;
4570
4756
  }
4757
+ getSQL() {
4758
+ return new SQL([this]);
4759
+ }
4571
4760
  }
4572
4761
  _e = entityKind;
4573
4762
  Param[_e] = 'Param';
@@ -4675,6 +4864,9 @@ class Placeholder {
4675
4864
  constructor(name) {
4676
4865
  this.name = name;
4677
4866
  }
4867
+ getSQL() {
4868
+ return new SQL([this]);
4869
+ }
4678
4870
  }
4679
4871
  _f = entityKind;
4680
4872
  Placeholder[_f] = 'Placeholder';
@@ -4692,6 +4884,10 @@ function fillPlaceholders(params, values) {
4692
4884
  return p;
4693
4885
  });
4694
4886
  }
4887
+ // Defined separately from the Column class to resolve circular dependency
4888
+ Column.prototype.getSQL = function () {
4889
+ return new SQL([this]);
4890
+ };
4695
4891
 
4696
4892
  var _a, _b, _c;
4697
4893
  class ColumnAliasProxyHandler {
@@ -4920,6 +5116,9 @@ exports.TableAliasProxyHandler = TableAliasProxyHandler;
4920
5116
  exports.TableName = TableName;
4921
5117
  exports.TransactionRollbackError = TransactionRollbackError;
4922
5118
  exports.TypedQueryBuilder = TypedQueryBuilder;
5119
+ exports.UniqueConstraint = UniqueConstraint;
5120
+ exports.UniqueConstraintBuilder = UniqueConstraintBuilder;
5121
+ exports.UniqueOnConstraintBuilder = UniqueOnConstraintBuilder;
4923
5122
  exports.View = View;
4924
5123
  exports.ViewBaseConfig = ViewBaseConfig;
4925
5124
  exports.ViewBuilder = ViewBuilder;
@@ -4953,6 +5152,8 @@ exports.extractTablesRelationalConfig = extractTablesRelationalConfig;
4953
5152
  exports.fillPlaceholders = fillPlaceholders;
4954
5153
  exports.foreignKey = foreignKey;
4955
5154
  exports.getMaterializedViewConfig = getMaterializedViewConfig;
5155
+ exports.getOperators = getOperators;
5156
+ exports.getOrderByOperators = getOrderByOperators;
4956
5157
  exports.getTableColumns = getTableColumns;
4957
5158
  exports.getTableConfig = getTableConfig;
4958
5159
  exports.getTableLikeName = getTableLikeName;
@@ -5002,9 +5203,7 @@ exports.notIlike = notIlike;
5002
5203
  exports.notInArray = notInArray;
5003
5204
  exports.notLike = notLike;
5004
5205
  exports.numeric = numeric;
5005
- exports.operators = operators;
5006
5206
  exports.or = or;
5007
- exports.orderByOperators = orderByOperators;
5008
5207
  exports.orderSelectedFields = orderSelectedFields;
5009
5208
  exports.param = param;
5010
5209
  exports.parsePgArray = parsePgArray;
@@ -5030,7 +5229,9 @@ exports.text = text;
5030
5229
  exports.time = time;
5031
5230
  exports.timestamp = timestamp;
5032
5231
  exports.tracer = tracer;
5232
+ exports.unique = unique;
5033
5233
  exports.uniqueIndex = uniqueIndex;
5234
+ exports.uniqueKeyName = uniqueKeyName;
5034
5235
  exports.uuid = uuid;
5035
5236
  exports.varchar = varchar;
5036
- //# sourceMappingURL=alias-d10e3a09.cjs.map
5237
+ //# sourceMappingURL=alias-3f5ac719.cjs.map