///
import BluebirdPromise = require("bluebird");
declare namespace Waterline {
type Adapter = Object;
/** The configuration for a Datastore (MongoDb, Postgres, etc.) */
interface DatastoreConfig {
adapter: string;
}
/** An DatastoreConfig thats been Initalized */
interface DatastoreConfigInstance extends DatastoreConfig {
identity: string;
}
/** Represents a Datastore (Connection) */
interface Datastore {
/** A datastore adapter (MySQL, MongoDB, Postgres, etc.) */
adapter: any;
/** A list of collections that have been initlized using this datastore */
collections: string[];
/** A instance of the datastore configuration */
config: DatastoreConfigInstance;
}
/** Waterline Configration */
interface Config {
/** A set of adapters to configure */
adapters: { [index: string]: Adapter };
/** A set of datastores to configure */
datastores: { [index: string]: DatastoreConfig };
}
type Ontology = {
collections: any;
/** A set of datastores */
datastores: { [index: string]: Datastore };
};
interface Waterline {
loadCollection(collection: CollectionClass): void;
registerModel(collection: CollectionClass): void;
initialize: (config: Config, cb: (err: Error, ontology: Ontology) => any) => any;
teardown(done?: Function): void;
collections: any;
}
interface CollectionClass {
(): Collection;
}
// used this comment https://github.com/balderdashy/waterline/issues/1154#issuecomment-167262575
export type LifecycleCallbacks = {
beforeValidate?:
| Array<{ (vaues: any, next: Function): void }>
| { (vaues: any, next: Function): void }
| undefined;
beforeCreate?:
| Array<{ (values: any, next: Function): void }>
| { (vaues: any, next: Function): void }
| undefined;
afterCreate?:
| Array<{ (newlyCreatedRecord: any, next: Function): void }>
| { (newlyCreatedRecord: any, next: Function): void }
| undefined;
beforeUpdate?:
| Array<{ (valuesToUpdate: any, next: Function): void }>
| { (valuesToUpdate: any, next: Function): void }
| undefined;
afterUpdate?:
| Array<{ (valuesToUpdate: any, next: Function): void }>
| { (valuesToUpdate: any, next: Function): void }
| undefined;
beforeDestroy?:
| Array<{ (criteria: any, next: Function): void }>
| { (valuesToUpdate: any, next: Function): void }
| undefined;
afterDestroy?:
| Array<{ (destroyedInstance: any, next: Function): void }>
| { (destroyedInstance: any, next: Function): void }
| undefined;
};
export type CollectionDefinition = LifecycleCallbacks & {
attributes?: Attributes | undefined;
connection?: string | undefined;
identity?: string | undefined;
tableName?: string | undefined;
migrate?: "alter" | "drop" | "safe" | undefined;
schema?: boolean | undefined;
types?: any;
datastore?: string | undefined;
primaryKey?: string | undefined;
dataEncryptionKeys?: any;
};
export type Collection = CollectionDefinition;
export type Attributes = { [index: string]: Attribute } & {
toJSON?: (() => string) | undefined;
toObject?: (() => any) | undefined;
};
export type FunctionAttribute = () => any;
// Data types https://github.com/balderdashy/waterline-docs/blob/master/models/data-types-attributes.md#data-types
export type AttributeType =
| "string"
| "text"
| "number"
| "integer"
| "float"
| "date"
| "time"
| "datetime"
| "boolean"
| "binary"
| "array"
| "json";
export type Attribute =
| string
| StringAttribute
| EmailAttribute
| IntegerAttribute
| NumberAttribute
| FloatAttribute
| DateAttribute
| TimeAttribute
| DatetimeAttribute
| BooleanAttribute
| BinaryAttribute
| ArrayAttribute
| JsonAttribute
| OneToOneAttribute
| OneToManyAttribute
| ManyToManyAttribute
| FunctionAttribute;
export type DefaultsToFn = () => T;
export type BaseAttribute = {
type?: string | undefined;
primaryKey?: boolean | undefined;
unique?: boolean | undefined;
required?: boolean | undefined;
enum?: T[] | undefined;
size?: number | undefined;
columnName?: string | undefined;
index?: boolean | undefined;
defaultsTo?: T | DefaultsToFn | undefined;
allowNull?: boolean | undefined;
validations?: AttributeValidations | undefined;
autoCreatedAt?: boolean | undefined;
autoUpdatedAt?: boolean | undefined;
};
export type StringAttribute = BaseAttribute & {
type: "string";
};
export type EmailAttribute = BaseAttribute & {
type: "email";
};
export type TextAttribute = BaseAttribute & {
type: "text";
};
export type IntegerAttribute = BaseAttribute & {
type: "integer";
autoIncrement?: boolean | undefined;
};
export type NumberAttribute = BaseAttribute & {
type: "number";
autoIncrement?: boolean | undefined;
};
export type FloatAttribute = BaseAttribute & {
type: "float";
};
export type DateAttribute = BaseAttribute & {
type: "date";
};
export type TimeAttribute = BaseAttribute & {
type: "time";
};
export type DatetimeAttribute = BaseAttribute & {
type: "datetime";
};
export type BooleanAttribute = BaseAttribute & {
type: "boolean";
};
export type BinaryAttribute = BaseAttribute & {
type: "binary";
};
export type ArrayAttribute = BaseAttribute & {
type: "array";
};
export type JsonAttribute = BaseAttribute & {
type: "json";
};
export type OneToOneAttribute = BaseAttribute & {
model: string;
};
export type OneToManyAttribute = BaseAttribute & {
collection: string;
via: string;
};
export type ManyToManyAttribute = BaseAttribute & {
collection: string;
via: string;
dominant?: boolean | undefined;
};
type AttributeValidationSyncFn = () => T;
type AttributeValidationAsyncFn = (cb: (value: T) => any) => void;
export type AttributeValidation = T | AttributeValidationSyncFn | AttributeValidationAsyncFn;
export interface AttributeValidations {
custom?: AttributeValidation | undefined;
isAfter?: AttributeValidation | undefined;
isBefore?: AttributeValidation | undefined;
isBoolean?: AttributeValidation | undefined;
isCreditCard?: AttributeValidation | undefined;
isEmail?: AttributeValidation | undefined;
isHexColor?: AttributeValidation | undefined;
isIn?: AttributeValidation | undefined;
isInteger?: AttributeValidation | undefined;
isIP?: AttributeValidation | undefined;
isNotEmptyString?: AttributeValidation | undefined;
isNotIn?: AttributeValidation | undefined;
isNumber?: AttributeValidation | undefined;
isString?: AttributeValidation | undefined;
isURL?: AttributeValidation | undefined;
isUUID?: AttributeValidation | undefined;
max?: AttributeValidation | undefined;
maxLength?: AttributeValidation | undefined;
min?: AttributeValidation | undefined;
minLength?: AttributeValidation | undefined;
regex?: AttributeValidation | undefined;
}
type WaterlinePromise = BluebirdPromise & {
exec(cb: (err: Error, result: T) => any): void;
};
type QueryBuilder = WaterlinePromise & {
where(condition: any): QueryBuilder;
limit(lim: number): QueryBuilder;
skip(num: number): QueryBuilder;
sort(
criteria: string | { [attribute: string]: string } | Array<{ [attribute: string]: string }>,
): QueryBuilder;
paginate(pagination?: { page: number; limit: number }): QueryBuilder;
populate(association: string): QueryBuilder;
populate(association: string, filter: any): QueryBuilder;
groupBy(attrOrExpr: string): QueryBuilder;
max(attribute: string): QueryBuilder;
min(attribute: string): QueryBuilder;
sum(attribute: string): QueryBuilder;
average(attribute: string): QueryBuilder;
meta(options: any): QueryBuilder;
};
type CRUDBuilder = WaterlinePromise & {
fetch(): CRUDBuilder;
};
type UpdateBuilder = CRUDBuilder & {
set(criteria: any): UpdateBuilder;
};
interface ModelInstance {
id?: number | string | undefined;
createdAt?: Date | undefined;
updatedAt?: Date | undefined;
toJSON(): any;
save(): WaterlinePromise;
}
export interface Callback {
(err: any, result: T): any;
}
export interface Model extends ModelInstance {
create(params: any, cb?: Callback): CRUDBuilder;
create(params: any[], cb?: Callback): CRUDBuilder;
createEach(params: any[], cb?: Callback): CRUDBuilder;
find(criteria?: any, cb?: Callback): QueryBuilder;
findOne(criteria?: any, cb?: Callback): QueryBuilder;
findOrCreate(criteria?: any, values?: any, cb?: Callback): QueryBuilder;
update(criteria: any, changes?: any, cb?: Callback): UpdateBuilder;
update(criteria: any, changes?: any[], cb?: Callback): UpdateBuilder;
updateOne(criteria: any, changes?: any, cb?: Callback, meta?: any): UpdateBuilder;
destroy(criteria: any, cb?: Callback): CRUDBuilder;
destroy(criteria: any[], cb?: Callback): CRUDBuilder;
destroyOne(criteria: any, cb?: Callback): CRUDBuilder;
count(criteria: any): WaterlinePromise;
count(criteria: any[]): WaterlinePromise;
query(sqlQuery: string, cb: Callback): void;
query(sqlQuery: string, data: any, cb: Callback): void;
native(cb: (err: Error, collection: any) => void): void;
stream(criteria: any, writeEnd: any): NodeJS.WritableStream | Error;
}
export interface StartOptions {
adapters: Record;
datastores: Record;
models: Record;
defaultModelSettings?: CollectionDefinition;
}
}
declare interface WaterlineStatic {
Model: {
extend: (params: Waterline.CollectionDefinition) => Waterline.CollectionClass;
};
Collection: {
extend: (params: Waterline.CollectionDefinition) => Waterline.CollectionClass;
};
new(): Waterline.Waterline;
start unknown>(
options: Waterline.StartOptions,
done: CB,
): CB;
stop unknown>(orm: Waterline.Waterline, done: CB): ReturnType;
}
declare var Waterline: WaterlineStatic;
export = Waterline;