spawn-or-lease-connection.js 2.3 KB

123456789101112131415161718192021222324
  1. // ███████╗██████╗ █████╗ ██╗ ██╗███╗ ██╗ ██████╗ ██████╗ ██╗ ███████╗ █████╗ ███████╗███████╗
  2. // ██╔════╝██╔══██╗██╔══██╗██║ ██║████╗ ██║ ██╔═══██╗██╔══██╗ ██║ ██╔════╝██╔══██╗██╔════╝██╔════╝
  3. // ███████╗██████╔╝███████║██║ █╗ ██║██╔██╗ ██║ ██║ ██║██████╔╝ ██║ █████╗ ███████║███████╗█████╗
  4. // ╚════██║██╔═══╝ ██╔══██║██║███╗██║██║╚██╗██║ ██║ ██║██╔══██╗ ██║ ██╔══╝ ██╔══██║╚════██║██╔══╝
  5. // ███████║██║ ██║ ██║╚███╔███╔╝██║ ╚████║ ╚██████╔╝██║ ██║ ███████╗███████╗██║ ██║███████║███████╗
  6. // ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝
  7. //
  8. // Returns either the leased connection that was passed in to the meta input of
  9. // a helper or spawns a new connection. This is a normalized helper so the actual
  10. // helper methods don't need to deal with the branching logic.
  11. var _ = require('@sailshq/lodash');
  12. var spawnConnection = require('./spawn-connection');
  13. module.exports = function spawnOrLeaseConnection(datastore, meta, cb) {
  14. if (!_.isUndefined(meta) && _.has(meta, 'leasedConnection')) {
  15. return setImmediate(function ensureAsync() {
  16. cb(null, meta.leasedConnection);
  17. });
  18. }
  19. // Otherwise spawn the connection
  20. spawnConnection(datastore, cb);
  21. };