Home / Class/ MySqlInsertBase Class — drizzle-orm Architecture

MySqlInsertBase Class — drizzle-orm Architecture

Architecture documentation for the MySqlInsertBase class in insert.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  d9829954_9fca_878f_0f03_c2600b4170e4["MySqlInsertBase"]
  a270d70d_1a42_692c_4fcf_09f69761d58e["insert.ts"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|defined in| a270d70d_1a42_692c_4fcf_09f69761d58e
  3a192f92_5a02_c797_e023_2f87d09c454d["constructor()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| 3a192f92_5a02_c797_e023_2f87d09c454d
  3cbaac64_3373_43fa_7259_13ef251e2421["onDuplicateKeyUpdate()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| 3cbaac64_3373_43fa_7259_13ef251e2421
  f8fa7333_b64d_b924_e736_6cbea306068f["$returningId()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| f8fa7333_b64d_b924_e736_6cbea306068f
  085b2a93_f3a2_eeba_1669_4c1c582c5d21["getSQL()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| 085b2a93_f3a2_eeba_1669_4c1c582c5d21
  ad9814cf_d44f_1168_824d_dbe34354c4d4["toSQL()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| ad9814cf_d44f_1168_824d_dbe34354c4d4
  cf21d777_f895_3344_58c7_86299891f99e["prepare()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| cf21d777_f895_3344_58c7_86299891f99e
  86685abe_7132_5f4a_555c_1ba8bb5367d7["$dynamic()"]
  d9829954_9fca_878f_0f03_c2600b4170e4 -->|method| 86685abe_7132_5f4a_555c_1ba8bb5367d7

Relationship Graph

Source Code

drizzle-orm/src/mysql-core/query-builders/insert.ts lines 212–338

export class MySqlInsertBase<
	TTable extends MySqlTable,
	TQueryResult extends MySqlQueryResultHKT,
	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	TPreparedQueryHKT extends PreparedQueryHKTBase,
	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	TReturning extends Record<string, unknown> | undefined = undefined,
	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	TDynamic extends boolean = false,
	// eslint-disable-next-line @typescript-eslint/no-unused-vars
	TExcludedMethods extends string = never,
> extends QueryPromise<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[]>
	implements
		RunnableQuery<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[], 'mysql'>,
		SQLWrapper
{
	static override readonly [entityKind]: string = 'MySqlInsert';

	declare protected $table: TTable;

	private config: MySqlInsertConfig<TTable>;
	protected cacheConfig?: WithCacheConfig;

	constructor(
		table: TTable,
		values: MySqlInsertConfig['values'],
		ignore: boolean,
		private session: MySqlSession,
		private dialect: MySqlDialect,
		select?: boolean,
	) {
		super();
		this.config = { table, values: values as any, select, ignore };
	}

	/**
	 * Adds an `on duplicate key update` clause to the query.
	 *
	 * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
	 *
	 * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
	 *
	 * @param config The `set` clause
	 *
	 * @example
	 * ```ts
	 * await db.insert(cars)
	 *   .values({ id: 1, brand: 'BMW'})
	 *   .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
	 * ```
	 *
	 * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
	 *
	 * ```ts
	 * import { sql } from 'drizzle-orm';
	 *
	 * await db.insert(cars)
	 *   .values({ id: 1, brand: 'BMW' })
	 *   .onDuplicateKeyUpdate({ set: { id: sql`id` } });
	 * ```
	 */
	onDuplicateKeyUpdate(
		config: MySqlInsertOnDuplicateKeyUpdateConfig<this>,
	): MySqlInsertWithout<this, TDynamic, 'onDuplicateKeyUpdate'> {
		const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
		this.config.onConflict = sql`update ${setSql}`;
		return this as any;
	}

	$returningId(): MySqlInsertWithout<
		MySqlInsertReturning<this, TDynamic>,
		TDynamic,
		'$returningId'
	> {
		const returning: SelectedFieldsOrdered = [];
		for (const [key, value] of Object.entries(this.config.table[Table.Symbol.Columns])) {
			if (value.primary) {
				returning.push({ field: value, path: [key] });
			}
		}
		this.config.returning = returning;

Domain

Frequently Asked Questions

What is the MySqlInsertBase class?
MySqlInsertBase is a class in the drizzle-orm codebase, defined in drizzle-orm/src/mysql-core/query-builders/insert.ts.
Where is MySqlInsertBase defined?
MySqlInsertBase is defined in drizzle-orm/src/mysql-core/query-builders/insert.ts at line 212.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free