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/mysql-core/db.cjs CHANGED
@@ -23,10 +23,10 @@ __export(db_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(db_exports);
25
25
  var import_entity = require("../entity.cjs");
26
+ var import_selection_proxy = require("../selection-proxy.cjs");
27
+ var import_subquery = require("../subquery.cjs");
26
28
  var import_query_builders = require("./query-builders/index.cjs");
27
29
  var import_query = require("./query-builders/query.cjs");
28
- var import_subquery = require("../subquery.cjs");
29
- var import_selection_proxy = require("../selection-proxy.cjs");
30
30
  class MySqlDatabase {
31
31
  constructor(dialect, session, schema, mode) {
32
32
  this.dialect = dialect;
@@ -51,6 +51,38 @@ class MySqlDatabase {
51
51
  }
52
52
  static [import_entity.entityKind] = "MySqlDatabase";
53
53
  query;
54
+ /**
55
+ * Creates a subquery that defines a temporary named result set as a CTE.
56
+ *
57
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
58
+ *
59
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
60
+ *
61
+ * @param alias The alias for the subquery.
62
+ *
63
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
64
+ *
65
+ * @example
66
+ *
67
+ * ```ts
68
+ * // Create a subquery with alias 'sq' and use it in the select query
69
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
70
+ *
71
+ * const result = await db.with(sq).select().from(sq);
72
+ * ```
73
+ *
74
+ * 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:
75
+ *
76
+ * ```ts
77
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
78
+ * const sq = db.$with('sq').as(db.select({
79
+ * name: sql<string>`upper(${users.name})`.as('name'),
80
+ * })
81
+ * .from(users));
82
+ *
83
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
84
+ * ```
85
+ */
54
86
  $with(alias) {
55
87
  return {
56
88
  as(qb) {
@@ -64,6 +96,25 @@ class MySqlDatabase {
64
96
  }
65
97
  };
66
98
  }
99
+ /**
100
+ * Incorporates a previously defined CTE (using `$with`) into the main query.
101
+ *
102
+ * This method allows the main query to reference a temporary named result set.
103
+ *
104
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
105
+ *
106
+ * @param queries The CTEs to incorporate into the main query.
107
+ *
108
+ * @example
109
+ *
110
+ * ```ts
111
+ * // Define a subquery 'sq' as a CTE using $with
112
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
113
+ *
114
+ * // Incorporate the CTE 'sq' into the main query and select from it
115
+ * const result = await db.with(sq).select().from(sq);
116
+ * ```
117
+ */
67
118
  with(...queries) {
68
119
  const self = this;
69
120
  function select(fields) {
@@ -96,12 +147,71 @@ class MySqlDatabase {
96
147
  distinct: true
97
148
  });
98
149
  }
150
+ /**
151
+ * Creates an update query.
152
+ *
153
+ * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
154
+ *
155
+ * Use `.set()` method to specify which values to update.
156
+ *
157
+ * See docs: {@link https://orm.drizzle.team/docs/update}
158
+ *
159
+ * @param table The table to update.
160
+ *
161
+ * @example
162
+ *
163
+ * ```ts
164
+ * // Update all rows in the 'cars' table
165
+ * await db.update(cars).set({ color: 'red' });
166
+ *
167
+ * // Update rows with filters and conditions
168
+ * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
169
+ * ```
170
+ */
99
171
  update(table) {
100
172
  return new import_query_builders.MySqlUpdateBuilder(table, this.session, this.dialect);
101
173
  }
174
+ /**
175
+ * Creates an insert query.
176
+ *
177
+ * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
178
+ *
179
+ * See docs: {@link https://orm.drizzle.team/docs/insert}
180
+ *
181
+ * @param table The table to insert into.
182
+ *
183
+ * @example
184
+ *
185
+ * ```ts
186
+ * // Insert one row
187
+ * await db.insert(cars).values({ brand: 'BMW' });
188
+ *
189
+ * // Insert multiple rows
190
+ * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
191
+ * ```
192
+ */
102
193
  insert(table) {
103
194
  return new import_query_builders.MySqlInsertBuilder(table, this.session, this.dialect);
104
195
  }
196
+ /**
197
+ * Creates a delete query.
198
+ *
199
+ * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
200
+ *
201
+ * See docs: {@link https://orm.drizzle.team/docs/delete}
202
+ *
203
+ * @param table The table to delete from.
204
+ *
205
+ * @example
206
+ *
207
+ * ```ts
208
+ * // Delete all rows in the 'cars' table
209
+ * await db.delete(cars);
210
+ *
211
+ * // Delete rows with filters and conditions
212
+ * await db.delete(cars).where(eq(cars.color, 'green'));
213
+ * ```
214
+ */
105
215
  delete(table) {
106
216
  return new import_query_builders.MySqlDeleteBase(table, this.session, this.dialect);
107
217
  }
@@ -113,36 +223,29 @@ class MySqlDatabase {
113
223
  }
114
224
  }
115
225
  const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(Math.random() * replicas.length)]) => {
116
- const select = (...args) => getReplica(replicas).select(args);
117
- const selectDistinct = (...args) => getReplica(replicas).selectDistinct(args);
118
- const $with = (...args) => getReplica(replicas).with(args);
119
- const update = (...args) => primary.update(args);
120
- const insert = (...args) => primary.insert(args);
121
- const $delete = (...args) => primary.delete(args);
122
- const execute = (...args) => primary.execute(args);
123
- const transaction = (...args) => primary.transaction(args);
124
- return new Proxy(
125
- {
126
- ...primary,
127
- update,
128
- insert,
129
- delete: $delete,
130
- execute,
131
- transaction,
132
- $primary: primary,
133
- select,
134
- selectDistinct,
135
- with: $with
136
- },
137
- {
138
- get(target, prop, _receiver) {
139
- if (prop === "query") {
140
- return getReplica(replicas).query;
141
- }
142
- return target[prop];
143
- }
226
+ const select = (...args) => getReplica(replicas).select(...args);
227
+ const selectDistinct = (...args) => getReplica(replicas).selectDistinct(...args);
228
+ const $with = (...args) => getReplica(replicas).with(...args);
229
+ const update = (...args) => primary.update(...args);
230
+ const insert = (...args) => primary.insert(...args);
231
+ const $delete = (...args) => primary.delete(...args);
232
+ const execute = (...args) => primary.execute(...args);
233
+ const transaction = (...args) => primary.transaction(...args);
234
+ return {
235
+ ...primary,
236
+ update,
237
+ insert,
238
+ delete: $delete,
239
+ execute,
240
+ transaction,
241
+ $primary: primary,
242
+ select,
243
+ selectDistinct,
244
+ with: $with,
245
+ get query() {
246
+ return getReplica(replicas).query;
144
247
  }
145
- );
248
+ };
146
249
  };
147
250
  // Annotate the CommonJS export names for ESM import in node:
148
251
  0 && (module.exports = {
@@ -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":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA2B;AAM3B,4BAMO;AACP,mBAAuC;AAavC,sBAA6B;AAC7B,6BAAsC;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,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,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,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":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA2B;AAG3B,6BAAsC;AAEtC,sBAA6B;AAG7B,4BAMO;AACP,mBAAuC;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,wBAAU,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmEA,MAA6B,OAAe;AAC3C,WAAO;AAAA,MACN,GACC,IACgD;AAChD,YAAI,OAAO,OAAO,YAAY;AAC7B,eAAK,GAAG,IAAI,mCAAa,CAAC;AAAA,QAC3B;AAEA,eAAO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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":[]}
@@ -3,6 +3,7 @@ import { entityKind } from "../entity.cjs";
3
3
  import type { TypedQueryBuilder } from "../query-builders/query-builder.cjs";
4
4
  import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from "../relations.cjs";
5
5
  import type { ColumnsSelection, SQLWrapper } from "../sql/sql.cjs";
6
+ import { WithSubquery } from "../subquery.cjs";
6
7
  import type { DrizzleTypeError } from "../utils.cjs";
7
8
  import type { MySqlDialect } from "./dialect.cjs";
8
9
  import { MySqlDeleteBase, MySqlInsertBuilder, MySqlSelectBuilder, MySqlUpdateBuilder, QueryBuilder } from "./query-builders/index.cjs";
@@ -11,7 +12,6 @@ import type { SelectedFields } from "./query-builders/select.types.cjs";
11
12
  import type { Mode, MySqlSession, MySqlTransaction, MySqlTransactionConfig, PreparedQueryHKTBase, QueryResultHKT, QueryResultKind } from "./session.cjs";
12
13
  import type { WithSubqueryWithSelection } from "./subquery.cjs";
13
14
  import type { MySqlTable } from "./table.cjs";
14
- import { WithSubquery } from "../subquery.cjs";
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;