diff --git a/packages/isomorphic-core/src/models/index.js b/packages/isomorphic-core/src/models/index.js index 9fc670de7..148979fac 100644 --- a/packages/isomorphic-core/src/models/index.js +++ b/packages/isomorphic-core/src/models/index.js @@ -1,16 +1,19 @@ const fs = require('fs'); const path = require('path'); -function loadModels(Sequelize, sequelize, {modelsDir = __dirname, modelsSubPath = '', schema} = {}) { +function loadModels(Sequelize, sequelize, {modelLocations = [{}], schema} = {}) { const db = {}; - const fullModelsDir = path.join(modelsDir, modelsSubPath) - for (const filename of fs.readdirSync(fullModelsDir)) { - if (filename.endsWith('.js')) { - let model = sequelize.import(path.join(fullModelsDir, filename)); - if (schema) { - model = model.schema(schema); + + for (const {modelsDir = __dirname, modelsSubpath = ''} of modelLocations) { + const fullModelsDir = path.join(modelsDir, modelsSubpath) + for (const filename of fs.readdirSync(fullModelsDir)) { + if (filename.endsWith('.js')) { + let model = sequelize.import(path.join(fullModelsDir, filename)); + if (schema) { + model = model.schema(schema); + } + db[model.name[0].toUpperCase() + model.name.substr(1)] = model; } - db[model.name[0].toUpperCase() + model.name.substr(1)] = model; } } @@ -23,5 +26,4 @@ function loadModels(Sequelize, sequelize, {modelsDir = __dirname, modelsSubPath return db; } - module.exports = loadModels diff --git a/packages/local-sync/src/shared/local-database-connector.js b/packages/local-sync/src/shared/local-database-connector.js index 3fe3ee45c..5509c4b46 100644 --- a/packages/local-sync/src/shared/local-database-connector.js +++ b/packages/local-sync/src/shared/local-database-connector.js @@ -37,7 +37,7 @@ class LocalDatabaseConnector { const newSequelize = this._sequelizePoolForDatabase(`a-${accountId}`); const db = loadModels(Sequelize, newSequelize, { - modelsSubPath: 'account', + modelLocations: [{modelsSubpath: 'account'}], schema: `a${accountId}`, }) @@ -72,7 +72,9 @@ class LocalDatabaseConnector { _sequelizeForShared() { const sequelize = this._sequelizePoolForDatabase(`shared`); - const db = loadModels(Sequelize, sequelize, {modelsSubPath: 'shared'}) + const db = loadModels(Sequelize, sequelize, { + modelLocations: [{modelsSubpath: 'shared'}], + }) HookAccountCRUD(db, sequelize);