AK-21/Graphite-Industrial-Intelligence
0
1import type { Cache } from "../cache/core/cache.js";2import { entityKind } from "../entity.js";3import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from "../relations.js";4import { type SQL, type SQLWrapper } from "../sql/sql.js";5import type { SQLiteAsyncDialect, SQLiteSyncDialect } from "./dialect.js";6import { SQLiteDeleteBase, SQLiteInsertBuilder, SQLiteSelectBuilder, SQLiteUpdateBuilder } from "./query-builders/index.js";7import type { DBResult, Result, SQLiteSession, SQLiteTransaction, SQLiteTransactionConfig } from "./session.js";8import type { SQLiteTable } from "./table.js";9import { WithSubquery } from "../subquery.js";10import type { DrizzleTypeError } from "../utils.js";11import { SQLiteCountBuilder } from "./query-builders/count.js";12import { RelationalQueryBuilder } from "./query-builders/query.js";13import type { SelectedFields } from "./query-builders/select.types.js";14import type { WithBuilder } from "./subquery.js";15import type { SQLiteViewBase } from "./view-base.js";16export 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 * @example47 *48 * ```ts49 * // Create a subquery with alias 'sq' and use it in the select query50 * 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 * ```ts58 * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query59 * 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 * @example79 *80 * ```ts81 * // Define a subquery 'sq' as a CTE using $with82 * 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 it85 * 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 * @example113 *114 * ```ts115 * // Select all columns and all rows from the 'cars' table116 * const allCars: Car[] = await db.select().from(cars);117 *118 * // Select specific columns and all rows from the 'cars' table119 * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({120 * id: cars.id,121 * brand: cars.brand122 * })123 * .from(cars);124 * ```125 *126 * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:127 *128 * ```ts129 * // Select specific columns along with expression and all rows from the 'cars' table130 * 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 * @example151 *152 * ```ts153 * // Select all unique rows from the 'cars' table154 * await db.selectDistinct()155 * .from(cars)156 * .orderBy(cars.id, cars.brand, cars.color);157 *158 * // Select all unique brands from the 'cars' table159 * 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 * @example178 *179 * ```ts180 * // Update all rows in the 'cars' table181 * await db.update(cars).set({ color: 'red' });182 *183 * // Update rows with filters and conditions184 * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));185 *186 * // Update with returning clause187 * 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 * @example207 *208 * ```ts209 * // Insert one row210 * await db.insert(cars).values({ brand: 'BMW' });211 *212 * // Insert multiple rows213 * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);214 *215 * // Insert with returning clause216 * 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 * @example232 *233 * ```ts234 * // Delete all rows in the 'cars' table235 * await db.delete(cars);236 *237 * // Delete rows with filters and conditions238 * await db.delete(cars).where(eq(cars.color, 'green'));239 *240 * // Delete with returning clause241 * 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}253export type SQLiteWithReplicas<Q> = Q & {254 $primary: Q;255 $replicas: Q[];256};257export 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>;258 