简体   繁体   English

Nodejs 模块导出

[英]Nodejs module exports

In my node app, I'm defining models (using Sequelize ORM) in two different files: models/app-acl.js and models/app-models.js;在我的节点应用程序中,我在两个不同的文件中定义模型(使用 Sequelize ORM):models/app-acl.js 和 models/app-models.js;

then I'm importing them in a third file (models.js) where I'm defining the relationships between models:然后我将它们导入到第三个文件 (models.js) 中,我在其中定义模型之间的关系:

//file models.js
var aclModel = require('./app-acl.js');
var appModel = require('./app-models.js');

//defining relationships...
aclModel.Group.hasMany(aclModel.User, {onDelete: 'CASCADE'});
...


//exporting models:
module.exports = {
    aclModel: aclModel,
    appModel: appModel
}

Doing this, in other parts of the app I can do这样做,在应用程序的其他部分我可以做到

var models = require('models/models.js') 

and access all models by models.aclModel... and models.appModel并通过 models.aclModel... 和 models.appModel 访问所有模型

The thing is that sometimes I need to do operation on arbitrary model, such as a "find" without knowing if it is a aclModel or a appModel;问题是有时我需要对任意模型进行操作,例如在不知道它是 aclModel 还是 appModel 的情况下进行“查找”; so I would like to export both under the same name;所以我想以相同的名称导出两者; something like:就像是:

module.exports = {
    models: aclModel + appModel //merged together
}

by this I would be able to do这样我就可以做到

models[name].find.....

being "name" a model from aclModel or from appModel, and it will work in both cases;从 aclModel 或 appModel 中“命名”一个模型,它在两种情况下都适用;

I thought to merge together aclModel and appModel in model.js before exporting like this:我想在像这样导出之前在 model.js 中将 aclModel 和 appModel 合并在一起:

function merge (destination, source) {
    for (var property in source)
        destination[property] = source[property];
    return destination;
}

module.exports = {
    models: merge(aclModel, appModel)
}

but this doesn't work;但这不起作用; does anybody have a solution for doing this?有没有人有这样做的解决方案?

OK, after clarifying your question in the comments (next time save everyone's time by including a real program that expresses your question not just snippets and ellipses), my first thought is your design is probably ultimately misguided but nonetheless is achievable and straightforward in javascript.好的,在评论中澄清您的问题后(​​下次通过包含一个真正的程序来表达您的问题而不仅仅是片段和省略号,从而节省每个人的时间),我的第一个想法是您的设计最终可能会被误导,但仍然可以在 javascript 中实现和直接。

Put your files in this structure:把你的文件放在这个结构中:

models/index.js
models/acl-model.js
models/app-model.js

In your models/index.js do this:在您的models/index.js执行以下操作:

var _ = require('lodash'); _.extend(module.exports, require('./acl-model'), require('./app-model'));

This will combine all the properties of your two model modules into the index module.这会将您的两个模型模块的所有属性组合到索引模块中。 In case of a name duplication conflict, the last one will win.如果出现名称重复冲突,最后一个获胜。

Then you can get a reference to the index with require('models');然后你可以通过require('models');获得对索引的引用require('models'); or a specific one with require('models/acl-model');或特定的require('models/acl-model'); . . The index.js one will support models[name] dynamic property lookup access. index.js将支持models[name]动态属性查找访问。 Thus you can do:因此你可以这样做:

var models = require('models'); var name = "Group"; models[name].find();

As long as one of your model modules has a model called "Group", that will work.只要您的模型模块之一有一个名为“组”的模型,它就会起作用。

Maybe you can use :也许你可以使用:

module.exports = {
    ...aclModel,
    ...appModel
}

it is a sample method这是一个示例方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM