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
@@ -3,6 +3,7 @@ import { entityKind } from "../entity.js";
3
3
  import type { TypedQueryBuilder } from "../query-builders/query-builder.js";
4
4
  import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from "../relations.js";
5
5
  import type { ColumnsSelection, SQLWrapper } from "../sql/sql.js";
6
+ import { WithSubquery } from "../subquery.js";
6
7
  import type { DrizzleTypeError } from "../utils.js";
7
8
  import type { MySqlDialect } from "./dialect.js";
8
9
  import { MySqlDeleteBase, MySqlInsertBuilder, MySqlSelectBuilder, MySqlUpdateBuilder, QueryBuilder } from "./query-builders/index.js";
@@ -11,7 +12,6 @@ import type { SelectedFields } from "./query-builders/select.types.js";
11
12
  import type { Mode, MySqlSession, MySqlTransaction, MySqlTransactionConfig, PreparedQueryHKTBase, QueryResultHKT, QueryResultKind } from "./session.js";
12
13
  import type { WithSubqueryWithSelection } from "./subquery.js";
13
14
  import type { MySqlTable } from "./table.js";
14
- import { WithSubquery } from "../subquery.js";
15
15
  export declare class MySqlDatabase<TQueryResult extends QueryResultHKT, TPreparedQueryHKT extends PreparedQueryHKTBase, TFullSchema extends Record<string, unknown> = {}, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
16
16
  protected readonly mode: Mode;
17
17
  static readonly [entityKind]: string;
@@ -27,9 +27,60 @@ export declare class MySqlDatabase<TQueryResult extends QueryResultHKT, TPrepare
27
27
  dialect: MySqlDialect,
28
28
  /** @internal */
29
29
  session: MySqlSession<any, any, any, any>, schema: RelationalSchemaConfig<TSchema> | undefined, mode: Mode);
30
+ /**
31
+ * Creates a subquery that defines a temporary named result set as a CTE.
32
+ *
33
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
34
+ *
35
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
36
+ *
37
+ * @param alias The alias for the subquery.
38
+ *
39
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
40
+ *
41
+ * @example
42
+ *
43
+ * ```ts
44
+ * // Create a subquery with alias 'sq' and use it in the select query
45
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
46
+ *
47
+ * const result = await db.with(sq).select().from(sq);
48
+ * ```
49
+ *
50
+ * 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:
51
+ *
52
+ * ```ts
53
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
54
+ * const sq = db.$with('sq').as(db.select({
55
+ * name: sql<string>`upper(${users.name})`.as('name'),
56
+ * })
57
+ * .from(users));
58
+ *
59
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
60
+ * ```
61
+ */
30
62
  $with<TAlias extends string>(alias: TAlias): {
31
63
  as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
32
64
  };
65
+ /**
66
+ * Incorporates a previously defined CTE (using `$with`) into the main query.
67
+ *
68
+ * This method allows the main query to reference a temporary named result set.
69
+ *
70
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
71
+ *
72
+ * @param queries The CTEs to incorporate into the main query.
73
+ *
74
+ * @example
75
+ *
76
+ * ```ts
77
+ * // Define a subquery 'sq' as a CTE using $with
78
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
79
+ *
80
+ * // Incorporate the CTE 'sq' into the main query and select from it
81
+ * const result = await db.with(sq).select().from(sq);
82
+ * ```
83
+ */
33
84
  with(...queries: WithSubquery[]): {
34
85
  select: {
35
86
  (): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;
@@ -40,12 +91,131 @@ export declare class MySqlDatabase<TQueryResult extends QueryResultHKT, TPrepare
40
91
  <TSelection_1 extends SelectedFields>(fields: TSelection_1): MySqlSelectBuilder<TSelection_1, TPreparedQueryHKT, "db">;
41
92
  };
42
93
  };
94
+ /**
95
+ * Creates a select query.
96
+ *
97
+ * 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.
98
+ *
99
+ * Use `.from()` method to specify which table to select from.
100
+ *
101
+ * See docs: {@link https://orm.drizzle.team/docs/select}
102
+ *
103
+ * @param fields The selection object.
104
+ *
105
+ * @example
106
+ *
107
+ * ```ts
108
+ * // Select all columns and all rows from the 'cars' table
109
+ * const allCars: Car[] = await db.select().from(cars);
110
+ *
111
+ * // Select specific columns and all rows from the 'cars' table
112
+ * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({
113
+ * id: cars.id,
114
+ * brand: cars.brand
115
+ * })
116
+ * .from(cars);
117
+ * ```
118
+ *
119
+ * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:
120
+ *
121
+ * ```ts
122
+ * // Select specific columns along with expression and all rows from the 'cars' table
123
+ * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({
124
+ * id: cars.id,
125
+ * lowerBrand: sql<string>`lower(${cars.brand})`,
126
+ * })
127
+ * .from(cars);
128
+ * ```
129
+ */
43
130
  select(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;
44
131
  select<TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;
132
+ /**
133
+ * Adds `distinct` expression to the select query.
134
+ *
135
+ * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.
136
+ *
137
+ * Use `.from()` method to specify which table to select from.
138
+ *
139
+ * See docs: {@link https://orm.drizzle.team/docs/select#distinct}
140
+ *
141
+ * @param fields The selection object.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * // Select all unique rows from the 'cars' table
146
+ * await db.selectDistinct()
147
+ * .from(cars)
148
+ * .orderBy(cars.id, cars.brand, cars.color);
149
+ *
150
+ * // Select all unique brands from the 'cars' table
151
+ * await db.selectDistinct({ brand: cars.brand })
152
+ * .from(cars)
153
+ * .orderBy(cars.brand);
154
+ * ```
155
+ */
45
156
  selectDistinct(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;
46
157
  selectDistinct<TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;
158
+ /**
159
+ * Creates an update query.
160
+ *
161
+ * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
162
+ *
163
+ * Use `.set()` method to specify which values to update.
164
+ *
165
+ * See docs: {@link https://orm.drizzle.team/docs/update}
166
+ *
167
+ * @param table The table to update.
168
+ *
169
+ * @example
170
+ *
171
+ * ```ts
172
+ * // Update all rows in the 'cars' table
173
+ * await db.update(cars).set({ color: 'red' });
174
+ *
175
+ * // Update rows with filters and conditions
176
+ * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
177
+ * ```
178
+ */
47
179
  update<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT>;
180
+ /**
181
+ * Creates an insert query.
182
+ *
183
+ * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
184
+ *
185
+ * See docs: {@link https://orm.drizzle.team/docs/insert}
186
+ *
187
+ * @param table The table to insert into.
188
+ *
189
+ * @example
190
+ *
191
+ * ```ts
192
+ * // Insert one row
193
+ * await db.insert(cars).values({ brand: 'BMW' });
194
+ *
195
+ * // Insert multiple rows
196
+ * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
197
+ * ```
198
+ */
48
199
  insert<TTable extends MySqlTable>(table: TTable): MySqlInsertBuilder<TTable, TQueryResult, TPreparedQueryHKT>;
200
+ /**
201
+ * Creates a delete query.
202
+ *
203
+ * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
204
+ *
205
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
206
+ *
207
+ * @param table The table to delete from.
208
+ *
209
+ * @example
210
+ *
211
+ * ```ts
212
+ * // Delete all rows in the 'cars' table
213
+ * await db.delete(cars);
214
+ *
215
+ * // Delete rows with filters and conditions
216
+ * await db.delete(cars).where(eq(cars.color, 'green'));
217
+ * ```
218
+ */
49
219
  delete<TTable extends MySqlTable>(table: TTable): MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT>;
50
220
  execute<T extends {
51
221
  [column: string]: any;
package/mysql-core/db.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { entityKind } from "../entity.js";
2
+ import { SelectionProxyHandler } from "../selection-proxy.js";
3
+ import { WithSubquery } from "../subquery.js";
2
4
  import {
3
5
  MySqlDeleteBase,
4
6
  MySqlInsertBuilder,
@@ -7,8 +9,6 @@ import {
7
9
  QueryBuilder
8
10
  } from "./query-builders/index.js";
9
11
  import { RelationalQueryBuilder } from "./query-builders/query.js";
10
- import { WithSubquery } from "../subquery.js";
11
- import { SelectionProxyHandler } from "../selection-proxy.js";
12
12
  class MySqlDatabase {
13
13
  constructor(dialect, session, schema, mode) {
14
14
  this.dialect = dialect;
@@ -33,6 +33,38 @@ class MySqlDatabase {
33
33
  }
34
34
  static [entityKind] = "MySqlDatabase";
35
35
  query;
36
+ /**
37
+ * Creates a subquery that defines a temporary named result set as a CTE.
38
+ *
39
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
40
+ *
41
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
42
+ *
43
+ * @param alias The alias for the subquery.
44
+ *
45
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
46
+ *
47
+ * @example
48
+ *
49
+ * ```ts
50
+ * // Create a subquery with alias 'sq' and use it in the select query
51
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
52
+ *
53
+ * const result = await db.with(sq).select().from(sq);
54
+ * ```
55
+ *
56
+ * 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:
57
+ *
58
+ * ```ts
59
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
60
+ * const sq = db.$with('sq').as(db.select({
61
+ * name: sql<string>`upper(${users.name})`.as('name'),
62
+ * })
63
+ * .from(users));
64
+ *
65
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
66
+ * ```
67
+ */
36
68
  $with(alias) {
37
69
  return {
38
70
  as(qb) {
@@ -46,6 +78,25 @@ class MySqlDatabase {
46
78
  }
47
79
  };
48
80
  }
81
+ /**
82
+ * Incorporates a previously defined CTE (using `$with`) into the main query.
83
+ *
84
+ * This method allows the main query to reference a temporary named result set.
85
+ *
86
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
87
+ *
88
+ * @param queries The CTEs to incorporate into the main query.
89
+ *
90
+ * @example
91
+ *
92
+ * ```ts
93
+ * // Define a subquery 'sq' as a CTE using $with
94
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
95
+ *
96
+ * // Incorporate the CTE 'sq' into the main query and select from it
97
+ * const result = await db.with(sq).select().from(sq);
98
+ * ```
99
+ */
49
100
  with(...queries) {
50
101
  const self = this;
51
102
  function select(fields) {
@@ -78,12 +129,71 @@ class MySqlDatabase {
78
129
  distinct: true
79
130
  });
80
131
  }
132
+ /**
133
+ * Creates an update query.
134
+ *
135
+ * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
136
+ *
137
+ * Use `.set()` method to specify which values to update.
138
+ *
139
+ * See docs: {@link https://orm.drizzle.team/docs/update}
140
+ *
141
+ * @param table The table to update.
142
+ *
143
+ * @example
144
+ *
145
+ * ```ts
146
+ * // Update all rows in the 'cars' table
147
+ * await db.update(cars).set({ color: 'red' });
148
+ *
149
+ * // Update rows with filters and conditions
150
+ * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
151
+ * ```
152
+ */
81
153
  update(table) {
82
154
  return new MySqlUpdateBuilder(table, this.session, this.dialect);
83
155
  }
156
+ /**
157
+ * Creates an insert query.
158
+ *
159
+ * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
160
+ *
161
+ * See docs: {@link https://orm.drizzle.team/docs/insert}
162
+ *
163
+ * @param table The table to insert into.
164
+ *
165
+ * @example
166
+ *
167
+ * ```ts
168
+ * // Insert one row
169
+ * await db.insert(cars).values({ brand: 'BMW' });
170
+ *
171
+ * // Insert multiple rows
172
+ * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
173
+ * ```
174
+ */
84
175
  insert(table) {
85
176
  return new MySqlInsertBuilder(table, this.session, this.dialect);
86
177
  }
178
+ /**
179
+ * Creates a delete query.
180
+ *
181
+ * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
182
+ *
183
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
184
+ *
185
+ * @param table The table to delete from.
186
+ *
187
+ * @example
188
+ *
189
+ * ```ts
190
+ * // Delete all rows in the 'cars' table
191
+ * await db.delete(cars);
192
+ *
193
+ * // Delete rows with filters and conditions
194
+ * await db.delete(cars).where(eq(cars.color, 'green'));
195
+ * ```
196
+ */
87
197
  delete(table) {
88
198
  return new MySqlDeleteBase(table, this.session, this.dialect);
89
199
  }
@@ -95,36 +205,29 @@ class MySqlDatabase {
95
205
  }
96
206
  }
97
207
  const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(Math.random() * replicas.length)]) => {
98
- const select = (...args) => getReplica(replicas).select(args);
99
- const selectDistinct = (...args) => getReplica(replicas).selectDistinct(args);
100
- const $with = (...args) => getReplica(replicas).with(args);
101
- const update = (...args) => primary.update(args);
102
- const insert = (...args) => primary.insert(args);
103
- const $delete = (...args) => primary.delete(args);
104
- const execute = (...args) => primary.execute(args);
105
- const transaction = (...args) => primary.transaction(args);
106
- return new Proxy(
107
- {
108
- ...primary,
109
- update,
110
- insert,
111
- delete: $delete,
112
- execute,
113
- transaction,
114
- $primary: primary,
115
- select,
116
- selectDistinct,
117
- with: $with
118
- },
119
- {
120
- get(target, prop, _receiver) {
121
- if (prop === "query") {
122
- return getReplica(replicas).query;
123
- }
124
- return target[prop];
125
- }
208
+ const select = (...args) => getReplica(replicas).select(...args);
209
+ const selectDistinct = (...args) => getReplica(replicas).selectDistinct(...args);
210
+ const $with = (...args) => getReplica(replicas).with(...args);
211
+ const update = (...args) => primary.update(...args);
212
+ const insert = (...args) => primary.insert(...args);
213
+ const $delete = (...args) => primary.delete(...args);
214
+ const execute = (...args) => primary.execute(...args);
215
+ const transaction = (...args) => primary.transaction(...args);
216
+ return {
217
+ ...primary,
218
+ update,
219
+ insert,
220
+ delete: $delete,
221
+ execute,
222
+ transaction,
223
+ $primary: primary,
224
+ select,
225
+ selectDistinct,
226
+ with: $with,
227
+ get query() {
228
+ return getReplica(replicas).query;
126
229
  }
127
- );
230
+ };
128
231
  };
129
232
  export {
130
233
  MySqlDatabase,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/mysql-core/db.ts"],"sourcesContent":["import type { ResultSetHeader } from 'mysql2/promise';\nimport { entityKind } from '~/entity.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 type { DrizzleTypeError } from '~/utils.ts';\nimport type { MySqlDialect } from './dialect.ts';\nimport {\n\tMySqlDeleteBase,\n\tMySqlInsertBuilder,\n\tMySqlSelectBuilder,\n\tMySqlUpdateBuilder,\n\tQueryBuilder,\n} from './query-builders/index.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type {\n\tMode,\n\tMySqlSession,\n\tMySqlTransaction,\n\tMySqlTransactionConfig,\n\tPreparedQueryHKTBase,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from './session.ts';\nimport type { WithSubqueryWithSelection } from './subquery.ts';\nimport type { MySqlTable } from './table.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\n\nexport class MySqlDatabase<\n\tTQueryResult extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTFullSchema extends Record<string, unknown> = {},\n\tTSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'MySqlDatabase';\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<TPreparedQueryHKT, TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: MySqlDialect,\n\t\t/** @internal */\n\t\treadonly session: MySqlSession<any, any, any, any>,\n\t\tschema: RelationalSchemaConfig<TSchema> | undefined,\n\t\tprotected readonly mode: Mode,\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 MySqlDatabase<TQueryResult, TPreparedQueryHKT, Record<string, any>>['query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as MySqlTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t\tthis.mode,\n\t\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(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\t\tfunction select(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\t\treturn new MySqlSelectBuilder({\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\tfunction selectDistinct(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\t\tfunction selectDistinct(\n\t\t\tfields?: SelectedFields,\n\t\t): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\t\treturn new MySqlSelectBuilder({\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\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\treturn { select, selectDistinct };\n\t}\n\n\tselect(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\tselect(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\treturn new MySqlSelectBuilder({ fields: fields ?? undefined, session: this.session, dialect: this.dialect });\n\t}\n\n\tselectDistinct(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields: TSelection,\n\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\tselectDistinct(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\treturn new MySqlSelectBuilder({\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\tupdate<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\tinsert<TTable extends MySqlTable>(table: TTable): MySqlInsertBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlInsertBuilder(table, this.session, this.dialect);\n\t}\n\n\tdelete<TTable extends MySqlTable>(table: TTable): MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlDeleteBase(table, this.session, this.dialect);\n\t}\n\n\texecute<T extends { [column: string]: any } = ResultSetHeader>(\n\t\tquery: SQLWrapper,\n\t): Promise<QueryResultKind<TQueryResult, T>> {\n\t\treturn this.session.execute(query.getSQL());\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (\n\t\t\ttx: MySqlTransaction<TQueryResult, TPreparedQueryHKT, TFullSchema, TSchema>,\n\t\t\tconfig?: MySqlTransactionConfig,\n\t\t) => Promise<T>,\n\t\tconfig?: MySqlTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type MySQLWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends TablesRelationalConfig,\n\tQ extends MySqlDatabase<\n\t\tHKT,\n\t\tTPreparedQueryHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): MySQLWithReplicas<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 $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\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\t$primary: primary,\n\t\t\tselect,\n\t\t\tselectDistinct,\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":"AACA,SAAS,kBAAkB;AAM3B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,8BAA8B;AAavC,SAAS,oBAAoB;AAC7B,SAAS,6BAA6B;AAE/B,MAAM,cAKX;AAAA,EAcD,YAEU,SAEA,SACT,QACmB,MAClB;AALQ;AAEA;AAEU;AAEnB,SAAK,IAAI,SACN,EAAE,QAAQ,OAAO,QAAQ,eAAe,OAAO,cAAc,IAC7D,EAAE,QAAQ,QAAW,eAAe,CAAC,EAAE;AAC1C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,IAAI;AACpE,QAAM,cAAgC,IAAI,SAAc,QAAQ,YAAY,IAAI;AAEhF,SAAO,IAAI;AAAA,IACV;AAAA,MACC,GAAG;AAAA,MACH;AAAA,MACA;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV;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/mysql-core/db.ts"],"sourcesContent":["import type { ResultSetHeader } from 'mysql2/promise';\nimport { entityKind } from '~/entity.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 { MySqlDialect } from './dialect.ts';\nimport {\n\tMySqlDeleteBase,\n\tMySqlInsertBuilder,\n\tMySqlSelectBuilder,\n\tMySqlUpdateBuilder,\n\tQueryBuilder,\n} from './query-builders/index.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type {\n\tMode,\n\tMySqlSession,\n\tMySqlTransaction,\n\tMySqlTransactionConfig,\n\tPreparedQueryHKTBase,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from './session.ts';\nimport type { WithSubqueryWithSelection } from './subquery.ts';\nimport type { MySqlTable } from './table.ts';\n\nexport class MySqlDatabase<\n\tTQueryResult extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTFullSchema extends Record<string, unknown> = {},\n\tTSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'MySqlDatabase';\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<TPreparedQueryHKT, TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: MySqlDialect,\n\t\t/** @internal */\n\t\treadonly session: MySqlSession<any, any, any, any>,\n\t\tschema: RelationalSchemaConfig<TSchema> | undefined,\n\t\tprotected readonly mode: Mode,\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 MySqlDatabase<TQueryResult, TPreparedQueryHKT, Record<string, any>>['query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as MySqlTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t\tthis.mode,\n\t\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(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\t\tfunction select(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\t\treturn new MySqlSelectBuilder({\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\tfunction selectDistinct(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\t\tfunction selectDistinct(\n\t\t\tfields?: SelectedFields,\n\t\t): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\t\treturn new MySqlSelectBuilder({\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\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\treturn { select, selectDistinct };\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(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\tselect(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\treturn new MySqlSelectBuilder({ fields: fields ?? undefined, session: this.session, dialect: this.dialect });\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(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields: TSelection,\n\t): MySqlSelectBuilder<TSelection, TPreparedQueryHKT>;\n\tselectDistinct(fields?: SelectedFields): MySqlSelectBuilder<SelectedFields | undefined, TPreparedQueryHKT> {\n\t\treturn new MySqlSelectBuilder({\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 * 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 */\n\tupdate<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlUpdateBuilder(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 */\n\tinsert<TTable extends MySqlTable>(table: TTable): MySqlInsertBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlInsertBuilder(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 */\n\tdelete<TTable extends MySqlTable>(table: TTable): MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\treturn new MySqlDeleteBase(table, this.session, this.dialect);\n\t}\n\n\texecute<T extends { [column: string]: any } = ResultSetHeader>(\n\t\tquery: SQLWrapper,\n\t): Promise<QueryResultKind<TQueryResult, T>> {\n\t\treturn this.session.execute(query.getSQL());\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (\n\t\t\ttx: MySqlTransaction<TQueryResult, TPreparedQueryHKT, TFullSchema, TSchema>,\n\t\t\tconfig?: MySqlTransactionConfig,\n\t\t) => Promise<T>,\n\t\tconfig?: MySqlTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type MySQLWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends TablesRelationalConfig,\n\tQ extends MySqlDatabase<\n\t\tHKT,\n\t\tTPreparedQueryHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): MySQLWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst $with: Q['with'] = (...args: []) => 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, any]) => primary.transaction(...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\t$primary: primary,\n\t\tselect,\n\t\tselectDistinct,\n\t\twith: $with,\n\t\tget query() {\n\t\t\treturn getReplica(replicas).query;\n\t\t},\n\t};\n};\n"],"mappings":"AACA,SAAS,kBAAkB;AAG3B,SAAS,6BAA6B;AAEtC,SAAS,oBAAoB;AAG7B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,8BAA8B;AAchC,MAAM,cAKX;AAAA,EAcD,YAEU,SAEA,SACT,QACmB,MAClB;AALQ;AAEA;AAEU;AAEnB,SAAK,IAAI,SACN,EAAE,QAAQ,OAAO,QAAQ,eAAe,OAAO,cAAc,IAC7D,EAAE,QAAQ,QAAW,eAAe,CAAC,EAAE;AAC1C,SAAK,QAAQ,CAAC;AACd,QAAI,KAAK,EAAE,QAAQ;AAClB,iBAAW,CAAC,WAAW,OAAO,KAAK,OAAO,QAAQ,KAAK,EAAE,MAAM,GAAG;AACjE,QAAC,KAAK,MAAuF,SAAS,IACrG,IAAI;AAAA,UACH,OAAQ;AAAA,UACR,KAAK,EAAE;AAAA,UACP,KAAK,EAAE;AAAA,UACP,OAAQ,WAAW,SAAS;AAAA,UAC5B;AAAA,UACA;AAAA,UACA;AAAA,UACA,KAAK;AAAA,QACN;AAAA,MACF;AAAA,IACD;AAAA,EACD;AAAA,EAxCA,QAAiB,UAAU,IAAY;AAAA,EAOvC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA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tE,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,SAAqB,QAAQ,YAAY,GAAG,IAAI;AAE1F,SAAO;AAAA,IACN,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,IAAI,QAAQ;AACX,aAAO,WAAW,QAAQ,EAAE;AAAA,IAC7B;AAAA,EACD;AACD;","names":[]}
@@ -33,6 +33,35 @@ class MySqlDeleteBase extends import_query_promise.QueryPromise {
33
33
  }
34
34
  static [import_entity.entityKind] = "MySqlDelete";
35
35
  config;
36
+ /**
37
+ * Adds a `where` clause to the query.
38
+ *
39
+ * Calling this method will delete only those rows that fulfill a specified condition.
40
+ *
41
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
42
+ *
43
+ * @param where the `where` clause.
44
+ *
45
+ * @example
46
+ * You can use conditional operators and `sql function` to filter the rows to be deleted.
47
+ *
48
+ * ```ts
49
+ * // Delete all cars with green color
50
+ * db.delete(cars).where(eq(cars.color, 'green'));
51
+ * // or
52
+ * db.delete(cars).where(sql`${cars.color} = 'green'`)
53
+ * ```
54
+ *
55
+ * You can logically combine conditional operators with `and()` and `or()` operators:
56
+ *
57
+ * ```ts
58
+ * // Delete all BMW cars with a green color
59
+ * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
60
+ *
61
+ * // Delete all cars with the green or blue color
62
+ * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
63
+ * ```
64
+ */
36
65
  where(where) {
37
66
  this.config.where = where;
38
67
  return this;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/mysql-core/query-builders/delete.ts"],"sourcesContent":["import { entityKind } from '~/entity.ts';\nimport type { MySqlDialect } from '~/mysql-core/dialect.ts';\nimport type {\n\tAnyQueryResultHKT,\n\tMySqlSession,\n\tPreparedQueryConfig,\n\tPreparedQueryHKTBase,\n\tPreparedQueryKind,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from '~/mysql-core/session.ts';\nimport type { MySqlTable } from '~/mysql-core/table.ts';\nimport { QueryPromise } from '~/query-promise.ts';\nimport type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';\nimport type { SelectedFieldsOrdered } from './select.types.ts';\n\nexport type MySqlDeleteWithout<\n\tT extends AnyMySqlDeleteBase,\n\tTDynamic extends boolean,\n\tK extends keyof T & string,\n> = TDynamic extends true ? T\n\t: Omit<\n\t\tMySqlDeleteBase<\n\t\t\tT['_']['table'],\n\t\t\tT['_']['queryResult'],\n\t\t\tT['_']['preparedQueryHKT'],\n\t\t\tTDynamic,\n\t\t\tT['_']['excludedMethods'] | K\n\t\t>,\n\t\tT['_']['excludedMethods'] | K\n\t>;\n\nexport type MySqlDelete<\n\tTTable extends MySqlTable = MySqlTable,\n\tTQueryResult extends QueryResultHKT = AnyQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,\n> = MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT, true, never>;\n\nexport interface MySqlDeleteConfig {\n\twhere?: SQL | undefined;\n\ttable: MySqlTable;\n\treturning?: SelectedFieldsOrdered;\n}\n\nexport type MySqlDeletePrepare<T extends AnyMySqlDeleteBase> = PreparedQueryKind<\n\tT['_']['preparedQueryHKT'],\n\tPreparedQueryConfig & {\n\t\texecute: QueryResultKind<T['_']['queryResult'], never>;\n\t\titerator: never;\n\t},\n\ttrue\n>;\n\ntype MySqlDeleteDynamic<T extends AnyMySqlDeleteBase> = MySqlDelete<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT']\n>;\n\ntype AnyMySqlDeleteBase = MySqlDeleteBase<any, any, any, any, any>;\n\nexport interface MySqlDeleteBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTDynamic extends boolean = false,\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<QueryResultKind<TQueryResult, never>> {\n\treadonly _: {\n\t\treadonly table: TTable;\n\t\treadonly queryResult: TQueryResult;\n\t\treadonly preparedQueryHKT: TPreparedQueryHKT;\n\t\treadonly dynamic: TDynamic;\n\t\treadonly excludedMethods: TExcludedMethods;\n\t};\n}\n\nexport class MySqlDeleteBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends QueryResultHKT,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTDynamic extends boolean = false,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<QueryResultKind<TQueryResult, never>> implements SQLWrapper {\n\tstatic readonly [entityKind]: string = 'MySqlDelete';\n\n\tprivate config: MySqlDeleteConfig;\n\n\tconstructor(\n\t\tprivate table: TTable,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t) {\n\t\tsuper();\n\t\tthis.config = { table };\n\t}\n\n\twhere(where: SQL | undefined): MySqlDeleteWithout<this, TDynamic, 'where'> {\n\t\tthis.config.where = where;\n\t\treturn this as any;\n\t}\n\n\t/** @internal */\n\tgetSQL(): SQL {\n\t\treturn this.dialect.buildDeleteQuery(this.config);\n\t}\n\n\ttoSQL(): Query {\n\t\tconst { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());\n\t\treturn rest;\n\t}\n\n\tprepare(): MySqlDeletePrepare<this> {\n\t\treturn this.session.prepareQuery(\n\t\t\tthis.dialect.sqlToQuery(this.getSQL()),\n\t\t\tthis.config.returning,\n\t\t) as MySqlDeletePrepare<this>;\n\t}\n\n\toverride execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {\n\t\treturn this.prepare().execute(placeholderValues);\n\t};\n\n\tprivate createIterator = (): ReturnType<this['prepare']>['iterator'] => {\n\t\tconst self = this;\n\t\treturn async function*(placeholderValues) {\n\t\t\tyield* self.prepare().iterator(placeholderValues);\n\t\t};\n\t};\n\n\titerator = this.createIterator();\n\n\t$dynamic(): MySqlDeleteDynamic<this> {\n\t\treturn this as any;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA2B;AAY3B,2BAA6B;AAiEtB,MAAM,wBAQH,kCAAyE;AAAA,EAKlF,YACS,OACA,SACA,SACP;AACD,UAAM;AAJE;AACA;AACA;AAGR,SAAK,SAAS,EAAE,MAAM;AAAA,EACvB;AAAA,EAXA,QAAiB,wBAAU,IAAY;AAAA,EAE/B;AAAA,EAWR,MAAM,OAAqE;AAC1E,SAAK,OAAO,QAAQ;AACpB,WAAO;AAAA,EACR;AAAA;AAAA,EAGA,SAAc;AACb,WAAO,KAAK,QAAQ,iBAAiB,KAAK,MAAM;AAAA,EACjD;AAAA,EAEA,QAAe;AACd,UAAM,EAAE,SAAS,UAAU,GAAG,KAAK,IAAI,KAAK,QAAQ,WAAW,KAAK,OAAO,CAAC;AAC5E,WAAO;AAAA,EACR;AAAA,EAEA,UAAoC;AACnC,WAAO,KAAK,QAAQ;AAAA,MACnB,KAAK,QAAQ,WAAW,KAAK,OAAO,CAAC;AAAA,MACrC,KAAK,OAAO;AAAA,IACb;AAAA,EACD;AAAA,EAES,UAAkD,CAAC,sBAAsB;AACjF,WAAO,KAAK,QAAQ,EAAE,QAAQ,iBAAiB;AAAA,EAChD;AAAA,EAEQ,iBAAiB,MAA+C;AACvE,UAAM,OAAO;AACb,WAAO,iBAAgB,mBAAmB;AACzC,aAAO,KAAK,QAAQ,EAAE,SAAS,iBAAiB;AAAA,IACjD;AAAA,EACD;AAAA,EAEA,WAAW,KAAK,eAAe;AAAA,EAE/B,WAAqC;AACpC,WAAO;AAAA,EACR;AACD;","names":[]}
1
+ {"version":3,"sources":["../../../src/mysql-core/query-builders/delete.ts"],"sourcesContent":["import { entityKind } from '~/entity.ts';\nimport type { MySqlDialect } from '~/mysql-core/dialect.ts';\nimport type {\n\tAnyQueryResultHKT,\n\tMySqlSession,\n\tPreparedQueryConfig,\n\tPreparedQueryHKTBase,\n\tPreparedQueryKind,\n\tQueryResultHKT,\n\tQueryResultKind,\n} from '~/mysql-core/session.ts';\nimport type { MySqlTable } from '~/mysql-core/table.ts';\nimport { QueryPromise } from '~/query-promise.ts';\nimport type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';\nimport type { SelectedFieldsOrdered } from './select.types.ts';\n\nexport type MySqlDeleteWithout<\n\tT extends AnyMySqlDeleteBase,\n\tTDynamic extends boolean,\n\tK extends keyof T & string,\n> = TDynamic extends true ? T\n\t: Omit<\n\t\tMySqlDeleteBase<\n\t\t\tT['_']['table'],\n\t\t\tT['_']['queryResult'],\n\t\t\tT['_']['preparedQueryHKT'],\n\t\t\tTDynamic,\n\t\t\tT['_']['excludedMethods'] | K\n\t\t>,\n\t\tT['_']['excludedMethods'] | K\n\t>;\n\nexport type MySqlDelete<\n\tTTable extends MySqlTable = MySqlTable,\n\tTQueryResult extends QueryResultHKT = AnyQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,\n> = MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT, true, never>;\n\nexport interface MySqlDeleteConfig {\n\twhere?: SQL | undefined;\n\ttable: MySqlTable;\n\treturning?: SelectedFieldsOrdered;\n}\n\nexport type MySqlDeletePrepare<T extends AnyMySqlDeleteBase> = PreparedQueryKind<\n\tT['_']['preparedQueryHKT'],\n\tPreparedQueryConfig & {\n\t\texecute: QueryResultKind<T['_']['queryResult'], never>;\n\t\titerator: never;\n\t},\n\ttrue\n>;\n\ntype MySqlDeleteDynamic<T extends AnyMySqlDeleteBase> = MySqlDelete<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT']\n>;\n\ntype AnyMySqlDeleteBase = MySqlDeleteBase<any, any, any, any, any>;\n\nexport interface MySqlDeleteBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends QueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTDynamic extends boolean = false,\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<QueryResultKind<TQueryResult, never>> {\n\treadonly _: {\n\t\treadonly table: TTable;\n\t\treadonly queryResult: TQueryResult;\n\t\treadonly preparedQueryHKT: TPreparedQueryHKT;\n\t\treadonly dynamic: TDynamic;\n\t\treadonly excludedMethods: TExcludedMethods;\n\t};\n}\n\nexport class MySqlDeleteBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends QueryResultHKT,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTDynamic extends boolean = false,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<QueryResultKind<TQueryResult, never>> implements SQLWrapper {\n\tstatic readonly [entityKind]: string = 'MySqlDelete';\n\n\tprivate config: MySqlDeleteConfig;\n\n\tconstructor(\n\t\tprivate table: TTable,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t) {\n\t\tsuper();\n\t\tthis.config = { table };\n\t}\n\n\t/** \n\t * Adds a `where` clause to the query.\n\t * \n\t * Calling this method will delete only those rows that fulfill a specified condition.\n\t * \n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t * \n\t * @param where the `where` clause.\n\t * \n\t * @example\n\t * You can use conditional operators and `sql function` to filter the rows to be deleted.\n\t * \n\t * ```ts\n\t * // Delete all cars with green color\n\t * db.delete(cars).where(eq(cars.color, 'green'));\n\t * // or\n\t * db.delete(cars).where(sql`${cars.color} = 'green'`)\n\t * ```\n\t * \n\t * You can logically combine conditional operators with `and()` and `or()` operators:\n\t * \n\t * ```ts\n\t * // Delete all BMW cars with a green color\n\t * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));\n\t * \n\t * // Delete all cars with the green or blue color\n\t * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));\n\t * ```\n\t*/\n\twhere(where: SQL | undefined): MySqlDeleteWithout<this, TDynamic, 'where'> {\n\t\tthis.config.where = where;\n\t\treturn this as any;\n\t}\n\n\t/** @internal */\n\tgetSQL(): SQL {\n\t\treturn this.dialect.buildDeleteQuery(this.config);\n\t}\n\n\ttoSQL(): Query {\n\t\tconst { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());\n\t\treturn rest;\n\t}\n\n\tprepare(): MySqlDeletePrepare<this> {\n\t\treturn this.session.prepareQuery(\n\t\t\tthis.dialect.sqlToQuery(this.getSQL()),\n\t\t\tthis.config.returning,\n\t\t) as MySqlDeletePrepare<this>;\n\t}\n\n\toverride execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {\n\t\treturn this.prepare().execute(placeholderValues);\n\t};\n\n\tprivate createIterator = (): ReturnType<this['prepare']>['iterator'] => {\n\t\tconst self = this;\n\t\treturn async function*(placeholderValues) {\n\t\t\tyield* self.prepare().iterator(placeholderValues);\n\t\t};\n\t};\n\n\titerator = this.createIterator();\n\n\t$dynamic(): MySqlDeleteDynamic<this> {\n\t\treturn this as any;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA2B;AAY3B,2BAA6B;AAiEtB,MAAM,wBAQH,kCAAyE;AAAA,EAKlF,YACS,OACA,SACA,SACP;AACD,UAAM;AAJE;AACA;AACA;AAGR,SAAK,SAAS,EAAE,MAAM;AAAA,EACvB;AAAA,EAXA,QAAiB,wBAAU,IAAY;AAAA,EAE/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAwCR,MAAM,OAAqE;AAC1E,SAAK,OAAO,QAAQ;AACpB,WAAO;AAAA,EACR;AAAA;AAAA,EAGA,SAAc;AACb,WAAO,KAAK,QAAQ,iBAAiB,KAAK,MAAM;AAAA,EACjD;AAAA,EAEA,QAAe;AACd,UAAM,EAAE,SAAS,UAAU,GAAG,KAAK,IAAI,KAAK,QAAQ,WAAW,KAAK,OAAO,CAAC;AAC5E,WAAO;AAAA,EACR;AAAA,EAEA,UAAoC;AACnC,WAAO,KAAK,QAAQ;AAAA,MACnB,KAAK,QAAQ,WAAW,KAAK,OAAO,CAAC;AAAA,MACrC,KAAK,OAAO;AAAA,IACb;AAAA,EACD;AAAA,EAES,UAAkD,CAAC,sBAAsB;AACjF,WAAO,KAAK,QAAQ,EAAE,QAAQ,iBAAiB;AAAA,EAChD;AAAA,EAEQ,iBAAiB,MAA+C;AACvE,UAAM,OAAO;AACb,WAAO,iBAAgB,mBAAmB;AACzC,aAAO,KAAK,QAAQ,EAAE,SAAS,iBAAiB;AAAA,IACjD;AAAA,EACD;AAAA,EAEA,WAAW,KAAK,eAAe;AAAA,EAE/B,WAAqC;AACpC,WAAO;AAAA,EACR;AACD;","names":[]}
@@ -34,6 +34,35 @@ export declare class MySqlDeleteBase<TTable extends MySqlTable, TQueryResult ext
34
34
  static readonly [entityKind]: string;
35
35
  private config;
36
36
  constructor(table: TTable, session: MySqlSession, dialect: MySqlDialect);
37
+ /**
38
+ * Adds a `where` clause to the query.
39
+ *
40
+ * Calling this method will delete only those rows that fulfill a specified condition.
41
+ *
42
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
43
+ *
44
+ * @param where the `where` clause.
45
+ *
46
+ * @example
47
+ * You can use conditional operators and `sql function` to filter the rows to be deleted.
48
+ *
49
+ * ```ts
50
+ * // Delete all cars with green color
51
+ * db.delete(cars).where(eq(cars.color, 'green'));
52
+ * // or
53
+ * db.delete(cars).where(sql`${cars.color} = 'green'`)
54
+ * ```
55
+ *
56
+ * You can logically combine conditional operators with `and()` and `or()` operators:
57
+ *
58
+ * ```ts
59
+ * // Delete all BMW cars with a green color
60
+ * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
61
+ *
62
+ * // Delete all cars with the green or blue color
63
+ * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
64
+ * ```
65
+ */
37
66
  where(where: SQL | undefined): MySqlDeleteWithout<this, TDynamic, 'where'>;
38
67
  toSQL(): Query;
39
68
  prepare(): MySqlDeletePrepare<this>;
@@ -34,6 +34,35 @@ export declare class MySqlDeleteBase<TTable extends MySqlTable, TQueryResult ext
34
34
  static readonly [entityKind]: string;
35
35
  private config;
36
36
  constructor(table: TTable, session: MySqlSession, dialect: MySqlDialect);
37
+ /**
38
+ * Adds a `where` clause to the query.
39
+ *
40
+ * Calling this method will delete only those rows that fulfill a specified condition.
41
+ *
42
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
43
+ *
44
+ * @param where the `where` clause.
45
+ *
46
+ * @example
47
+ * You can use conditional operators and `sql function` to filter the rows to be deleted.
48
+ *
49
+ * ```ts
50
+ * // Delete all cars with green color
51
+ * db.delete(cars).where(eq(cars.color, 'green'));
52
+ * // or
53
+ * db.delete(cars).where(sql`${cars.color} = 'green'`)
54
+ * ```
55
+ *
56
+ * You can logically combine conditional operators with `and()` and `or()` operators:
57
+ *
58
+ * ```ts
59
+ * // Delete all BMW cars with a green color
60
+ * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
61
+ *
62
+ * // Delete all cars with the green or blue color
63
+ * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
64
+ * ```
65
+ */
37
66
  where(where: SQL | undefined): MySqlDeleteWithout<this, TDynamic, 'where'>;
38
67
  toSQL(): Query;
39
68
  prepare(): MySqlDeletePrepare<this>;
@@ -10,6 +10,35 @@ class MySqlDeleteBase extends QueryPromise {
10
10
  }
11
11
  static [entityKind] = "MySqlDelete";
12
12
  config;
13
+ /**
14
+ * Adds a `where` clause to the query.
15
+ *
16
+ * Calling this method will delete only those rows that fulfill a specified condition.
17
+ *
18
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
19
+ *
20
+ * @param where the `where` clause.
21
+ *
22
+ * @example
23
+ * You can use conditional operators and `sql function` to filter the rows to be deleted.
24
+ *
25
+ * ```ts
26
+ * // Delete all cars with green color
27
+ * db.delete(cars).where(eq(cars.color, 'green'));
28
+ * // or
29
+ * db.delete(cars).where(sql`${cars.color} = 'green'`)
30
+ * ```
31
+ *
32
+ * You can logically combine conditional operators with `and()` and `or()` operators:
33
+ *
34
+ * ```ts
35
+ * // Delete all BMW cars with a green color
36
+ * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
37
+ *
38
+ * // Delete all cars with the green or blue color
39
+ * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
40
+ * ```
41
+ */
13
42
  where(where) {
14
43
  this.config.where = where;
15
44
  return this;