Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function spawnConnection(datastore, cb) {
// Validate datastore
if (!datastore || !datastore.manager || !datastore.config) {
return cb(new Error('Spawn Connection requires a valid datastore.'));
}
PG.getConnection({
manager: datastore.manager,
meta: datastore.config
})
.switch({
error: function error(err) {
return cb(err);
},
failed: function failedToConnect(err) {
// Setup some basic troubleshooting tips
console.error('Troubleshooting tips:');
console.error('');
// Used below to indicate whether the error is potentially related to config
// (in which case we'll display a generic message explaining how to configure all the things)
var isPotentiallyConfigRelated;
isPotentiallyConfigRelated = true;
.exec(function rollbackCb() {
// Only release the connection if the leased flag is false
if (leased) {
return cb();
}
// Release the connection back into the pool
PG.releaseConnection({
connection: connection
})
.exec(function releaseCb() {
return cb();
});
});
};
.exec(function releaseCb() {
return cb(err);
});
});
return;
}
// Only release the connection if it wasn't leased from outside the
// adapter.
if (leased) {
return cb();
}
// Release the connection back into the pool
PG.releaseConnection({
connection: connection
})
.exec(function releaseCb() {
return cb();
});
});
};
module.exports = function releaseConnection(connection, leased, cb) {
// If this connection was leased outside of the Adapter, don't release it.
if (leased) {
return setImmediate(function ensureAsync() {
return cb();
});
}
PG.releaseConnection({
connection: connection
}).switch({
error: function error(err) {
return cb(new Error('There was an error releasing the connection back into the pool.' + err.stack));
},
badConnection: function badConnection() {
return cb(new Error('Bad connection when trying to release an active connection.'));
},
success: function success() {
return cb();
}
});
};
error: function error(err) {
// If the connection was leased from outside the adapter, don't release it.
if (leased) {
return cb(new Error('There was an error starting a transaction. ' + err.stack));
}
PG.releaseConnection({
connection: connection
}).exec({
error: function error(err) {
return cb(new Error('There was an error releasing the connection back into the pool. ' + err.stack));
},
success: function success() {
return cb(new Error('There was an error starting a transaction. ' + err.stack));
}
});
},
success: function success() {
.exec(function rollbackCb() {
// Only release the connection if it wasn't leased from outside the
// adapter.
if (leased) {
return cb(err);
}
// Release the connection back into the pool
PG.releaseConnection({
connection: connection
})
.exec(function releaseCb() {
return cb(err);
});
});
throw new Error('Invalid options argument. Options must contain: connection, nativeQuery, and leased.');
}
if (!_.has(options, 'connection') || !_.isObject(options.connection)) {
throw new Error('Invalid option used in options argument. Missing or invalid connection.');
}
if (!_.has(options, 'nativeQuery')) {
throw new Error('Invalid option used in options argument. Missing or invalid nativeQuery.');
}
// ╦═╗╦ ╦╔╗╔ ┌┐┌┌─┐┌┬┐┬┬ ┬┌─┐ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
// ╠╦╝║ ║║║║ │││├─┤ │ │└┐┌┘├┤ │─┼┐│ │├┤ ├┬┘└┬┘
// ╩╚═╚═╝╝╚╝ ┘└┘┴ ┴ ┴ ┴ └┘ └─┘ └─┘└└─┘└─┘┴└─ ┴
PG.sendNativeQuery({
connection: options.connection,
nativeQuery: options.nativeQuery,
valuesToEscape: options.valuesToEscape,
})
.switch({
// If there was an error, check if the connection should be
// released back into the pool automatically.
error: function error(err) {
if (!options.disconnectOnError) {
return cb(err);
}
releaseConnection(options.connection, options.leased, function releaseConnectionCb(err) {
return cb(err);
});
},
module.exports = function runNativeQuery(connection, query, valuesToEscape, cb) {
PG.sendNativeQuery({
connection: connection,
nativeQuery: query,
valuesToEscape: valuesToEscape,
})
.switch({
error: function error(err) {
return cb(err);
},
// If the query failed, try and parse it into a normalized format.
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = PG.parseNativeQueryError({
nativeQueryError: report.error
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = PG.parseNativeQueryError({
nativeQueryError: report.error
}).execSync();
} catch (e) {
return cb(e);
}
// If the catch all error was used, return an error instance instead of
// the footprint.
var catchAllError = false;
if (parsedError.footprint.identity === 'catchall') {
catchAllError = true;
}
if (catchAllError) {
return cb(report.error);
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = PG.parseNativeQueryError({
nativeQueryError: report.error
}).execSync();
} catch (e) {
if (!options.disconnectOnError) {
return cb(e);
}
releaseConnection(options.connection, function releaseConnectionCb() {
return cb(e);
});
return;
}
// If the catch all error was used, return an error instance instead of
// the footprint.
var catchAllError = false;