Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions helpers/register-data-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ module.exports = require('machine').build({
description: 'Register a new datastore for making connections.',


sync: true,


inputs: {

identity: {
Expand Down Expand Up @@ -199,16 +196,42 @@ module.exports = require('machine').build({
};
});

// Store the connection
inputs.datastores[inputs.identity] = {
var dataStore = {
manager: report.manager,
config: inputs.config,
driver: PG
};

// Store the db schema for the connection
inputs.modelDefinitions[inputs.identity] = dbSchema;

return exits.success();
// Test datastore config by trying to acquire and release a connection.
PG.getConnection(report)
.switch({
failed: function (e) {
return exits.error(new Error('There was an error creating a connection for Datastore `' + inputs.identity + '` ' + 'with a url of: ' + inputs.config.url + '\n\n' + e.error.stack));
},
error: function (e) {
return exits.error(new Error('There was an error creating a connection for Datastore `' + inputs.identity + '` ' + ' with a url of: ' + inputs.config.url + '\n\n' + e.stack));
},
success: function (connection) {
// release the connection.
PG.releaseConnection(connection)
.switch({
badConnection: function () {
return exits.error(new Error('There was an error releasing connection for Datastore `' + inputs.identity + '` ' + ' with a url of: ' + inputs.config.url));
},
error: function (e) {
return exits.error(new Error('There was an error releasing connection for Datastore `' + inputs.identity + '` ' + ' with a url of: ' + inputs.config.url + '\n\n' + e.stack));
},
success: function () {
// Store the datastore
inputs.datastores[inputs.identity] = dataStore;

// Store the db schema for the connection
inputs.modelDefinitions[inputs.identity] = dbSchema;

return exits.success();
}
});
},
});
}
});
30 changes: 13 additions & 17 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,19 @@ module.exports = (function sailsPostgresql() {
return cb(new Error('Invalid datastore config. A datastore should contain a unique identity property.'));
}

try {
Helpers.registerDataStore({
identity: identity,
config: datastoreConfig,
models: models,
datastores: datastores,
modelDefinitions: modelDefinitions
}).execSync();
} catch (e) {
setImmediate(function done() {
return cb(redactPasswords(e));
});
return;
}

setImmediate(function done() {
return cb();
Helpers.registerDataStore({
identity: identity,
config: datastoreConfig,
models: models,
datastores: datastores,
modelDefinitions: modelDefinitions
}).switch({
error: function (err) {
return cb(redactPasswords(err));
},
success: function () {
return cb();
}
});
},

Expand Down