UNPKG

flint-orm

Version:

Type-safe SQLite ORM for JavaScript

123 lines (122 loc) 4.67 kB
import type { Executor } from './executor'; import { SelectBuilder, DeleteBuilder } from './query/builder'; import type { Executable, InsertStage1, UpdateStage1, JoinSelectStage1 } from './query/builder'; import type { AnyTable } from './schema/table'; import type { Condition } from './query/conditions'; import type { ColumnDef } from './schema/columns'; export type { Executable, SelectBuilder, InsertStage1, UpdateStage1, JoinSelectStage1, JoinBuilder, SingleJoinBuilder } from './query/builder'; export type { JoinResult } from './query/builder'; /** * A raw SQL expression with parameters. */ export interface SQLExpression { sql: string; params: unknown[]; } /** * Create a flint database client from an executor. * * This is the shared core — driver-specific entry points call this * with their executor implementation. * * @example * // Used internally by driver entry points: * import { createClient } from '../flint' * export function flint(details) { * const executor = new BunSqliteExecutor(new Database(details.url)) * return createClient(executor) * } */ export declare function createClient(executor: Executor): { /** * Start a SELECT query — pass a table definition. * * @example * const rows = db.selectFrom(users).execute() */ selectFrom: <T extends AnyTable>(table: T) => SelectBuilder<T>; /** * Start an INSERT — call `.values(row)` next. * * @example * db.insert(users).values({ id: "u1", name: "Alice" }).execute() */ insert: <T extends AnyTable>(table: T) => InsertStage1<T>; /** * Start an UPDATE — call `.set(partial)` next. * * @example * db.update(users).set({ name: "Bob" }).where(eq(users.id, "u1")).execute() */ update: <T extends AnyTable>(table: T) => UpdateStage1<T>; /** * Start a DELETE — call `.where(condition)` next. * * @example * db.delete(users).where(eq(users.id, "u1")).execute() */ delete: <T extends AnyTable>(table: T) => DeleteBuilder<T, false, keyof import(".").InferRow<T>>; /** * Start a LEFT JOIN — call `.on(child)` next. * * @example * db.leftJoin(users).on(posts).execute() */ leftJoin: <Parent extends AnyTable>(parent: Parent) => JoinSelectStage1<Parent>; /** * Start an INNER JOIN — call `.on(child)` next. * * @example * db.innerJoin(users).on(posts).execute() */ innerJoin: <Parent extends AnyTable>(parent: Parent) => JoinSelectStage1<Parent>; /** * Run multiple queries atomically in a single transaction. * * @example * db.batch([ * db.insert(users).values({ id: "u1", name: "Alice" }), * db.insert(posts).values({ id: "p1", userId: "u1", title: "Hello" }), * ]) */ batch: (queries: Executable[]) => Promise<void>; /** * Count all rows in a table, optionally filtered by a condition. */ count: <T extends AnyTable>(table: T, condition?: Condition) => Promise<number>; /** * Count non-null values of a column, optionally filtered by a condition. */ countColumn: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => Promise<number>; /** * Sum non-null values of a column, optionally filtered by a condition. */ sum: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => Promise<number | null>; /** * Average non-null values of a column, optionally filtered by a condition. */ avg: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => Promise<number | null>; /** * Find the minimum non-null value of a column, optionally filtered by a condition. */ min: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => Promise<number | null>; /** * Find the maximum non-null value of a column, optionally filtered by a condition. */ max: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => Promise<number | null>; /** * Execute raw SQL directly against the database. */ $run(query: string, ...params: unknown[]): Promise<{ rowsAffected: number; }>; /** Direct access to the underlying executor. */ $executor: Executor; }; /** * Tagged template for building parameterized SQL expressions. * * @example * const expr = sql`name = ${"Alice"} AND age > ${18}` */ export declare function sql(strings: TemplateStringsArray, ...values: unknown[]): SQLExpression;