forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anydb-sql.d.ts
194 lines (171 loc) · 6.67 KB
/
anydb-sql.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Type definitions for anydb-sql
// Project: https://github.com/doxout/anydb-sql
// Definitions by: Gorgi Kosev <https://github.com/spion>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../bluebird/bluebird.d.ts" />
declare module "anydb-sql" {
import Promise = require('bluebird');
interface AnyDBPool extends anydbSQL.DatabaseConnection {
query:(text:string, values:any[], callback:(err:Error, result:any)=>void)=>void
begin:()=>anydbSQL.Transaction
close:(err:Error)=>void
}
interface Dictionary<T> { [key:string]:T; }
module anydbSQL {
export interface OrderByValueNode {}
export interface ColumnDefinition {
primaryKey?:boolean;
dataType?:string;
references?: {table:string; column: string}
notNull?:boolean
}
export interface TableDefinition {
name:string
columns:Dictionary<ColumnDefinition>
has?:Dictionary<{from:string; many?:boolean}>
}
export interface QueryLike {
query:string;
values: any[]
text:string
}
export interface DatabaseConnection {
queryAsync<T>(query:string, ...params:any[]):Promise<{rowCount:number;rows:T[]}>
queryAsync<T>(query:QueryLike):Promise<{rowCount:number;rows:T[]}>
}
export interface Transaction extends DatabaseConnection {
rollback():void
commitAsync():Promise<void>
}
export interface SubQuery<T> {
select(node:Column<T>):SubQuery<T>
where(...nodes:any[]):SubQuery<T>
from(table:TableNode):SubQuery<T>
group(...nodes:any[]):SubQuery<T>
order(criteria:OrderByValueNode):SubQuery<T>
notExists(subQuery:SubQuery<any>):SubQuery<T>
}
interface Executable<T> {
get():Promise<T>
getWithin(tx:DatabaseConnection):Promise<T>
exec():Promise<void>
all():Promise<T[]>
execWithin(tx:DatabaseConnection):Promise<void>
allWithin(tx:DatabaseConnection):Promise<T[]>
toQuery():QueryLike;
}
interface Queryable<T> {
where(...nodes:any[]):Query<T>
delete():ModifyingQuery
select<U>(...nodes:any[]):Query<U>
selectDeep<U>(table: Table<T>): Query<T>
selectDeep<U>(...nodesOrTables:any[]):Query<U>
}
export interface Query<T> extends Executable<T>, Queryable<T> {
from(table:TableNode):Query<T>
update(o:Dictionary<any>):ModifyingQuery
update(o:{}):ModifyingQuery
group(...nodes:any[]):Query<T>
order(...criteria:OrderByValueNode[]):Query<T>
limit(l:number):Query<T>
offset(o:number):Query<T>
}
export interface ModifyingQuery extends Executable<void> {
returning<U>(...nodes:any[]):Query<U>
where(...nodes:any[]):ModifyingQuery
}
export interface TableNode {
join(table:TableNode):JoinTableNode
leftJoin(table:TableNode):JoinTableNode
}
export interface JoinTableNode extends TableNode {
on(filter:BinaryNode):TableNode
on(filter:string):TableNode
}
interface CreateQuery extends Executable<void> {
ifNotExists():Executable<void>
}
interface DropQuery extends Executable<void> {
ifExists():Executable<void>
}
export interface Table<T> extends TableNode, Queryable<T> {
create():CreateQuery
drop():DropQuery
as(name:string):Table<T>
update(o:any):ModifyingQuery
insert(row:T):ModifyingQuery
insert(rows:T[]):ModifyingQuery
select():Query<T>
select<U>(...nodes:any[]):Query<U>
from<U>(table:TableNode):Query<U>
star():Column<any>
subQuery<U>():SubQuery<U>
eventEmitter:{emit:(type:string, ...args:any[])=>void
on:(eventName:string, handler:Function)=>void}
columns:Column<any>[]
sql: SQL;
alter():AlterQuery<T>
}
export interface AlterQuery<T> extends Executable<void> {
addColumn(column:Column<any>): AlterQuery<T>;
addColumn(name: string, options:string): AlterQuery<T>;
dropColumn(column: Column<any>): AlterQuery<T>;
renameColumn(column: Column<any>, newColumn: Column<any>):AlterQuery<T>;
renameColumn(column: Column<any>, newName: string):AlterQuery<T>;
renameColumn(name: string, newName: string):AlterQuery<T>;
rename(newName: string): AlterQuery<T>
}
export interface SQL {
functions: {
LOWER(c:Column<string>):Column<string>
}
}
export interface BinaryNode {
and(node:BinaryNode):BinaryNode
or(node:BinaryNode):BinaryNode
}
export interface Column<T> {
in(arr:T[]):BinaryNode
in(subQuery:SubQuery<T>):BinaryNode
notIn(arr:T[]):BinaryNode
equals(node:any):BinaryNode
notEquals(node:any):BinaryNode
gte(node:any):BinaryNode
lte(node:any):BinaryNode
gt(node:any):BinaryNode
lt(node:any):BinaryNode
like(str:string):BinaryNode
multiply:{
(node:Column<T>):Column<T>
(n:number):Column<number>
}
isNull():BinaryNode
isNotNull():BinaryNode
sum():Column<number>
count():Column<number>
count(name:string):Column<number>
distinct():Column<T>
as(name:string):Column<T>
ascending:OrderByValueNode
descending:OrderByValueNode
asc:OrderByValueNode
desc:OrderByValueNode
}
export interface AnydbSql extends DatabaseConnection {
define<T>(map:TableDefinition):Table<T>;
transaction<T>(fn:(tx:Transaction)=>Promise<T>):Promise<T>
allOf(...tables:Table<any>[]):any
models:Dictionary<Table<any>>
functions:{LOWER:(name:Column<string>)=>Column<string>
RTRIM:(name:Column<string>)=>Column<string>}
makeFunction(name:string):Function
begin():Transaction
open():void;
close():void;
getPool():AnyDBPool;
dialect():string;
}
}
function anydbSQL(config:Object):anydbSQL.AnydbSql;
export = anydbSQL;
}