forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added definitions for anydb-sql and anydb-sql-migrations
- Loading branch information
1 parent
35989a3
commit f3aecb4
Showing
4 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path="../anydb-sql/anydb-sql.d.ts" /> | ||
/// <reference path="anydb-sql-migrations" /> | ||
import anydbsql = require('anydb-sql'); | ||
import { Table, Column } from 'anydb-sql' | ||
import migrator = require('anydb-sql-migrations'); | ||
|
||
function do_not_run() { | ||
|
||
var db = anydbsql({ | ||
url: 'postgres://user:pass@host:port/database', | ||
connections: { min: 2, max: 20 } | ||
}); | ||
|
||
migrator | ||
.create(db, '/path/to/migrations/dir') | ||
.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Type definitions for anydb-sql-migrations | ||
// Project: https://github.com/spion/anydb-sql-migrations | ||
// Definitions by: Gorgi Kosev <https://github.com/spion> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
|
||
/// <reference path="../bluebird/bluebird.d.ts" /> | ||
/// <reference path="../anydb-sql/anydb-sql.d.ts" /> | ||
|
||
declare module "anydb-sql-migrations" { | ||
import Promise = require('bluebird'); | ||
import { Column, Table, Transaction, AnydbSql } from 'anydb-sql'; | ||
export interface Migration { | ||
version: string; | ||
} | ||
export interface MigrationsTable extends Table<Migration> { | ||
version: Column<string>; | ||
} | ||
export interface MigFn { | ||
(tx: Transaction): Promise<any>; | ||
} | ||
export interface MigrationTask { | ||
up: MigFn; | ||
down: MigFn; | ||
name: string; | ||
} | ||
export function create(db: AnydbSql, tasks: any): { | ||
run: () => Promise<any>; | ||
migrateTo: (target?: string) => Promise<any>; | ||
check: (f: (m: { | ||
type: string; | ||
items: MigrationTask[]; | ||
}) => any) => Promise<any>; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/// <reference path="anydb-sql.d.ts" /> | ||
import anydbsql = require('anydb-sql'); | ||
import { Table, Column } from 'anydb-sql' | ||
|
||
function do_not_run() { | ||
|
||
var db = anydbsql({ | ||
url: 'postgres://user:pass@host:port/database', | ||
connections: { min: 2, max: 20 } | ||
}); | ||
|
||
// Table Post | ||
|
||
interface Post { | ||
content: string; | ||
userId: string; | ||
date: string; | ||
} | ||
|
||
interface PostTable extends Table<Post> { | ||
content: Column<string>; | ||
userId: Column<string>; | ||
date: Column<string>; | ||
} | ||
|
||
var post = <PostTable>db.define<Post>({ | ||
name: 'posts', | ||
columns: { | ||
content: {}, | ||
userId: {}, | ||
date: {} | ||
} | ||
}); | ||
|
||
// Table User | ||
|
||
interface User { | ||
id: string; | ||
email: string; | ||
password: string; | ||
name: string; | ||
} | ||
|
||
interface UserTable extends Table<User> { | ||
id: Column<string>; | ||
email: Column<string>; | ||
password: Column<string>; | ||
name: Column<string>; | ||
} | ||
|
||
var user = <UserTable>db.define<User>({ | ||
name: 'users', | ||
columns: { | ||
id: { primaryKey: true }, | ||
email: {}, | ||
password: {}, | ||
name: {}, | ||
date: {} | ||
}, | ||
has: { | ||
posts: { from: 'posts', many: true }, | ||
group: { from: 'groups'} | ||
} | ||
}); | ||
|
||
user.select(user.name, post.content) | ||
.from(user.join(post).on(user.id.equals(post.userId))) | ||
.where(post.date.gt('123')) | ||
.all() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |