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

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

问题描述

我正在寻找一个使用 sequelize ORM 的示例 nodejs 应用程序.

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

我主要对在整个应用程序中如何定义和使用模型感兴趣.我想得到一些验证,即我自己做的事情是好的"做事方式.

解决方案

小故事

这种情况下的技巧不是在文件中初始化模型,而只是提供初始化所需的信息,并让一个集中的模块负责模型的设置和实例化.>

所以步骤是:

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

更长的故事

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

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

这是一个非常古老的答案!(向下阅读以获取信息)

它在很多方面都很陈旧且有限!

  • 首先,正如@jinlesthula 在评论中提到的(我也经历过) - 要求这些文件存在问题.这是因为 requirereaddirSync 的工作方式不同!

  • 第二 - 你的关系非常有限 - 代码没有为这些关联提供选项,所以你UNABLE 创建 belongsToMany 因为它需要 through 属性.您可以制作最基本的关联.

  • 第三 - 你的模型关系非常有限!如果你仔细阅读代码,你会发现关系是一个对象而不是一个数组,所以如果你想让多个相同类型的关联(比如有两次 belongsTo) - 你不能!

  • 第四 - 你不需要那个单身的东西.nodejs 中的每个模块本身都是单例的,所以这一切都毫无缘由地变得非常复杂.

您应该看到 Farm 的回答!(该文章的链接已损坏,但我将使用 sequelize 的官方示例修复它:https://github.com/sequelize/express-example/blob/master/models/index.js - 你可以浏览整个项目以了解发生了什么).

附言我正在编辑这篇文章,因为它被点赞,以至于人们甚至看不到任何新的答案(就像我一样).

只是将链接更改为同一帖子的副本,但在 Github 页面中

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.

解决方案

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

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

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