Merge pull request #27874 from mernxl/mongoose

FixType(mongoose): fix issues around undefined(optional) plugin options
This commit is contained in:
Ron Buckton
2018-08-09 17:23:21 -07:00
committed by GitHub
5 changed files with 28 additions and 16 deletions

View File

@@ -151,7 +151,8 @@ declare module "mongoose" {
* @param fn plugin callback
* @param opts optional options
*/
export function plugin<T>(fn: Function, opts?: T): typeof mongoose;
export function plugin(fn: Function): typeof mongoose;
export function plugin<T>(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<T>(plugin: (schema: Schema, options?: T) => void, opts?: T): this;
plugin(plugin: (schema: Schema) => void): this;
plugin<T>(plugin: (schema: Schema, options: T) => void, opts: T): this;
/**
* Defines a post hook for the document

View File

@@ -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<any>(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<any>(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)

View File

@@ -14,7 +14,8 @@ declare module "mongoose" {
function model<T extends Document>(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model<T>;
function modelNames(): string[];
function plugin<T>(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose;
function plugin(plugin: (schema: Schema) => void): Mongoose;
function plugin<T>(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<T extends Document>(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model<T>;
modelNames(): string[];
plugin<T>(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose;
plugin(plugin: (schema: Schema) => void): Mongoose;
plugin<T>(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<T>(plugin: (schema: Schema, options?: T) => void, options?: T): Schema;
plugin(plugin: (schema: Schema) => void): Schema;
plugin<T>(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;

View File

@@ -173,7 +173,8 @@ declare module "mongoose" {
* @param fn plugin callback
* @param opts optional options
*/
export function plugin<T>(fn: Function, opts?: T): typeof mongoose;
export function plugin(fn: Function): typeof mongoose;
export function plugin<T>(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<T>(plugin: (schema: Schema, options?: T) => void, opts?: T): this;
plugin(plugin: (schema: Schema) => void): this;
plugin<T>(plugin: (schema: Schema, options: T) => void, opts: T): this;
/**
* Defines a post hook for the document

View File

@@ -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 {