Mongoose 的头文件 'plugin' 方法(通过 .methods 和 .statics 扩展)
Header files for Mongoose' 'plugin' method (extending via .methods and .statics)
我正在尝试为 script that extends my Mongoose-model by using the .plugin method. Current signature from Mongoose header-files 创建 Typescript 头文件:
export class Schema {
// ...
plugin(plugin: (schema: Schema, options?: Object) => void,
options?: Object): Schema;
// ...
}
来自 Mongoose-lib 的一些实际代码:
/**
* Registers a plugin for this schema.
*
* @param {Function} plugin callback
* @param {Object} [opts]
* @see plugins
* @api public
*/
Schema.prototype.plugin = function (fn, opts) {
fn(this, opts);
return this;
};
然后是我自己的模型,扩展plugin;
import passportLocalMongoose = require('passport-local-mongoose')
// ...
var userSchema = new mongoose.Schema({
email: String,
password: String,
});
// ...
userSchema.plugin(passportLocalMongoose, {
usernameField: "email",
usernameLowerCase: true
});
摘自 passport-local-mongoose 来源:
module.exports = function(schema, options) {
// ...
schema.methods.setPassword = function (password, cb) {
// ...
}
schema.statics.authenticate = function() {
// ...
}
// ...
}
我的主程序出现问题app.js
// ...
userSchema.authenticate() // <<< Typescript error, undefined
// OR
userSchemaInstance.setPassword(pass, cb) // <<< Typescript error, undefined
问题是 .authenticate 等是通过 .methods and .statics 动态添加的 ...
我找不到在打字稿头文件中对此建模的方法。
我尝试了泛型之类的东西,但我无法(动态)将提供的插件方法应用回原始模型。我还尝试 plugin
返回通用 T extends S & P
(其中 S 从第一个参数和 P = 插件本身扩展架构)。运气不好:-(
有什么解决这个问题的建议或例子吗?
在 passport-local-mongoose.d.ts
文件中声明接口:
declare module 'mongoose' {
// methods
export interface PassportLocalDocument extends Document {
setPassword(pass: string, cb: (err: any) => void);
}
// statics
export interface PassportLocalModel<T extends PassportLocalDocument> extends Model<T> {
authenticate(username: string, password: string, cb: (err: any) => void);
}
// plugin options
export interface PassportLocalOptions {
usernameField?: string;
usernameLowerCase?: boolean;
}
export interface PassportLocalSchema extends Schema {
plugin(
plugin: (schema: PassportLocalSchema, options?: PassportLocalOptions) => void,
options?: PassportLocalOptions): Schema;
}
export function model<T extends PassportLocalDocument>(
name: string,
schema?: PassportLocalSchema,
collection?: string,
skipInit?: boolean): PassportLocalModel<T>;
}
declare module 'passport-local-mongoose' {
import mongoose = require('mongoose');
var _: (schema: mongoose.Schema, Options?: Object) => void;
export = _;
}
在您的app.ts
中使用它:
import mongoose = require('mongoose');
import passportLocalMongoose = require('passport-local-mongoose');
interface UserDocument extends mongoose.PassportLocalDocument {
email: string,
password: string;
}
var userSchema = <mongoose.PassportLocalSchema>new mongoose.Schema({
email: String,
password: String
});
userSchema.plugin(passportLocalMongoose, {
usernameField: 'email',
usernameLowerCase: true
});
var User = mongoose.model<UserDocument>('User', userSchema);
User.authenticate(userName, pass, cb);
var user = new User();
user.setPassword(newPass, cb);
我正在尝试为 script that extends my Mongoose-model by using the .plugin method. Current signature from Mongoose header-files 创建 Typescript 头文件:
export class Schema {
// ...
plugin(plugin: (schema: Schema, options?: Object) => void,
options?: Object): Schema;
// ...
}
来自 Mongoose-lib 的一些实际代码:
/**
* Registers a plugin for this schema.
*
* @param {Function} plugin callback
* @param {Object} [opts]
* @see plugins
* @api public
*/
Schema.prototype.plugin = function (fn, opts) {
fn(this, opts);
return this;
};
然后是我自己的模型,扩展plugin;
import passportLocalMongoose = require('passport-local-mongoose')
// ...
var userSchema = new mongoose.Schema({
email: String,
password: String,
});
// ...
userSchema.plugin(passportLocalMongoose, {
usernameField: "email",
usernameLowerCase: true
});
摘自 passport-local-mongoose 来源:
module.exports = function(schema, options) {
// ...
schema.methods.setPassword = function (password, cb) {
// ...
}
schema.statics.authenticate = function() {
// ...
}
// ...
}
我的主程序出现问题app.js
// ...
userSchema.authenticate() // <<< Typescript error, undefined
// OR
userSchemaInstance.setPassword(pass, cb) // <<< Typescript error, undefined
问题是 .authenticate 等是通过 .methods and .statics 动态添加的 ...
我找不到在打字稿头文件中对此建模的方法。
我尝试了泛型之类的东西,但我无法(动态)将提供的插件方法应用回原始模型。我还尝试 plugin
返回通用 T extends S & P
(其中 S 从第一个参数和 P = 插件本身扩展架构)。运气不好:-(
有什么解决这个问题的建议或例子吗?
在 passport-local-mongoose.d.ts
文件中声明接口:
declare module 'mongoose' {
// methods
export interface PassportLocalDocument extends Document {
setPassword(pass: string, cb: (err: any) => void);
}
// statics
export interface PassportLocalModel<T extends PassportLocalDocument> extends Model<T> {
authenticate(username: string, password: string, cb: (err: any) => void);
}
// plugin options
export interface PassportLocalOptions {
usernameField?: string;
usernameLowerCase?: boolean;
}
export interface PassportLocalSchema extends Schema {
plugin(
plugin: (schema: PassportLocalSchema, options?: PassportLocalOptions) => void,
options?: PassportLocalOptions): Schema;
}
export function model<T extends PassportLocalDocument>(
name: string,
schema?: PassportLocalSchema,
collection?: string,
skipInit?: boolean): PassportLocalModel<T>;
}
declare module 'passport-local-mongoose' {
import mongoose = require('mongoose');
var _: (schema: mongoose.Schema, Options?: Object) => void;
export = _;
}
在您的app.ts
中使用它:
import mongoose = require('mongoose');
import passportLocalMongoose = require('passport-local-mongoose');
interface UserDocument extends mongoose.PassportLocalDocument {
email: string,
password: string;
}
var userSchema = <mongoose.PassportLocalSchema>new mongoose.Schema({
email: String,
password: String
});
userSchema.plugin(passportLocalMongoose, {
usernameField: 'email',
usernameLowerCase: true
});
var User = mongoose.model<UserDocument>('User', userSchema);
User.authenticate(userName, pass, cb);
var user = new User();
user.setPassword(newPass, cb);