Extends Database for an automatic connection session, with methods for executing multiple database queries. The type isn't available directly, it can only be created via methods Database.task and Database.tx.
When executing more than one request at a time, one should allocate and release the connection only once, while executing all the required queries within the same connection session. More importantly, a transaction can only work within a single connection.
This is an interface for tasks/transactions to implement a connection session, during which you can execute multiple queries against the same connection that's released automatically when the task/transaction is finished.
Each task/transaction manages the connection automatically. When executed on the root Database object, the connection is allocated from the pool, and once the method's callback has finished, the connection is released back to the pool. However, when invoked inside another task or transaction, the method reuses the parent connection.
Example
db.task(t => {
// t = task protocol context;
// t.ctx = Task Context;
return t.one('select * from users where id=$1', 123)
.then(user => {
return t.any('select * from events where login=$1', user.name);
});
})
.then(events => {
// success;
})
.catch(error => {
// error;
});
Members
(readonly) ctx :TaskContext
Task/Transaction Context object - contains individual properties for each task/transaction.
Type:
Examples
db.task(t => {
return t.ctx; // task context object
})
.then(ctx => {
console.log('Task Duration:', ctx.duration);
});
db.tx(t => {
return t.ctx; // transaction context object
})
.then(ctx => {
console.log('Transaction Duration:', ctx.duration);
});
Methods
batch(values, cbopt) → {external:Promise}
Alternative Syntax: batch(values, {cb})
⇒ Promise
Settles a predefined array of mixed values by redirecting to method spex.batch.
For complete method documentation see spex.batch.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
values |
array | ||
cb |
function |
<optional> |
Returns:
- Type
- external:Promise
page(source, destopt, limitopt) → {external:Promise}
Alternative Syntax: page(source, {dest, limit})
⇒ Promise
Resolves a dynamic sequence of arrays/pages with mixed values, by redirecting to method spex.page.
For complete method documentation see spex.page.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
source |
function | |||
dest |
function |
<optional> |
||
limit |
number |
<optional> |
0 |
Returns:
- Type
- external:Promise
sequence(source, destopt, limitopt, trackopt) → {external:Promise}
Alternative Syntax: sequence(source, {dest, limit, track})
⇒ Promise
Resolves a dynamic sequence of mixed values by redirecting to method spex.sequence.
For complete method documentation see spex.sequence.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
source |
function | |||
dest |
function |
<optional> |
||
limit |
number |
<optional> |
0 | |
track |
boolean |
<optional> |
false |
Returns:
- Type
- external:Promise