import { entityKind } from "../../entity.cjs"; import type { PgDialect } from "../dialect.cjs"; import type { IndexColumn } from "../indexes.cjs"; import type { PgPreparedQuery, PgQueryResultHKT, PgQueryResultKind, PgSession, PreparedQueryConfig } from "../session.cjs"; import type { PgTable, TableConfig } from "../table.cjs"; import type { TypedQueryBuilder } from "../../query-builders/query-builder.cjs"; import type { SelectResultFields } from "../../query-builders/select.types.cjs"; import { QueryPromise } from "../../query-promise.cjs"; import type { RunnableQuery } from "../../runnable-query.cjs"; import type { ColumnsSelection, Placeholder, Query, SQLWrapper } from "../../sql/sql.cjs"; import { Param, SQL } from "../../sql/sql.cjs"; import type { Subquery } from "../../subquery.cjs"; import type { InferInsertModel } from "../../table.cjs"; import type { AnyPgColumn } from "../columns/common.cjs"; import { QueryBuilder } from "./query-builder.cjs"; import type { SelectedFieldsFlat, SelectedFieldsOrdered } from "./select.types.cjs"; import type { PgUpdateSetSource } from "./update.cjs"; export interface PgInsertConfig { table: TTable; values: Record[] | PgInsertSelectQueryBuilder | SQL; withList?: Subquery[]; onConflict?: SQL; returningFields?: SelectedFieldsFlat; returning?: SelectedFieldsOrdered; select?: boolean; overridingSystemValue_?: boolean; } export type PgInsertValue, OverrideT extends boolean = false> = { [Key in keyof InferInsertModel]: InferInsertModel[Key] | SQL | Placeholder; } & {}; export type PgInsertSelectQueryBuilder = TypedQueryBuilder<{ [K in keyof TTable['$inferInsert']]: AnyPgColumn | SQL | SQL.Aliased | TTable['$inferInsert'][K]; }>; export declare class PgInsertBuilder { private table; private session; private dialect; private withList?; private overridingSystemValue_?; static readonly [entityKind]: string; constructor(table: TTable, session: PgSession, dialect: PgDialect, withList?: Subquery[] | undefined, overridingSystemValue_?: boolean | undefined); private authToken?; overridingSystemValue(): Omit, 'overridingSystemValue'>; values(value: PgInsertValue): PgInsertBase; values(values: PgInsertValue[]): PgInsertBase; select(selectQuery: (qb: QueryBuilder) => PgInsertSelectQueryBuilder): PgInsertBase; select(selectQuery: (qb: QueryBuilder) => SQL): PgInsertBase; select(selectQuery: SQL): PgInsertBase; select(selectQuery: PgInsertSelectQueryBuilder): PgInsertBase; } export type PgInsertWithout = TDynamic extends true ? T : Omit, T['_']['excludedMethods'] | K>; export type PgInsertReturning = PgInsertBase, TDynamic, T['_']['excludedMethods']>; export type PgInsertReturningAll = PgInsertBase; export interface PgInsertOnConflictDoUpdateConfig { target: IndexColumn | IndexColumn[]; /** @deprecated use either `targetWhere` or `setWhere` */ where?: SQL; targetWhere?: SQL; setWhere?: SQL; set: PgUpdateSetSource; } export type PgInsertPrepare = PgPreparedQuery : T['_']['returning'][]; }>; export type PgInsertDynamic = PgInsert; export type AnyPgInsert = PgInsertBase; export type PgInsert | undefined = Record | undefined> = PgInsertBase; export interface PgInsertBase | undefined = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends TypedQueryBuilder : TReturning[]>, QueryPromise : TReturning[]>, RunnableQuery : TReturning[], 'pg'>, SQLWrapper { readonly _: { readonly dialect: 'pg'; readonly table: TTable; readonly queryResult: TQueryResult; readonly selectedFields: TSelectedFields; readonly returning: TReturning; readonly dynamic: TDynamic; readonly excludedMethods: TExcludedMethods; readonly result: TReturning extends undefined ? PgQueryResultKind : TReturning[]; }; } export declare class PgInsertBase | undefined = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise : TReturning[]> implements TypedQueryBuilder : TReturning[]>, RunnableQuery : TReturning[], 'pg'>, SQLWrapper { private session; private dialect; static readonly [entityKind]: string; private config; constructor(table: TTable, values: PgInsertConfig['values'], session: PgSession, dialect: PgDialect, withList?: Subquery[], select?: boolean, overridingSystemValue_?: boolean); /** * 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, TDynamic, 'returning'>; returning(fields: TSelectedFields): PgInsertWithout, TDynamic, 'returning'>; /** * 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) * .values({ id: 1, brand: 'BMW' }) * .onConflictDoNothing(); * * // Explicitly specify conflict target * await db.insert(cars) * .values({ id: 1, brand: 'BMW' }) * .onConflictDoNothing({ target: cars.id }); * ``` */ onConflictDoNothing(config?: { target?: IndexColumn | IndexColumn[]; where?: SQL; }): PgInsertWithout; /** * Adds an `on conflict do update` clause to the query. * * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action. * * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts} * * @param config The `target`, `set` and `where` clauses. * * @example * ```ts * // Update the row if there's a conflict * await db.insert(cars) * .values({ id: 1, brand: 'BMW' }) * .onConflictDoUpdate({ * target: cars.id, * set: { brand: 'Porsche' } * }); * * // Upsert with 'where' clause * await db.insert(cars) * .values({ id: 1, brand: 'BMW' }) * .onConflictDoUpdate({ * target: cars.id, * set: { brand: 'newBMW' }, * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`, * }); * ``` */ onConflictDoUpdate(config: PgInsertOnConflictDoUpdateConfig): PgInsertWithout; toSQL(): Query; prepare(name: string): PgInsertPrepare; private authToken?; execute: ReturnType['execute']; $dynamic(): PgInsertDynamic; }