Home / Class/ PgInsertBase Class — drizzle-orm Architecture

PgInsertBase Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  8e4153bf_b365_8b49_3724_c65caf9729e7["PgInsertBase"]
  6f066777_3f37_e9b7_9d78_03e1a3ea895d["insert.ts"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|defined in| 6f066777_3f37_e9b7_9d78_03e1a3ea895d
  028cba5c_688a_6fec_1abb_1bf93d36ed39["constructor()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| 028cba5c_688a_6fec_1abb_1bf93d36ed39
  ae5b7c9a_656d_20f0_9ae3_a6fde018a835["returning()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| ae5b7c9a_656d_20f0_9ae3_a6fde018a835
  fba983f0_1d07_c4f1_24ea_04d4bb16e761["onConflictDoNothing()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| fba983f0_1d07_c4f1_24ea_04d4bb16e761
  cb84ca6f_f73b_34f7_b84d_8d8d46727562["onConflictDoUpdate()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| cb84ca6f_f73b_34f7_b84d_8d8d46727562
  289b8f44_f991_873e_8b18_c230303cab24["getSQL()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| 289b8f44_f991_873e_8b18_c230303cab24
  1d5e10ce_8ce7_5bbf_a69f_3b3f4da57100["toSQL()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| 1d5e10ce_8ce7_5bbf_a69f_3b3f4da57100
  f5b5654b_74de_3eb2_aff2_55d831b2c94f["_prepare()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| f5b5654b_74de_3eb2_aff2_55d831b2c94f
  acaa96e0_1466_7d45_aeaf_7edd65b0b100["prepare()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| acaa96e0_1466_7d45_aeaf_7edd65b0b100
  e566025a_9b47_d861_debd_3fa4aea89f59["setToken()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| e566025a_9b47_d861_debd_3fa4aea89f59
  0906fbcc_adaa_a2d8_9ed7_bc07387fdbc6["getSelectedFields()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| 0906fbcc_adaa_a2d8_9ed7_bc07387fdbc6
  a861cfc2_e7b7_1b1a_666d_d4689bd95426["$dynamic()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7 -->|method| a861cfc2_e7b7_1b1a_666d_d4689bd95426

Relationship Graph

Source Code

drizzle-orm/src/pg-core/query-builders/insert.ts lines 233–451

export class PgInsertBase<
	TTable extends PgTable,
	TQueryResult extends PgQueryResultHKT,
	TSelectedFields extends ColumnsSelection | undefined = undefined,
	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 ? PgQueryResultKind<TQueryResult, never> : TReturning[]>
	implements
		TypedQueryBuilder<
			TSelectedFields,
			TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]
		>,
		RunnableQuery<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[], 'pg'>,
		SQLWrapper
{
	static override readonly [entityKind]: string = 'PgInsert';

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

	constructor(
		table: TTable,
		values: PgInsertConfig['values'],
		private session: PgSession,
		private dialect: PgDialect,
		withList?: Subquery[],
		select?: boolean,
		overridingSystemValue_?: boolean,
	) {
		super();
		this.config = { table, values: values as any, withList, select, overridingSystemValue_ };
	}

	/**
	 * Adds a `returning` clause to the query.
	 *
	 * Calling this method will return the specified fields of the inserted rows. If no fields are specified, all fields will be returned.
	 *
	 * See docs: {@link https://orm.drizzle.team/docs/insert#insert-returning}
	 *
	 * @example
	 * ```ts
	 * // Insert one row and return all fields
	 * const insertedCar: Car[] = await db.insert(cars)
	 *   .values({ brand: 'BMW' })
	 *   .returning();
	 *
	 * // Insert one row and return only the id
	 * const insertedCarId: { id: number }[] = await db.insert(cars)
	 *   .values({ brand: 'BMW' })
	 *   .returning({ id: cars.id });
	 * ```
	 */
	returning(): PgInsertWithout<PgInsertReturningAll<this, TDynamic>, TDynamic, 'returning'>;
	returning<TSelectedFields extends SelectedFieldsFlat>(
		fields: TSelectedFields,
	): PgInsertWithout<PgInsertReturning<this, TDynamic, TSelectedFields>, TDynamic, 'returning'>;
	returning(
		fields: SelectedFieldsFlat = this.config.table[Table.Symbol.Columns],
	): PgInsertWithout<AnyPgInsert, TDynamic, 'returning'> {
		this.config.returningFields = fields;
		this.config.returning = orderSelectedFields<PgColumn>(fields);
		return this as any;
	}

	/**
	 * Adds an `on conflict do nothing` clause to the query.
	 *
	 * Calling this method simply avoids inserting a row as its alternative action.
	 *
	 * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
	 *
	 * @param config The `target` and `where` clauses.
	 *
	 * @example
	 * ```ts
	 * // Insert one row and cancel the insert if there's a conflict
	 * await db.insert(cars)

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free