Azul.js Express Transaction Middleware
Simplify use of transactions on a per-request basis when using Azul.js with Express. For more information, see the Azul.js transaction guide.
app.post('/articles', at.route(function(req, res, next, Article, Author) {
Author.objects.findOrCreate({ name: req.body.author }).then(function(author) {
return author.createArticle({ title: req.body.title }).save();
})
.then(function(article) {
res.send({ article: article.json });
})
.catch(next);
}));
For reference, the full setup for the above example would look something like this:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var BPromise = require('bluebird');
var azul = require('azul');
var env = process.env.NODE_ENV || 'development';
var config = require('./azulfile')[env];
var db = azul(config);
var at = require('azul-transaction')(db);
db.model('Article', {
title: db.attr(),
author: db.belongsTo(),
});
db.model('Author', {
name: db.attr(),
articles: db.hasMany(),
});
app.use(bodyParser.urlencoded());
// insert above code here
API
azulTransaction(db)
db
Type: Database
The database from which to create transactions.
License
This project is distributed under the MIT license.