Created
January 10, 2016 14:57
-
-
Save bostjanpisler/15eeb55c87163ef00ec2 to your computer and use it in GitHub Desktop.
Sequelize transaction for User -> hasMany -> Organization / Organization -> belongsTo -> User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Correct way to save related model data with transaction | |
# A transaction is required if we want validation of related model data | |
# Sequelize docs don't provide exact instructions for this | |
await db.sequelize.transaction((t) -> | |
return db.User.create(data, { transaction: t, fields: ['email', 'password'] }).then((user) -> | |
return user.createOrganization(data.org, { transaction: t, fields: ['name'] }); | |
) | |
).nodeify defer err, user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment