MySqlDatabase Class — drizzle-orm Architecture
Architecture documentation for the MySqlDatabase class in db.ts from the drizzle-orm codebase.
Entity Profile
Dependency Diagram
graph TD 10fbad18_129f_69a2_7ce3_79ea63dcf5d7["MySqlDatabase"] 55a32696_6538_e52d_9dd2_18695934fe32["db.ts"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|defined in| 55a32696_6538_e52d_9dd2_18695934fe32 5baa28d9_1767_f500_f6a1_4fc11896ce6a["constructor()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 5baa28d9_1767_f500_f6a1_4fc11896ce6a 000b48d8_e365_427b_6fb5_c012ef49681f["$count()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 000b48d8_e365_427b_6fb5_c012ef49681f 511441a5_4f3e_873f_b0b5_120a4a7fe793["with()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 511441a5_4f3e_873f_b0b5_120a4a7fe793 d9537013_dc8d_a9d5_6b54_e1724abbdd2f["select()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| d9537013_dc8d_a9d5_6b54_e1724abbdd2f 1a12f96f_7b6a_911e_9e6f_4669ecb64b4f["selectDistinct()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 1a12f96f_7b6a_911e_9e6f_4669ecb64b4f 7d0caa08_9013_3d08_d559_97226a65da99["update()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 7d0caa08_9013_3d08_d559_97226a65da99 62c45664_eb64_3a1d_8f7d_f6902281c37f["insert()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 62c45664_eb64_3a1d_8f7d_f6902281c37f 638ae7c8_a4ce_62e5_4a9e_69da696090de["delete()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 638ae7c8_a4ce_62e5_4a9e_69da696090de bb5b0e59_e8b3_8d1c_fe75_9eed0b8f371b["execute()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| bb5b0e59_e8b3_8d1c_fe75_9eed0b8f371b 80122acc_0876_1ed2_a42b_578f382b11a7["transaction()"] 10fbad18_129f_69a2_7ce3_79ea63dcf5d7 -->|method| 80122acc_0876_1ed2_a42b_578f382b11a7
Relationship Graph
Source Code
drizzle-orm/src/mysql-core/db.ts lines 34–489
export class MySqlDatabase<
TQueryResult extends MySqlQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase,
TFullSchema extends Record<string, unknown> = {},
TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,
> {
static readonly [entityKind]: string = 'MySqlDatabase';
declare readonly _: {
readonly schema: TSchema | undefined;
readonly fullSchema: TFullSchema;
readonly tableNamesMap: Record<string, string>;
};
query: TFullSchema extends Record<string, never>
? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
: {
[K in keyof TSchema]: RelationalQueryBuilder<TPreparedQueryHKT, TSchema, TSchema[K]>;
};
constructor(
/** @internal */
readonly dialect: MySqlDialect,
/** @internal */
readonly session: MySqlSession<any, any, any, any>,
schema: RelationalSchemaConfig<TSchema> | undefined,
protected readonly mode: Mode,
) {
this._ = schema
? {
schema: schema.schema,
fullSchema: schema.fullSchema as TFullSchema,
tableNamesMap: schema.tableNamesMap,
}
: {
schema: undefined,
fullSchema: {} as TFullSchema,
tableNamesMap: {},
};
this.query = {} as typeof this['query'];
if (this._.schema) {
for (const [tableName, columns] of Object.entries(this._.schema)) {
(this.query as MySqlDatabase<TQueryResult, TPreparedQueryHKT, Record<string, any>>['query'])[tableName] =
new RelationalQueryBuilder(
schema!.fullSchema,
this._.schema,
this._.tableNamesMap,
schema!.fullSchema[tableName] as MySqlTable,
columns,
dialect,
session,
this.mode,
);
}
}
this.$cache = { invalidate: async (_params: any) => {} };
}
/**
* Creates a subquery that defines a temporary named result set as a CTE.
*
* It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
*
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
*
* @param alias The alias for the subquery.
*
* Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
*
* @example
*
* ```ts
* // Create a subquery with alias 'sq' and use it in the select query
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
*
* const result = await db.with(sq).select().from(sq);
* ```
*
* 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:
*
* ```ts
Domain
Defined In
Source
Frequently Asked Questions
What is the MySqlDatabase class?
MySqlDatabase is a class in the drizzle-orm codebase, defined in drizzle-orm/src/mysql-core/db.ts.
Where is MySqlDatabase defined?
MySqlDatabase is defined in drizzle-orm/src/mysql-core/db.ts at line 34.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free