// Type definitions for bookshelfjs v0.13.0 // Project: http://bookshelfjs.org/ // Definitions by: Andrew Schurman , Vesa Poikajärvi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.2 import Knex = require('knex'); import knex = require('knex'); import BlueBird = require('bluebird'); import Lodash = require('lodash'); import createError = require('create-error'); interface Bookshelf extends Bookshelf.Events { VERSION: string; knex: knex; Model: typeof Bookshelf.Model; Collection: typeof Bookshelf.Collection; plugin(name: string | string[] | Function, options?: any): Bookshelf; transaction(callback: (transaction: knex.Transaction) => PromiseLike): BlueBird; } declare function Bookshelf(knex: knex): Bookshelf; declare namespace Bookshelf { type SortOrder = 'ASC'|'asc'|'DESC'|'desc'; abstract class Events { on(event?: string, callback?: EventFunction, context?: any): void; off(event?: string): void; trigger(event?: string, ...args: any[]): void; triggerThen(name: string, ...args: any[]): BlueBird; once(event: string, callback: EventFunction, context?: any): void; } interface IModelBase { /** Should be declared as a getter instead of a plain property. */ hasTimestamps?: boolean | string[]; /** Should be declared as a getter instead of a plain property. Should be required, but cannot have abstract properties yet. */ tableName?: string; } interface ModelBase> extends IModelBase { } abstract class ModelBase> extends Events> { /** If overriding, must use a getter instead of a plain property. */ idAttribute: string; // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/base/model.js#L178 // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/base/model.js#L213 id: any; // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/base/model.js#L28 attributes: any; constructor(attributes?: any, options?: ModelOptions); clear(): T; clone(): T; escape(attribute: string): string; format(attributes: any): any; get(attribute: string): any; has(attribute: string): boolean; hasChanged(attribute?: string): boolean; isNew(): boolean; parse(response: Object): Object; previousAttributes(): any; previous(attribute: string): any; related>(relation: string): R | Collection; serialize(options?: SerializeOptions): any; set(attribute?: { [key: string]: any }, options?: SetOptions): T; set(attribute: string, value?: any, options?: SetOptions): T; timestamp(options?: TimestampOptions): any; toJSON(options?: SerializeOptions): any; unset(attribute: string): T; // lodash methods invert(): R; keys(): string[]; omit(predicate?: Lodash.ObjectIterator, thisArg?: any): R; omit(...attributes: string[]): R; pairs(): any[][]; pick(predicate?: Lodash.ObjectIterator, thisArg?: any): R; pick(...attributes: string[]): R; values(): any[]; } interface ModelSubclass { new(): Model; } class Model> extends ModelBase { static collection>(models?: T[], options?: CollectionOptions): Collection; static count(column?: string, options?: SyncOptions): BlueBird; /** @deprecated use Typescript classes */ static extend>(prototypeProperties?: any, classProperties?: any): Function; // should return a type static fetchAll>(): BlueBird>; /** @deprecated should use `new` objects instead. */ static forge(attributes?: any, options?: ModelOptions): T; static where(properties: { [key: string]: any }): T; static where(key: string, operatorOrValue: string | number | boolean, valueIfOperator?: string | string[] | number | number[] | boolean): T; belongsTo>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R; belongsToMany>(target: { new (...args: any[]): R }, table?: string, foreignKey?: string, otherKey?: string, foreignKeyTarget?: string, otherKeyTarget?: string): Collection; count(column?: string, options?: SyncOptions): BlueBird; destroy(options?: DestroyOptions): BlueBird; fetch(options?: FetchOptions): BlueBird; fetchAll(options?: FetchAllOptions): BlueBird>; hasMany>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): Collection; hasOne>(target: { new (...args: any[]): R }, foreignKey?: string, foreignKeyTarget?: string): R; load(relations: string | string[], options?: LoadOptions): BlueBird; morphMany>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): Collection; morphOne>(target: { new (...args: any[]): R }, name?: string, columnNames?: string[], morphValue?: string): R; morphTo(name: string, columnNames?: string[], ...target: ModelSubclass[]): T; morphTo(name: string, ...target: ModelSubclass[]): T; orderBy(column: string, order?: SortOrder): T; // Declaration order matters otherwise TypeScript gets confused between query() and query(...query: string[]) query(): Knex.QueryBuilder; query(callback: (qb: Knex.QueryBuilder) => void): T; query(...query: string[]): T; query(query: { [key: string]: any }): T; refresh(options?: FetchOptions): BlueBird; resetQuery(): T; save(key?: string, val?: any, options?: SaveOptions): BlueBird; save(attrs?: { [key: string]: any }, options?: SaveOptions): BlueBird; through>(interim: ModelSubclass, throughForeignKey?: string, otherKey?: string): R; where(properties: { [key: string]: any }): T; where(key: string, operatorOrValue: string | number | boolean, valueIfOperator?: string | string[] | number | number[] | boolean): T; // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/errors.js // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/model.js#L1280 static NotFoundError: createError.Error; static NoRowsUpdatedError: createError.Error; static NoRowsDeletedError: createError.Error; } abstract class CollectionBase> extends Events { // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/base/collection.js#L573 length: number; // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/base/collection.js#L21 constructor(models?: T[], options?: CollectionOptions); add(models: T[] | { [key: string]: any }[], options?: CollectionAddOptions): Collection; at(index: number): T; clone(): Collection; fetch(options?: CollectionFetchOptions): BlueBird>; findWhere(match: { [key: string]: any }): T; get(id: any): T; invokeThen(name: string, ...args: any[]): BlueBird; parse(response: any): any; pluck(attribute: string): any[]; pop(): void; push(model: any): Collection; reduceThen(iterator: (prev: R, cur: T, idx: number, array: T[]) => R, initialValue: R, context: any): BlueBird; remove(model: T, options?: EventOptions): T; remove(model: T[], options?: EventOptions): T[]; reset(model: any[], options?: CollectionAddOptions): T[]; serialize(options?: SerializeOptions): any[]; set(models: T[] | { [key: string]: any }[], options?: CollectionSetOptions): Collection; shift(options?: EventOptions): void; slice(begin?: number, end?: number): void; toJSON(options?: SerializeOptions): any[]; unshift(model: any, options?: CollectionAddOptions): void; where(match: { [key: string]: any }, firstOnly: boolean): T | Collection; // lodash methods all(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): boolean; all(predicate?: R): boolean; any(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): boolean; any(predicate?: R): boolean; chain(): Lodash.LoDashExplicitObjectWrapper; collect(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T[]; collect(predicate?: R): T[]; contains(value: any, fromIndex?: number): boolean; countBy(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): Lodash.Dictionary; countBy(predicate?: R): Lodash.Dictionary; detect(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T; detect(predicate?: R): T; difference(...values: T[]): T[]; drop(n?: number): T[]; each(callback?: Lodash.ListIterator, thisArg?: any): Lodash.List; each(callback?: Lodash.DictionaryIterator, thisArg?: any): Lodash.Dictionary; each(callback?: Lodash.ObjectIterator, thisArg?: any): T; every(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): boolean; every(predicate?: R): boolean; filter(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T[]; filter(predicate?: R): T[]; find(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T; find(predicate?: R): T; first(): T; foldl(callback?: Lodash.MemoIterator, accumulator?: R, thisArg?: any): R; foldr(callback?: Lodash.MemoIterator, accumulator?: R, thisArg?: any): R; forEach(callback?: Lodash.ListIterator, thisArg?: any): Lodash.List; forEach(callback?: Lodash.DictionaryIterator, thisArg?: any): Lodash.Dictionary; forEach(callback?: Lodash.ObjectIterator, thisArg?: any): T; groupBy(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): Lodash.Dictionary; groupBy(predicate?: R): Lodash.Dictionary; head(): T; include(value: any, fromIndex?: number): boolean; indexOf(value: any, fromIndex?: number): number; initial(): T[]; inject(callback?: Lodash.MemoIterator, accumulator?: R, thisArg?: any): R; invoke(methodName: string | Function, ...args: any[]): any; isEmpty(): boolean; keys(): string[]; last(): T; lastIndexOf(value: any, fromIndex?: number): number; // See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1ec3d51/lodash/lodash-3.10.d.ts#L7119 // See https://github.com/Microsoft/TypeScript/blob/v1.8.10/lib/lib.core.es7.d.ts#L1122 map(predicate?: Lodash.ListIterator | string, thisArg?: any): U[]; map(predicate?: Lodash.DictionaryIterator | string, thisArg?: any): U[]; map(predicate?: string): U[]; max(predicate?: Lodash.ListIterator | string, thisArg?: any): T; max(predicate?: R): T; min(predicate?: Lodash.ListIterator | string, thisArg?: any): T; min(predicate?: R): T; reduce(callback?: Lodash.MemoIterator, accumulator?: R, thisArg?: any): R; reduceRight(callback?: Lodash.MemoIterator, accumulator?: R, thisArg?: any): R; reject(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T[]; reject(predicate?: R): T[]; rest(): T[]; select(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T[]; select(predicate?: R): T[]; shuffle(): T[]; size(): number; some(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): boolean; some(predicate?: R): boolean; sortBy(predicate?: Lodash.ListIterator | Lodash.DictionaryIterator | string, thisArg?: any): T[]; sortBy(predicate?: R): T[]; tail(): T[]; take(n?: number): T[]; toArray(): T[]; without(...values: any[]): T[]; } class Collection> extends CollectionBase { /** @deprecated use Typescript classes */ static extend(prototypeProperties?: any, classProperties?: any): Function; /** @deprecated should use `new` objects instead. */ static forge(attributes?: any, options?: ModelOptions): T; attach(ids: any | any[], options?: SyncOptions): BlueBird>; count(column?: string, options?: SyncOptions): BlueBird; create(model: { [key: string]: any }, options?: CollectionCreateOptions): BlueBird; detach(ids: any[], options?: SyncOptions): BlueBird; detach(options?: SyncOptions): BlueBird; fetchOne(options?: CollectionFetchOneOptions): BlueBird; load(relations: string | string[], options?: SyncOptions): BlueBird>; orderBy(column: string, order?: SortOrder): Collection; // Declaration order matters otherwise TypeScript gets confused between query() and query(...query: string[]) query(): Knex.QueryBuilder; query(callback: (qb: Knex.QueryBuilder) => void): Collection; query(...query: string[]): Collection; query(query: { [key: string]: any }): Collection; resetQuery(): Collection; through>(interim: ModelSubclass, throughForeignKey?: string, otherKey?: string): Collection; updatePivot(attributes: any, options?: PivotOptions): BlueBird; withPivot(columns: string[]): Collection; // See https://github.com/tgriesser/bookshelf/blob/0.9.4/src/collection.js#L389 static EmptyError: createError.Error; } interface ModelOptions { tableName?: string; hasTimestamps?: boolean; parse?: boolean; } interface LoadOptions extends SyncOptions { withRelated: (string | WithRelatedQuery)[]; } interface FetchOptions extends SyncOptions { require?: boolean; columns?: string | string[]; withRelated?: (string | WithRelatedQuery)[]; } interface WithRelatedQuery { [index: string]: (query: Knex.QueryBuilder) => Knex.QueryBuilder; } interface FetchAllOptions extends FetchOptions { } interface SaveOptions extends SyncOptions { method?: string; defaults?: string; patch?: boolean; require?: boolean; } interface DestroyOptions extends SyncOptions { require?: boolean; } interface SerializeOptions { shallow?: boolean; omitPivot?: boolean; } interface SetOptions { unset?: boolean; } interface TimestampOptions { method?: string; } interface SyncOptions { transacting?: Knex.Transaction; debug?: boolean; withSchema?: string; } interface CollectionOptions { comparator?: boolean | string | ((a: T, b: T) => number); } interface CollectionAddOptions extends EventOptions { at?: number; merge?: boolean; } interface CollectionFetchOptions { require?: boolean; withRelated?: string | string[]; } interface CollectionFetchOneOptions { require?: boolean; columns?: string | string[]; } interface CollectionSetOptions extends EventOptions { add?: boolean; remove?: boolean; merge?: boolean; } interface PivotOptions { query?: Function | any; require?: boolean; } interface EventOptions { silent?: boolean; } interface EventFunction { (model: T, attrs: any, options: any): BlueBird | void; } interface CollectionCreateOptions extends ModelOptions, SyncOptions, CollectionAddOptions, SaveOptions { } } export = Bookshelf;