我应该为 Node.js 和 MySQL 使用哪个 ORM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6007353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which ORM should I use for Node.js and MySQL?
提问by Near Privman
I'm rewriting a project to use Node.js. I'd like to keep using MySQL as the DB (even though I don't mind rewriting the schema). I'm looking for a simple-to-use, reasonable-performance ORM, which supports caching, many-to-one and many-to-many relations. From the MySQL ORMs I could find, persistencejsand sequelizeseem the most mature. Do you have experience with either? What are the relevant pros and cons I should be aware of in my decision?
我正在重写一个项目以使用 Node.js。我想继续使用 MySQL 作为数据库(即使我不介意重写架构)。我正在寻找一种易于使用、性能合理的 ORM,它支持缓存、多对一和多对多关系。从我能找到的 MySQL ORM 中,persistencejs和sequelize似乎是最成熟的。你有这方面的经验吗?在我的决定中,我应该注意哪些相关的利弊?
采纳答案by dresende
May I suggest Node ORM?
我可以建议节点 ORM 吗?
https://github.com/dresende/node-orm2
https://github.com/dresende/node-orm2
There's documentation on the Readme, supports MySQL, PostgreSQL and SQLite.
Readme 上有文档,支持 MySQL、PostgreSQL 和 SQLite。
MongoDB is available since version 2.1.x(released in July 2013)
MongoDB 从2.1.x版(2013 年 7 月发布)开始可用
UPDATE: This package is no longer maintained, per the project's README. It instead recommends bookshelfand sequelize
回答by alessioalex
I would choose Sequelizebecause of it's excellent documentation. It's just a honest opinion (I never really used MySQL with Node that much).
我会选择Sequelize,因为它的文档非常好。这只是一个诚实的意见(我从未真正将 MySQL 与 Node 一起使用过那么多)。
回答by hey_lu
First off, please note that I haven't used either of them (but have used Node.js).
首先,请注意我没有使用过它们中的任何一个(但使用过 Node.js)。
Both libraries are documented quite well and have a stable API. However, persistence.js seems to be used in more projects. I don't know if all of them still use it, though.
这两个库都有很好的文档记录,并且有一个稳定的 API。不过,persistence.js 似乎用在更多的项目中。不过,我不知道他们是否还在使用它。
The developer of sequelize sometimes blogs about it at blog.depold.com. When you'd like to use primary keys as foreign keys, you'll need the patch that's described in this blog post. If you'd like help for persistence.js there is a google group devoted to it.
sequelize 的开发者有时会在blog.depold.com上写博客。当您想使用主键作为外键时,您将需要此博客文章中描述的补丁。如果您需要有关 persistence.js 的帮助,有一个专门针对它的 google 小组。
From the examples I gather that sequelize is a bit more JavaScript-like (more sugar) than persistance.js but has support for fewer datastores (only MySQL, while persistance.js can even use in-browser stores).
从我收集的示例来看,sequelize 比persistance.js 更像JavaScript(更多糖),但支持更少的数据存储(只有MySQL,而persistance.js 甚至可以使用浏览器内存储)。
I think that sequelize might be the way to go for you, as you only need MySQL support. However, if you need some convenient features (for instance search) or want to use a different database later on you'd need to use persistence.js.
我认为 sequelize 可能是适合您的方式,因为您只需要 MySQL 支持。但是,如果您需要一些方便的功能(例如搜索)或想稍后使用不同的数据库,则需要使用 persistence.js。
回答by Josh Smith
One major difference between Sequelize and Persistence.js is that the former supports a STRING
datatype, i.e. VARCHAR(255)
. I felt really uncomfortable making everything TEXT
.
Sequelize 和 Persistence.js 之间的一个主要区别是前者支持一种STRING
数据类型,即VARCHAR(255)
. 做所有事情我都觉得很不舒服TEXT
。