如何组织使用续集的节点应用程序?(How to organize a node app that uses sequelize?)

编程入门 行业动态 更新时间:2024-10-28 16:30:21
如何组织使用续集的节点应用程序?(How to organize a node app that uses sequelize?)

我正在寻找一个使用后续ORM的节点nodejs应用程序。

我的主要担心是,如果这些模型由于require()依赖循环而具有彼此的复杂关系,则似乎不可能在单独的js文件中定义模型。 也许人们在一个非常长的文件中定义所有的模型?

我主要关注如何通过应用程序定义和使用模型。 我想有一些验证,我自己做的是做好事情的“好”方式。

I am looking for an example nodejs app that uses the sequelize ORM.

My main concern is that it seems next to impossible to define your models in separate js files if those models have complex relationships to one another because of require() dependency loops. Maybe people define all their models in one file that is very very long?

I am mainly interested in how the models are defined and use through out the app. I would like to have some validation that what i am doing on my own is the "good" way to do things.

最满意答案

短篇小说

这种情况下的诀窍不是初始化文件中的模型而只是为了初始化提供必要的信息,并让集中式模块负责模型设置和实例化。

所以步骤是:

有几个模型文件与模型的数据,如字段,关系和选项。 有一个单例模块加载所有这些文件并设置所有的模型类和关系。 在app.js文件中设置单例模块。 从单例模块中获取模型类不要对模型文件使用require ,而是从单例中加载模型。

更长的故事

以下是有关该解决方案的相关源代码的更详细说明:

http://jeydotc.github.io/blog/2012/10/30/EXPRESS-WITH-SEQUELIZE.html

编辑:这是一个非常古老的答案! (阅读信息)

它在很多方面是老的和有限的!

首先 ,在评论中提到的@jinglesthula(和我也经历过) - 需要这些文件有问题。 这是因为require不能像readdirSync一样工作!

第二 - 你的关系非常有限 - 代码不提供这些关联的选项 ,所以你无法 through属性来创建belongsToMany 。 你可以做最基本的关系。

第三 - 你在模特关系中非常有限! 如果你仔细阅读代码,你会看到关系是一个Object而不是一个Array ,所以如果你想创建多个相同类型的关联(比如有两次belongsTo ) - 你不能!

第四 - 你不需要单身一事。 nodejs中的每个模块本身都是单例,所以这样做是非常复杂的,没有任何理由。

你应该看到农场的答案! (链接到文章是坏的,但我会修复它与后续的官方示例: https : //github.com/sequelize/express-example/blob/master/models/index.js - 您可以浏览整个项目,以了解发生了什么)。

ps我正在编辑这篇文章,因为它是如此高调,人们甚至不会看到任何新的答案(像我一样)。

编辑:只需将链接更改为相同帖子的副本,但在Github页面中

The short story

The trick in this case is not to initialize the model in the file but just to provide the necesary information for its initialization and let a centralized module take care of the models setup and instantiation.

So the steps are:

Have several Model files with data about the model, like fields, relationships and options. Have a singleton module which loads all those files and setup all the model classes and relationships. Setup your singleton module at the app.js file. Get the model classes from the singleton module do not use require on your model files, load the models from the singleton instead.

The longer story

Here is a more detailed description of this solution with the corresponding source code:

http://jeydotc.github.io/blog/2012/10/30/EXPRESS-WITH-SEQUELIZE.html

EDIT: This is a very old answer! (read down for info)

It's old and limited in many ways!

First, as @jinglesthula mentioned in comments (and I experienced it too) - there are problems with requiring those files. It's because require doesn't work the same way as readdirSync!

Second - you are very limited in relations - the code doesn't provide options to those associations so you are UNABLE to create belongsToMany as it needs through property. You can make the most basic assocs.

Third - you are very limited in model relations! If you read closely the code, you will see that relations is an Object instead of an Array, so if you want to make more than one associations of the same type (like having two times belongsTo) - you cannot!

Fourth - You don't need that singleton thingy. Every module in nodejs is singleton by itself, so all this makes is pretty complex for no reason.

You should see Farm's answer! (The link to the article is broken, but I'll fix it with this official sample from sequelize: https://github.com/sequelize/express-example/blob/master/models/index.js - you can browse the whole project to get an idea of what's going on).

p.s. I'm editing this post as it's so upvoted that people won't even see any new answers (as I did).

Edit: Just changed the link to a copy of the same post, but in a Github Page

更多推荐

本文发布于:2023-08-06 18:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   续集   应用程序   组织   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!