Skip to content

Instantly share code, notes, and snippets.

@bostjanpisler
Created January 10, 2016 14:57
Show Gist options
  • Save bostjanpisler/15eeb55c87163ef00ec2 to your computer and use it in GitHub Desktop.
Save bostjanpisler/15eeb55c87163ef00ec2 to your computer and use it in GitHub Desktop.
Sequelize transaction for User -> hasMany -> Organization / Organization -> belongsTo -> User
# 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