db.d.cts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import type { Cache } from "../cache/core/cache.cjs";
  2. import { entityKind } from "../entity.cjs";
  3. import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from "../relations.cjs";
  4. import { type SQL, type SQLWrapper } from "../sql/sql.cjs";
  5. import type { SQLiteAsyncDialect, SQLiteSyncDialect } from "./dialect.cjs";
  6. import { SQLiteDeleteBase, SQLiteInsertBuilder, SQLiteSelectBuilder, SQLiteUpdateBuilder } from "./query-builders/index.cjs";
  7. import type { DBResult, Result, SQLiteSession, SQLiteTransaction, SQLiteTransactionConfig } from "./session.cjs";
  8. import type { SQLiteTable } from "./table.cjs";
  9. import { WithSubquery } from "../subquery.cjs";
  10. import type { DrizzleTypeError } from "../utils.cjs";
  11. import { SQLiteCountBuilder } from "./query-builders/count.cjs";
  12. import { RelationalQueryBuilder } from "./query-builders/query.cjs";
  13. import type { SelectedFields } from "./query-builders/select.types.cjs";
  14. import type { WithBuilder } from "./subquery.cjs";
  15. import type { SQLiteViewBase } from "./view-base.cjs";
  16. export declare class BaseSQLiteDatabase<TResultKind extends 'sync' | 'async', TRunResult, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
  17. private resultKind;
  18. static readonly [entityKind]: string;
  19. readonly _: {
  20. readonly schema: TSchema | undefined;
  21. readonly fullSchema: TFullSchema;
  22. readonly tableNamesMap: Record<string, string>;
  23. };
  24. query: TFullSchema extends Record<string, never> ? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'> : {
  25. [K in keyof TSchema]: RelationalQueryBuilder<TResultKind, TFullSchema, TSchema, TSchema[K]>;
  26. };
  27. constructor(resultKind: TResultKind,
  28. /** @internal */
  29. dialect: {
  30. sync: SQLiteSyncDialect;
  31. async: SQLiteAsyncDialect;
  32. }[TResultKind],
  33. /** @internal */
  34. session: SQLiteSession<TResultKind, TRunResult, TFullSchema, TSchema>, schema: RelationalSchemaConfig<TSchema> | undefined);
  35. /**
  36. * Creates a subquery that defines a temporary named result set as a CTE.
  37. *
  38. * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
  39. *
  40. * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
  41. *
  42. * @param alias The alias for the subquery.
  43. *
  44. * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
  45. *
  46. * @example
  47. *
  48. * ```ts
  49. * // Create a subquery with alias 'sq' and use it in the select query
  50. * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
  51. *
  52. * const result = await db.with(sq).select().from(sq);
  53. * ```
  54. *
  55. * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
  56. *
  57. * ```ts
  58. * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
  59. * const sq = db.$with('sq').as(db.select({
  60. * name: sql<string>`upper(${users.name})`.as('name'),
  61. * })
  62. * .from(users));
  63. *
  64. * const result = await db.with(sq).select({ name: sq.name }).from(sq);
  65. * ```
  66. */
  67. $with: WithBuilder;
  68. $count(source: SQLiteTable | SQLiteViewBase | SQL | SQLWrapper, filters?: SQL<unknown>): SQLiteCountBuilder<SQLiteSession<TResultKind, TRunResult, TFullSchema, TSchema>>;
  69. /**
  70. * Incorporates a previously defined CTE (using `$with`) into the main query.
  71. *
  72. * This method allows the main query to reference a temporary named result set.
  73. *
  74. * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
  75. *
  76. * @param queries The CTEs to incorporate into the main query.
  77. *
  78. * @example
  79. *
  80. * ```ts
  81. * // Define a subquery 'sq' as a CTE using $with
  82. * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
  83. *
  84. * // Incorporate the CTE 'sq' into the main query and select from it
  85. * const result = await db.with(sq).select().from(sq);
  86. * ```
  87. */
  88. with(...queries: WithSubquery[]): {
  89. select: {
  90. (): SQLiteSelectBuilder<undefined, TResultKind, TRunResult>;
  91. <TSelection extends SelectedFields>(fields: TSelection): SQLiteSelectBuilder<TSelection, TResultKind, TRunResult>;
  92. };
  93. selectDistinct: {
  94. (): SQLiteSelectBuilder<undefined, TResultKind, TRunResult>;
  95. <TSelection extends SelectedFields>(fields: TSelection): SQLiteSelectBuilder<TSelection, TResultKind, TRunResult>;
  96. };
  97. update: <TTable extends SQLiteTable>(table: TTable) => SQLiteUpdateBuilder<TTable, TResultKind, TRunResult>;
  98. insert: <TTable extends SQLiteTable>(into: TTable) => SQLiteInsertBuilder<TTable, TResultKind, TRunResult>;
  99. delete: <TTable extends SQLiteTable>(from: TTable) => SQLiteDeleteBase<TTable, TResultKind, TRunResult>;
  100. };
  101. /**
  102. * Creates a select query.
  103. *
  104. * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.
  105. *
  106. * Use `.from()` method to specify which table to select from.
  107. *
  108. * See docs: {@link https://orm.drizzle.team/docs/select}
  109. *
  110. * @param fields The selection object.
  111. *
  112. * @example
  113. *
  114. * ```ts
  115. * // Select all columns and all rows from the 'cars' table
  116. * const allCars: Car[] = await db.select().from(cars);
  117. *
  118. * // Select specific columns and all rows from the 'cars' table
  119. * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({
  120. * id: cars.id,
  121. * brand: cars.brand
  122. * })
  123. * .from(cars);
  124. * ```
  125. *
  126. * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:
  127. *
  128. * ```ts
  129. * // Select specific columns along with expression and all rows from the 'cars' table
  130. * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({
  131. * id: cars.id,
  132. * lowerBrand: sql<string>`lower(${cars.brand})`,
  133. * })
  134. * .from(cars);
  135. * ```
  136. */
  137. select(): SQLiteSelectBuilder<undefined, TResultKind, TRunResult>;
  138. select<TSelection extends SelectedFields>(fields: TSelection): SQLiteSelectBuilder<TSelection, TResultKind, TRunResult>;
  139. /**
  140. * Adds `distinct` expression to the select query.
  141. *
  142. * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.
  143. *
  144. * Use `.from()` method to specify which table to select from.
  145. *
  146. * See docs: {@link https://orm.drizzle.team/docs/select#distinct}
  147. *
  148. * @param fields The selection object.
  149. *
  150. * @example
  151. *
  152. * ```ts
  153. * // Select all unique rows from the 'cars' table
  154. * await db.selectDistinct()
  155. * .from(cars)
  156. * .orderBy(cars.id, cars.brand, cars.color);
  157. *
  158. * // Select all unique brands from the 'cars' table
  159. * await db.selectDistinct({ brand: cars.brand })
  160. * .from(cars)
  161. * .orderBy(cars.brand);
  162. * ```
  163. */
  164. selectDistinct(): SQLiteSelectBuilder<undefined, TResultKind, TRunResult>;
  165. selectDistinct<TSelection extends SelectedFields>(fields: TSelection): SQLiteSelectBuilder<TSelection, TResultKind, TRunResult>;
  166. /**
  167. * Creates an update query.
  168. *
  169. * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
  170. *
  171. * Use `.set()` method to specify which values to update.
  172. *
  173. * See docs: {@link https://orm.drizzle.team/docs/update}
  174. *
  175. * @param table The table to update.
  176. *
  177. * @example
  178. *
  179. * ```ts
  180. * // Update all rows in the 'cars' table
  181. * await db.update(cars).set({ color: 'red' });
  182. *
  183. * // Update rows with filters and conditions
  184. * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
  185. *
  186. * // Update with returning clause
  187. * const updatedCar: Car[] = await db.update(cars)
  188. * .set({ color: 'red' })
  189. * .where(eq(cars.id, 1))
  190. * .returning();
  191. * ```
  192. */
  193. update<TTable extends SQLiteTable>(table: TTable): SQLiteUpdateBuilder<TTable, TResultKind, TRunResult>;
  194. $cache: {
  195. invalidate: Cache['onMutate'];
  196. };
  197. /**
  198. * Creates an insert query.
  199. *
  200. * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
  201. *
  202. * See docs: {@link https://orm.drizzle.team/docs/insert}
  203. *
  204. * @param table The table to insert into.
  205. *
  206. * @example
  207. *
  208. * ```ts
  209. * // Insert one row
  210. * await db.insert(cars).values({ brand: 'BMW' });
  211. *
  212. * // Insert multiple rows
  213. * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
  214. *
  215. * // Insert with returning clause
  216. * const insertedCar: Car[] = await db.insert(cars)
  217. * .values({ brand: 'BMW' })
  218. * .returning();
  219. * ```
  220. */
  221. insert<TTable extends SQLiteTable>(into: TTable): SQLiteInsertBuilder<TTable, TResultKind, TRunResult>;
  222. /**
  223. * Creates a delete query.
  224. *
  225. * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
  226. *
  227. * See docs: {@link https://orm.drizzle.team/docs/delete}
  228. *
  229. * @param table The table to delete from.
  230. *
  231. * @example
  232. *
  233. * ```ts
  234. * // Delete all rows in the 'cars' table
  235. * await db.delete(cars);
  236. *
  237. * // Delete rows with filters and conditions
  238. * await db.delete(cars).where(eq(cars.color, 'green'));
  239. *
  240. * // Delete with returning clause
  241. * const deletedCar: Car[] = await db.delete(cars)
  242. * .where(eq(cars.id, 1))
  243. * .returning();
  244. * ```
  245. */
  246. delete<TTable extends SQLiteTable>(from: TTable): SQLiteDeleteBase<TTable, TResultKind, TRunResult>;
  247. run(query: SQLWrapper | string): DBResult<TResultKind, TRunResult>;
  248. all<T = unknown>(query: SQLWrapper | string): DBResult<TResultKind, T[]>;
  249. get<T = unknown>(query: SQLWrapper | string): DBResult<TResultKind, T>;
  250. values<T extends unknown[] = unknown[]>(query: SQLWrapper | string): DBResult<TResultKind, T[]>;
  251. transaction<T>(transaction: (tx: SQLiteTransaction<TResultKind, TRunResult, TFullSchema, TSchema>) => Result<TResultKind, T>, config?: SQLiteTransactionConfig): Result<TResultKind, T>;
  252. }
  253. export type SQLiteWithReplicas<Q> = Q & {
  254. $primary: Q;
  255. $replicas: Q[];
  256. };
  257. export declare const withReplicas: <TResultKind extends "sync" | "async", TRunResult, TFullSchema extends Record<string, unknown>, TSchema extends TablesRelationalConfig, Q extends BaseSQLiteDatabase<TResultKind, TRunResult, TFullSchema, TSchema extends Record<string, unknown> ? ExtractTablesWithRelations<TFullSchema> : TSchema>>(primary: Q, replicas: [Q, ...Q[]], getReplica?: (replicas: Q[]) => Q) => SQLiteWithReplicas<Q>;