KSDaemon/sails-hook-sequelize

Problem if datastores > 1

Droppix opened this issue · 1 comments

Hi,

You have a problem of synchronization on the name connection, in index.js (line 256):

if (connectionDescription.dialect === 'postgres') {

    syncTasks.push(connections[connectionName].showAllSchemas().then(schemas => {
        let modelName, modelDef, tableSchema;

        for (modelName in models) {
            modelDef = models[modelName];
            tableSchema = modelDef.options.schema || '';

            if (tableSchema !== '' && schemas.indexOf(tableSchema) < 0) { // there is no schema in db for model
                connections[connectionName].createSchema(tableSchema);
                schemas.push(tableSchema);
            }
        }

        return connections[connectionName].sync({ force: forceSyncFlag, alter: alterFlag });
    }));

} else {}

Since "syncTasks" can take some time, connectionName can contain a wrong value, in my case it contains the last item of the data array.
Your must replace by this :

if (connectionDescription.dialect === 'postgres') {
    let cn = connectionName
    syncTasks.push(connections[cn].showAllSchemas().then(schemas => {
        let modelName, modelDef, tableSchema;

        for (modelName in models) {
            modelDef = models[modelName];
            tableSchema = modelDef.options.schema || '';

            if (tableSchema !== '' && schemas.indexOf(tableSchema) < 0) { // there is no schema in db for model
                connections[cn].createSchema(tableSchema);
                schemas.push(tableSchema);
            }
        }

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

For reproduction, you must have several datastore in file : config/datastores.js

module.exports.datastores = {

  sequelizeHost: {
    user: '',
    password: '',
    database: '',
    dialect: 'postgres',
    options: {
        dialect: 'postgres',
        host   : '',
        port   : x,
        logging: false,
        operatorsAliases: false,
        dialectOptions: {
          ssl: true
        }    
    }        
  },

  sequelizeDev: {
    adapter: {}, // force to skip
    user: '',
    password: '',
    database: '',
    options: {
        dialect: 'postgres',
        host   : 'localhost',
        port   : 5432,
        logging: false,
        operatorsAliases: false
    }
  },

  sequelizeTests: {
    adapter: {}, // force to skip
    user: '',
    password: '',
    database: '',
    options: {
        dialect: 'postgres',
        host   : 'localhost',
        port   : 5432,
        logging: false,
        operatorsAliases: false
    }
  }
};

and in the config/models .js :

connection: 'sequelizeHost'

So, it would be nice to have an option that would inhibit a connection (in order for it to work, I had to add -> adapter : {})

Hi @Droppix ! Thanks for pointing out. I've fixed it.
I don't see any reason to introduce new option, as if you do not need a datastore connection in you app — simply remove (or comment out) it :)
And using adapter attribute i'm trying to figure out what connections are waterline, so i skip them.