UNPKG

flint-orm

Version:

Type-safe SQLite ORM for JavaScript

177 lines (176 loc) 4.64 kB
import type { ColumnDef } from '../schema/columns'; /** A condition node used in WHERE clauses. */ export type Condition = { type: 'eq'; column: ColumnDef<any, any>; value: unknown; } | { type: 'eqColumn'; left: ColumnDef<any, any>; right: ColumnDef<any, any>; } | { type: 'gt'; column: ColumnDef<any, any>; value: unknown; } | { type: 'gte'; column: ColumnDef<any, any>; value: unknown; } | { type: 'lt'; column: ColumnDef<any, any>; value: unknown; } | { type: 'lte'; column: ColumnDef<any, any>; value: unknown; } | { type: 'neq'; column: ColumnDef<any, any>; value: unknown; } | { type: 'in'; column: ColumnDef<any, any>; values: unknown[]; } | { type: 'notIn'; column: ColumnDef<any, any>; values: unknown[]; } | { type: 'isNull'; column: ColumnDef<any, any>; } | { type: 'isNotNull'; column: ColumnDef<any, any>; } | { type: 'like'; column: ColumnDef<any, any>; pattern: string; } | { type: 'glob'; column: ColumnDef<any, any>; pattern: string; } | { type: 'between'; column: ColumnDef<any, any>; low: unknown; high: unknown; } | { type: 'and'; conditions: Condition[]; } | { type: 'or'; conditions: Condition[]; }; /** * Check if a column equals a value, or if two columns are equal. * * @example * // Value comparison * where(eq(users.name, "Alice")) * * // Column-to-column comparison * where(eq(orders.userId, users.id)) */ export declare function eq<T>(column: ColumnDef<T, any>, value: T): Condition; export declare function eq<T>(left: ColumnDef<T, any>, right: ColumnDef<T, any>): Condition; /** * Combine conditions with AND. * * @example * where(and(eq(users.name, "Alice"), eq(users.active, true))) */ export declare function and(...conditions: Condition[]): Condition; /** * Combine conditions with OR. * * @example * where(or(eq(users.role, "admin"), eq(users.role, "moderator"))) */ export declare function or(...conditions: Condition[]): Condition; /** * Check if a column's value is in the given array. * * @example * where(isIn(users.id, ["u1", "u2", "u3"])) */ export declare function isIn<T>(column: ColumnDef<T, any>, values: T[]): Condition; /** * Check if a column's value is not in the given array. * * @example * where(isNotIn(users.id, ["u4", "u5"])) */ export declare function isNotIn<T>(column: ColumnDef<T, any>, values: T[]): Condition; /** * Check if a column is NULL. * * @example * where(isNull(users.deletedAt)) */ export declare function isNull(column: ColumnDef<any, any>): Condition; /** * Check if a column is NOT NULL. * * @example * where(isNotNull(users.name)) */ export declare function isNotNull(column: ColumnDef<any, any>): Condition; /** * Pattern match using SQL `LIKE`. Use `%` for any sequence of characters, `_` for a single character. * * @example * where(like(users.name, "A%")) */ export declare function like(column: ColumnDef<any, any>, pattern: string): Condition; /** * Pattern match using SQL `GLOB`. Use `*` for any sequence of characters, `?` for a single character. * * @example * where(glob(users.name, "A*")) */ export declare function glob(column: ColumnDef<any, any>, pattern: string): Condition; /** * Check if a column's value is between `low` and `high` (inclusive). * * @example * where(between(users.age, 18, 65)) */ export declare function between<T>(column: ColumnDef<T, any>, low: T, high: T): Condition; /** * Check if a column's value is greater than a value. * * @example * where(gt(users.age, 18)) */ export declare function gt<T>(column: ColumnDef<T, any>, value: T): Condition; /** * Check if a column's value is greater than or equal to a value. * * @example * where(gte(users.age, 18)) */ export declare function gte<T>(column: ColumnDef<T, any>, value: T): Condition; /** * Check if a column's value is less than a value. * * @example * where(lt(users.age, 65)) */ export declare function lt<T>(column: ColumnDef<T, any>, value: T): Condition; /** * Check if a column's value is less than or equal to a value. * * @example * where(lte(users.age, 65)) */ export declare function lte<T>(column: ColumnDef<T, any>, value: T): Condition; /** * Check if a column's value is not equal to a value. * * @example * where(neq(users.id, "u1")) */ export declare function neq<T>(column: ColumnDef<T, any>, value: T): Condition; export declare function compileCondition(cond: Condition, params: unknown[]): string; export declare function compileConditions(conditions: Condition[], params: unknown[]): string;