sdepold/sequelize — Issue #116: Docs should be enchanced for new node.js users
Author: ustun
Created: 2011-12-15 12:16:25+00:00
State: open

I am new to node.js, so while I understand that this is the intended behaviour, this might bite new comers, and I believe this should be documented. Saving to a db using node is definitely very different from rails, django etc. due to non-blocking constraints.

Consider the following. Multiple calls to save() creates multiple rows in db.

var Sequelize = require("sequelize");
var sequelize = new Sequelize('sequelize_test', 'root', 'root',
{logging: false});

var User = sequelize.define('User', {username: Sequelize.STRING});

var user = User.build({username: "Ustun"});

user.save();
user.username = "Ozgur";
user.save();

or even for:

user.save();
user.updateAttributes({'username': "Ozgur"});

Two users are created instead of one in both cases. What should have been written is:

user.save()
.on('success', function (usr) {
usr.username = "Ozgur";
usr.save();
});

There are a few similar gotchas like this, as in initial db creation, we need the call to sync, and what comes afterwards should be given as a callback to sync. So the following does not work obviously, but still should be documented I guess.

var sequelize = new Sequelize('sequelize_test', 'root', 'root',
{logging: false});
sequelize.sync();

var User = sequelize.define('User', {username: Sequelize.STRING});
sequelize.sync();