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, Database.tx, or their derivations.
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, optionsopt) → {external: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 | ||||||||||
options |
Object |
<optional> |
Optional Parameters. Properties
|
- Deprecated:
- Consider using async/await syntax instead, or if you must have pre-generated promises, then Promise.allSettled.
- Source:
Returns:
- Type
- external:Promise
page(source, optionsopt) → {external: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 | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
source |
function | |||||||||||||||||
options |
Object |
<optional> |
Optional Parameters. Properties
|
Returns:
- Type
- external:Promise
sequence(source, optionsopt) → {external: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 | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
source |
function | ||||||||||||||||||||||
options |
Object |
<optional> |
Optional Parameters. Properties
|
Returns:
- Type
- external:Promise