UNPKG

@iarayan/ch-orm

Version:

A Developer-First ClickHouse ORM with Powerful CLI Tools

475 lines 15.7 kB
import { Connection } from "../connection/Connection"; import { QueryOptions, QueryResult } from "../types/connection"; import { Raw } from "./Raw"; /** * Type for where clause conditions */ type WhereCondition = { column: string | Raw; operator: string; value: any; boolean: "AND" | "OR"; not?: boolean; }; /** * Type for order by clauses */ type OrderByClause = { column: string | Raw; direction: "ASC" | "DESC"; }; /** * Type for join clauses */ type JoinClause = { table: string; type: "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS" | "ANY" | "ALL"; conditions: { first: string | Raw; operator: string; second: string | Raw; boolean: "AND" | "OR"; }[]; }; /** * Query builder class for constructing SQL queries with a fluent interface * Provides an Eloquent-like query building experience */ export declare class QueryBuilder { /** * Database connection instance */ protected connection: Connection; /** * Table name to query */ protected tableName: string; /** * Query type (SELECT, INSERT, UPDATE, DELETE) */ protected queryType: "SELECT" | "INSERT" | "UPDATE" | "DELETE"; /** * Columns to select */ protected columns: (string | Raw)[]; /** * Where conditions */ protected wheres: WhereCondition[]; /** * Having conditions */ protected havings: WhereCondition[]; /** * Order by clauses */ protected orders: OrderByClause[]; /** * Group by columns */ protected groups: (string | Raw)[]; /** * Join clauses */ protected joins: JoinClause[]; /** * Limit value */ protected limitValue: number | null; /** * Offset value */ protected offsetValue: number | null; /** * FINAL modifier flag */ protected finalFlag: boolean; /** * Sample rate value */ protected sampleRate: number | null; /** * Values for INSERT */ protected insertValues: Record<string, any>[]; /** * Values for UPDATE */ protected updateValues: Record<string, any>; /** * WITH clause expressions */ protected withExpressions: { name: string; query: QueryBuilder | Raw; }[]; /** * Create a new QueryBuilder instance * @param connection - ClickHouse connection * @param table - Table name (optional) */ constructor(connection: Connection, table?: string); /** * Set the table to query * @param table - Table name * @returns QueryBuilder instance for chaining */ from(table: string): this; /** * Set the table to query * @param table - Table name * @returns QueryBuilder instance for chaining */ table(table: string): this; /** * Add a FINAL modifier to the query * @returns QueryBuilder instance for chaining */ final(): this; /** * Add a SAMPLE modifier to the query * @param rate - Sample rate (0.0 to 1.0) * @returns QueryBuilder instance for chaining */ sample(rate: number): this; /** * Set the columns to select * @param columns - Column names or Raw expressions * @returns QueryBuilder instance for chaining */ select(...columns: (string | Raw)[]): this; /** * Add a where clause * @param column - Column name or Raw expression * @param operator - Comparison operator or value if operator is omitted * @param value - Value to compare (optional if operator is actually the value) * @returns QueryBuilder instance for chaining */ where(column: string | Raw | Record<string, any>, operator?: string | any, value?: any): this; /** * Add an OR where clause * @param column - Column name or Raw expression * @param operator - Comparison operator or value if operator is omitted * @param value - Value to compare (optional if operator is actually the value) * @returns QueryBuilder instance for chaining */ orWhere(column: string | Raw | Record<string, any>, operator?: string | any, value?: any): this; /** * Add a where not clause * @param column - Column name or Raw expression * @param operator - Comparison operator or value if operator is omitted * @param value - Value to compare (optional if operator is actually the value) * @returns QueryBuilder instance for chaining */ whereNot(column: string | Raw, operator?: string | any, value?: any): this; /** * Add a where in clause * @param column - Column name or Raw expression * @param values - Array of values to check against * @returns QueryBuilder instance for chaining */ whereIn(column: string | Raw, values: any[]): this; /** * Add an or where in clause * @param column - Column name or Raw expression * @param values - Array of values to check against * @returns QueryBuilder instance for chaining */ orWhereIn(column: string | Raw, values: any[]): this; /** * Add a where not in clause * @param column - Column name or Raw expression * @param values - Array of values to check against * @returns QueryBuilder instance for chaining */ whereNotIn(column: string | Raw, values: any[]): this; /** * Add a where between clause * @param column - Column name or Raw expression * @param values - Array of two values [min, max] * @returns QueryBuilder instance for chaining */ whereBetween(column: string | Raw, values: [any, any]): this; /** * Add a where not between clause * @param column - Column name or Raw expression * @param values - Array of two values [min, max] * @returns QueryBuilder instance for chaining */ whereNotBetween(column: string | Raw, values: [any, any]): this; /** * Add a where null clause * @param column - Column name or Raw expression * @returns QueryBuilder instance for chaining */ whereNull(column: string | Raw): this; /** * Add a where not null clause * @param column - Column name or Raw expression * @returns QueryBuilder instance for chaining */ whereNotNull(column: string | Raw): this; /** * Add a raw where clause * @param sql - Raw SQL for where clause * @param bindings - Parameter bindings for the SQL * @returns QueryBuilder instance for chaining */ whereRaw(sql: string, bindings?: any[]): this; /** * Add a raw or where clause * @param sql - Raw SQL for where clause * @param bindings - Parameter bindings for the SQL * @returns QueryBuilder instance for chaining */ orWhereRaw(sql: string, bindings?: any[]): this; /** * Add an inner join clause * @param table - Table to join * @param first - First column or a callback function * @param operator - Comparison operator * @param second - Second column * @returns QueryBuilder instance for chaining */ join(table: string, first: string | Raw | ((join: JoinClause) => void), operator?: string, second?: string | Raw): this; /** * Add a left join clause * @param table - Table to join * @param first - First column or a callback function * @param operator - Comparison operator * @param second - Second column * @returns QueryBuilder instance for chaining */ leftJoin(table: string, first: string | Raw | ((join: JoinClause) => void), operator?: string, second?: string | Raw): this; /** * Add a right join clause * @param table - Table to join * @param first - First column or a callback function * @param operator - Comparison operator * @param second - Second column * @returns QueryBuilder instance for chaining */ rightJoin(table: string, first: string | Raw | ((join: JoinClause) => void), operator?: string, second?: string | Raw): this; /** * Add a full join clause * @param table - Table to join * @param first - First column or a callback function * @param operator - Comparison operator * @param second - Second column * @returns QueryBuilder instance for chaining */ fullJoin(table: string, first: string | Raw | ((join: JoinClause) => void), operator?: string, second?: string | Raw): this; /** * Add a cross join clause * @param table - Table to join * @returns QueryBuilder instance for chaining */ crossJoin(table: string): this; /** * Helper method for adding join clauses of different types * @param type - Join type * @param table - Table to join * @param first - First column or a callback function * @param operator - Comparison operator * @param second - Second column * @returns QueryBuilder instance for chaining */ private joinWithType; /** * Add a group by clause * @param columns - Columns to group by * @returns QueryBuilder instance for chaining */ groupBy(...columns: (string | Raw)[]): this; /** * Add a having clause * @param column - Column name or Raw expression * @param operator - Comparison operator or value if operator is omitted * @param value - Value to compare (optional if operator is actually the value) * @returns QueryBuilder instance for chaining */ having(column: string | Raw, operator?: string | any, value?: any): this; /** * Add an or having clause * @param column - Column name or Raw expression * @param operator - Comparison operator or value if operator is omitted * @param value - Value to compare (optional if operator is actually the value) * @returns QueryBuilder instance for chaining */ orHaving(column: string | Raw, operator?: string | any, value?: any): this; /** * Add an order by clause * @param column - Column to order by * @param direction - Direction (ASC or DESC) * @returns QueryBuilder instance for chaining */ orderBy(column: string | Raw, direction?: "ASC" | "DESC"): this; /** * Add an order by desc clause * @param column - Column to order by * @returns QueryBuilder instance for chaining */ orderByDesc(column: string | Raw): this; /** * Set the limit value * @param value - Limit value * @returns QueryBuilder instance for chaining */ limit(value: number): this; /** * Set the offset value * @param value - Offset value * @returns QueryBuilder instance for chaining */ offset(value: number): this; /** * Add a with clause * @param name - CTE name * @param query - Query builder instance or Raw expression * @returns QueryBuilder instance for chaining */ with(name: string, query: QueryBuilder | Raw): this; /** * Set insert values * @param values - Values to insert * @returns QueryBuilder instance for chaining */ values(values: Record<string, any> | Record<string, any>[]): this; /** * Set update values * @param values - Values to update * @returns QueryBuilder instance for chaining */ updateQuery(values: Record<string, any>): this; /** * Set query type to DELETE * @returns QueryBuilder instance for chaining */ deleteQuery(): this; /** * Build the SQL query string * @returns SQL query string */ toSql(): string; /** * Build a SELECT query * @returns SQL query string */ protected buildSelectQuery(): string; /** * Build an INSERT query * @returns SQL query string */ protected buildInsertQuery(): string; /** * Build an UPDATE query * @returns SQL query string */ protected buildUpdateQuery(): string; /** * Build a DELETE query * @returns SQL query string */ protected buildDeleteQuery(): string; /** * Execute the query and get results * @param options - Query options * @returns Query result */ get<T = any>(options?: QueryOptions): Promise<T[]>; /** * Execute the query and get the first result * @param options - Query options * @returns Single result or null if not found */ first<T = any>(options?: QueryOptions): Promise<T | null>; /** * Execute the query and get a value from the first result * @param column - Column to retrieve * @param options - Query options * @returns Column value or null if not found */ value<T = any>(column: string, options?: QueryOptions): Promise<T | null>; /** * Execute the query and get an array of values from a single column * @param column - Column to retrieve * @param options - Query options * @returns Array of column values */ pluck<T = any>(column: string, options?: QueryOptions): Promise<T[]>; /** * Execute the query and get a count of the results * @param options - Query options * @returns Count of results */ count(options?: QueryOptions): Promise<number>; /** * Execute the query and determine if any results exist * @param options - Query options * @returns True if any results exist */ exists(options?: QueryOptions): Promise<boolean>; /** * Execute the query and determine if no results exist * @param options - Query options * @returns True if no results exist */ doesntExist(options?: QueryOptions): Promise<boolean>; /** * Execute the query and get the minimum value for a column * @param column - Column to get minimum for * @param options - Query options * @returns Minimum value */ min<T = any>(column: string, options?: QueryOptions): Promise<T | null>; /** * Execute the query and get the maximum value for a column * @param column - Column to get maximum for * @param options - Query options * @returns Maximum value */ max<T = any>(column: string, options?: QueryOptions): Promise<T | null>; /** * Execute the query and get the sum of values for a column * @param column - Column to sum * @param options - Query options * @returns Sum of values */ sum<T = number>(column: string, options?: QueryOptions): Promise<T | null>; /** * Execute the query and get the average of values for a column * @param column - Column to average * @param options - Query options * @returns Average of values */ avg<T = number>(column: string, options?: QueryOptions): Promise<T | null>; /** * Execute an insert query * @param values - Values to insert * @param options - Query options * @returns Query result */ insert(values: Record<string, any> | Record<string, any>[], options?: QueryOptions): Promise<QueryResult>; /** * Execute an update query * @param values - Values to update * @param options - Query options * @returns Query result */ update(values: Record<string, any>, options?: QueryOptions): Promise<QueryResult>; /** * Execute a delete query * @param options - Query options * @returns Query result */ delete(options?: QueryOptions): Promise<QueryResult>; /** * Execute a raw SQL query * @param sql - Raw SQL query to execute * @param options - Query options * @returns Query result data */ rawQuery<T = any>(sql: string, options?: QueryOptions): Promise<T[]>; } export {}; //# sourceMappingURL=QueryBuilder.d.ts.map