Sto creando un'applicazione nel nodo js usando mongodb. Sto usando mangusta come ODM. Il problema è che ho molte funzioni statiche nel modello di mangusta che sono collegate allo schema.
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;
C'è un modo per scrivere queste funzioni in un altro file (o modulo) e importarle e collegarle allo schema. Grazie.
risposte:
0 per risposta № 1Non l'ho provato da solo, ma forse solo un gruppoinsieme le statiche in un file e assegnandole alla proprietà statica nello schema? Inoltre, potresti usare query builder per interrogare il tuo db invece di creare statiche molto specifiche.