-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathtypes.d.ts
More file actions
221 lines (195 loc) · 6.2 KB
/
types.d.ts
File metadata and controls
221 lines (195 loc) · 6.2 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* 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 "sql" {
type SQLDialects =
| "mssql"
| "mysql"
| "oracle"
| "postgres"
| "sqlite"
;
interface OrderByValueNode {}
interface Named<Name extends string> {
name?: Name;
}
interface ColumnDefinition<Name extends string, Type> extends Named<Name> {
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 extends string, Row> {
name: Name;
schema: string;
columns: {[CName in keyof Row]: ColumnDefinition<CName, Row[CName]>};
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<T> extends Executable {
where(...nodes:any[]):Query<T>
delete():ModifyingQuery
select(star: Column<void, void>): Query<T>;
select<N1 extends string, T1>(n1: Column<N1, T1>):Query<{[N in N1]: T1}>;
select<N1 extends string, T1, N2 extends string, T2>(
n1: Column<N1, T1>,
n2: Column<N2, T2>):Query<{[N in N1]: T1} & {[N in N2]: T2}>
select<N1 extends string, T1, N2 extends string, T2, N3 extends string, T3>(
n1: Column<N1, T1>,
n2: Column<N2, T2>,
n3: Column<N3, T3>):Query<{[N in N1]: T1} & {[N in N2]: T2} & {[N in N3]: T3}>
select<U>(...nodesOrTables:any[]):Query<U>
}
interface Query<T> extends Executable, Queryable<T> {
resultType: T;
from(table:TableNode):Query<T>
from(statement:string):Query<T>
update(o:{[key: string]: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>
}
interface SubQuery<T> {
select<Name>(node:Column<Name, T>):SubQuery<T>
select(...nodes: any[]):SubQuery<T>
where(...nodes:any[]):SubQuery<T>
from(table:TableNode):SubQuery<T>
from(statement:string):SubQuery<T>
group(...nodes:any[]):SubQuery<T>
order(criteria:OrderByValueNode):SubQuery<T>
exists():BinaryNode
notExists(): BinaryNode;
notExists(subQuery:SubQuery<any>):BinaryNode
}
interface ModifyingQuery extends Executable {
returning<U>(...nodes:any[]):Query<U>
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<T> = {
[Name in keyof T]: Column<Name, T[Name]>
}
type Table<Name extends string, T> = TableNode & Queryable<T> & Named<Name> & Columns<T> & {
getName(): string;
getSchema(): string;
literal(statement: string): any;
create():CreateQuery
drop():DropQuery
as<OtherName extends string>(name:OtherName):Table<OtherName, T>
update(o: Partial<T>):ModifyingQuery
insert(row:T):ModifyingQuery
insert(rows:T[]):ModifyingQuery
select():Query<T>
select<U>(...nodes:any[]):Query<U>
from<U>(table:TableNode):Query<U>
from<U>(statement:string):Query<U>
star({prefix: string}?):Column<void, void>
subQuery<U>():SubQuery<U>
columns:Column<void, void>[]
sql: SQL;
alter():AlterQuery<T>;
indexes(): IndexQuery;
}
interface AlterQuery<T> extends Executable {
addColumn(column:Column<any, any>): AlterQuery<T>;
addColumn(name: string, options:string): AlterQuery<T>;
dropColumn(column: Column<any, any>|string): AlterQuery<T>;
renameColumn(column: Column<any, any>, newColumn: Column<any, any>):AlterQuery<T>;
renameColumn(column: Column<any, any>, newName: string):AlterQuery<T>;
renameColumn(name: string, newName: string):AlterQuery<T>;
rename(newName: string): AlterQuery<T>
}
interface IndexQuery {
create(): IndexCreationQuery;
create(indexName: string): IndexCreationQuery;
drop(indexName: string): Executable;
drop(...columns: Column<any, any>[]): Executable
}
interface IndexCreationQuery extends Executable {
unique(): IndexCreationQuery;
using(name: string): IndexCreationQuery;
on(...columns: (Column<any, any>|OrderByValueNode)[]): IndexCreationQuery;
withParser(parserName: string): IndexCreationQuery;
fulltext(): IndexCreationQuery;
spatial(): IndexCreationQuery;
}
interface SQL {
functions: {
LOWER<Name>(c:Column<Name, string>):Column<Name, string>
}
}
interface BinaryNode {
and(node:BinaryNode):BinaryNode
or(node:BinaryNode):BinaryNode
}
interface Column<Name, T> {
name: Name
in(arr:T[]):BinaryNode
in(subQuery:SubQuery<T>):BinaryNode
notIn(arr:T[]):BinaryNode
equals(node: T|Column<any, T>):BinaryNode
notEquals(node: T|Column<any, T>):BinaryNode
gte(node: T|Column<any, T>):BinaryNode
lte(node: T|Column<any, T>):BinaryNode
gt(node:T|Column<any, T>):BinaryNode
lt(node: T|Column<any, T>):BinaryNode
like(str:string):BinaryNode
multiply:{
(node:Column<any, T>):Column<any, T>
(n:number):Column<any, number> //todo check column names
}
isNull():BinaryNode
isNotNull():BinaryNode
//todo check column names
sum():Column<any, number>
count():Column<any, number>
count(name:string):Column<any, number>
distinct():Column<Name, T>
as<OtherName>(name:OtherName):Column<OtherName, T>
ascending:OrderByValueNode
descending:OrderByValueNode
asc:OrderByValueNode
desc:OrderByValueNode
}
function define<Name extends string, T>(map:TableDefinition<Name, T>): Table<Name, T>;
function setDialect(dialect: SQLDialects): void;
}