2017-07-19 85 views
2

我正在使用mongodb在節點js中構建應用程序。我使用貓鼬作爲ODM。 問題是我有很多靜態函數附加到模式的貓鼬模型。在貓鼬中組織靜態函數

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;

有沒有寫在另一個文件(或模塊)和進口這些功能,並將它們連接到架構的方式。 謝謝。

回答

0

我沒有嘗試過自己,但也許只是將靜態組合在一個文件中,並將它們分配給模式中的靜態屬性?此外,也許你可以使用查詢生成器來查詢你的數據庫,而不是創建非常具體的靜態。