/** * This is an adaptation of https://github.com/doxout/anydb-sql/blob/4e4c0ff4a7f2efb7f820baaafea1f624f1ae0399/d.ts/anydb-sql.d.ts * Whole project is MIT licensed, so, we can use it. We also feed back any * improvements, questions, concerns. */ declare module "node-sql-2" { type SQLDialects = | "mssql" | "mysql" | "oracle" | "postgres" | "sqlite" ; interface OrderByValueNode {} interface Named { name?: Name; } interface ColumnDefinition extends Named { jsType?: Type; dataType: string; primaryKey?: boolean; references?: { table:string; column: string; onDelete?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; onUpdate?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; }; notNull?: boolean; unique?: boolean; defaultValue?: Type; } interface TableDefinition { name: Name; schema: string; columns: {[CName in keyof Row]: ColumnDefinition}; dialect?: SQLDialects; isTemporary?: boolean; foreignKeys?: { table: string, columns: (keyof Row)[], refColumns: string[], onDelete?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; onUpdate?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; } } interface QueryLike { values: any[] text:string } interface Executable { toQuery():QueryLike; } interface Queryable extends Executable { where(...nodes:any[]):Query delete():ModifyingQuery select(star: Column): Query; select(n1: Column):Query<{[N in N1]: T1}>; select( n1: Column, n2: Column):Query<{[N in N1]: T1} & {[N in N2]: T2}> select( n1: Column, n2: Column, n3: Column):Query<{[N in N1]: T1} & {[N in N2]: T2} & {[N in N3]: T3}> select(...nodesOrTables:any[]):Query } interface Query extends Executable, Queryable { resultType: T; from(table:TableNode):Query from(statement:string):Query update(o:{[key: string]:any}):ModifyingQuery update(o:{}):ModifyingQuery group(...nodes:any[]):Query order(...criteria:OrderByValueNode[]):Query limit(l:number):Query offset(o:number):Query } interface SubQuery { select(node:Column):SubQuery select(...nodes: any[]):SubQuery where(...nodes:any[]):SubQuery from(table:TableNode):SubQuery from(statement:string):SubQuery group(...nodes:any[]):SubQuery order(criteria:OrderByValueNode):SubQuery exists():BinaryNode notExists(): BinaryNode; notExists(subQuery:SubQuery):BinaryNode } interface ModifyingQuery extends Executable { returning(...nodes:any[]):Query where(...nodes:any[]):ModifyingQuery } interface TableNode { join(table:TableNode):JoinTableNode leftJoin(table:TableNode):JoinTableNode } interface JoinTableNode extends TableNode { on(filter:BinaryNode):TableNode on(filter:string):TableNode } interface CreateQuery extends Executable { ifNotExists():Executable } interface DropQuery extends Executable { ifExists():Executable } type Columns = { [Name in keyof T]: Column } type Table = TableNode & Queryable & Named & Columns & { getName(): string; getSchema(): string; literal(statement: string): any; create():CreateQuery drop():DropQuery as(name:OtherName):Table update(o: Partial):ModifyingQuery insert(row:T):ModifyingQuery insert(rows:T[]):ModifyingQuery select():Query select(...nodes:any[]):Query from(table:TableNode):Query from(statement:string):Query star():Column subQuery():SubQuery columns:Column[] sql: SQL; alter():AlterQuery; indexes(): IndexQuery; } interface AlterQuery extends Executable { addColumn(column:Column): AlterQuery; addColumn(name: string, options:string): AlterQuery; dropColumn(column: Column|string): AlterQuery; renameColumn(column: Column, newColumn: Column):AlterQuery; renameColumn(column: Column, newName: string):AlterQuery; renameColumn(name: string, newName: string):AlterQuery; rename(newName: string): AlterQuery } interface IndexQuery { create(): IndexCreationQuery; create(indexName: string): IndexCreationQuery; drop(indexName: string): Executable; drop(...columns: Column[]): Executable } interface IndexCreationQuery extends Executable { unique(): IndexCreationQuery; using(name: string): IndexCreationQuery; on(...columns: (Column|OrderByValueNode)[]): IndexCreationQuery; withParser(parserName: string): IndexCreationQuery; fulltext(): IndexCreationQuery; spatial(): IndexCreationQuery; } interface SQL { functions: { LOWER(c:Column):Column } array(arr:T[]):BinaryNode } interface BinaryNode { and(node:BinaryNode):BinaryNode or(node:BinaryNode):BinaryNode } interface Column { name: Name in(arr:T[]):BinaryNode in(subQuery:SubQuery):BinaryNode notIn(arr:T[]):BinaryNode equals(node: T|Column):BinaryNode notEquals(node: T|Column):BinaryNode gte(node: T|Column):BinaryNode lte(node: T|Column):BinaryNode gt(node:T|Column):BinaryNode lt(node: T|Column):BinaryNode like(str:string):BinaryNode multiply:{ (node:Column):Column (n:number):Column //todo check column names } isNull():BinaryNode isNotNull():BinaryNode //todo check column names sum():Column count():Column count(name:string):Column distinct():Column as(name:OtherName):Column ascending:OrderByValueNode descending:OrderByValueNode asc:OrderByValueNode desc:OrderByValueNode existsKeyElement(node:BinaryNode):BinaryNode existsAllKeyElements(node:BinaryNode):BinaryNode } function define(map:TableDefinition): Table; function setDialect(dialect: SQLDialects): void; }