Skip to content

Commit

Permalink
refactor: 💡 fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcxa committed Mar 28, 2020
1 parent b4c79a1 commit 3346737
Show file tree
Hide file tree
Showing 8 changed files with 1,286 additions and 745 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"max-depth": ["error", 5],
"max-nested-callbacks": ["error", 5],
"max-params": ["error", 5],
"no-trailing-spaces": ["warn", { "skipBlankLines": true }],
"no-trailing-spaces": ["warn"],
"object-curly-spacing": ["warn", "always"],
"keyword-spacing": ["warn", { "before": true, "after": true, "overrides": {} }],
"space-before-blocks": "warn",
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ module.exports.sequelize = {

## Connections

Sequelize connection
Sequelize connection.

**Important note:** `dialect` keyword MUST be present in connection or connection.options.

```javascript
somePostgresqlServer: {
Expand Down
52 changes: 29 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = sails => {
exposeToGlobal: true
}
},
configure () {
configure() {
const cls = sails.config[this.configKey].clsNamespace;
// If custom log function is specified, use it for SQL logging or use sails logger of defined level
if (typeof cls === 'string' && cls !== '') {
Expand All @@ -25,7 +25,7 @@ module.exports = sails => {

// Override sails internal loadModels function
// needs to be done in configure()
sails.modules.loadModels = function load (cb) {
sails.modules.loadModels = function load(cb) {

// call the original sails loadModels function so we have access to it's returned models
originalLoadModels((err, modelDefs) => {
Expand All @@ -48,7 +48,7 @@ module.exports = sails => {
});
};
},
initialize (next) {
initialize(next) {

if (sails.config.hooks.orm === false) {
this.initAdapters();
Expand All @@ -65,7 +65,7 @@ module.exports = sails => {
}
},

reload (next) {
reload(next) {
let connections;
const self = this;

Expand Down Expand Up @@ -95,13 +95,13 @@ module.exports = sails => {
});
},

initAdapters () {
initAdapters() {
if (typeof (sails.adapters) === 'undefined') {
sails.adapters = {};
}
},

initConnections () {
initConnections() {
const connections = {};
let connection, connectionName;

Expand All @@ -112,15 +112,15 @@ module.exports = sails => {
const datastoreName = sails.config.models.connection || sails.config.models.datastore || 'default';

sails.log.verbose('Using default connection named ' + datastoreName);
if (!datastores.hasOwnProperty(datastoreName)) {
if (!Object.prototype.hasOwnProperty.call(datastores, datastoreName)) {
throw new Error('Default connection \'' + datastoreName + '\' not found in config/connections');
}

for (connectionName in datastores) {
connection = datastores[connectionName];

// Skip waterline connections
if (connection.adapter) {
// Skip waterline and possible non sequelize connections
if (connection.adapter || !(connection.dialect || connection.options.dialect)) {
continue;
}

Expand All @@ -146,13 +146,13 @@ module.exports = sails => {
return connections;
},

initModels () {
initModels() {
if (typeof (sails.models) === 'undefined') {
sails.models = {};
}
},

defineModels (models, connections) {
defineModels(models, connections) {
let modelDef, modelName, modelClass, cm, im, connectionName;
const sequelizeMajVersion = parseInt(Sequelize.version.split('.')[0], 10);

Expand All @@ -171,7 +171,9 @@ module.exports = sails => {

sails.log.verbose('Loading Sequelize model \'' + modelDef.globalId + '\'');
connectionName = modelDef.connection || modelDef.datastore || defaultConnection;
modelClass = connections[connectionName].define(modelDef.globalId, modelDef.attributes, modelDef.options);
modelClass = connections[connectionName].define(modelDef.globalId,
modelDef.attributes,
modelDef.options);

if (sequelizeMajVersion >= 4) {
for (cm in modelDef.options.classMethods) {
Expand Down Expand Up @@ -203,7 +205,7 @@ module.exports = sails => {
}
},

setAssociation (modelDef) {
setAssociation(modelDef) {
if (modelDef.associations !== null) {
sails.log.verbose('Loading associations for \'' + modelDef.globalId + '\'');
if (typeof modelDef.associations === 'function') {
Expand All @@ -212,7 +214,7 @@ module.exports = sails => {
}
},

setDefaultScope (modelDef, model) {
setDefaultScope(modelDef, model) {
if (modelDef.defaultScope !== null) {
sails.log.verbose('Loading default scope for \'' + modelDef.globalId + '\'');
if (typeof modelDef.defaultScope === 'function') {
Expand All @@ -222,8 +224,8 @@ module.exports = sails => {
}
},

migrateSchema (next, connections, models) {
let connectionDescription, connectionName, migrate, forceSyncFlag, alterFlag;
migrateSchema(next, connections, models) {
let connectionDescription, cn, migrate, forceSyncFlag, alterFlag;
const syncTasks = [];

// Try to read settings from old Sails then from the new.
Expand Down Expand Up @@ -251,13 +253,14 @@ module.exports = sails => {
alterFlag = false;
}

for (connectionName in datastores) {
(function(){
for (cn in datastores) {
(function (connectionName) {
var syncConnectionName = connectionName;
connectionDescription = datastores[syncConnectionName];

// Skip waterline connections
if (connectionDescription.adapter) {
// Skip waterline and possible non sequelize connections
if (connectionDescription.adapter ||
!(connectionDescription.dialect || connectionDescription.options.dialect)) {
return;
}

Expand All @@ -277,14 +280,17 @@ module.exports = sails => {
schemas.push(tableSchema);
}
}

console.log('migrateSchema in ' + syncConnectionName);
return connections[syncConnectionName].sync({ force: forceSyncFlag, alter: alterFlag });
}));

} else {
syncTasks.push(connections[syncConnectionName].sync({ force: forceSyncFlag, alter: alterFlag }));
syncTasks.push(connections[syncConnectionName].sync({
force: forceSyncFlag,
alter: alterFlag
}));
}
}());
}(cn));
}

Promise.all(syncTasks).then(() => next()).catch(e => next(e));
Expand Down
Loading

0 comments on commit 3346737

Please sign in to comment.