[英]Mongoose discriminators, instantiate documents
We're using inherited schemas with Mongoose's discriminator functionality, which we implemented according to the documentation . 我们使用继承的模式和Mongoose的鉴别器功能,我们根据文档实现了这些功能。
Through our REST API we want to POST
documents of the different types and handle the logic with a common controller function. 通过我们的REST API,我们希望POST
不同类型的文档,并使用通用控制器功能处理逻辑。
Suppose, we have a REST method for creating a document, which can be either of super type GenericEvent
, or sub types ClickedLinkEvent
, or SignedUpEvent
(to follow the example from the docs linked above). 假设,我们有一个用于创建文档的REST方法,该方法可以是超类型GenericEvent
,也可以是子类型ClickedLinkEvent
或SignedUpEvent
(以跟随上面链接的文档中的示例)。 What I'm currently doing is something like: 我目前正在做的是:
var GenericEventModel = require('GenericEventModel');
var ClickedLinkEventModel = require('ClickedLinkEvent');
var SignedUpEventModel = require('SignedUpEvent');
// REST logic for creating a document of a specific type
module.exports.createEvent = function(req, res, next) {
var modelType = req.params.type; // as provided via REST parameter
var Model = getModel(modelType); // get the appropriate model type
new Model(req.body).save(next);
}
// TODO want to avoid this
function getMongooseModel(type) {
switch (modelType) {
case 'GenericEvent': return GenericEventModel;
case 'ClickedLinkEvent': return ClickedLinkEventModel;
case 'SignedUpEvent': return SignedUpEventModel;
// and many more, needs to be extended, every time a new type is added …
}
}
Having to manually curate the getMongooseModel
function whenever a new model type is added seems rather error-prone, and I suspect other people who will be working on the code will simply forget about it. 每当添加新的模型类型时,必须手动策划getMongooseModel
函数似乎容易出错,我怀疑其他将要编写代码的人会忘记它。
So, my question: Is there an existing function in the Mongoose API which can give me the appropriate model for a given discriminator key by looking at all known sub schemas? 那么,我的问题是:Mongoose API中是否存在一个函数,通过查看所有已知的子模式,可以为给定的鉴别器密钥提供适当的模型?
A better implementation should be to use an object instead of a switch. 更好的实现应该是使用对象而不是开关。
var modelMap = {
'GenericEvent': GenericEventModel,
'ClickedLinkEvent': ClickedLinkEventModel,
'SignedUpEvent': SignedUpEventModel
};
function getMongooseModel(type) {
return modelList[type];
}
After that in order to avoid errors you can use Mongoose#plugin(fn, [opts]) in order to populate the modelMap
object. 之后为了避免错误,您可以使用Mongoose#plugin(fn,[opts])来填充modelMap
对象。
Or you can just use Mongoose#model(name, [schema], [collection], [skipInit]) . 或者你可以使用Mongoose#model(name,[schema],[collection],[skipInit]) 。
Defines a model or retrieves it. 定义模型或检索它。
Something like this should work: 这样的事情应该有效:
module.exports.createEvent = function(req, res, next) {
var Model = mongoose.model(req.params.type);
(new Model(req.body)).save(next);
}
With a proper declaration of your model class in mongoose, model name must match your request parameter of course. 通过在mongoose中正确声明模型类,模型名称必须与您的请求参数匹配。
var GenericEventModel = mongoose.model('GenericEvent', GenericEventModelSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.