[iso-core]: Make loadModels reusable by cloud-core

This commit is contained in:
Juan Tejada 2016-11-23 12:16:12 -08:00
parent eacbb99f15
commit f98c38fef4
2 changed files with 15 additions and 11 deletions

View file

@ -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

View file

@ -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);