@jakub.knejzlik/ts-query
Version:
TypeScript implementation of SQL builder
145 lines (144 loc) • 6.04 kB
TypeScript
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 { SQLiteFlavor } from "./flavors/sqlite";
import { IMetadata, ISequelizable, ISequelizableOptions, ISerializable, MetadataOperationType } from "./interfaces";
import { DeleteMutation, InsertMutation, UpdateMutation } from "./Mutation";
export type InputValue = ExpressionValue | Dayjs;
export type TableSource = string | SelectQuery;
export declare class Table implements ISequelizable, ISerializable {
source: TableSource;
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;
}
type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS";
declare class Join {
protected _type: JoinType;
protected _table: Table;
protected _condition?: Condition;
constructor(table: Table, condition?: Condition, type?: JoinType);
clone(): this;
getTableName(): string;
toSQL(flavor: ISQLFlavor, options?: ISequelizableOptions): string;
toJSON(): any;
static fromJSON(json: any): Join;
serialize(): string;
static deserialize(json: string): Join;
}
interface SelectField {
name: ExpressionValue;
alias?: string;
}
interface Order {
field: ExpressionBase;
direction: "ASC" | "DESC";
}
declare class SelectBaseQuery extends QueryBase {
protected _fields: 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 {
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;
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;
serialize(): string;
toJSON(): any;
static fromJSON(json: any): SelectQuery;
}
export declare const Query: {
table: (name: string, 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;
createViewAs: (table: string, select: SelectQuery) => CreateViewAsSelect;
createOrReplaceViewAs: (table: string, select: SelectQuery) => CreateViewAsSelect;
deserialize: (json: string) => SelectQuery | DeleteMutation | InsertMutation | UpdateMutation | CreateTableAsSelect | CreateViewAsSelect;
deserializeRaw: (json: string) => ExpressionBase | SelectQuery | DeleteMutation | InsertMutation | UpdateMutation | CreateTableAsSelect | CreateViewAsSelect;
flavors: {
mysql: MySQLFlavor;
awsTimestream: AWSTimestreamFlavor;
sqlite: SQLiteFlavor;
};
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 };