drizzle-orm 0.29.2-6d706c1 → 0.29.2-aa669c4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -140
- package/mysql-core/db.cjs +1 -7
- package/mysql-core/db.cjs.map +1 -1
- package/mysql-core/db.d.cts +0 -2
- package/mysql-core/db.d.ts +0 -2
- package/mysql-core/db.js +1 -7
- package/mysql-core/db.js.map +1 -1
- package/mysql-core/dialect.cjs +16 -20
- package/mysql-core/dialect.cjs.map +1 -1
- package/mysql-core/dialect.d.cts +2 -3
- package/mysql-core/dialect.d.ts +2 -3
- package/mysql-core/dialect.js +16 -20
- package/mysql-core/dialect.js.map +1 -1
- package/mysql-core/query-builders/delete.cjs +2 -2
- package/mysql-core/query-builders/delete.cjs.map +1 -1
- package/mysql-core/query-builders/delete.d.cts +1 -3
- package/mysql-core/query-builders/delete.d.ts +1 -3
- package/mysql-core/query-builders/delete.js +2 -2
- package/mysql-core/query-builders/delete.js.map +1 -1
- package/mysql-core/query-builders/update.cjs +4 -5
- package/mysql-core/query-builders/update.cjs.map +1 -1
- package/mysql-core/query-builders/update.d.cts +2 -5
- package/mysql-core/query-builders/update.d.ts +2 -5
- package/mysql-core/query-builders/update.js +4 -5
- package/mysql-core/query-builders/update.js.map +1 -1
- package/package.json +49 -49
- package/pg-core/db.cjs +1 -28
- package/pg-core/db.cjs.map +1 -1
- package/pg-core/db.d.cts +0 -11
- package/pg-core/db.d.ts +0 -11
- package/pg-core/db.js +1 -28
- package/pg-core/db.js.map +1 -1
- package/pg-core/dialect.cjs +18 -23
- package/pg-core/dialect.cjs.map +1 -1
- package/pg-core/dialect.d.cts +3 -4
- package/pg-core/dialect.d.ts +3 -4
- package/pg-core/dialect.js +18 -23
- package/pg-core/dialect.js.map +1 -1
- package/pg-core/query-builders/delete.cjs +2 -2
- package/pg-core/query-builders/delete.cjs.map +1 -1
- package/pg-core/query-builders/delete.d.cts +1 -3
- package/pg-core/query-builders/delete.d.ts +1 -3
- package/pg-core/query-builders/delete.js +2 -2
- package/pg-core/query-builders/delete.js.map +1 -1
- package/pg-core/query-builders/insert.cjs +4 -5
- package/pg-core/query-builders/insert.cjs.map +1 -1
- package/pg-core/query-builders/insert.d.cts +2 -5
- package/pg-core/query-builders/insert.d.ts +2 -5
- package/pg-core/query-builders/insert.js +4 -5
- package/pg-core/query-builders/insert.js.map +1 -1
- package/pg-core/query-builders/update.cjs +4 -6
- package/pg-core/query-builders/update.cjs.map +1 -1
- package/pg-core/query-builders/update.d.cts +2 -5
- package/pg-core/query-builders/update.d.ts +2 -5
- package/pg-core/query-builders/update.js +4 -6
- package/pg-core/query-builders/update.js.map +1 -1
- package/sqlite-core/db.cjs +1 -10
- package/sqlite-core/db.cjs.map +1 -1
- package/sqlite-core/db.d.cts +0 -3
- package/sqlite-core/db.d.ts +0 -3
- package/sqlite-core/db.js +1 -10
- package/sqlite-core/db.js.map +1 -1
- package/sqlite-core/dialect.cjs +18 -23
- package/sqlite-core/dialect.cjs.map +1 -1
- package/sqlite-core/dialect.d.cts +3 -4
- package/sqlite-core/dialect.d.ts +3 -4
- package/sqlite-core/dialect.js +18 -23
- package/sqlite-core/dialect.js.map +1 -1
- package/sqlite-core/query-builders/delete.cjs +2 -2
- package/sqlite-core/query-builders/delete.cjs.map +1 -1
- package/sqlite-core/query-builders/delete.d.cts +1 -3
- package/sqlite-core/query-builders/delete.d.ts +1 -3
- package/sqlite-core/query-builders/delete.js +2 -2
- package/sqlite-core/query-builders/delete.js.map +1 -1
- package/sqlite-core/query-builders/insert.cjs +4 -5
- package/sqlite-core/query-builders/insert.cjs.map +1 -1
- package/sqlite-core/query-builders/insert.d.cts +2 -5
- package/sqlite-core/query-builders/insert.d.ts +2 -5
- package/sqlite-core/query-builders/insert.js +4 -5
- package/sqlite-core/query-builders/insert.js.map +1 -1
- package/sqlite-core/query-builders/update.cjs +4 -5
- package/sqlite-core/query-builders/update.cjs.map +1 -1
- package/sqlite-core/query-builders/update.d.cts +2 -5
- package/sqlite-core/query-builders/update.d.ts +2 -5
- package/sqlite-core/query-builders/update.js +4 -5
- package/sqlite-core/query-builders/update.js.map +1 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
package/README.md
CHANGED
|
@@ -1,156 +1,44 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<
|
|
3
|
-
<img
|
|
4
|
-
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/min/drizzle-orm">
|
|
5
|
-
<a href="https://discord.gg/yfjTbVXMW4" target="_blank"><img alt="Discord" src="https://img.shields.io/discord/1043890932593987624?label=Discord"></a>
|
|
6
|
-
<img alt="License" src="https://img.shields.io/npm/l/drizzle-orm">
|
|
7
|
-
<h6><i>If you know SQL, you know Drizzle ORM</i></h6>
|
|
8
|
-
<hr />
|
|
2
|
+
<img src="./misc/readme/logo-github-sq-dark.svg#gh-dark-mode-only" />
|
|
3
|
+
<img src="./misc/readme/logo-github-sq-light.svg#gh-light-mode-only" />
|
|
9
4
|
</div>
|
|
10
5
|
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
<br/>
|
|
7
|
+
<div align="center">
|
|
8
|
+
<h3>Headless ORM for NodeJS, TypeScript and JavaScript 🚀</h3>
|
|
9
|
+
<a href="https://orm.drizzle.team">Website</a> •
|
|
10
|
+
<a href="https://orm.drizzle.team/docs/overview">Documentation</a> •
|
|
11
|
+
<a href="https://x.com/drizzleorm">Twitter</a> •
|
|
12
|
+
<a href="https://driz.link/discord">Discord</a>
|
|
13
|
+
</div>
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
<br/>
|
|
16
|
+
<br/>
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
### What's Drizzle?
|
|
19
|
+
Drizzle is a modern TypeScript ORM developers [wanna use in their next project](https://stateofdb.com/tools/drizzle).
|
|
20
|
+
It is [lightweight](https://bundlephobia.com/package/drizzle-orm) at only ~7.4kb minified+gzipped, it's tree shakeable with exactly 0 dependencies.
|
|
17
21
|
|
|
18
|
-
-
|
|
19
|
-
- [Smart automated migrations generation](https://github.com/drizzle-team/drizzle-kit-mirror)
|
|
20
|
-
- No ORM learning curve
|
|
21
|
-
- SQL-like syntax for table definitions and queries
|
|
22
|
-
- Best in class fully typed joins
|
|
23
|
-
- Fully typed partial and non-partial selects of any complexity
|
|
24
|
-
- Auto-inferring of TS types for DB models for selections and insertions separately
|
|
25
|
-
- [Zod schema generation](/drizzle-zod/README.md)
|
|
26
|
-
- Zero dependencies
|
|
22
|
+
**Drizzle supports every PostgreSQL, MySQL and SQLite databases**, including serverless ones like [Turso](https://orm.drizzle.team/docs/get-started-sqlite#turso), [Neon](https://orm.drizzle.team/docs/get-started-postgresql#neon), [Xata](xata.io), [PlanetScale](https://orm.drizzle.team/docs/get-started-mysql#planetscale), [Cloudflare D1](https://orm.drizzle.team/docs/get-started-sqlite#cloudflare-d1), [FlyIO LiteFS](https://fly.io/docs/litefs/), [Vercel Postgres](https://orm.drizzle.team/docs/get-started-postgresql#vercel-postgres), [Supabase](https://orm.drizzle.team/docs/get-started-postgresql#supabase) and [AWS Data API](https://orm.drizzle.team/docs/get-started-postgresql#aws-data-api). No bells and whistles, no rust binaries, no serverless adapters, everything just works out of the box.
|
|
27
23
|
|
|
28
|
-
|
|
24
|
+
**Drizzle is serverless-ready by design**, it works in every major JavaScript runtime like NodeJS, Bun, Deno, Cloudflare Workers, Supabase functions, any Edge runtime and even in Browsers.
|
|
25
|
+
With Drizzle you can be [**fast out of the box**](https://orm.drizzle.team/benchmarks), save time and costs while never introducing any data proxies into your infrastructure.
|
|
29
26
|
|
|
30
|
-
|
|
27
|
+
While you can use Drizzle as a JavaScript library, it shines in the TypeScript. It lets you [**declare SQL schema**](https://orm.drizzle.team/docs/sql-schema-declaration) and build both [**relational**](https://orm.drizzle.team/docs/rqb) and [**SQL-like queries**](https://orm.drizzle.team/docs/select), while keeping the balance between type-safety and extensibility for toolmakers to build on top.
|
|
31
28
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
| Cloudflare D1 | ✅ | [Docs](https://driz.link/docs-d1) | [Website](https://developers.cloudflare.com/d1) |
|
|
39
|
-
| libSQL | ✅ | [Docs](/examples/libsql/README.md) | [Website](https://libsql.org) |
|
|
40
|
-
| Turso | ✅ | [Docs](https://driz.link/docs-turso) | [Website](https://turso.tech) |
|
|
41
|
-
| PlanetScale | ✅ | [Docs](https://driz.link/docs-planetscale) | [Website](https://planetscale.com/) |
|
|
42
|
-
| Neon | ✅ | [Docs](https://driz.link/docs-neon) | [Website](https://neon.tech/) |
|
|
43
|
-
| Vercel Postgres | ✅ | [Docs](https://driz.link/docs-vercel-postgres) | [Website](https://vercel.com/docs/storage/vercel-postgres/quickstart) |
|
|
44
|
-
| Supabase | ✅ | [Docs](https://driz.link/docs-supabase) | [Website](https://supabase.com) |
|
|
45
|
-
| DynamoDB | ⏳ | | |
|
|
46
|
-
| MS SQL | ⏳ | | |
|
|
47
|
-
| CockroachDB | ⏳ | | |
|
|
29
|
+
### Ecosystem
|
|
30
|
+
While Drizzle ORM remains a thin typed layer on top of SQL, we made a set of tools for people to have best possible developer experience.
|
|
31
|
+
|
|
32
|
+
Drizzle comes with a powerful [**Drizzle Kit**](https://orm.drizzle.team/kit-docs/overview) CLI companion for you to have hasstle-free migrations. It can generate SQL migration files for you or apply schema changes directly to the database.
|
|
33
|
+
|
|
34
|
+
And we have a [**Drizzle Studio**](https://orm.drizzle.team/drizzle-studio/overview) for you to effortlessly browse and manipulate data in your database of choice.
|
|
48
35
|
|
|
49
|
-
|
|
36
|
+
### Documentation
|
|
37
|
+
Check out the full documentation on [the website](https://orm.drizzle.team/docs/overview)
|
|
50
38
|
|
|
39
|
+
### Our sponsors ❤️
|
|
51
40
|
<p align="center">
|
|
52
41
|
<a href="https://drizzle.team" target="_blank">
|
|
53
42
|
<img src='https://api.drizzle.team/github/sponsors/svg'/>
|
|
54
43
|
</a>
|
|
55
44
|
</p>
|
|
56
|
-
|
|
57
|
-
## Installation
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
npm install drizzle-orm
|
|
61
|
-
npm install -D drizzle-kit
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
## Feature showcase (PostgreSQL)
|
|
65
|
-
|
|
66
|
-
> **Note**: don't forget to install `pg` and `@types/pg` packages for this example to work.
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
70
|
-
import { integer, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core';
|
|
71
|
-
import { InferModel, eq, sql } from 'drizzle-orm';
|
|
72
|
-
import { Pool } from 'pg';
|
|
73
|
-
|
|
74
|
-
export const users = pgTable('users', {
|
|
75
|
-
id: serial('id').primaryKey(),
|
|
76
|
-
fullName: text('full_name').notNull(),
|
|
77
|
-
phone: varchar('phone', { length: 20 }).notNull(),
|
|
78
|
-
role: text('role', { enum: ['user', 'admin'] }).default('user').notNull(),
|
|
79
|
-
cityId: integer('city_id').references(() => cities.id),
|
|
80
|
-
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
81
|
-
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
export type User = InferModel<typeof users>;
|
|
85
|
-
export type NewUser = InferModel<typeof users, 'insert'>;
|
|
86
|
-
|
|
87
|
-
export const cities = pgTable('cities', {
|
|
88
|
-
id: serial('id').primaryKey(),
|
|
89
|
-
name: text('name').notNull(),
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
export type City = InferModel<typeof cities>;
|
|
93
|
-
export type NewCity = InferModel<typeof cities, 'insert'>;
|
|
94
|
-
|
|
95
|
-
const pool = new Pool({
|
|
96
|
-
connectionString: 'postgres://user:password@host:port/db',
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
const db = drizzle(pool);
|
|
100
|
-
|
|
101
|
-
// Insert
|
|
102
|
-
const newUser: NewUser = {
|
|
103
|
-
fullName: 'John Doe',
|
|
104
|
-
phone: '+123456789',
|
|
105
|
-
};
|
|
106
|
-
const insertedUsers /* : User[] */ = await db.insert(users).values(newUser).returning();
|
|
107
|
-
const insertedUser = insertedUsers[0]!;
|
|
108
|
-
|
|
109
|
-
const newCity: NewCity = {
|
|
110
|
-
name: 'New York',
|
|
111
|
-
};
|
|
112
|
-
const insertedCities /* : City[] */ = await db.insert(cities).values(newCity).returning();
|
|
113
|
-
const insertedCity = insertedCities[0]!;
|
|
114
|
-
|
|
115
|
-
// Update
|
|
116
|
-
const updateResult /* : { updated: Date }[] */ = await db.update(users)
|
|
117
|
-
.set({ cityId: insertedCity.id, updatedAt: new Date() })
|
|
118
|
-
.where(eq(users.id, insertedUser.id))
|
|
119
|
-
.returning({ updated: users.updatedAt });
|
|
120
|
-
|
|
121
|
-
// Select
|
|
122
|
-
const allUsers /* : User[] */ = await db.select().from(users);
|
|
123
|
-
|
|
124
|
-
// Select custom fields
|
|
125
|
-
const upperCaseNames /* : { id: number; name: string }[] */ = await db
|
|
126
|
-
.select({
|
|
127
|
-
id: users.id,
|
|
128
|
-
name: sql<string>`upper(${users.fullName})`,
|
|
129
|
-
})
|
|
130
|
-
.from(users);
|
|
131
|
-
|
|
132
|
-
// Joins
|
|
133
|
-
// You wouldn't BELIEVE how SMART the result type is! 😱
|
|
134
|
-
const allUsersWithCities = await db
|
|
135
|
-
.select({
|
|
136
|
-
id: users.id,
|
|
137
|
-
name: users.fullName,
|
|
138
|
-
city: {
|
|
139
|
-
id: cities.id,
|
|
140
|
-
name: cities.name,
|
|
141
|
-
},
|
|
142
|
-
})
|
|
143
|
-
.from(users)
|
|
144
|
-
.leftJoin(cities, eq(users.cityId, cities.id));
|
|
145
|
-
|
|
146
|
-
// Delete
|
|
147
|
-
const deletedNames /* : { name: string }[] */ = await db.delete(users)
|
|
148
|
-
.where(eq(users.id, insertedUser.id))
|
|
149
|
-
.returning({ name: users.fullName });
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
**See full docs for further reference:**
|
|
153
|
-
|
|
154
|
-
- [PostgreSQL](./drizzle-orm/src/pg-core/README.md)
|
|
155
|
-
- [MySQL](./drizzle-orm/src/mysql-core/README.md)
|
|
156
|
-
- [SQLite](./drizzle-orm/src/sqlite-core/README.md)
|
package/mysql-core/db.cjs
CHANGED
|
@@ -134,13 +134,7 @@ class MySqlDatabase {
|
|
|
134
134
|
distinct: true
|
|
135
135
|
});
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
return new import_query_builders.MySqlUpdateBuilder(table, self.session, self.dialect, queries);
|
|
139
|
-
}
|
|
140
|
-
function delete_(table) {
|
|
141
|
-
return new import_query_builders.MySqlDeleteBase(table, self.session, self.dialect, queries);
|
|
142
|
-
}
|
|
143
|
-
return { select, selectDistinct, update, delete: delete_ };
|
|
137
|
+
return { select, selectDistinct };
|
|
144
138
|
}
|
|
145
139
|
select(fields) {
|
|
146
140
|
return new import_query_builders.MySqlSelectBuilder({ fields: fields ?? void 0, session: this.session, dialect: this.dialect });
|
package/mysql-core/db.cjs.map
CHANGED
|
@@ -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 { 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\t/**\n\t\t * Creates a select query.\n\t\t * \n\t\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\t * \n\t\t * Use `.from()` method to specify which table to select from.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t * \n\t\t * @param fields The selection object.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t * \n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({ \n\t\t * id: cars.id, \n\t\t * brand: cars.brand \n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t * \n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t * \n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\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\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t * \n\t\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\t * \n\t\t * Use `.from()` method to specify which table to select from.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t * \n\t\t * @param fields The selection object.\n\t\t * \n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t * \n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\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\t/**\n\t\t * Creates an update query.\n\t\t * \n\t\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\t * \n\t\t * Use `.set()` method to specify which values to update.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/update} \n\t\t * \n\t\t * @param table The table to update.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t * \n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\t\treturn new MySqlUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t * \n\t\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\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t * \n\t\t * @param table The table to delete from.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t * \n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends MySqlTable>(table: TTable): MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\t\treturn new MySqlDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, update, delete: delete_ };\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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MAAM,KAAK,OAAO,IAAI,SAAS,MAAM,CAAC,MACnE;AAC1B,QAAM,SAAsB,IAAI,SAAa,WAAW,QAAQ,EAAE,OAAO,GAAG,IAAI;AAChF,QAAM,iBAAsC,IAAI,SAAa,WAAW,QAAQ,EAAE,eAAe,GAAG,IAAI;AACxG,QAAM,QAAmB,IAAI,SAAa,WAAW,QAAQ,EAAE,KAAK,GAAG,IAAI;AAE3E,QAAM,SAAsB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACtE,QAAM,SAAsB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACtE,QAAM,UAAuB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACvE,QAAM,UAAwB,IAAI,SAAgB,QAAQ,QAAQ,GAAG,IAAI;AACzE,QAAM,cAAgC,IAAI,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":[]}
|
|
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,E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,UACA,aAAmC,MAAM,SAAS,KAAK,MAAM,KAAK,OAAO,IAAI,SAAS,MAAM,CAAC,MACnE;AAC1B,QAAM,SAAsB,IAAI,SAAa,WAAW,QAAQ,EAAE,OAAO,GAAG,IAAI;AAChF,QAAM,iBAAsC,IAAI,SAAa,WAAW,QAAQ,EAAE,eAAe,GAAG,IAAI;AACxG,QAAM,QAAmB,IAAI,SAAa,WAAW,QAAQ,EAAE,KAAK,GAAG,IAAI;AAE3E,QAAM,SAAsB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACtE,QAAM,SAAsB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACtE,QAAM,UAAuB,IAAI,SAAgB,QAAQ,OAAO,GAAG,IAAI;AACvE,QAAM,UAAwB,IAAI,SAAgB,QAAQ,QAAQ,GAAG,IAAI;AACzE,QAAM,cAAgC,IAAI,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":[]}
|
package/mysql-core/db.d.cts
CHANGED
|
@@ -90,8 +90,6 @@ export declare class MySqlDatabase<TQueryResult extends QueryResultHKT, TPrepare
|
|
|
90
90
|
(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;
|
|
91
91
|
<TSelection_1 extends SelectedFields>(fields: TSelection_1): MySqlSelectBuilder<TSelection_1, TPreparedQueryHKT, "db">;
|
|
92
92
|
};
|
|
93
|
-
update: <TTable extends MySqlTable<import("./table.ts").TableConfig>>(table: TTable) => MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT>;
|
|
94
|
-
delete: <TTable_1 extends MySqlTable<import("./table.ts").TableConfig>>(table: TTable_1) => MySqlDeleteBase<TTable_1, TQueryResult, TPreparedQueryHKT, false, never>;
|
|
95
93
|
};
|
|
96
94
|
/**
|
|
97
95
|
* Creates a select query.
|
package/mysql-core/db.d.ts
CHANGED
|
@@ -90,8 +90,6 @@ export declare class MySqlDatabase<TQueryResult extends QueryResultHKT, TPrepare
|
|
|
90
90
|
(): MySqlSelectBuilder<undefined, TPreparedQueryHKT>;
|
|
91
91
|
<TSelection_1 extends SelectedFields>(fields: TSelection_1): MySqlSelectBuilder<TSelection_1, TPreparedQueryHKT, "db">;
|
|
92
92
|
};
|
|
93
|
-
update: <TTable extends MySqlTable<import("./table.js").TableConfig>>(table: TTable) => MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT>;
|
|
94
|
-
delete: <TTable_1 extends MySqlTable<import("./table.js").TableConfig>>(table: TTable_1) => MySqlDeleteBase<TTable_1, TQueryResult, TPreparedQueryHKT, false, never>;
|
|
95
93
|
};
|
|
96
94
|
/**
|
|
97
95
|
* Creates a select query.
|
package/mysql-core/db.js
CHANGED
|
@@ -116,13 +116,7 @@ class MySqlDatabase {
|
|
|
116
116
|
distinct: true
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
|
-
|
|
120
|
-
return new MySqlUpdateBuilder(table, self.session, self.dialect, queries);
|
|
121
|
-
}
|
|
122
|
-
function delete_(table) {
|
|
123
|
-
return new MySqlDeleteBase(table, self.session, self.dialect, queries);
|
|
124
|
-
}
|
|
125
|
-
return { select, selectDistinct, update, delete: delete_ };
|
|
119
|
+
return { select, selectDistinct };
|
|
126
120
|
}
|
|
127
121
|
select(fields) {
|
|
128
122
|
return new MySqlSelectBuilder({ fields: fields ?? void 0, session: this.session, dialect: this.dialect });
|
package/mysql-core/db.js.map
CHANGED
|
@@ -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 { 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\t/**\n\t\t * Creates a select query.\n\t\t * \n\t\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\t * \n\t\t * Use `.from()` method to specify which table to select from.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t * \n\t\t * @param fields The selection object.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t * \n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({ \n\t\t * id: cars.id, \n\t\t * brand: cars.brand \n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t * \n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t * \n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\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\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t * \n\t\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\t * \n\t\t * Use `.from()` method to specify which table to select from.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t * \n\t\t * @param fields The selection object.\n\t\t * \n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t * \n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\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\t/**\n\t\t * Creates an update query.\n\t\t * \n\t\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\t * \n\t\t * Use `.set()` method to specify which values to update.\n\t\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/update} \n\t\t * \n\t\t * @param table The table to update.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t * \n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\t\treturn new MySqlUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t * \n\t\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\t * \n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t * \n\t\t * @param table The table to delete from.\n\t\t * \n\t\t * @example\n\t\t * \n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t * \n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends MySqlTable>(table: TTable): MySqlDeleteBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\t\treturn new MySqlDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, update, delete: delete_ };\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,Q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ql2/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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HANGED
|
@@ -71,24 +71,10 @@ class MySqlDialect {
|
|
|
71
71
|
escapeString(str) {
|
|
72
72
|
return `'${str.replace(/'/g, "''")}'`;
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
if (!queries?.length)
|
|
76
|
-
return void 0;
|
|
77
|
-
const withSqlChunks = [import_sql.sql`with `];
|
|
78
|
-
for (const [i, w] of queries.entries()) {
|
|
79
|
-
withSqlChunks.push(import_sql.sql`${import_sql.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
80
|
-
if (i < queries.length - 1) {
|
|
81
|
-
withSqlChunks.push(import_sql.sql`, `);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
withSqlChunks.push(import_sql.sql` `);
|
|
85
|
-
return import_sql.sql.join(withSqlChunks);
|
|
86
|
-
}
|
|
87
|
-
buildDeleteQuery({ table, where, returning, withList }) {
|
|
88
|
-
const withSql = this.buildWithCTE(withList);
|
|
74
|
+
buildDeleteQuery({ table, where, returning }) {
|
|
89
75
|
const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
90
76
|
const whereSql = where ? import_sql.sql` where ${where}` : void 0;
|
|
91
|
-
return import_sql.sql
|
|
77
|
+
return import_sql.sql`delete from ${table}${whereSql}${returningSql}`;
|
|
92
78
|
}
|
|
93
79
|
buildUpdateSet(table, set) {
|
|
94
80
|
const setEntries = Object.entries(set);
|
|
@@ -104,12 +90,11 @@ class MySqlDialect {
|
|
|
104
90
|
})
|
|
105
91
|
);
|
|
106
92
|
}
|
|
107
|
-
buildUpdateQuery({ table, set, where, returning
|
|
108
|
-
const withSql = this.buildWithCTE(withList);
|
|
93
|
+
buildUpdateQuery({ table, set, where, returning }) {
|
|
109
94
|
const setSql = this.buildUpdateSet(table, set);
|
|
110
95
|
const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
111
96
|
const whereSql = where ? import_sql.sql` where ${where}` : void 0;
|
|
112
|
-
return import_sql.sql
|
|
97
|
+
return import_sql.sql`update ${table} set ${setSql}${whereSql}${returningSql}`;
|
|
113
98
|
}
|
|
114
99
|
/**
|
|
115
100
|
* Builds selection SQL with provided fields/expressions
|
|
@@ -189,7 +174,18 @@ class MySqlDialect {
|
|
|
189
174
|
}
|
|
190
175
|
}
|
|
191
176
|
const isSingleTable = !joins || joins.length === 0;
|
|
192
|
-
|
|
177
|
+
let withSql;
|
|
178
|
+
if (withList?.length) {
|
|
179
|
+
const withSqlChunks = [import_sql.sql`with `];
|
|
180
|
+
for (const [i, w] of withList.entries()) {
|
|
181
|
+
withSqlChunks.push(import_sql.sql`${import_sql.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
182
|
+
if (i < withList.length - 1) {
|
|
183
|
+
withSqlChunks.push(import_sql.sql`, `);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
withSqlChunks.push(import_sql.sql` `);
|
|
187
|
+
withSql = import_sql.sql.join(withSqlChunks);
|
|
188
|
+
}
|
|
193
189
|
const distinctSql = distinct ? import_sql.sql` distinct` : void 0;
|
|
194
190
|
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
195
191
|
const tableSql = (() => {
|