Home / Class/ SQLiteInsertBuilder Class — drizzle-orm Architecture

SQLiteInsertBuilder Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  9a1a6def_0a41_ac90_3178_42081723bdb6["SQLiteInsertBuilder"]
  84987318_b83b_9a2c_d793_542fa0b46175["insert.ts"]
  9a1a6def_0a41_ac90_3178_42081723bdb6 -->|defined in| 84987318_b83b_9a2c_d793_542fa0b46175
  0ac1469c_f144_9cdb_5993_05fc00928732["constructor()"]
  9a1a6def_0a41_ac90_3178_42081723bdb6 -->|method| 0ac1469c_f144_9cdb_5993_05fc00928732
  b6a6eac5_5713_aa86_0946_39c5c164dc08["values()"]
  9a1a6def_0a41_ac90_3178_42081723bdb6 -->|method| b6a6eac5_5713_aa86_0946_39c5c164dc08
  21bd51fb_d3aa_b5a4_db7f_d81e1bf46b78["select()"]
  9a1a6def_0a41_ac90_3178_42081723bdb6 -->|method| 21bd51fb_d3aa_b5a4_db7f_d81e1bf46b78

Relationship Graph

Source Code

drizzle-orm/src/sqlite-core/query-builders/insert.ts lines 40–107

export class SQLiteInsertBuilder<
	TTable extends SQLiteTable,
	TResultType extends 'sync' | 'async',
	TRunResult,
> {
	static readonly [entityKind]: string = 'SQLiteInsertBuilder';

	constructor(
		protected table: TTable,
		protected session: SQLiteSession<any, any, any, any>,
		protected dialect: SQLiteDialect,
		private withList?: Subquery[],
	) {}

	values(value: SQLiteInsertValue<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	values(values: SQLiteInsertValue<TTable>[]): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	values(
		values: SQLiteInsertValue<TTable> | SQLiteInsertValue<TTable>[],
	): SQLiteInsertBase<TTable, TResultType, TRunResult> {
		values = Array.isArray(values) ? values : [values];
		if (values.length === 0) {
			throw new Error('values() must be called with at least one value');
		}
		const mappedValues = values.map((entry) => {
			const result: Record<string, Param | SQL> = {};
			const cols = this.table[Table.Symbol.Columns];
			for (const colKey of Object.keys(entry)) {
				const colValue = entry[colKey as keyof typeof entry];
				result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
			}
			return result;
		});

		// if (mappedValues.length > 1 && mappedValues.some((t) => Object.keys(t).length === 0)) {
		// 	throw new Error(
		// 		`One of the values you want to insert is empty. In SQLite you can insert only one empty object per statement. For this case Drizzle with use "INSERT INTO ... DEFAULT VALUES" syntax`,
		// 	);
		// }

		return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
	}

	select(
		selectQuery: (qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable>,
	): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	select(selectQuery: (qb: QueryBuilder) => SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	select(selectQuery: SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	select(selectQuery: SQLiteInsertSelectQueryBuilder<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;
	select(
		selectQuery:
			| SQL
			| SQLiteInsertSelectQueryBuilder<TTable>
			| ((qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable> | SQL),
	): SQLiteInsertBase<TTable, TResultType, TRunResult> {
		const select = typeof selectQuery === 'function' ? selectQuery(new QueryBuilder()) : selectQuery;

		if (
			!is(select, SQL)
			&& !haveSameKeys(this.table[Columns], select._.selectedFields)
		) {
			throw new Error(
				'Insert select error: selected fields are not the same or are in a different order compared to the table definition',
			);
		}

		return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
	}
}

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free