17

I need to use Node.js, but it doesn't have a good ORM for MySQL. So I'm planning on using SQLAlchemy to define my schema. And then use node-mysql to do low-level queries (of course, I wouldn't be able to use SQLAlchemy's query language coz it's in python.)

What do you guys think?

sequelize seems to be the best one...but it doesn't seem that many people are using it. Also, what about migrations? How would I handle that?

node-orm doesn't seem very active either.

Thoughts on this?

user847495
  • 9,831
  • 17
  • 45
  • 48
  • Good summary here: http://www.quora.com/Database-Migrations/What-are-my-options-for-SQL-database-migrations-with-Node-js – vaughan Jun 18 '13 at 05:59
  • https://bitbucket.org/zzzeek/alembic would be the library to use. Written by the author of SQLAlchemy. – vaughan Jun 18 '13 at 06:13

3 Answers3

8

Sequelize is pretty good ORM for MySQL and has excellent documentation. You can use node-migrate for migrations.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • This answer is out of date. Sequelize has a migrations functionality now. But it is broken for schema migration undo and quite limited. – vaughan Jun 18 '13 at 06:12
  • 9
    I just wish Sequelize was less magic. I want nice plain JavaScript objects. You can't even use `prototype` on the 'classes' that Sequelize creates. You have to use it's magic 'instanceMethods' crap. JS is functional enough you shouldn't have to do that. – d11wtq Aug 03 '14 at 10:50
  • 4
    Unfortunately, sequelize migrations are a joke. If you have a trivial table and define it from the command line, it will magically create a migration for it, yay! But for anything from the real world it's unusable: Relations? Nope, no support. Indices? Nope, can't have. Generate migration from existing model? Nope, can't do. So you end up writing everything twice: Once in the model and once for the migration (but different syntax). If you mess it up you're screwed. – jlh Oct 23 '17 at 15:47
  • @d11wtq You might want to look into v4 because using prototype works fine now. You can define class methods with `MyUser.findByEmail = function () {}` or instance methods with `MyUser.prototype.sendBirthdayWishes = function () {}`. – jlh Oct 23 '17 at 15:50
5

We are using Sequelize.js in our Node project and I guess it kind of does the job done but there are gotchas. One example is that the MySQL Sequelize query engine does case sensitive string matching on the SQL string that you feed it (this.sql.indexOf('SELECT') == 0). This means it can fail if your SQL happens to be lowercase. SQL keywords are usually case insensitive (although upper case by convention) so the Sequelize implementation seems like a hack.

There is migration support in Sequelize as of version 1.3.0 but I haven't used it and I'm considering rolling my own instead.

I come from a background of having used the Ruby ActiveRecord ORM and in light of this and the gotcha mentioned above I'm hesitant to recommend Sequelize. Unfortunately, I don't know what better alternatives are out there.

UPDATE1: there are other ORMs suggested under "Which ORM should I use for Node.js and MySQL?".

UPDATE2: I've released my Sequelize.js migration code on Github

Community
  • 1
  • 1
Peter Marklund
  • 1,033
  • 10
  • 9
0

You can use light-orm and mysql:

  1. https://npmjs.org/package/light-orm - ORM wrapper
  2. https://npmjs.org/package/mysql - Driver

Bookshelf.js is also pretty good, but, in some aspects, not flexible. For example, you can not execute SQL request and convert result to models. So, use light-orm.

Oleksandr Knyga
  • 625
  • 9
  • 9