2. Sails.js
● MVC web application framework inspired by Ruby on Rails
● Built on Node.js and Express.js
● Delivered with Waterline ORM containing a set of adapters to various
databases
3. The problem
Request context
There is no ability to get
the current request/
user/session in any
application place.
Because of async nature
of the JS they should be
passed as function
arguments.
Random DB connection
DB adapter uses only
one connection for each
request or random from
pool (if pool mode is on).
Error handling
In case of uncatched
exception there is no
chance to figure to
whom the response
should be sent to.
4. Domain API
var domain = require('domain');
domain = domain.create();
const net = require('net');
net.createServer((req, res) => {
domain.run(() => {
setImmediate(() => {
let active = require('domain').active;
res.write('bye');
res.end();
});
});
}).listen(8000);
5. The solution
Request identification
Generate and assign
uuid for each incoming
request. Store request
with its id to domain in
the middleware.
DB adapter
Take request from the
domain context and use
dedicated DB connection
for each request.
Finalize request
Commit or rollback
transaction and return
connection to the pool.
6. Express
middleware
Create domain, set
request to the domain,
start the transaction
Custom DB
adapter
Checks current domain and
uses dedicated connection.
Single transaction for the
whole request lifecycle
Business
logic
Has access and can
store data to its own
request context
Single
response point
Finish transaction and
release connection