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 60bfe461_a9dc_4322_6fda_a22d548a71a9["buildInsertQuery()"] 3977a3fd_2f08_73b1_4df4_3d0fd1858514["PgDialect"] 60bfe461_a9dc_4322_6fda_a22d548a71a9 -->|defined in| 3977a3fd_2f08_73b1_4df4_3d0fd1858514 bcbd4f74_c6c7_2989_e283_a503bf2039fa["buildWithCTE()"] 60bfe461_a9dc_4322_6fda_a22d548a71a9 -->|calls| bcbd4f74_c6c7_2989_e283_a503bf2039fa e700cd38_c0c8_fbeb_26a9_7fbf17216117["buildSelection()"] 60bfe461_a9dc_4322_6fda_a22d548a71a9 -->|calls| e700cd38_c0c8_fbeb_26a9_7fbf17216117 style 60bfe461_a9dc_4322_6fda_a22d548a71a9 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
drizzle-orm/src/pg-core/dialect.ts lines 513–580
buildInsertQuery(
{ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }: PgInsertConfig,
): SQL {
const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
const columns: Record<string, PgColumn> = table[Table.Symbol.Columns];
const colEntries: [string, PgColumn][] = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
const insertOrder = colEntries.map(
([, column]) => sql.identifier(this.casing.getColumnCasing(column)),
);
if (select) {
const select = valuesOrSelect as AnyPgSelectQueryBuilder | 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
Defined In
Source
Frequently Asked Questions
What does buildInsertQuery() do?
buildInsertQuery() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/dialect.ts.
Where is buildInsertQuery() defined?
buildInsertQuery() is defined in drizzle-orm/src/pg-core/dialect.ts at line 513.
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