UNPKG

quicklite

Version:

A lightweight ORM toolkit for SQLite in Node.js applications

194 lines (193 loc) 5.13 kB
import Database from 'better-sqlite3'; /** * Comparison operators for WHERE clauses */ export type ComparisonOperator = '=' | '<>' | '>' | '>=' | '<' | '<=' | 'LIKE' | 'IN' | 'NOT IN' | 'IS NULL' | 'IS NOT NULL' | 'BETWEEN'; /** * WHERE condition */ export interface WhereCondition { /** * Field name */ field: string; /** * Comparison operator */ operator: ComparisonOperator; /** * Comparison value */ value?: any; } /** * JOIN condition */ export interface JoinCondition { /** * Table to join */ table: string; /** * Join type */ type: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL'; /** * ON clause */ on: string; } /** * ORDER BY condition */ export interface OrderCondition { /** * Field to order by */ field: string; /** * Sort direction */ direction: 'ASC' | 'DESC'; } /** * SQL query builder for constructing complex queries */ export declare class QueryBuilder { private db; private tableName; private selectFields; private whereConditions; private orConditions; private joinConditions; private groupByFields; private havingConditions; private orderByConditions; private limitValue?; private offsetValue?; private params; /** * Creates a new QueryBuilder instance * @param db Database instance * @param tableName Table name */ constructor(db: Database.Database, tableName: string); /** * Sets the fields to select * @param fields Field names * @returns this instance for chaining */ select(...fields: string[]): QueryBuilder; /** * Adds a WHERE condition * @param field Field name * @param operator Comparison operator * @param value Comparison value * @returns this instance for chaining */ where(field: string, operator: ComparisonOperator, value?: any): QueryBuilder; /** * Adds an AND condition * @param field Field name * @param operator Comparison operator * @param value Comparison value * @returns this instance for chaining */ and(field: string, operator: ComparisonOperator, value?: any): QueryBuilder; /** * Adds an OR condition * @param conditions Callback for creating OR conditions * @returns this instance for chaining */ or(conditions: (qb: QueryBuilder) => void): QueryBuilder; /** * Adds a grouped AND condition with parentheses * @param conditions Callback for creating grouped conditions * @returns this instance for chaining */ andWhere(conditions: (qb: QueryBuilder) => void): QueryBuilder; /** * Adds a table join * @param table Table to join * @param type Join type * @param on ON clause * @returns this instance for chaining */ join(table: string, type: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL', on: string): QueryBuilder; /** * Adds an INNER JOIN * @param table Table to join * @param on ON clause * @returns this instance for chaining */ innerJoin(table: string, on: string): QueryBuilder; /** * Adds a LEFT JOIN * @param table Table to join * @param on ON clause * @returns this instance for chaining */ leftJoin(table: string, on: string): QueryBuilder; /** * Sets GROUP BY fields * @param fields Field names * @returns this instance for chaining */ groupBy(...fields: string[]): QueryBuilder; /** * Adds a HAVING condition * @param field Field name * @param operator Comparison operator * @param value Comparison value * @returns this instance for chaining */ having(field: string, operator: ComparisonOperator, value?: any): QueryBuilder; /** * Sets ORDER BY clause * @param field Field name * @param direction Sort direction * @returns this instance for chaining */ orderBy(field: string, direction?: 'ASC' | 'DESC'): QueryBuilder; /** * Sets LIMIT clause * @param limit Maximum number of results * @returns this instance for chaining */ limit(limit: number): QueryBuilder; /** * Sets OFFSET clause * @param offset Number of results to skip * @returns this instance for chaining */ offset(offset: number): QueryBuilder; /** * Builds a WHERE condition string from conditions * @param conditions WHERE conditions * @returns WHERE condition string */ private buildWhereConditionString; /** * Builds the complete SQL query * @returns SQL query and parameters */ build(): { sql: string; params: any[]; }; /** * Executes the query and returns all results * @returns Query results */ all<R = any>(): R[]; /** * Executes the query and returns the first result * @returns First result or null if none */ first<R = any>(): R | null; /** * Counts the number of matching records * @returns Record count */ count(): number; }