diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index fbdad58c4b..5eea1625b5 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -151,7 +151,8 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: T): typeof mongoose; + export function plugin(fn: Function): typeof mongoose; + export function plugin(fn: Function, opts: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -655,7 +656,8 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; + plugin(plugin: (schema: Schema) => void): 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 cadc7d9a83..2fe1e1b12e 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -293,7 +293,7 @@ schema.method('name', cb).method({ }); schema.path('a', mongoose.Schema.Types.Buffer).path('a'); schema.pathType('m1').toLowerCase(); -schema.plugin(function (schema, opts) { +schema.plugin(function (schema: mongoose.Schema, opts?: any) { schema.get('path'); if (opts) { opts.hasOwnProperty(''); @@ -564,25 +564,25 @@ new mongoose.Schema({ } }); -(new mongoose.Schema({})).plugin(function (schema: any, options: any) { +(new mongoose.Schema({})).plugin(function (schema: mongoose.Schema, options: any) { schema.add({ lastMod: Date }) schema.pre('save', function (next: Function) { - this.lastMod = new Date + (this as any).lastMod = new Date next() }) if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}, { index: true }).plugin(function (schema: any, options: any) { +}, { index: true }).plugin(function (schema: mongoose.Schema, options: any) { schema.add({ lastMod: Date }) schema.pre('save', function (next: Function) { - this.lastMod = new Date + (this as any).lastMod = new Date next() }) if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}); +}, {index: true}); new mongoose.Schema({foo: String}, {strict: 'throw'}); @@ -593,6 +593,11 @@ export default function(schema: mongoose.Schema) { } // plugins +function MyPlugin(schema: mongoose.Schema, opts?: string) { +} +new mongoose.Schema({}) + .plugin(MyPlugin) + interface PluginOption { modelName: string; timestamp: string; @@ -602,7 +607,7 @@ function logger(modelName: string, timestamp: string) { // call special logger with options } -function AwesomeLoggerPlugin(schema: mongoose.Schema, options?: PluginOption) { +function AwesomeLoggerPlugin(schema: mongoose.Schema, options: PluginOption) { if (options) { schema.pre('save', function (next: Function) { logger(options.modelName, options.timestamp) diff --git a/types/mongoose/v3/index.d.ts b/types/mongoose/v3/index.d.ts index b38a20179c..40e254deda 100644 --- a/types/mongoose/v3/index.d.ts +++ b/types/mongoose/v3/index.d.ts @@ -14,7 +14,8 @@ declare module "mongoose" { function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; function modelNames(): string[]; - function plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; + function plugin(plugin: (schema: Schema) => void): 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 +34,8 @@ declare module "mongoose" { get(key: string): any; model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; modelNames(): string[]; - plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; + plugin(plugin: (schema: Schema) => void): Mongoose; + plugin(plugin: (schema: Schema, options: T) => void, options: T): Mongoose; set(key: string, value: any): void; mongo: any; @@ -273,7 +275,8 @@ declare module "mongoose" { path(path: string): any; path(path: string, constructor: any): Schema; pathType(path: string): string; - plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Schema; + plugin(plugin: (schema: Schema) => void): 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/v4/index.d.ts b/types/mongoose/v4/index.d.ts index 6dfd75308a..ee3cd4c045 100644 --- a/types/mongoose/v4/index.d.ts +++ b/types/mongoose/v4/index.d.ts @@ -173,7 +173,8 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: T): typeof mongoose; + export function plugin(fn: Function): typeof mongoose; + export function plugin(fn: Function, opts: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -678,7 +679,8 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; + plugin(plugin: (schema: Schema) => void): 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 ddf96848b9..b2d2a834fd 100644 --- a/types/mongoose/v4/mongoose-tests.ts +++ b/types/mongoose/v4/mongoose-tests.ts @@ -262,7 +262,7 @@ schema.method('name', cb).method({ }); schema.path('a', mongoose.Schema.Types.Buffer).path('a'); schema.pathType('m1').toLowerCase(); -schema.plugin(function (schema, opts) { +schema.plugin(function (schema: mongoose.Schema, opts?: any) { schema.get('path'); if (opts) { opts.hasOwnProperty(''); @@ -424,7 +424,7 @@ new mongoose.Schema({ if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}); +}, {index: true}); // plugins interface PluginOption {