docs/reference/waterline/datastores/leaseConnection.md
.leaseConnection()Lease a new connection from the datastore for use in running multiple queries on the same connection (i.e. so that the logic provided in during can reuse the db connection).
await datastore.leaseConnection(during);
Or
var result = await datastore.leaseConnection(during);| Argument | Type | Details | |
|---|---|---|---|
| 1 | during | ((function)) | A procedural parameter that Sails will call automatically when a connection has been obtained and made ready for you. It will receive the arguments specified in the "During" usage table below. |
| Argument | Type | Details | |
|---|---|---|---|
| 1 | db | ((ref)) | Your newly-leased database connection. (See .usingConnection() for more information on what to do with this.) |
Note that prior to Sails 1.1.0, the recommended usage of
.leaseConnection()expected your "during" code to call a callback (proceed) when it finished. This is no longer necessary as long as you do not actually include a second argument in the function signature of your "during" code.
| Type | Details |
|---|---|
| ((Ref?)) | The optional result data sent back from during. In other words, if, in your during function, you did return 'foo';, then this will be 'foo'. |
| Name | Type | When? |
|---|---|---|
| UsageError | ((Error)) | Thrown if something invalid was passed in. |
| AdapterError | ((Error)) | Thrown if something went wrong in the database adapter. |
| Error | ((Error)) | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
Lease a database connection from the default datastore, then use it to send two queries before releasing it back to the pool.
var inventory = await sails.getDatastore()
.leaseConnection(async (db)=> {
var location = await Location.findOne({ id: inputs.locationId })
.usingConnection(db);
if (!location) {
let err = new Error('Cannot find location with that id (`'+inputs.locationId+'`)');
err.code = 'E_NO_SUCH_LOCATION';
throw err;
}
// Get all products at the location
var productOfferings = await ProductOffering.find({ location: inputs.locationId })
.populate('productType')
.usingConnection(db);
return productOfferings;
})
.intercept('E_NO_SUCH_LOCATION', 'notFound');
// All done! Whatever we were doing with that database connection worked.
// Now we can proceed with our business.