mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-13 21:06:14 +08:00
UpdateType(mongoose): make plugin function generic (#27456)
This commit is contained in:
committed by
Andy
parent
fc53fb37ab
commit
62533f9619
4
types/mongoose/index.d.ts
vendored
4
types/mongoose/index.d.ts
vendored
@@ -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<T>(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<T>(plugin: (schema: Schema, options?: T) => void, opts?: T): this;
|
||||
|
||||
/**
|
||||
* Defines a post hook for the document
|
||||
|
||||
@@ -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<PluginOption>(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'})
|
||||
|
||||
mongoose.plugin<PluginOption>(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'})
|
||||
|
||||
|
||||
/*
|
||||
* section document.js
|
||||
* http://mongoosejs.com/docs/api.html#document-js
|
||||
|
||||
6
types/mongoose/v3/index.d.ts
vendored
6
types/mongoose/v3/index.d.ts
vendored
@@ -14,7 +14,7 @@ declare module "mongoose" {
|
||||
|
||||
function model<T extends Document>(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model<T>;
|
||||
function modelNames(): string[];
|
||||
function plugin(plugin: (schema: Schema, options?: Object) => void, options?: Object): 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 +33,7 @@ declare module "mongoose" {
|
||||
get(key: string): any;
|
||||
model<T extends Document>(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model<T>;
|
||||
modelNames(): string[];
|
||||
plugin(plugin: (schema: Schema, options?: Object) => void, options?: Object): Mongoose;
|
||||
plugin<T>(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<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;
|
||||
|
||||
@@ -387,4 +387,27 @@ var eq = id.equals(id2);
|
||||
|
||||
var kitty1 = new Kitty({});
|
||||
var kitty2 = new Kitty({});
|
||||
var kittyEq = kitty1._id.equals(kitty2._id);
|
||||
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<PluginOption>(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'})
|
||||
|
||||
mongoose.plugin<PluginOption>(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'})
|
||||
|
||||
4
types/mongoose/v4/index.d.ts
vendored
4
types/mongoose/v4/index.d.ts
vendored
@@ -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<T>(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<T>(plugin: (schema: Schema, options?: T) => void, opts?: T): this;
|
||||
|
||||
/**
|
||||
* Defines a post hook for the document
|
||||
|
||||
@@ -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<PluginOption>(AwesomeLoggerPlugin, {modelName: 'Executive', timestamp: 'yyyy/MM/dd'})
|
||||
|
||||
mongoose.plugin<PluginOption>(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';
|
||||
|
||||
Reference in New Issue
Block a user