insert.d.cts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { entityKind } from "../../entity.cjs";
  2. import type { TypedQueryBuilder } from "../../query-builders/query-builder.cjs";
  3. import type { SelectResultFields } from "../../query-builders/select.types.cjs";
  4. import { QueryPromise } from "../../query-promise.cjs";
  5. import type { RunnableQuery } from "../../runnable-query.cjs";
  6. import type { Placeholder, Query, SQLWrapper } from "../../sql/sql.cjs";
  7. import { Param, SQL } from "../../sql/sql.cjs";
  8. import type { SQLiteDialect } from "../dialect.cjs";
  9. import type { IndexColumn } from "../indexes.cjs";
  10. import type { SQLitePreparedQuery, SQLiteSession } from "../session.cjs";
  11. import { SQLiteTable } from "../table.cjs";
  12. import type { Subquery } from "../../subquery.cjs";
  13. import { type DrizzleTypeError, type Simplify } from "../../utils.cjs";
  14. import type { AnySQLiteColumn } from "../columns/common.cjs";
  15. import { QueryBuilder } from "./query-builder.cjs";
  16. import type { SelectedFieldsFlat, SelectedFieldsOrdered } from "./select.types.cjs";
  17. import type { SQLiteUpdateSetSource } from "./update.cjs";
  18. export interface SQLiteInsertConfig<TTable extends SQLiteTable = SQLiteTable> {
  19. table: TTable;
  20. values: Record<string, Param | SQL>[] | SQLiteInsertSelectQueryBuilder<TTable> | SQL;
  21. withList?: Subquery[];
  22. onConflict?: SQL[];
  23. returning?: SelectedFieldsOrdered;
  24. select?: boolean;
  25. }
  26. export type SQLiteInsertValue<TTable extends SQLiteTable> = Simplify<{
  27. [Key in keyof TTable['$inferInsert']]: TTable['$inferInsert'][Key] | SQL | Placeholder;
  28. }>;
  29. export type SQLiteInsertSelectQueryBuilder<TTable extends SQLiteTable> = TypedQueryBuilder<{
  30. [K in keyof TTable['$inferInsert']]: AnySQLiteColumn | SQL | SQL.Aliased | TTable['$inferInsert'][K];
  31. }>;
  32. export declare class SQLiteInsertBuilder<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult> {
  33. protected table: TTable;
  34. protected session: SQLiteSession<any, any, any, any>;
  35. protected dialect: SQLiteDialect;
  36. private withList?;
  37. static readonly [entityKind]: string;
  38. constructor(table: TTable, session: SQLiteSession<any, any, any, any>, dialect: SQLiteDialect, withList?: Subquery[] | undefined);
  39. values(value: SQLiteInsertValue<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  40. values(values: SQLiteInsertValue<TTable>[]): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  41. select(selectQuery: (qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  42. select(selectQuery: (qb: QueryBuilder) => SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  43. select(selectQuery: SQL): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  44. select(selectQuery: SQLiteInsertSelectQueryBuilder<TTable>): SQLiteInsertBase<TTable, TResultType, TRunResult>;
  45. }
  46. export type SQLiteInsertWithout<T extends AnySQLiteInsert, TDynamic extends boolean, K extends keyof T & string> = TDynamic extends true ? T : Omit<SQLiteInsertBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['returning'], TDynamic, T['_']['excludedMethods'] | K>, T['_']['excludedMethods'] | K>;
  47. export type SQLiteInsertReturning<T extends AnySQLiteInsert, TDynamic extends boolean, TSelectedFields extends SelectedFieldsFlat> = SQLiteInsertWithout<SQLiteInsertBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], SelectResultFields<TSelectedFields>, TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  48. export type SQLiteInsertReturningAll<T extends AnySQLiteInsert, TDynamic extends boolean> = SQLiteInsertWithout<SQLiteInsertBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['table']['$inferSelect'], TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  49. export type SQLiteInsertOnConflictDoUpdateConfig<T extends AnySQLiteInsert> = {
  50. target: IndexColumn | IndexColumn[];
  51. /** @deprecated - use either `targetWhere` or `setWhere` */
  52. where?: SQL;
  53. targetWhere?: SQL;
  54. setWhere?: SQL;
  55. set: SQLiteUpdateSetSource<T['_']['table']>;
  56. };
  57. export type SQLiteInsertDynamic<T extends AnySQLiteInsert> = SQLiteInsert<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['returning']>;
  58. export type SQLiteInsertExecute<T extends AnySQLiteInsert> = T['_']['returning'] extends undefined ? T['_']['runResult'] : T['_']['returning'][];
  59. export type SQLiteInsertPrepare<T extends AnySQLiteInsert> = SQLitePreparedQuery<{
  60. type: T['_']['resultType'];
  61. run: T['_']['runResult'];
  62. all: T['_']['returning'] extends undefined ? DrizzleTypeError<'.all() cannot be used without .returning()'> : T['_']['returning'][];
  63. get: T['_']['returning'] extends undefined ? DrizzleTypeError<'.get() cannot be used without .returning()'> : T['_']['returning'];
  64. values: T['_']['returning'] extends undefined ? DrizzleTypeError<'.values() cannot be used without .returning()'> : any[][];
  65. execute: SQLiteInsertExecute<T>;
  66. }>;
  67. export type AnySQLiteInsert = SQLiteInsertBase<any, any, any, any, any, any>;
  68. export type SQLiteInsert<TTable extends SQLiteTable = SQLiteTable, TResultType extends 'sync' | 'async' = 'sync' | 'async', TRunResult = unknown, TReturning = any> = SQLiteInsertBase<TTable, TResultType, TRunResult, TReturning, true, never>;
  69. export interface SQLiteInsertBase<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult, TReturning = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends SQLWrapper, QueryPromise<TReturning extends undefined ? TRunResult : TReturning[]>, RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], 'sqlite'> {
  70. readonly _: {
  71. readonly dialect: 'sqlite';
  72. readonly table: TTable;
  73. readonly resultType: TResultType;
  74. readonly runResult: TRunResult;
  75. readonly returning: TReturning;
  76. readonly dynamic: TDynamic;
  77. readonly excludedMethods: TExcludedMethods;
  78. readonly result: TReturning extends undefined ? TRunResult : TReturning[];
  79. };
  80. }
  81. export declare class SQLiteInsertBase<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult, TReturning = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? TRunResult : TReturning[]> implements RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], 'sqlite'>, SQLWrapper {
  82. private session;
  83. private dialect;
  84. static readonly [entityKind]: string;
  85. constructor(table: TTable, values: SQLiteInsertConfig['values'], session: SQLiteSession<any, any, any, any>, dialect: SQLiteDialect, withList?: Subquery[], select?: boolean);
  86. /**
  87. * Adds a `returning` clause to the query.
  88. *
  89. * Calling this method will return the specified fields of the inserted rows. If no fields are specified, all fields will be returned.
  90. *
  91. * See docs: {@link https://orm.drizzle.team/docs/insert#insert-returning}
  92. *
  93. * @example
  94. * ```ts
  95. * // Insert one row and return all fields
  96. * const insertedCar: Car[] = await db.insert(cars)
  97. * .values({ brand: 'BMW' })
  98. * .returning();
  99. *
  100. * // Insert one row and return only the id
  101. * const insertedCarId: { id: number }[] = await db.insert(cars)
  102. * .values({ brand: 'BMW' })
  103. * .returning({ id: cars.id });
  104. * ```
  105. */
  106. returning(): SQLiteInsertReturningAll<this, TDynamic>;
  107. returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): SQLiteInsertReturning<this, TDynamic, TSelectedFields>;
  108. /**
  109. * Adds an `on conflict do nothing` clause to the query.
  110. *
  111. * Calling this method simply avoids inserting a row as its alternative action.
  112. *
  113. * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
  114. *
  115. * @param config The `target` and `where` clauses.
  116. *
  117. * @example
  118. * ```ts
  119. * // Insert one row and cancel the insert if there's a conflict
  120. * await db.insert(cars)
  121. * .values({ id: 1, brand: 'BMW' })
  122. * .onConflictDoNothing();
  123. *
  124. * // Explicitly specify conflict target
  125. * await db.insert(cars)
  126. * .values({ id: 1, brand: 'BMW' })
  127. * .onConflictDoNothing({ target: cars.id });
  128. * ```
  129. */
  130. onConflictDoNothing(config?: {
  131. target?: IndexColumn | IndexColumn[];
  132. where?: SQL;
  133. }): this;
  134. /**
  135. * Adds an `on conflict do update` clause to the query.
  136. *
  137. * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
  138. *
  139. * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
  140. *
  141. * @param config The `target`, `set` and `where` clauses.
  142. *
  143. * @example
  144. * ```ts
  145. * // Update the row if there's a conflict
  146. * await db.insert(cars)
  147. * .values({ id: 1, brand: 'BMW' })
  148. * .onConflictDoUpdate({
  149. * target: cars.id,
  150. * set: { brand: 'Porsche' }
  151. * });
  152. *
  153. * // Upsert with 'where' clause
  154. * await db.insert(cars)
  155. * .values({ id: 1, brand: 'BMW' })
  156. * .onConflictDoUpdate({
  157. * target: cars.id,
  158. * set: { brand: 'newBMW' },
  159. * where: sql`${cars.createdAt} > '2023-01-01'::date`,
  160. * });
  161. * ```
  162. */
  163. onConflictDoUpdate(config: SQLiteInsertOnConflictDoUpdateConfig<this>): this;
  164. toSQL(): Query;
  165. prepare(): SQLiteInsertPrepare<this>;
  166. run: ReturnType<this['prepare']>['run'];
  167. all: ReturnType<this['prepare']>['all'];
  168. get: ReturnType<this['prepare']>['get'];
  169. values: ReturnType<this['prepare']>['values'];
  170. execute(): Promise<SQLiteInsertExecute<this>>;
  171. $dynamic(): SQLiteInsertDynamic<this>;
  172. }