diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 6933a916f2..1beed12c69 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -151,7 +151,7 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: any): typeof mongoose; + export function plugin(fn: Function, opts?: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -655,7 +655,7 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: any) => void, opts?: any): this; + plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; /** * Defines a post hook for the document diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index c9d9c9266a..cadc7d9a83 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -592,6 +592,30 @@ export default function(schema: mongoose.Schema) { }); } +// plugins +interface PluginOption { + modelName: string; + timestamp: string; +} + +function logger(modelName: string, timestamp: string) { + // call special logger with options +} + +function AwesomeLoggerPlugin(schema: mongoose.Schema, options?: PluginOption) { + if (options) { + schema.pre('save', function (next: Function) { + logger(options.modelName, options.timestamp) + }) + } +} + +new mongoose.Schema({}) + .plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) + +mongoose.plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) + + /* * section document.js * http://mongoosejs.com/docs/api.html#document-js diff --git a/types/mongoose/v3/index.d.ts b/types/mongoose/v3/index.d.ts index 805f3f1de2..b38a20179c 100644 --- a/types/mongoose/v3/index.d.ts +++ b/types/mongoose/v3/index.d.ts @@ -14,7 +14,7 @@ declare module "mongoose" { function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; function modelNames(): string[]; - function plugin(plugin: (schema: Schema, options?: Object) => void, options?: Object): Mongoose; + function plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; function get(key: string): any; function set(key: string, value: any): void; @@ -33,7 +33,7 @@ declare module "mongoose" { get(key: string): any; model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; modelNames(): string[]; - plugin(plugin: (schema: Schema, options?: Object) => void, options?: Object): Mongoose; + plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; set(key: string, value: any): void; mongo: any; @@ -273,7 +273,7 @@ declare module "mongoose" { path(path: string): any; path(path: string, constructor: any): Schema; pathType(path: string): string; - plugin(plugin: (schema: Schema, options?: Object) => void, options?: Object): Schema; + plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Schema; pre(method: string, fn: HookSyncCallback, errorCb?: HookErrorCallback): Schema; pre(method: string, isAsync: boolean, fn: HookAsyncCallback, errorCb?: HookErrorCallback): Schema; diff --git a/types/mongoose/v3/mongoose-tests.ts b/types/mongoose/v3/mongoose-tests.ts index ee56b9cc1b..4659348fd7 100644 --- a/types/mongoose/v3/mongoose-tests.ts +++ b/types/mongoose/v3/mongoose-tests.ts @@ -387,4 +387,27 @@ var eq = id.equals(id2); var kitty1 = new Kitty({}); var kitty2 = new Kitty({}); -var kittyEq = kitty1._id.equals(kitty2._id); \ No newline at end of file +var kittyEq = kitty1._id.equals(kitty2._id); + +// plugins +interface PluginOption { + modelName: string; + timestamp: string; +} + +function logger(modelName: string, timestamp: string) { + // call special logger with options +} + +function AwesomeLoggerPlugin(schema: mongoose.Schema, options?: PluginOption) { + if (options) { + schema.pre('save', function (next: Function) { + logger(options.modelName, options.timestamp) + }) + } +} + +new mongoose.Schema({}) + .plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) + +mongoose.plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) diff --git a/types/mongoose/v4/index.d.ts b/types/mongoose/v4/index.d.ts index a09aefa8e2..6dfd75308a 100644 --- a/types/mongoose/v4/index.d.ts +++ b/types/mongoose/v4/index.d.ts @@ -173,7 +173,7 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: any): typeof mongoose; + export function plugin(fn: Function, opts?: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -678,7 +678,7 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: any) => void, opts?: any): this; + plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; /** * Defines a post hook for the document diff --git a/types/mongoose/v4/mongoose-tests.ts b/types/mongoose/v4/mongoose-tests.ts index 82f92ec9ad..ddf96848b9 100644 --- a/types/mongoose/v4/mongoose-tests.ts +++ b/types/mongoose/v4/mongoose-tests.ts @@ -426,6 +426,29 @@ new mongoose.Schema({ } }); +// plugins +interface PluginOption { + modelName: string; + timestamp: string; +} + +function logger(modelName: string, timestamp: string) { + // call special logger with options +} + +function AwesomeLoggerPlugin(schema: mongoose.Schema, options?: PluginOption) { + if (options) { + schema.pre('save', function (next: Function) { + logger(options.modelName, options.timestamp) + }) + } +} + +new mongoose.Schema({}) + .plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) + +mongoose.plugin(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'}) + export default function(schema: mongoose.Schema) { schema.pre('init', function(this: mongoose.Document, next: (err?: Error) => void, data: any): void { data.name = 'Hello world';