|
| 1 | + |
| 2 | +/** |
| 3 | + * This is an adaptation of https://github.com/doxout/anydb-sql/blob/4e4c0ff4a7f2efb7f820baaafea1f624f1ae0399/d.ts/anydb-sql.d.ts |
| 4 | + * Whole project is MIT licensed, so, we can use it. We also feed back any |
| 5 | + * improvements, questions, concerns. |
| 6 | + */ |
| 7 | +declare module "sql" { |
| 8 | + |
| 9 | + interface OrderByValueNode {} |
| 10 | + |
| 11 | + interface Named<Name extends string> { |
| 12 | + name?: Name; |
| 13 | + } |
| 14 | + interface ColumnDefinition<Name extends string, Type> extends Named<Name> { |
| 15 | + jsType?: Type; |
| 16 | + dataType: string; |
| 17 | + primaryKey?: boolean; |
| 18 | + references?: { |
| 19 | + table:string; |
| 20 | + column: string; |
| 21 | + onDelete?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; |
| 22 | + onUpdate?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; |
| 23 | + }; |
| 24 | + notNull?: boolean; |
| 25 | + unique?: boolean; |
| 26 | + defaultValue?: Type; |
| 27 | + } |
| 28 | + |
| 29 | + interface TableDefinition<Name extends string, Row> { |
| 30 | + name: Name; |
| 31 | + schema: string; |
| 32 | + columns: {[CName in keyof Row]: ColumnDefinition<CName, Row[CName]>}; |
| 33 | + isTemporary?: boolean; |
| 34 | + foreignKeys?: { |
| 35 | + table: string, |
| 36 | + columns: (keyof Row)[], |
| 37 | + refColumns: string[], |
| 38 | + onDelete?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; |
| 39 | + onUpdate?: 'restrict' | 'cascade' | 'no action' | 'set null' | 'set default'; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + interface QueryLike { |
| 44 | + values: any[] |
| 45 | + text:string |
| 46 | + } |
| 47 | + |
| 48 | + interface Executable { |
| 49 | + toQuery():QueryLike; |
| 50 | + } |
| 51 | + |
| 52 | + interface Queryable<T> extends Executable { |
| 53 | + where(...nodes:any[]):Query<T> |
| 54 | + delete():ModifyingQuery |
| 55 | + select(star: Column<void, void>): Query<T>; |
| 56 | + select<N1 extends string, T1>(n1: Column<N1, T1>):Query<{[N in N1]: T1}>; |
| 57 | + select<N1 extends string, T1, N2 extends string, T2>( |
| 58 | + n1: Column<N1, T1>, |
| 59 | + n2: Column<N2, T2>):Query<{[N in N1]: T1} & {[N in N2]: T2}> |
| 60 | + select<N1 extends string, T1, N2 extends string, T2, N3 extends string, T3>( |
| 61 | + n1: Column<N1, T1>, |
| 62 | + n2: Column<N2, T2>, |
| 63 | + n3: Column<N3, T3>):Query<{[N in N1]: T1} & {[N in N2]: T2} & {[N in N3]: T3}> |
| 64 | + select<U>(...nodesOrTables:any[]):Query<U> |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + interface Query<T> extends Executable, Queryable<T> { |
| 69 | + resultType: T; |
| 70 | + |
| 71 | + from(table:TableNode):Query<T> |
| 72 | + from(statement:string):Query<T> |
| 73 | + update(o:{[key: string]:any}):ModifyingQuery |
| 74 | + update(o:{}):ModifyingQuery |
| 75 | + group(...nodes:any[]):Query<T> |
| 76 | + order(...criteria:OrderByValueNode[]):Query<T> |
| 77 | + limit(l:number):Query<T> |
| 78 | + offset(o:number):Query<T> |
| 79 | + } |
| 80 | + |
| 81 | + interface SubQuery<T> { |
| 82 | + select<Name>(node:Column<Name, T>):SubQuery<T> |
| 83 | + select(...nodes: any[]):SubQuery<T> |
| 84 | + where(...nodes:any[]):SubQuery<T> |
| 85 | + from(table:TableNode):SubQuery<T> |
| 86 | + from(statement:string):SubQuery<T> |
| 87 | + group(...nodes:any[]):SubQuery<T> |
| 88 | + order(criteria:OrderByValueNode):SubQuery<T> |
| 89 | + exists():BinaryNode |
| 90 | + notExists(): BinaryNode; |
| 91 | + notExists(subQuery:SubQuery<any>):BinaryNode |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + interface ModifyingQuery extends Executable { |
| 96 | + returning<U>(...nodes:any[]):Query<U> |
| 97 | + where(...nodes:any[]):ModifyingQuery |
| 98 | + } |
| 99 | + |
| 100 | + interface TableNode { |
| 101 | + join(table:TableNode):JoinTableNode |
| 102 | + leftJoin(table:TableNode):JoinTableNode |
| 103 | + } |
| 104 | + |
| 105 | + interface JoinTableNode extends TableNode { |
| 106 | + on(filter:BinaryNode):TableNode |
| 107 | + on(filter:string):TableNode |
| 108 | + } |
| 109 | + |
| 110 | + interface CreateQuery extends Executable { |
| 111 | + ifNotExists():Executable |
| 112 | + } |
| 113 | + interface DropQuery extends Executable { |
| 114 | + ifExists():Executable |
| 115 | + } |
| 116 | + |
| 117 | + type Columns<T> = { |
| 118 | + [Name in keyof T]: Column<Name, T[Name]> |
| 119 | + } |
| 120 | + type Table<Name extends string, T> = TableNode & Queryable<T> & Named<Name> & Columns<T> & { |
| 121 | + getName(): string; |
| 122 | + getSchema(): string; |
| 123 | + |
| 124 | + literal(statement: string): any; |
| 125 | + |
| 126 | + create():CreateQuery |
| 127 | + drop():DropQuery |
| 128 | + as<OtherName extends string>(name:OtherName):Table<OtherName, T> |
| 129 | + update(o: Partial<T>):ModifyingQuery |
| 130 | + insert(row:T):ModifyingQuery |
| 131 | + insert(rows:T[]):ModifyingQuery |
| 132 | + select():Query<T> |
| 133 | + select<U>(...nodes:any[]):Query<U> |
| 134 | + from<U>(table:TableNode):Query<U> |
| 135 | + from<U>(statement:string):Query<U> |
| 136 | + star():Column<void, void> |
| 137 | + subQuery<U>():SubQuery<U> |
| 138 | + columns:Column<void, void>[] |
| 139 | + sql: SQL; |
| 140 | + alter():AlterQuery<T>; |
| 141 | + indexes(): IndexQuery; |
| 142 | + } |
| 143 | + |
| 144 | + interface AlterQuery<T> extends Executable { |
| 145 | + addColumn(column:Column<any, any>): AlterQuery<T>; |
| 146 | + addColumn(name: string, options:string): AlterQuery<T>; |
| 147 | + dropColumn(column: Column<any, any>|string): AlterQuery<T>; |
| 148 | + renameColumn(column: Column<any, any>, newColumn: Column<any, any>):AlterQuery<T>; |
| 149 | + renameColumn(column: Column<any, any>, newName: string):AlterQuery<T>; |
| 150 | + renameColumn(name: string, newName: string):AlterQuery<T>; |
| 151 | + rename(newName: string): AlterQuery<T> |
| 152 | + } |
| 153 | + interface IndexQuery { |
| 154 | + create(): IndexCreationQuery; |
| 155 | + create(indexName: string): IndexCreationQuery; |
| 156 | + drop(indexName: string): Executable; |
| 157 | + drop(...columns: Column<any, any>[]): Executable |
| 158 | + } |
| 159 | + interface IndexCreationQuery extends Executable { |
| 160 | + unique(): IndexCreationQuery; |
| 161 | + using(name: string): IndexCreationQuery; |
| 162 | + on(...columns: (Column<any, any>|OrderByValueNode)[]): IndexCreationQuery; |
| 163 | + withParser(parserName: string): IndexCreationQuery; |
| 164 | + fulltext(): IndexCreationQuery; |
| 165 | + spatial(): IndexCreationQuery; |
| 166 | + } |
| 167 | + |
| 168 | + interface SQL { |
| 169 | + functions: { |
| 170 | + LOWER<Name>(c:Column<Name, string>):Column<Name, string> |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + interface BinaryNode { |
| 175 | + and(node:BinaryNode):BinaryNode |
| 176 | + or(node:BinaryNode):BinaryNode |
| 177 | + } |
| 178 | + |
| 179 | + interface Column<Name, T> { |
| 180 | + name: Name |
| 181 | + in(arr:T[]):BinaryNode |
| 182 | + in(subQuery:SubQuery<T>):BinaryNode |
| 183 | + notIn(arr:T[]):BinaryNode |
| 184 | + equals(node: T|Column<any, T>):BinaryNode |
| 185 | + notEquals(node: T|Column<any, T>):BinaryNode |
| 186 | + gte(node: T|Column<any, T>):BinaryNode |
| 187 | + lte(node: T|Column<any, T>):BinaryNode |
| 188 | + gt(node:T|Column<any, T>):BinaryNode |
| 189 | + lt(node: T|Column<any, T>):BinaryNode |
| 190 | + like(str:string):BinaryNode |
| 191 | + multiply:{ |
| 192 | + (node:Column<any, T>):Column<any, T> |
| 193 | + (n:number):Column<any, number> //todo check column names |
| 194 | + } |
| 195 | + isNull():BinaryNode |
| 196 | + isNotNull():BinaryNode |
| 197 | + //todo check column names |
| 198 | + sum():Column<any, number> |
| 199 | + count():Column<any, number> |
| 200 | + count(name:string):Column<any, number> |
| 201 | + distinct():Column<Name, T> |
| 202 | + as<OtherName>(name:OtherName):Column<OtherName, T> |
| 203 | + ascending:OrderByValueNode |
| 204 | + descending:OrderByValueNode |
| 205 | + asc:OrderByValueNode |
| 206 | + desc:OrderByValueNode |
| 207 | + } |
| 208 | + |
| 209 | + function define<Name extends string, T>(map:TableDefinition<Name, T>): Table<Name, T>; |
| 210 | + |
| 211 | +} |
0 commit comments