333

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, persistencejs and sequelize seem the most mature. Do you have experience with either? What are the relevant pros and cons I should be aware of in my decision?

hughc
  • 79
  • 9
Near Privman
  • 3,640
  • 2
  • 14
  • 11
  • But it's a very interesting theme. Would you like to get an access to third-party developers to change data on your site? May be it's better idea to create intermediate js library that sends request parameters (meta language) to a backend? – sergzach May 27 '11 at 08:20
  • 3
    @sergzach Care to elaborate? Google for "hibernate caching". – ponzao May 27 '11 at 20:41
  • Hm... Some sites allow user applications. Or I didn't understand you, excuse me please. – sergzach May 27 '11 at 22:18
  • 3
    @sergzach: Caching is actually a fairly major feature of a number of ORM solutions. Look at Cayenne, for example. – Nate C-K Sep 06 '11 at 03:20
  • Try [Oreo](https://github.com/will123195/oreo). https://github.com/will123195/oreo – Will Sep 30 '14 at 22:30
  • There is also [Bookshel.js](http://bookshelfjs.org/). – Salman von Abbas Dec 15 '14 at 12:29
  • I developed this ORM that caches the whole database in memory. You can access data in a syncronous way, and control all the modifications with transactions. https://github.com/jbaylina/syncorm – jbaylina Mar 20 '15 at 18:24
  • ...and closed by someone who knows neither Node.js nor MySQL :-/ (just to get a bounty ?) – Supersharp Jun 28 '16 at 12:52
  • "closed as not constructive by Kev" ?! seriously?... pardon my sarcasm. – Ovi Jul 20 '16 at 11:14

4 Answers4

157

I would choose Sequelize because of it's excellent documentation. It's just a honest opinion (I never really used MySQL with Node that much).

alessioalex
  • 57,958
  • 15
  • 152
  • 119
  • 11
    I would also like to point out that node-orm2 or persistence.js do not provide a migrations mechanism. persistence.js does not support postgres. sequelize does both of these things. – airtonix Oct 06 '13 at 06:09
  • node-orm2 as of recently [has migrations](https://github.com/locomote/node-migrate-orm2) – dxg Apr 18 '14 at 05:55
  • 4
    I used node-orm2 and due to Sequelize's better documentation I was able to get it up and running much easier – Michael J. Calkins Jun 16 '14 at 21:53
  • 1
    Sequelize is no longer in development. – petomalina Sep 22 '14 at 20:22
  • 2
    @Gelidus Where did you get this information? – William Lepinski Sep 22 '14 at 21:39
  • 1
    @WilliamLepinski well, when I was installing it via npm, it told me so. But now I can see there is new commit 3 days ago. Sorry for that. :) – petomalina Sep 23 '14 at 06:34
  • Np, @Gelidus. I was worried because I'm using it on a project. – William Lepinski Sep 26 '14 at 19:39
  • 16
    Sadly, the Sequelize documentation has recently become terrible. I started using Sequelize recently and was quite pleased by the docs. Now it's an auto-generated pile of broken links, outdated information, and incomplete examples. I'll still stick with it though. It isn't that hard to learn. – Brad Jan 14 '15 at 02:49
  • hi all, what is your opinion on bookselfjs.org ? – codebased Jan 17 '15 at 03:03
  • 2
    Just deleted my old comment and I now 100% agree with @Brad , unfortunately :( – Andrey Popov May 25 '15 at 12:24
  • Sequelize - documentation and support is great but you're tied in with their chosen promise library and there's no support for prepared statements. – backdesk Jan 06 '16 at 15:40
  • *I never really used MySQL with Node that much*. What do you prefer instead? – Green Jan 20 '16 at 07:42
  • @Green MongoDB for fast prototyping, PostgreSql / Sqlite / LevelDB for the rest. – alessioalex Feb 04 '16 at 13:16
  • Sequelize does not have db automatic sync mechanism ! – Rizwan Patel Sep 12 '16 at 11:04
  • I just struggled with sequelize for many hours and I'm completely frustrated. There's no way to specify characters sets and collations on a per-column basis (only per-table or per-db defaults). This is a huge show stopper for me have serious doubts about this piece of software. – jlh Jun 22 '17 at 15:59
  • 1
    Don't use sequelize,... thats all I can say – Dr.Knowitall Jan 03 '18 at 23:59
  • Disagree. Sequelize docs is poor. Impossible to find a decent example of updating relationships (upsert). Objection has a proper documentation, similar to React-router v4. I moved out of Sequelize to ObjectionJs and I am happy with it! – Leonardo Jan 19 '18 at 00:29
  • I too feel like Sequelize documentation has gone downhill. They don't know whether they want you to use Sync() or manual migrations, and the documentation seems to waffle between the two. I was led down the Sync() path by an express sample project, only to later find out that Sync() is mainly a "development tool" and should not be used for production. If that is the case then remove it from the docs and all of the samples please. This is just one example of sequelize not having its stuff together. Imagine a whole ORM and docs full of this type of discrepancy. – Dennis W Apr 01 '18 at 12:55
104

May I suggest Node ORM?

https://github.com/dresende/node-orm2

There's documentation on the Readme, supports MySQL, PostgreSQL and SQLite.

MongoDB is available since version 2.1.x (released in July 2013)

UPDATE: This package is no longer maintained, per the project's README. It instead recommends bookshelf and sequelize

dresende
  • 2,168
  • 1
  • 14
  • 24
20

First off, please note that I haven't used either of them (but have used 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.

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.

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

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.

pimvdb
  • 141,012
  • 68
  • 291
  • 345
hey_lu
  • 213
  • 2
  • 6
11

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.

Josh Smith
  • 13,621
  • 16
  • 66
  • 112
  • 8
    Currently Persistence.js supprts VARCHAR too. – alehro Jan 15 '13 at 14:02
  • 9
    In databases like PostgreSQL, TEXT and VARCHAR are exactly the same, the only difference is that if you put a byte limit (VARCHAR(255)) then you have an overhead for checking the limit. There is absolutely no issue in using TEXT for everything in PostgreSQL. – rewritten Oct 18 '14 at 15:45
  • 3
    Using TEXT in PostgreSQL is actually the better practice, even if it's not portable. – cowbert Sep 01 '15 at 07:23