db.d.cts 9.9 KB

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