drizzle-orm 0.29.0 → 0.29.1-e78b53e

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 (146) hide show
  1. package/mysql-core/db.cjs +134 -31
  2. package/mysql-core/db.cjs.map +1 -1
  3. package/mysql-core/db.d.cts +171 -1
  4. package/mysql-core/db.d.ts +171 -1
  5. package/mysql-core/db.js +134 -31
  6. package/mysql-core/db.js.map +1 -1
  7. package/mysql-core/query-builders/delete.cjs +29 -0
  8. package/mysql-core/query-builders/delete.cjs.map +1 -1
  9. package/mysql-core/query-builders/delete.d.cts +29 -0
  10. package/mysql-core/query-builders/delete.d.ts +29 -0
  11. package/mysql-core/query-builders/delete.js +29 -0
  12. package/mysql-core/query-builders/delete.js.map +1 -1
  13. package/mysql-core/query-builders/insert.cjs +26 -0
  14. package/mysql-core/query-builders/insert.cjs.map +1 -1
  15. package/mysql-core/query-builders/insert.d.cts +26 -0
  16. package/mysql-core/query-builders/insert.d.ts +26 -0
  17. package/mysql-core/query-builders/insert.js +26 -0
  18. package/mysql-core/query-builders/insert.js.map +1 -1
  19. package/mysql-core/query-builders/query.cjs +9 -1
  20. package/mysql-core/query-builders/query.cjs.map +1 -1
  21. package/mysql-core/query-builders/query.d.cts +1 -0
  22. package/mysql-core/query-builders/query.d.ts +1 -0
  23. package/mysql-core/query-builders/query.js +9 -1
  24. package/mysql-core/query-builders/query.js.map +1 -1
  25. package/mysql-core/query-builders/select.cjs +381 -0
  26. package/mysql-core/query-builders/select.cjs.map +1 -1
  27. package/mysql-core/query-builders/select.d.cts +604 -0
  28. package/mysql-core/query-builders/select.d.ts +604 -0
  29. package/mysql-core/query-builders/select.js +381 -0
  30. package/mysql-core/query-builders/select.js.map +1 -1
  31. package/mysql-core/query-builders/update.cjs +33 -0
  32. package/mysql-core/query-builders/update.cjs.map +1 -1
  33. package/mysql-core/query-builders/update.d.cts +33 -0
  34. package/mysql-core/query-builders/update.d.ts +33 -0
  35. package/mysql-core/query-builders/update.js +33 -0
  36. package/mysql-core/query-builders/update.js.map +1 -1
  37. package/mysql-core/view.cjs +2 -2
  38. package/mysql-core/view.cjs.map +1 -1
  39. package/mysql-core/view.d.cts +1 -1
  40. package/mysql-core/view.d.ts +1 -1
  41. package/mysql-core/view.js +2 -2
  42. package/mysql-core/view.js.map +1 -1
  43. package/mysql-proxy/migrator.cjs +2 -2
  44. package/mysql-proxy/migrator.cjs.map +1 -1
  45. package/mysql-proxy/migrator.js +2 -2
  46. package/mysql-proxy/migrator.js.map +1 -1
  47. package/package.json +25 -1
  48. package/pg-core/db.cjs +153 -34
  49. package/pg-core/db.cjs.map +1 -1
  50. package/pg-core/db.d.cts +211 -0
  51. package/pg-core/db.d.ts +211 -0
  52. package/pg-core/db.js +153 -34
  53. package/pg-core/db.js.map +1 -1
  54. package/pg-core/dialect.cjs +1 -1
  55. package/pg-core/dialect.cjs.map +1 -1
  56. package/pg-core/dialect.js +1 -1
  57. package/pg-core/dialect.js.map +1 -1
  58. package/pg-core/query-builders/delete.cjs +29 -0
  59. package/pg-core/query-builders/delete.cjs.map +1 -1
  60. package/pg-core/query-builders/delete.d.cts +49 -0
  61. package/pg-core/query-builders/delete.d.ts +49 -0
  62. package/pg-core/query-builders/delete.js +29 -0
  63. package/pg-core/query-builders/delete.js.map +1 -1
  64. package/pg-core/query-builders/insert.cjs +51 -0
  65. package/pg-core/query-builders/insert.cjs.map +1 -1
  66. package/pg-core/query-builders/insert.d.cts +71 -0
  67. package/pg-core/query-builders/insert.d.ts +71 -0
  68. package/pg-core/query-builders/insert.js +51 -0
  69. package/pg-core/query-builders/insert.js.map +1 -1
  70. package/pg-core/query-builders/query.cjs +9 -2
  71. package/pg-core/query-builders/query.cjs.map +1 -1
  72. package/pg-core/query-builders/query.d.cts +1 -0
  73. package/pg-core/query-builders/query.d.ts +1 -0
  74. package/pg-core/query-builders/query.js +9 -2
  75. package/pg-core/query-builders/query.js.map +1 -1
  76. package/pg-core/query-builders/select.cjs +354 -48
  77. package/pg-core/query-builders/select.cjs.map +1 -1
  78. package/pg-core/query-builders/select.d.cts +555 -59
  79. package/pg-core/query-builders/select.d.ts +555 -59
  80. package/pg-core/query-builders/select.js +354 -48
  81. package/pg-core/query-builders/select.js.map +1 -1
  82. package/pg-core/query-builders/update.cjs +33 -0
  83. package/pg-core/query-builders/update.cjs.map +1 -1
  84. package/pg-core/query-builders/update.d.cts +55 -0
  85. package/pg-core/query-builders/update.d.ts +55 -0
  86. package/pg-core/query-builders/update.js +33 -0
  87. package/pg-core/query-builders/update.js.map +1 -1
  88. package/pg-proxy/migrator.cjs +1 -1
  89. package/pg-proxy/migrator.cjs.map +1 -1
  90. package/pg-proxy/migrator.js +1 -1
  91. package/pg-proxy/migrator.js.map +1 -1
  92. package/sql/functions/aggregate.cjs +69 -0
  93. package/sql/functions/aggregate.cjs.map +1 -0
  94. package/sql/functions/aggregate.d.cts +104 -0
  95. package/sql/functions/aggregate.d.ts +104 -0
  96. package/sql/functions/aggregate.js +38 -0
  97. package/sql/functions/aggregate.js.map +1 -0
  98. package/sql/functions/index.cjs +23 -0
  99. package/sql/functions/index.cjs.map +1 -0
  100. package/sql/functions/index.d.cts +1 -0
  101. package/sql/functions/index.d.ts +1 -0
  102. package/sql/functions/index.js +2 -0
  103. package/sql/functions/index.js.map +1 -0
  104. package/sql/index.cjs +3 -1
  105. package/sql/index.cjs.map +1 -1
  106. package/sql/index.d.cts +1 -0
  107. package/sql/index.d.ts +1 -0
  108. package/sql/index.js +1 -0
  109. package/sql/index.js.map +1 -1
  110. package/sql/sql.cjs.map +1 -1
  111. package/sql/sql.js.map +1 -1
  112. package/sqlite-core/db.cjs +155 -36
  113. package/sqlite-core/db.cjs.map +1 -1
  114. package/sqlite-core/db.d.cts +187 -0
  115. package/sqlite-core/db.d.ts +187 -0
  116. package/sqlite-core/db.js +155 -36
  117. package/sqlite-core/db.js.map +1 -1
  118. package/sqlite-core/query-builders/delete.cjs +29 -0
  119. package/sqlite-core/query-builders/delete.cjs.map +1 -1
  120. package/sqlite-core/query-builders/delete.d.cts +49 -0
  121. package/sqlite-core/query-builders/delete.d.ts +49 -0
  122. package/sqlite-core/query-builders/delete.js +29 -0
  123. package/sqlite-core/query-builders/delete.js.map +1 -1
  124. package/sqlite-core/query-builders/insert.cjs +51 -0
  125. package/sqlite-core/query-builders/insert.cjs.map +1 -1
  126. package/sqlite-core/query-builders/insert.d.cts +71 -0
  127. package/sqlite-core/query-builders/insert.d.ts +71 -0
  128. package/sqlite-core/query-builders/insert.js +51 -0
  129. package/sqlite-core/query-builders/insert.js.map +1 -1
  130. package/sqlite-core/query-builders/select.cjs +291 -0
  131. package/sqlite-core/query-builders/select.cjs.map +1 -1
  132. package/sqlite-core/query-builders/select.d.cts +434 -0
  133. package/sqlite-core/query-builders/select.d.ts +434 -0
  134. package/sqlite-core/query-builders/select.js +291 -0
  135. package/sqlite-core/query-builders/select.js.map +1 -1
  136. package/sqlite-core/query-builders/select.types.cjs.map +1 -1
  137. package/sqlite-core/query-builders/update.cjs +33 -0
  138. package/sqlite-core/query-builders/update.cjs.map +1 -1
  139. package/sqlite-core/query-builders/update.d.cts +55 -0
  140. package/sqlite-core/query-builders/update.d.ts +55 -0
  141. package/sqlite-core/query-builders/update.js +33 -0
  142. package/sqlite-core/query-builders/update.js.map +1 -1
  143. package/version.cjs +1 -1
  144. package/version.d.cts +1 -1
  145. package/version.d.ts +1 -1
  146. package/version.js +1 -1
package/pg-core/db.d.ts CHANGED
@@ -28,23 +28,234 @@ export declare class PgDatabase<TQueryResult extends QueryResultHKT, TFullSchema
28
28
  dialect: PgDialect,
29
29
  /** @internal */
30
30
  session: PgSession<any, any, any>, schema: RelationalSchemaConfig<TSchema> | undefined);
31
+ /**
32
+ * Creates a subquery that defines a temporary named result set as a CTE.
33
+ *
34
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
35
+ *
36
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
37
+ *
38
+ * @param alias The alias for the subquery.
39
+ *
40
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
41
+ *
42
+ * @example
43
+ *
44
+ * ```ts
45
+ * // Create a subquery with alias 'sq' and use it in the select query
46
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
47
+ *
48
+ * const result = await db.with(sq).select().from(sq);
49
+ * ```
50
+ *
51
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
52
+ *
53
+ * ```ts
54
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
55
+ * const sq = db.$with('sq').as(db.select({
56
+ * name: sql<string>`upper(${users.name})`.as('name'),
57
+ * })
58
+ * .from(users));
59
+ *
60
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
61
+ * ```
62
+ */
31
63
  $with<TAlias extends string>(alias: TAlias): {
32
64
  as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
33
65
  };
66
+ /**
67
+ * Incorporates a previously defined CTE (using `$with`) into the main query.
68
+ *
69
+ * This method allows the main query to reference a temporary named result set.
70
+ *
71
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
72
+ *
73
+ * @param queries The CTEs to incorporate into the main query.
74
+ *
75
+ * @example
76
+ *
77
+ * ```ts
78
+ * // Define a subquery 'sq' as a CTE using $with
79
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
80
+ *
81
+ * // Incorporate the CTE 'sq' into the main query and select from it
82
+ * const result = await db.with(sq).select().from(sq);
83
+ * ```
84
+ */
34
85
  with(...queries: WithSubquery[]): {
35
86
  select: {
36
87
  (): PgSelectBuilder<undefined>;
37
88
  <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "db">;
38
89
  };
39
90
  };
91
+ /**
92
+ * Creates a select query.
93
+ *
94
+ * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.
95
+ *
96
+ * Use `.from()` method to specify which table to select from.
97
+ *
98
+ * See docs: {@link https://orm.drizzle.team/docs/select}
99
+ *
100
+ * @param fields The selection object.
101
+ *
102
+ * @example
103
+ *
104
+ * ```ts
105
+ * // Select all columns and all rows from the 'cars' table
106
+ * const allCars: Car[] = await db.select().from(cars);
107
+ *
108
+ * // Select specific columns and all rows from the 'cars' table
109
+ * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({
110
+ * id: cars.id,
111
+ * brand: cars.brand
112
+ * })
113
+ * .from(cars);
114
+ * ```
115
+ *
116
+ * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:
117
+ *
118
+ * ```ts
119
+ * // Select specific columns along with expression and all rows from the 'cars' table
120
+ * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({
121
+ * id: cars.id,
122
+ * lowerBrand: sql<string>`lower(${cars.brand})`,
123
+ * })
124
+ * .from(cars);
125
+ * ```
126
+ */
40
127
  select(): PgSelectBuilder<undefined>;
41
128
  select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
129
+ /**
130
+ * Adds `distinct` expression to the select query.
131
+ *
132
+ * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.
133
+ *
134
+ * Use `.from()` method to specify which table to select from.
135
+ *
136
+ * See docs: {@link https://orm.drizzle.team/docs/select#distinct}
137
+ *
138
+ * @param fields The selection object.
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * // Select all unique rows from the 'cars' table
143
+ * await db.selectDistinct()
144
+ * .from(cars)
145
+ * .orderBy(cars.id, cars.brand, cars.color);
146
+ *
147
+ * // Select all unique brands from the 'cars' table
148
+ * await db.selectDistinct({ brand: cars.brand })
149
+ * .from(cars)
150
+ * .orderBy(cars.brand);
151
+ * ```
152
+ */
42
153
  selectDistinct(): PgSelectBuilder<undefined>;
43
154
  selectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
155
+ /**
156
+ * Adds `distinct on` expression to the select query.
157
+ *
158
+ * Calling this method will specify how the unique rows are determined.
159
+ *
160
+ * Use `.from()` method to specify which table to select from.
161
+ *
162
+ * See docs: {@link https://orm.drizzle.team/docs/select#distinct}
163
+ *
164
+ * @param on The expression defining uniqueness.
165
+ * @param fields The selection object.
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * // Select the first row for each unique brand from the 'cars' table
170
+ * await db.selectDistinctOn([cars.brand])
171
+ * .from(cars)
172
+ * .orderBy(cars.brand);
173
+ *
174
+ * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table
175
+ * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })
176
+ * .from(cars)
177
+ * .orderBy(cars.brand, cars.color);
178
+ * ```
179
+ */
44
180
  selectDistinctOn(on: (PgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;
45
181
  selectDistinctOn<TSelection extends SelectedFields>(on: (PgColumn | SQLWrapper)[], fields: TSelection): PgSelectBuilder<TSelection>;
182
+ /**
183
+ * Creates an update query.
184
+ *
185
+ * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
186
+ *
187
+ * Use `.set()` method to specify which values to update.
188
+ *
189
+ * See docs: {@link https://orm.drizzle.team/docs/update}
190
+ *
191
+ * @param table The table to update.
192
+ *
193
+ * @example
194
+ *
195
+ * ```ts
196
+ * // Update all rows in the 'cars' table
197
+ * await db.update(cars).set({ color: 'red' });
198
+ *
199
+ * // Update rows with filters and conditions
200
+ * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
201
+ *
202
+ * // Update with returning clause
203
+ * const updatedCar: Car[] = await db.update(cars)
204
+ * .set({ color: 'red' })
205
+ * .where(eq(cars.id, 1))
206
+ * .returning();
207
+ * ```
208
+ */
46
209
  update<TTable extends PgTable>(table: TTable): PgUpdateBuilder<TTable, TQueryResult>;
210
+ /**
211
+ * Creates an insert query.
212
+ *
213
+ * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
214
+ *
215
+ * See docs: {@link https://orm.drizzle.team/docs/insert}
216
+ *
217
+ * @param table The table to insert into.
218
+ *
219
+ * @example
220
+ *
221
+ * ```ts
222
+ * // Insert one row
223
+ * await db.insert(cars).values({ brand: 'BMW' });
224
+ *
225
+ * // Insert multiple rows
226
+ * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
227
+ *
228
+ * // Insert with returning clause
229
+ * const insertedCar: Car[] = await db.insert(cars)
230
+ * .values({ brand: 'BMW' })
231
+ * .returning();
232
+ * ```
233
+ */
47
234
  insert<TTable extends PgTable>(table: TTable): PgInsertBuilder<TTable, TQueryResult>;
235
+ /**
236
+ * Creates a delete query.
237
+ *
238
+ * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
239
+ *
240
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
241
+ *
242
+ * @param table The table to delete from.
243
+ *
244
+ * @example
245
+ *
246
+ * ```ts
247
+ * // Delete all rows in the 'cars' table
248
+ * await db.delete(cars);
249
+ *
250
+ * // Delete rows with filters and conditions
251
+ * await db.delete(cars).where(eq(cars.color, 'green'));
252
+ *
253
+ * // Delete with returning clause
254
+ * const deletedCar: Car[] = await db.delete(cars)
255
+ * .where(eq(cars.id, 1))
256
+ * .returning();
257
+ * ```
258
+ */
48
259
  delete<TTable extends PgTable>(table: TTable): PgDeleteBase<TTable, TQueryResult>;
49
260
  refreshMaterializedView<TView extends PgMaterializedView>(view: TView): PgRefreshMaterializedView<TQueryResult>;
50
261
  execute<TRow extends Record<string, unknown> = Record<string, unknown>>(query: SQLWrapper): Promise<QueryResultKind<TQueryResult, TRow>>;
package/pg-core/db.js CHANGED
@@ -6,10 +6,10 @@ import {
6
6
  PgUpdateBuilder,
7
7
  QueryBuilder
8
8
  } from "./query-builders/index.js";
9
+ import { SelectionProxyHandler } from "../selection-proxy.js";
9
10
  import { WithSubquery } from "../subquery.js";
10
11
  import { RelationalQueryBuilder } from "./query-builders/query.js";
11
12
  import { PgRefreshMaterializedView } from "./query-builders/refresh-materialized-view.js";
12
- import { SelectionProxyHandler } from "../selection-proxy.js";
13
13
  class PgDatabase {
14
14
  constructor(dialect, session, schema) {
15
15
  this.dialect = dialect;
@@ -32,6 +32,38 @@ class PgDatabase {
32
32
  }
33
33
  static [entityKind] = "PgDatabase";
34
34
  query;
35
+ /**
36
+ * Creates a subquery that defines a temporary named result set as a CTE.
37
+ *
38
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
39
+ *
40
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
41
+ *
42
+ * @param alias The alias for the subquery.
43
+ *
44
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
45
+ *
46
+ * @example
47
+ *
48
+ * ```ts
49
+ * // Create a subquery with alias 'sq' and use it in the select query
50
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
51
+ *
52
+ * const result = await db.with(sq).select().from(sq);
53
+ * ```
54
+ *
55
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
56
+ *
57
+ * ```ts
58
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
59
+ * const sq = db.$with('sq').as(db.select({
60
+ * name: sql<string>`upper(${users.name})`.as('name'),
61
+ * })
62
+ * .from(users));
63
+ *
64
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
65
+ * ```
66
+ */
35
67
  $with(alias) {
36
68
  return {
37
69
  as(qb) {
@@ -45,6 +77,25 @@ class PgDatabase {
45
77
  }
46
78
  };
47
79
  }
80
+ /**
81
+ * Incorporates a previously defined CTE (using `$with`) into the main query.
82
+ *
83
+ * This method allows the main query to reference a temporary named result set.
84
+ *
85
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
86
+ *
87
+ * @param queries The CTEs to incorporate into the main query.
88
+ *
89
+ * @example
90
+ *
91
+ * ```ts
92
+ * // Define a subquery 'sq' as a CTE using $with
93
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
94
+ *
95
+ * // Incorporate the CTE 'sq' into the main query and select from it
96
+ * const result = await db.with(sq).select().from(sq);
97
+ * ```
98
+ */
48
99
  with(...queries) {
49
100
  const self = this;
50
101
  function select(fields) {
@@ -80,12 +131,87 @@ class PgDatabase {
80
131
  distinct: { on }
81
132
  });
82
133
  }
134
+ /**
135
+ * Creates an update query.
136
+ *
137
+ * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
138
+ *
139
+ * Use `.set()` method to specify which values to update.
140
+ *
141
+ * See docs: {@link https://orm.drizzle.team/docs/update}
142
+ *
143
+ * @param table The table to update.
144
+ *
145
+ * @example
146
+ *
147
+ * ```ts
148
+ * // Update all rows in the 'cars' table
149
+ * await db.update(cars).set({ color: 'red' });
150
+ *
151
+ * // Update rows with filters and conditions
152
+ * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
153
+ *
154
+ * // Update with returning clause
155
+ * const updatedCar: Car[] = await db.update(cars)
156
+ * .set({ color: 'red' })
157
+ * .where(eq(cars.id, 1))
158
+ * .returning();
159
+ * ```
160
+ */
83
161
  update(table) {
84
162
  return new PgUpdateBuilder(table, this.session, this.dialect);
85
163
  }
164
+ /**
165
+ * Creates an insert query.
166
+ *
167
+ * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
168
+ *
169
+ * See docs: {@link https://orm.drizzle.team/docs/insert}
170
+ *
171
+ * @param table The table to insert into.
172
+ *
173
+ * @example
174
+ *
175
+ * ```ts
176
+ * // Insert one row
177
+ * await db.insert(cars).values({ brand: 'BMW' });
178
+ *
179
+ * // Insert multiple rows
180
+ * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
181
+ *
182
+ * // Insert with returning clause
183
+ * const insertedCar: Car[] = await db.insert(cars)
184
+ * .values({ brand: 'BMW' })
185
+ * .returning();
186
+ * ```
187
+ */
86
188
  insert(table) {
87
189
  return new PgInsertBuilder(table, this.session, this.dialect);
88
190
  }
191
+ /**
192
+ * Creates a delete query.
193
+ *
194
+ * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
195
+ *
196
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
197
+ *
198
+ * @param table The table to delete from.
199
+ *
200
+ * @example
201
+ *
202
+ * ```ts
203
+ * // Delete all rows in the 'cars' table
204
+ * await db.delete(cars);
205
+ *
206
+ * // Delete rows with filters and conditions
207
+ * await db.delete(cars).where(eq(cars.color, 'green'));
208
+ *
209
+ * // Delete with returning clause
210
+ * const deletedCar: Car[] = await db.delete(cars)
211
+ * .where(eq(cars.id, 1))
212
+ * .returning();
213
+ * ```
214
+ */
89
215
  delete(table) {
90
216
  return new PgDeleteBase(table, this.session, this.dialect);
91
217
  }
@@ -100,40 +226,33 @@ class PgDatabase {
100
226
  }
101
227
  }
102
228
  const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(Math.random() * replicas.length)]) => {
103
- const select = (...args) => getReplica(replicas).select(args);
104
- const selectDistinct = (...args) => getReplica(replicas).selectDistinct(args);
105
- const selectDistinctOn = (...args) => getReplica(replicas).selectDistinctOn(args);
106
- const $with = (...args) => getReplica(replicas).with(args);
107
- const update = (...args) => primary.update(args);
108
- const insert = (...args) => primary.insert(args);
109
- const $delete = (...args) => primary.delete(args);
110
- const execute = (...args) => primary.execute(args);
111
- const transaction = (...args) => primary.transaction(args);
112
- const refreshMaterializedView = (...args) => primary.refreshMaterializedView(args);
113
- return new Proxy(
114
- {
115
- ...primary,
116
- update,
117
- insert,
118
- delete: $delete,
119
- execute,
120
- transaction,
121
- refreshMaterializedView,
122
- $primary: primary,
123
- select,
124
- selectDistinct,
125
- selectDistinctOn,
126
- with: $with
127
- },
128
- {
129
- get(target, prop, _receiver) {
130
- if (prop === "query") {
131
- return getReplica(replicas).query;
132
- }
133
- return target[prop];
134
- }
229
+ const select = (...args) => getReplica(replicas).select(...args);
230
+ const selectDistinct = (...args) => getReplica(replicas).selectDistinct(...args);
231
+ const selectDistinctOn = (...args) => getReplica(replicas).selectDistinctOn(...args);
232
+ const $with = (...args) => getReplica(replicas).with(...args);
233
+ const update = (...args) => primary.update(...args);
234
+ const insert = (...args) => primary.insert(...args);
235
+ const $delete = (...args) => primary.delete(...args);
236
+ const execute = (...args) => primary.execute(...args);
237
+ const transaction = (...args) => primary.transaction(...args);
238
+ const refreshMaterializedView = (...args) => primary.refreshMaterializedView(...args);
239
+ return {
240
+ ...primary,
241
+ update,
242
+ insert,
243
+ delete: $delete,
244
+ execute,
245
+ transaction,
246
+ refreshMaterializedView,
247
+ $primary: primary,
248
+ select,
249
+ selectDistinct,
250
+ selectDistinctOn,
251
+ with: $with,
252
+ get query() {
253
+ return getReplica(replicas).query;
135
254
  }
136
- );
255
+ };
137
256
  };
138
257
  export {
139
258
  PgDatabase,
package/pg-core/db.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/pg-core/db.ts"],"sourcesContent":["import { entityKind } from '~/entity.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport {\n\tPgDeleteBase,\n\tPgInsertBuilder,\n\tPgSelectBuilder,\n\tPgUpdateBuilder,\n\tQueryBuilder,\n} from '~/pg-core/query-builders/index.ts';\nimport type {\n\tPgSession,\n\tPgTransaction,\n\tPgTransactionConfig,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from '~/pg-core/session.ts';\nimport type { PgTable } from '~/pg-core/table.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';\nimport type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { DrizzleTypeError } from '~/utils.ts';\nimport type { PgColumn } from './columns/index.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { PgRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithSubqueryWithSelection } from './subquery.ts';\nimport type { PgMaterializedView } from './view.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\n\nexport class PgDatabase<\n\tTQueryResult extends QueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'PgDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t};\n\n\tquery: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: PgDialect,\n\t\t/** @internal */\n\t\treadonly session: PgSession<any, any, any>,\n\t\tschema: RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }\n\t\t\t: { schema: undefined, tableNamesMap: {} };\n\t\tthis.query = {} as typeof this['query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this.query as PgDatabase<TQueryResult, Record<string, any>>['query'])[tableName] = new RelationalQueryBuilder(\n\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\tthis._.schema,\n\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\tschema!.fullSchema[tableName] as PgTable,\n\t\t\t\t\tcolumns,\n\t\t\t\t\tdialect,\n\t\t\t\t\tsession,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t$with<TAlias extends string>(alias: TAlias) {\n\t\treturn {\n\t\t\tas<TSelection extends ColumnsSelection>(\n\t\t\t\tqb: TypedQueryBuilder<TSelection> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection>),\n\t\t\t): WithSubqueryWithSelection<TSelection, TAlias> {\n\t\t\t\tif (typeof qb === 'function') {\n\t\t\t\t\tqb = qb(new QueryBuilder());\n\t\t\t\t}\n\n\t\t\t\treturn new Proxy(\n\t\t\t\t\tnew WithSubquery(qb.getSQL(), qb.getSelectedFields() as SelectedFields, alias, true),\n\t\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t\t) as WithSubqueryWithSelection<TSelection, TAlias>;\n\t\t\t},\n\t\t};\n\t}\n\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\tfunction select(): PgSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\t\tfunction select(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\t\treturn new PgSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\treturn { select };\n\t}\n\n\tselect(): PgSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\tselect(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\tselectDistinct(): PgSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\tselectDistinct(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\tselectDistinctOn(on: (PgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (PgColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): PgSelectBuilder<TSelection>;\n\tselectDistinctOn(\n\t\ton: (PgColumn | SQLWrapper)[],\n\t\tfields?: SelectedFields,\n\t): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\tupdate<TTable extends PgTable>(table: TTable): PgUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new PgUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\tinsert<TTable extends PgTable>(table: TTable): PgInsertBuilder<TTable, TQueryResult> {\n\t\treturn new PgInsertBuilder(table, this.session, this.dialect);\n\t}\n\n\tdelete<TTable extends PgTable>(table: TTable): PgDeleteBase<TTable, TQueryResult> {\n\t\treturn new PgDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends PgMaterializedView>(view: TView): PgRefreshMaterializedView<TQueryResult> {\n\t\treturn new PgRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper,\n\t): Promise<QueryResultKind<TQueryResult, TRow>> {\n\t\treturn this.session.execute(query.getSQL());\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: PgTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type PgWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends QueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends TablesRelationalConfig,\n\tQ extends PgDatabase<HKT, TFullSchema, TSchema>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): PgWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: any) => getReplica(replicas).select(args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: any) => getReplica(replicas).selectDistinct(args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: any) => getReplica(replicas).selectDistinctOn(args);\n\tconst $with: Q['with'] = (...args: any) => getReplica(replicas).with(args);\n\n\tconst update: Q['update'] = (...args: any) => primary.update(args);\n\tconst insert: Q['insert'] = (...args: any) => primary.insert(args);\n\tconst $delete: Q['delete'] = (...args: any) => primary.delete(args);\n\tconst execute: Q['execute'] = (...args: any) => primary.execute(args);\n\tconst transaction: Q['transaction'] = (...args: any) => primary.transaction(args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: any) => primary.refreshMaterializedView(args);\n\n\treturn new Proxy<Q & { $primary: Q }>(\n\t\t{\n\t\t\t...primary,\n\t\t\tupdate,\n\t\t\tinsert,\n\t\t\tdelete: $delete,\n\t\t\texecute,\n\t\t\ttransaction,\n\t\t\trefreshMaterializedView,\n\t\t\t$primary: primary,\n\t\t\tselect,\n\t\t\tselectDistinct,\n\t\t\tselectDistinctOn,\n\t\t\twith: $with,\n\t\t},\n\t\t{\n\t\t\tget(target, prop, _receiver) {\n\t\t\t\tif (prop === 'query') {\n\t\t\t\t\treturn getReplica(replicas).query;\n\t\t\t\t}\n\t\t\t\treturn target[prop as keyof typeof target];\n\t\t\t},\n\t\t},\n\t);\n};\n"],"mappings":"AAAA,SAAS,kBAAkB;AAE3B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAY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AAW,QAAQ,EAAE,OAAO,IAAI;AAC9E,QAAM,iBAAsC,IAAI,SAAc,WAAW,QAAQ,EAAE,eAAe,IAAI;AACtG,QAAM,mBAA0C,IAAI,SAAc,WAAW,QAAQ,EAAE,iBAAiB,IAAI;AAC5G,QAAM,QAAmB,IAAI,SAAc,WAAW,QAAQ,EAAE,KAAK,IAAI;AAEzE,QAAM,SAAsB,IAAI,SAAc,QAAQ,OAAO,IAAI;AACjE,QAAM,SAAsB,IAAI,SAAc,QAAQ,OAAO,IAAI;AACjE,QAAM,UAAuB,IAAI,SAAc,QAAQ,OAAO,IAAI;AAClE,QAAM,UAAwB,IAAI,SAAc,QAAQ,QAAQ,IAAI;AACpE,QAAM,cAAgC,IAAI,SAAc,QAAQ,YAAY,IAAI;AAChF,QAAM,0BAAwD,IAAI,SAAc,QAAQ,wBAAwB,IAAI;AAEpH,SAAO,IAAI;AAAA,IACV;AAAA,MACC,GAAG;AAAA,MACH;AAAA,MACA;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV;AAAA,MACA;AAAA,MACA;AAAA,MACA,MAAM;AAAA,IACP;AAAA,IACA;AAAA,MACC,IAAI,QAAQ,MAAM,WAAW;AAC5B,YAAI,SAAS,SAAS;AACrB,iBAAO,WAAW,QAAQ,EAAE;AAAA,QAC7B;AACA,eAAO,OAAO,IAA2B;AAAA,MAC1C;AAAA,IACD;AAAA,EACD;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/pg-core/db.ts"],"sourcesContent":["import { entityKind } from '~/entity.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport {\n\tPgDeleteBase,\n\tPgInsertBuilder,\n\tPgSelectBuilder,\n\tPgUpdateBuilder,\n\tQueryBuilder,\n} from '~/pg-core/query-builders/index.ts';\nimport type {\n\tPgSession,\n\tPgTransaction,\n\tPgTransactionConfig,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from '~/pg-core/session.ts';\nimport type { PgTable } from '~/pg-core/table.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\nimport type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { DrizzleTypeError } from '~/utils.ts';\nimport type { PgColumn } from './columns/index.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { PgRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithSubqueryWithSelection } from './subquery.ts';\nimport type { PgMaterializedView } from './view.ts';\n\nexport class PgDatabase<\n\tTQueryResult extends QueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'PgDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t};\n\n\tquery: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: PgDialect,\n\t\t/** @internal */\n\t\treadonly session: PgSession<any, any, any>,\n\t\tschema: RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }\n\t\t\t: { schema: undefined, tableNamesMap: {} };\n\t\tthis.query = {} as typeof this['query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this.query as PgDatabase<TQueryResult, Record<string, any>>['query'])[tableName] = new RelationalQueryBuilder(\n\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\tthis._.schema,\n\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\tschema!.fullSchema[tableName] as PgTable,\n\t\t\t\t\tcolumns,\n\t\t\t\t\tdialect,\n\t\t\t\t\tsession,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Creates a subquery that defines a temporary named result set as a CTE.\n\t *\n\t * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param alias The alias for the subquery.\n\t *\n\t * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Create a subquery with alias 'sq' and use it in the select query\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t *\n\t * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:\n\t *\n\t * ```ts\n\t * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query\n\t * const sq = db.$with('sq').as(db.select({\n\t * name: sql<string>`upper(${users.name})`.as('name'),\n\t * })\n\t * .from(users));\n\t *\n\t * const result = await db.with(sq).select({ name: sq.name }).from(sq);\n\t * ```\n\t */\n\t$with<TAlias extends string>(alias: TAlias) {\n\t\treturn {\n\t\t\tas<TSelection extends ColumnsSelection>(\n\t\t\t\tqb: TypedQueryBuilder<TSelection> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection>),\n\t\t\t): WithSubqueryWithSelection<TSelection, TAlias> {\n\t\t\t\tif (typeof qb === 'function') {\n\t\t\t\t\tqb = qb(new QueryBuilder());\n\t\t\t\t}\n\n\t\t\t\treturn new Proxy(\n\t\t\t\t\tnew WithSubquery(qb.getSQL(), qb.getSelectedFields() as SelectedFields, alias, true),\n\t\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t\t) as WithSubqueryWithSelection<TSelection, TAlias>;\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Incorporates a previously defined CTE (using `$with`) into the main query.\n\t *\n\t * This method allows the main query to reference a temporary named result set.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param queries The CTEs to incorporate into the main query.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Define a subquery 'sq' as a CTE using $with\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * // Incorporate the CTE 'sq' into the main query and select from it\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t */\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\tfunction select(): PgSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\t\tfunction select(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\t\treturn new PgSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\treturn { select };\n\t}\n\n\t/**\n\t * Creates a select query.\n\t *\n\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Select all columns and all rows from the 'cars' table\n\t * const allCars: Car[] = await db.select().from(cars);\n\t *\n\t * // Select specific columns and all rows from the 'cars' table\n\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t * id: cars.id,\n\t * brand: cars.brand\n\t * })\n\t * .from(cars);\n\t * ```\n\t *\n\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t *\n\t * ```ts\n\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t * id: cars.id,\n\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t * })\n\t * .from(cars);\n\t * ```\n\t */\n\tselect(): PgSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\tselect(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct` expression to the select query.\n\t *\n\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select all unique rows from the 'cars' table\n\t * await db.selectDistinct()\n\t * .from(cars)\n\t * .orderBy(cars.id, cars.brand, cars.color);\n\t *\n\t * // Select all unique brands from the 'cars' table\n\t * await db.selectDistinct({ brand: cars.brand })\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t * ```\n\t */\n\tselectDistinct(): PgSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;\n\tselectDistinct(fields?: SelectedFields): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct on` expression to the select query.\n\t *\n\t * Calling this method will specify how the unique rows are determined.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param on The expression defining uniqueness.\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select the first row for each unique brand from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand])\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t *\n\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t * .from(cars)\n\t * .orderBy(cars.brand, cars.color);\n\t * ```\n\t */\n\tselectDistinctOn(on: (PgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (PgColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): PgSelectBuilder<TSelection>;\n\tselectDistinctOn(\n\t\ton: (PgColumn | SQLWrapper)[],\n\t\tfields?: SelectedFields,\n\t): PgSelectBuilder<SelectedFields | undefined> {\n\t\treturn new PgSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\t/**\n\t * Creates an update query.\n\t *\n\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t *\n\t * Use `.set()` method to specify which values to update.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t *\n\t * @param table The table to update.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Update all rows in the 'cars' table\n\t * await db.update(cars).set({ color: 'red' });\n\t *\n\t * // Update rows with filters and conditions\n\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t *\n\t * // Update with returning clause\n\t * const updatedCar: Car[] = await db.update(cars)\n\t * .set({ color: 'red' })\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tupdate<TTable extends PgTable>(table: TTable): PgUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new PgUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates an insert query.\n\t *\n\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t *\n\t * @param table The table to insert into.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Insert one row\n\t * await db.insert(cars).values({ brand: 'BMW' });\n\t *\n\t * // Insert multiple rows\n\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t *\n\t * // Insert with returning clause\n\t * const insertedCar: Car[] = await db.insert(cars)\n\t * .values({ brand: 'BMW' })\n\t * .returning();\n\t * ```\n\t */\n\tinsert<TTable extends PgTable>(table: TTable): PgInsertBuilder<TTable, TQueryResult> {\n\t\treturn new PgInsertBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates a delete query.\n\t *\n\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t *\n\t * @param table The table to delete from.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Delete all rows in the 'cars' table\n\t * await db.delete(cars);\n\t *\n\t * // Delete rows with filters and conditions\n\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t *\n\t * // Delete with returning clause\n\t * const deletedCar: Car[] = await db.delete(cars)\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tdelete<TTable extends PgTable>(table: TTable): PgDeleteBase<TTable, TQueryResult> {\n\t\treturn new PgDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends PgMaterializedView>(view: TView): PgRefreshMaterializedView<TQueryResult> {\n\t\treturn new PgRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper,\n\t): Promise<QueryResultKind<TQueryResult, TRow>> {\n\t\treturn this.session.execute(query.getSQL());\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: PgTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type PgWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends QueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends TablesRelationalConfig,\n\tQ extends PgDatabase<HKT, TFullSchema, TSchema>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): PgWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);\n\tconst $with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);\n\n\tconst update: Q['update'] = (...args: [any]) => primary.update(...args);\n\tconst insert: Q['insert'] = (...args: [any]) => primary.insert(...args);\n\tconst $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);\n\tconst execute: Q['execute'] = (...args: [any]) => primary.execute(...args);\n\tconst transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>\n\t\tprimary.refreshMaterializedView(...args);\n\n\treturn {\n\t\t...primary,\n\t\tupdate,\n\t\tinsert,\n\t\tdelete: $delete,\n\t\texecute,\n\t\ttransaction,\n\t\trefreshMaterializedView,\n\t\t$primary: primary,\n\t\tselect,\n\t\tselectDistinct,\n\t\tselectDistinctOn,\n\t\tw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AAG,IAAI;AACtE,QAAM,SAAsB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACtE,QAAM,UAAuB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACvE,QAAM,UAAwB,IAAI,SAAgB,QAAQ,QAAQ,GAAG,IAAI;AACzE,QAAM,cAAgC,IAAI,SAAgB,QAAQ,YAAY,GAAG,IAAI;AACrF,QAAM,0BAAwD,IAAI,SACjE,QAAQ,wBAAwB,GAAG,IAAI;AAExC,SAAO;AAAA,IACN,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,IAAI,QAAQ;AACX,aAAO,WAAW,QAAQ,EAAE;AAAA,IAC7B;AAAA,EACD;AACD;","names":[]}
@@ -190,7 +190,7 @@ class PgDialect {
190
190
  }
191
191
  let distinctSql;
192
192
  if (distinct) {
193
- distinctSql = distinct === true ? import_sql.sql` distinct` : import_sql.sql` distinct on (${import_sql.sql.join(distinct.on, ", ")})`;
193
+ distinctSql = distinct === true ? import_sql.sql` distinct` : import_sql.sql` distinct on (${import_sql.sql.join(distinct.on, import_sql.sql`, `)})`;
194
194
  }
195
195
  const selection = this.buildSelection(fieldsList, { isSingleTable });
196
196
  const tableSql = (() => {