Home / Function/ buildInsertQuery() — drizzle-orm Function Reference

buildInsertQuery() — drizzle-orm Function Reference

Architecture documentation for the buildInsertQuery() function in dialect.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  0396b741_ac98_c02e_ce6b_3b28587ddc46["buildInsertQuery()"]
  f80973ef_149c_bd36_66ee_1e7e9024a9d5["GelDialect"]
  0396b741_ac98_c02e_ce6b_3b28587ddc46 -->|defined in| f80973ef_149c_bd36_66ee_1e7e9024a9d5
  508f5cc1_d856_0ba4_7831_119c8e91ea8f["buildWithCTE()"]
  0396b741_ac98_c02e_ce6b_3b28587ddc46 -->|calls| 508f5cc1_d856_0ba4_7831_119c8e91ea8f
  e2fb541e_5197_4994_9f97_bb47abf659ff["buildSelection()"]
  0396b741_ac98_c02e_ce6b_3b28587ddc46 -->|calls| e2fb541e_5197_4994_9f97_bb47abf659ff
  style 0396b741_ac98_c02e_ce6b_3b28587ddc46 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/gel-core/dialect.ts lines 508–575

	buildInsertQuery(
		{ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }: GelInsertConfig,
	): SQL {
		const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
		const columns: Record<string, GelColumn> = table[Table.Symbol.Columns];

		const colEntries: [string, GelColumn][] = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());

		const insertOrder = colEntries.map(
			([, column]) => sql.identifier(this.casing.getColumnCasing(column)),
		);

		if (select) {
			const select = valuesOrSelect as AnyGelSelectQueryBuilder | SQL;

			if (is(select, SQL)) {
				valuesSqlList.push(select);
			} else {
				valuesSqlList.push(select.getSQL());
			}
		} else {
			const values = valuesOrSelect as Record<string, Param | SQL>[];
			valuesSqlList.push(sql.raw('values '));

			for (const [valueIndex, value] of values.entries()) {
				const valueList: (SQLChunk | SQL)[] = [];
				for (const [fieldName, col] of colEntries) {
					const colValue = value[fieldName];
					if (colValue === undefined || (is(colValue, Param) && colValue.value === undefined)) {
						// eslint-disable-next-line unicorn/no-negated-condition
						if (col.defaultFn !== undefined) {
							const defaultFnResult = col.defaultFn();
							const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
							valueList.push(defaultValue);
							// eslint-disable-next-line unicorn/no-negated-condition
						} else if (!col.default && col.onUpdateFn !== undefined) {
							const onUpdateFnResult = col.onUpdateFn();
							const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
							valueList.push(newValue);
						} else {
							valueList.push(sql`default`);
						}
					} else {
						valueList.push(colValue);
					}
				}

				valuesSqlList.push(valueList);
				if (valueIndex < values.length - 1) {
					valuesSqlList.push(sql`, `);
				}
			}
		}

		const withSql = this.buildWithCTE(withList);

		const valuesSql = sql.join(valuesSqlList);

		const returningSql = returning
			? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}`
			: undefined;

		const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : undefined;

		const overridingSql = overridingSystemValue_ === true ? sql`overriding system value ` : undefined;

		return sql`${withSql}insert into ${table} ${insertOrder} ${overridingSql}${valuesSql}${onConflictSql}${returningSql}`;
	}

Domain

Subdomains

Frequently Asked Questions

What does buildInsertQuery() do?
buildInsertQuery() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/gel-core/dialect.ts.
Where is buildInsertQuery() defined?
buildInsertQuery() is defined in drizzle-orm/src/gel-core/dialect.ts at line 508.
What does buildInsertQuery() call?
buildInsertQuery() calls 2 function(s): buildSelection, buildWithCTE.

Analyze Your Own Codebase

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

Try Supermodel Free