UNPKG

@jakub.knejzlik/ts-query

Version:

TypeScript implementation of SQL builder

168 lines (167 loc) 7.11 kB
import { Dayjs } from "dayjs"; import { Condition } from "./Condition"; import { CreateTableAsSelect } from "./CreateTableAsSelect"; import { CreateViewAsSelect } from "./CreateViewAsSelect"; import { ExpressionBase, ExpressionRawValue, ExpressionValue, RawExpression } from "./Expression"; import { ISQLFlavor } from "./Flavor"; import { AWSTimestreamFlavor } from "./flavors/aws-timestream"; import { MySQLFlavor } from "./flavors/mysql"; import { PostgresFlavor } from "./flavors/postgres"; import { SQLiteFlavor } from "./flavors/sqlite"; import { ICompilable, IMetadata, IQueryTarget, ISequelizable, ISequelizableOptions, ISerializable, ISerializableOptions, MetadataOperationType } from "./interfaces"; import { DeleteMutation, InsertMutation, UpdateMutation } from "./Mutation"; export type InputValue = ExpressionValue | Dayjs | null; export type TableSource = string | SelectQuery | CreateTableAsSelect | CreateViewAsSelect; export declare class Table implements ISequelizable, ISerializable { source: string | SelectQuery; alias?: string; constructor(source: TableSource, alias?: string); clone(): this; getTableName(): string | undefined; toSQL(flavor: ISQLFlavor, options?: ISequelizableOptions): string; toJSON(): any; static fromJSON(json: any): Table; serialize(): string; static deserialize(json: string): Table; } export declare const escapeTable: (table: TableSource, flavor: ISQLFlavor, options?: ISequelizableOptions) => string; export declare class QueryBase implements ISequelizable, IMetadata { protected _tables: Table[]; protected _joins?: Join[]; getOperationType(): MetadataOperationType; get table(): Table | undefined; get tables(): Table[]; from(table: TableSource, alias?: string): this; getTableNames(): string[]; /** * join function to join tables with all join types */ join(table: Table, condition?: Condition, type?: JoinType): this; innerJoin(table: Table, condition: Condition): this; leftJoin(table: Table, condition: Condition): this; rightJoin(table: Table, condition: Condition): this; fullJoin(table: Table, condition: Condition): this; crossJoin(table: Table, condition: Condition): this; clone(): this; toSQL(flavor: ISQLFlavor, options?: ISequelizableOptions): string; } export type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS"; export declare class Join { protected _type: JoinType; protected _table: Table; protected _condition?: Condition; constructor(table: Table, condition?: Condition, type?: JoinType); getType(): JoinType; getTable(): Table; getCondition(): Condition | undefined; clone(): this; getTableName(): string; toSQL(flavor: ISQLFlavor, options?: ISequelizableOptions): string; toJSON(): any; static fromJSON(json: any): Join; serialize(): string; static deserialize(json: string): Join; } export interface SelectField { name: ExpressionValue; alias?: string; } export interface OrderClause { field: ExpressionBase; direction: "ASC" | "DESC"; } type Order = OrderClause; declare class SelectBaseQuery extends QueryBase { protected _fields: SelectField[]; getFields(): SelectField[]; clone(): this; field(name: ExpressionValue, alias?: string): this; addField(name: ExpressionValue, alias?: string): this; addFields(fields: SelectField[]): this; removeFields(): this; fields(fields: SelectField[]): this; toSQL(flavor: ISQLFlavor, options?: ISequelizableOptions): string; } export declare enum UnionType { UNION = "UNION", UNION_ALL = "UNION ALL" } export declare class SelectQuery extends SelectBaseQuery implements ISerializable, ICompilable { protected _where: Condition[]; protected _having: Condition[]; protected _limit?: number; protected _offset?: number; protected _orderBy: Order[]; protected _groupBy: ExpressionBase[]; protected _unionQueries: { query: SelectQuery; type: UnionType; }[]; clone(): this; getWhere(): Condition[]; getHaving(): Condition[]; getJoins(): Join[]; getUnionQueries(): { query: SelectQuery; type: UnionType; }[]; where(condition: Condition | null): this; removeWhere(): this; having(condition: Condition): this; removeHaving(): this; getLimit(): number | undefined; clearLimit(): this; limit(limit: number): this; getOffset(): number | undefined; clearOffset(): this; offset(offset: number): SelectQuery; getOrderBy(): Order[]; orderBy(field: ExpressionValue, direction?: "ASC" | "DESC"): this; removeOrderBy(): this; getGroupBy(): ExpressionBase[]; groupBy(...field: ExpressionValue[]): this; removeGroupBy(): this; union(query: SelectQuery, type?: UnionType): this; getTableNames(): string[]; toSQL(flavor?: ISQLFlavor, options?: ISequelizableOptions, transformProcessed?: boolean): string; /** * Compile this query using the provided target. * @param target The query target to use for compilation * @returns The compiled output in the target's format */ compile<T>(target: IQueryTarget<T>): T; serialize(opts?: ISerializableOptions): string; toJSON(): any; static fromJSON(json: any): SelectQuery; } export declare const Query: { table: (source: TableSource, alias?: string) => Table; select: () => SelectQuery; stats: () => SelectQuery; delete: (from: string, alias?: string) => DeleteMutation; update: (table: string, alias?: string) => UpdateMutation; insert: (into: string) => InsertMutation; createTableAs: (table: string, select: SelectQuery) => CreateTableAsSelect; createOrReplaceTableAs: (table: string, select: SelectQuery) => CreateTableAsSelect; createTableIfNotExistsAs: (table: string, select: SelectQuery) => CreateTableAsSelect; createViewAs: (table: string, select: SelectQuery) => CreateViewAsSelect; createOrReplaceViewAs: (table: string, select: SelectQuery) => CreateViewAsSelect; values: (data: Record<string, any>[]) => SelectQuery; deserialize: (payload: string) => SelectQuery | CreateTableAsSelect | CreateViewAsSelect | DeleteMutation | InsertMutation | UpdateMutation; deserializeRaw: (json: string) => ExpressionBase | SelectQuery | CreateTableAsSelect | CreateViewAsSelect | DeleteMutation | InsertMutation | UpdateMutation; flavors: { mysql: MySQLFlavor; awsTimestream: AWSTimestreamFlavor; sqlite: SQLiteFlavor; postgres: PostgresFlavor; }; null: () => RawExpression; raw: (val: ExpressionRawValue) => RawExpression; expr: (val: InputValue) => ExpressionBase; exprValue: (val: InputValue) => import("./Expression").ValueExpression; value: (val: InputValue) => import("./Expression").ValueExpression; column: (col: ExpressionRawValue) => string; S: (literals: string | readonly string[]) => ExpressionBase; string: (literals: string | readonly string[]) => ExpressionBase; }; export { Query as Q };