// Type definitions for db.js v0.14.0 // Project: https://github.com/aaronpowell/db.js/ // Definitions by: Chris Wrench // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module DbJs { interface ErrorListener { (err: Error): void; } interface OpenOptions { server: string; version: number; schema?: any; } interface DbJsStatic { open(options: OpenOptions): Promise; delete(dbName: string): Promise; cmp(key1: any, key2: any): number; } // Query API interface ExecutableQuery { execute(): Promise; } interface CountableQuery { count(): ExecutableQuery; } interface KeysQuery extends DescableQuery, ExecutableQuery, FilterableQuery, DistinctableQuery, MappableQuery { } interface KeyableQuery { keys(): KeysQuery; } interface FilterQuery extends KeyableQuery, ExecutableQuery, FilterableQuery, DescableQuery, DistinctableQuery, ModifiableQuery, LimitableQuery, MappableQuery { } interface FilterableQuery { filter(index: string, value: TValue): FilterQuery; filter(filter: (value: T) => boolean): FilterQuery; } interface DescQuery extends KeyableQuery, CountableQuery, ExecutableQuery, FilterableQuery, DescableQuery, ModifiableQuery, MappableQuery { } interface DescableQuery { desc(): DescQuery; } interface DistinctQuery extends KeyableQuery, ExecutableQuery, FilterableQuery, DescableQuery, ModifiableQuery, MappableQuery, CountableQuery { } interface DistinctableQuery { distinct(filter?: (value: T) => boolean): DistinctQuery; } interface ModifiableQuery { modify(filter: (value: T) => boolean): ExecutableQuery; modify(modifyObj: any): ExecutableQuery; } interface LimitableQuery { limit(n: any, m: any): ExecutableQuery; } interface MappableQuery { map(fn: (value: T) => TMap): Query; } interface Query extends Promise, KeyableQuery, ExecutableQuery, FilterableQuery, DescableQuery, DistinctableQuery, ModifiableQuery, LimitableQuery, MappableQuery, CountableQuery { } interface IndexQuery extends Query { only(...args: any[]): Query; bound(lowerBound: any, upperBound: any): Query; upperBound(upperBound: any): Query; lowerBound(lowerBound: any): Query; range(opts: any): Query; all(): Query; } interface KeyValuePair { key: TKey; item: TValue; } interface BaseServer { getIndexedDB(): IDBDatabase; close(): void; } interface IndexAccessibleServer { [store: string]: TypedObjectStoreServer; } interface ObjectStoreServer { add(table: string, entity: T): Promise; add(table: string, ...entities: T[]): Promise; add(table: string, entity: KeyValuePair): Promise>; add(table: string, ...entities: KeyValuePair[]): Promise[]>; update(table: string, entity: T): Promise; update(table: string, ...entities: T[]): Promise; update(table: string, entity: KeyValuePair): Promise>; update(table: string, ...entities: KeyValuePair[]): Promise[]>; remove(table: string, key: TKey): Promise; remove(table: string, ...keys: TKey[]): Promise; clear(table: string): Promise; get(table: string, key: any): Promise; query(table: string): IndexQuery; query(table: string, index: string): IndexQuery; count(): Promise; count(keyOrRange: any): Promise; count(table: string, key: any): Promise; addEventListener(type: 'abort', listener: (ev: Event) => any): void; addEventListener(type: 'versionchange', listener: (ev: Event) => any): void; addEventListener(type: 'error', listener: (err: Error) => any): void; addEventListener(type: string, listener: EventListener | ErrorListener): void; abort(listener: (ev: Event) => any): ObjectStoreServer; versionchange(listener: (ev: Event) => any): ObjectStoreServer; error(listener: (ev: Error) => any): ObjectStoreServer; } interface TypedObjectStoreServer { add(entity: T): Promise; add(...entities: T[]): Promise; add(entity: KeyValuePair): Promise>; add(...entities: KeyValuePair[]): Promise[]>; update(entity: T): Promise; update(...entities: T[]): Promise; update(entity: KeyValuePair): Promise>; update(...entities: KeyValuePair[]): Promise[]>; remove(key: TKey): Promise; remove(...keys: TKey[]): Promise; clear(): Promise; get(key: any): Promise; query(): IndexQuery; query(index: string): IndexQuery; count(key: any): Promise; } type Server = DbJs.IndexAccessibleServer & DbJs.ObjectStoreServer & DbJs.BaseServer; } declare module "db.js" { var db: DbJs.DbJsStatic; export = db; } declare var db: DbJs.DbJsStatic;