import { entityKind } from "../../entity.js"; import type { TypedQueryBuilder } from "../../query-builders/query-builder.js"; import type { SelectResultFields } from "../../query-builders/select.types.js"; import { QueryPromise } from "../../query-promise.js"; import type { RunnableQuery } from "../../runnable-query.js"; import type { Placeholder, Query, SQLWrapper } from "../../sql/sql.js"; import { Param, SQL } from "../../sql/sql.js"; import type { SQLiteDialect } from "../dialect.js"; import type { IndexColumn } from "../indexes.js"; import type { SQLitePreparedQuery, SQLiteSession } from "../session.js"; import { SQLiteTable } from "../table.js"; import type { Subquery } from "../../subquery.js"; import { type DrizzleTypeError, type Simplify } from "../../utils.js"; import type { AnySQLiteColumn } from "../columns/common.js"; import { QueryBuilder } from "./query-builder.js"; import type { SelectedFieldsFlat, SelectedFieldsOrdered } from "./select.types.js"; import type { SQLiteUpdateSetSource } from "./update.js"; export interface SQLiteInsertConfig { table: TTable; values: Record[] | SQLiteInsertSelectQueryBuilder | SQL; withList?: Subquery[]; onConflict?: SQL[]; returning?: SelectedFieldsOrdered; select?: boolean; } export type SQLiteInsertValue = Simplify<{ [Key in keyof TTable['$inferInsert']]: TTable['$inferInsert'][Key] | SQL | Placeholder; }>; export type SQLiteInsertSelectQueryBuilder = TypedQueryBuilder<{ [K in keyof TTable['$inferInsert']]: AnySQLiteColumn | SQL | SQL.Aliased | TTable['$inferInsert'][K]; }>; export declare class SQLiteInsertBuilder { protected table: TTable; protected session: SQLiteSession; protected dialect: SQLiteDialect; private withList?; static readonly [entityKind]: string; constructor(table: TTable, session: SQLiteSession, dialect: SQLiteDialect, withList?: Subquery[] | undefined); values(value: SQLiteInsertValue): SQLiteInsertBase; values(values: SQLiteInsertValue[]): SQLiteInsertBase; select(selectQuery: (qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder): SQLiteInsertBase; select(selectQuery: (qb: QueryBuilder) => SQL): SQLiteInsertBase; select(selectQuery: SQL): SQLiteInsertBase; select(selectQuery: SQLiteInsertSelectQueryBuilder): SQLiteInsertBase; } export type SQLiteInsertWithout = TDynamic extends true ? T : Omit, T['_']['excludedMethods'] | K>; export type SQLiteInsertReturning = SQLiteInsertWithout, TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>; export type SQLiteInsertReturningAll = SQLiteInsertWithout, TDynamic, 'returning'>; export type SQLiteInsertOnConflictDoUpdateConfig = { target: IndexColumn | IndexColumn[]; /** @deprecated - use either `targetWhere` or `setWhere` */ where?: SQL; targetWhere?: SQL; setWhere?: SQL; set: SQLiteUpdateSetSource; }; export type SQLiteInsertDynamic = SQLiteInsert; export type SQLiteInsertExecute = T['_']['returning'] extends undefined ? T['_']['runResult'] : T['_']['returning'][]; export type SQLiteInsertPrepare = SQLitePreparedQuery<{ type: T['_']['resultType']; run: T['_']['runResult']; all: T['_']['returning'] extends undefined ? DrizzleTypeError<'.all() cannot be used without .returning()'> : T['_']['returning'][]; get: T['_']['returning'] extends undefined ? DrizzleTypeError<'.get() cannot be used without .returning()'> : T['_']['returning']; values: T['_']['returning'] extends undefined ? DrizzleTypeError<'.values() cannot be used without .returning()'> : any[][]; execute: SQLiteInsertExecute; }>; export type AnySQLiteInsert = SQLiteInsertBase; export type SQLiteInsert = SQLiteInsertBase; export interface SQLiteInsertBase extends SQLWrapper, QueryPromise, RunnableQuery { readonly _: { readonly dialect: 'sqlite'; readonly table: TTable; readonly resultType: TResultType; readonly runResult: TRunResult; readonly returning: TReturning; readonly dynamic: TDynamic; readonly excludedMethods: TExcludedMethods; readonly result: TReturning extends undefined ? TRunResult : TReturning[]; }; } export declare class SQLiteInsertBase extends QueryPromise implements RunnableQuery, SQLWrapper { private session; private dialect; static readonly [entityKind]: string; constructor(table: TTable, values: SQLiteInsertConfig['values'], session: SQLiteSession, dialect: SQLiteDialect, withList?: Subquery[], select?: 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(): SQLiteInsertReturningAll; returning(fields: TSelectedFields): SQLiteInsertReturning; /** * 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; }): this; /** * 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' }, * where: sql`${cars.createdAt} > '2023-01-01'::date`, * }); * ``` */ onConflictDoUpdate(config: SQLiteInsertOnConflictDoUpdateConfig): this; toSQL(): Query; prepare(): SQLiteInsertPrepare; run: ReturnType['run']; all: ReturnType['all']; get: ReturnType['get']; values: ReturnType['values']; execute(): Promise>; $dynamic(): SQLiteInsertDynamic; }