在mongoose中组织静态函数

我使用mongodb在节点js中构build应用程序。 我正在使用mongoose作为ODM。 问题是我有很多静态函数在mongoose模型附加到模式。

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; var userSchema = new Schema({ profile: { firstName: { type: String }, lastName: { type: String } }, auth:{ username:{ type: String }, password:{ type: String } }, account:{ status:{ type: Boolean, default: false } } }); userSchema.statics.function1 = function(params, callback){ //some operation } userSchema.statics.function2 = function(params, callback){ //some operation } userSchema.statics.function3 = function(params, callback){ //some operation } userSchema.statics.function4 = function(params, callback){ //some operation } //.... upto 50 to 70 static functions var User = mongoose.model('User', userSchema); module.exports = User; 

有没有办法在另一个文件(或模块)中写入这些函数,并将其导入并附加到模式。 谢谢。

我还没有自己尝试过,但也许只是将静态文件组合在一个文件中,并将其分配给模式中的静态属性? 此外,也许你可以使用查询生成器来查询你的数据库,而不是创build非常具体的静态。