insert.d.cts 10 KB

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