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

查看:108
本文介绍了如何组织使用续集的节点应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

简短的故事



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

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.

所以步骤是:


  • 有几个模型文件,包含有关模型的数据,如字段,关系和选项。

  • 有一个单例模块加载所有这些文件并设置所有模型类和关系

  • 在app.js文件中设置您的单例模块。

  • 从单例模块获取模型类不要在您的模型文件中使用 require ,而不是从单身人士加载模型。

  • 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.

这是一个mo请使用相应的源代码详细说明此解决方案:

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

它在很多方面是老的,有限的


  • 第一个,作为评论中提到的@jinglesthula(和我也经历过) - 需要这些文件有问题。这是因为要求不像 readdirSync

  • 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!

第二个 - 您的关系中的非常有限,代码不会向这些关联提供选项 UNABLE 创建 belongsToMany ,因为它需要通过属性。你可以做出最基本的关系。

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.

第三个 - 你的模型关系非常有限!如果仔细阅读代码,您将看到该关系是一个对象,而不是数组,所以如果要使多个相同类型的关联(如有两次 belongsTo ) - 你不能!

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!

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

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.

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

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).

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

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

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

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

这篇关于如何组织使用续集的节点应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆