@devbro/sql-generator
Version:
generic sql generator
102 lines (96 loc) • 4.81 kB
text/typescript
import { Blueprint } from './Blueprint.mjs';
import { C as CompiledSql, s as selectType, j as joinType, e as whereType, a as whereOp, b as whereOpColumn, d as whereNull, P as Parameter, h as havingType, c as whereRaw, J as JoinCondition } from './types-C_aDrXJN.mjs';
import { SchemaGrammar } from './SchemaGrammar.mjs';
declare class Schema {
private readonly connection;
private readonly grammar;
constructor(connection: Connection | null, grammar: SchemaGrammar);
createTable(tableName: string, structMethod: (blueprint: Blueprint) => void): Promise<void>;
dropTable(tableName: string): Promise<void>;
tables(): Promise<any>;
tableExists(table_name: string): Promise<boolean>;
dropTableIfExists(tableName: string): Promise<void>;
}
declare abstract class QueryGrammar {
sqlParts: string[];
toSql(query: Query): CompiledSql;
compileSelect(selects: selectType[]): CompiledSql;
compileTable(tableName: string): CompiledSql;
compileJoin(joins: joinType[]): CompiledSql;
compileWhere(wheres: whereType[]): CompiledSql;
compileWhereOperation(w: whereOp): CompiledSql;
compileWhereOperationColumn(w: whereOpColumn): CompiledSql;
compileOrderBy(orderBy: string[]): CompiledSql;
compileLimit(limit: number | null): CompiledSql;
compileOffset(offset: number | null): CompiledSql;
abstract getVariablePlaceholder(): string;
compileWhereNull(w: whereNull): CompiledSql;
compileInsert(query: Query, data: Record<string, Parameter>): CompiledSql;
abstract compileInsertGetId(query: Query, data: Record<string, Parameter>, options: {
primaryKey: string[];
}): CompiledSql;
compileUpdate(query: Query, data: Record<string, Parameter>): CompiledSql;
compileDelete(query: Query): CompiledSql;
compileUpsert(query: Query, data: Record<string, Parameter>, conflictFields: string[], updateFields: string[]): CompiledSql;
compileGroupBy(groupBy: string[]): CompiledSql;
compileHaving(having: havingType[]): CompiledSql;
compileHavingOperation(w: whereOp): CompiledSql;
compileHavingRaw(w: whereRaw): CompiledSql;
}
type QueryParts = {
select: selectType[];
table: string;
join: joinType[];
where: whereType[];
groupBy: string[];
having: havingType[];
orderBy: string[];
limit: number | null;
offset: number | null;
};
declare class Query {
private readonly connection;
private readonly grammar;
allowedOperations: string[];
parts: QueryParts;
constructor(connection: Connection | null, grammar: QueryGrammar);
table(tableName: string): this;
whereOp(column: string, operation: (typeof this.allowedOperations)[number], value: Parameter, joinCondition?: JoinCondition, negateCondition?: boolean): this;
whereColumn(column1: string, operation: (typeof this.allowedOperations)[number], column2: string, joinCondition?: JoinCondition, negateCondition?: boolean): this;
whereNull(column: string, joinCondition?: JoinCondition, negateCondition?: boolean): this;
clearWhere(): this;
select(selects: selectType[]): this;
groupBy(columns: string[]): this;
havingOp(column: string, operation: (typeof this.allowedOperations)[number], value: Parameter, joinCondition?: JoinCondition, negateCondition?: boolean): this;
havingRaw(sql: string, bindings: Parameter[], joinCondition?: JoinCondition, negateCondition?: boolean): this;
orderBy(column: string, direction?: 'asc' | 'desc'): this;
limit(limit: number): this;
offset(offset: number): this;
toSql(): CompiledSql;
get(): Promise<any>;
getCursor(): Promise<any>;
getConnection(): Connection | null;
insert(data: Record<string, Parameter>): Promise<any>;
insertGetId(data: Record<string, Parameter>, options?: {
primaryKey: string[];
}): Promise<any>;
update(data: Record<string, Parameter>): Promise<any>;
upsert(data: Record<string, Parameter>, uniqueColumns: string[], updateColumns: string[]): Promise<any>;
delete(): Promise<any>;
innerJoin(table: string, condtions: whereType[]): this;
leftJoin(table: string, condtions: whereType[]): this;
rightJoin(table: string, condtions: whereType[]): this;
fullJoin(table: string, condtions: whereType[]): this;
}
declare abstract class Connection {
abstract connect(): Promise<boolean>;
abstract runQuery(sql: CompiledSql): Promise<any>;
abstract runCursor(sql: CompiledSql): Promise<any>;
abstract disconnect(): Promise<boolean>;
abstract getQuery(): Query;
abstract getSchema(): Schema;
abstract beginTransaction(): Promise<void>;
abstract commit(): Promise<void>;
abstract rollback(): Promise<void>;
}
export { Connection as C, type QueryParts as Q, Schema as S, Query as a, QueryGrammar as b };