mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-12-27 02:23:28 +08:00
[local-sync]: Let local-sync start, add loadModels to iso-core
This commit is contained in:
parent
3fac21458c
commit
5b7214e464
5 changed files with 50 additions and 98 deletions
|
@ -7,4 +7,5 @@ module.exports = {
|
|||
IMAPConnection: require('./src/imap-connection'),
|
||||
IMAPErrors: require('./src/imap-errors'),
|
||||
PromiseUtils: require('./src/promise-utils'),
|
||||
loadModels: require('./src/models'),
|
||||
}
|
||||
|
|
27
packages/isomorphic-core/src/models/index.js
Normal file
27
packages/isomorphic-core/src/models/index.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function loadModels(Sequelize, sequelize, modelsPath, {schema} = {}) {
|
||||
const db = {};
|
||||
const dirname = path.join(__dirname, modelsPath)
|
||||
for (const filename of fs.readdirSync(dirname)) {
|
||||
if (filename.endsWith('.js')) {
|
||||
let model = require(filename)(sequelize, Sequelize) // eslint-disable-line
|
||||
if (schema) {
|
||||
model = model.schema(schema);
|
||||
}
|
||||
db[model.name[0].toUpperCase() + model.name.substr(1)] = model;
|
||||
}
|
||||
}
|
||||
|
||||
Object.keys(db).forEach((modelName) => {
|
||||
if ("associate" in db[modelName]) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
|
||||
module.exports = loadModels
|
|
@ -2,7 +2,7 @@ import {createLogger} from './src/shared/logger'
|
|||
|
||||
export function activate() {
|
||||
global.Logger = createLogger('local-sync')
|
||||
require('./src/local-api/app.js');
|
||||
// require('./src/local-api/app.js');
|
||||
require('./src/local-sync-worker/app.js');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,7 @@
|
|||
const LocalDatabaseConnector = require('../shared/local-database-connector')
|
||||
|
||||
const prepareEnvironmentInfo = (callback) => {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const os = require('os')
|
||||
global.instanceId = os.hostname();
|
||||
callback();
|
||||
} else {
|
||||
const request = require('request')
|
||||
request('http://169.254.169.254/latest/meta-data/instance-id', (error, response, body) => {
|
||||
global.instanceId = body;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
prepareEnvironmentInfo(() => {
|
||||
const SyncProcessManager = require('./sync-process-manager')
|
||||
const manager = new SyncProcessManager();
|
||||
|
||||
|
@ -32,4 +19,3 @@ prepareEnvironmentInfo(() => {
|
|||
});
|
||||
|
||||
global.manager = manager;
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const Sequelize = require('sequelize');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {PromiseUtils} = require('isomorphic-core');
|
||||
const {loadModels, PromiseUtils} = require('isomorphic-core');
|
||||
const HookTransactionLog = require('./hook-transaction-log');
|
||||
const HookAccountCRUD = require('./hook-account-crud');
|
||||
const HookIncrementVersionOnSave = require('./hook-increment-version-on-save');
|
||||
|
@ -18,46 +18,7 @@ class LocalDatabaseConnector {
|
|||
this._cache = {};
|
||||
}
|
||||
|
||||
_readModelsInDirectory(sequelize, dirname, {schema} = {}) {
|
||||
const db = {};
|
||||
for (const filename of fs.readdirSync(dirname)) {
|
||||
if (filename.endsWith('.js')) {
|
||||
let model = sequelize.import(path.join(dirname, filename));
|
||||
if (schema) {
|
||||
model = model.schema(schema);
|
||||
}
|
||||
db[model.name[0].toUpperCase() + model.name.substr(1)] = model;
|
||||
}
|
||||
}
|
||||
|
||||
Object.keys(db).forEach((modelName) => {
|
||||
if ("associate" in db[modelName]) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
_sequelizePoolForDatabase(dbname) {
|
||||
if (process.env.DB_HOSTNAME) {
|
||||
return new Sequelize(dbname, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
|
||||
host: process.env.DB_HOSTNAME,
|
||||
dialect: "mysql",
|
||||
charset: 'utf8',
|
||||
logging: false,
|
||||
pool: {
|
||||
min: 1,
|
||||
max: 15,
|
||||
idle: 5000,
|
||||
},
|
||||
define: {
|
||||
charset: 'utf8',
|
||||
collate: 'utf8_general_ci',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return new Sequelize(dbname, '', '', {
|
||||
storage: path.join(STORAGE_DIR, `${dbname}.sqlite`),
|
||||
dialect: "sqlite",
|
||||
|
@ -74,26 +35,8 @@ class LocalDatabaseConnector {
|
|||
return this._cache[accountId];
|
||||
}
|
||||
|
||||
let newSequelize = null;
|
||||
|
||||
if (process.env.DB_HOSTNAME) {
|
||||
if (!this._accountsRootSequelize) {
|
||||
this._accountsRootSequelize = this._sequelizePoolForDatabase(`account_data`);
|
||||
}
|
||||
|
||||
// Create a new sequelize instance, but tie it to the same connection pool
|
||||
// as the other account instances.
|
||||
newSequelize = this._sequelizePoolForDatabase(`account_data`);
|
||||
newSequelize.dialect = this._accountsRootSequelize.dialect;
|
||||
newSequelize.config = this._accountsRootSequelize.config;
|
||||
newSequelize.connectionManager.close()
|
||||
newSequelize.connectionManager = this._accountsRootSequelize.connectionManager;
|
||||
} else {
|
||||
newSequelize = this._sequelizePoolForDatabase(`a-${accountId}`);
|
||||
}
|
||||
|
||||
const modelsPath = path.join(__dirname, 'models/account');
|
||||
const db = this._readModelsInDirectory(newSequelize, modelsPath, {schema: `a${accountId}`})
|
||||
const newSequelize = this._sequelizePoolForDatabase(`a-${accountId}`);
|
||||
const db = loadModels(Sequelize, newSequelize, 'account', {schema: `a${accountId}`})
|
||||
|
||||
HookTransactionLog(db, newSequelize);
|
||||
HookIncrementVersionOnSave(db, newSequelize);
|
||||
|
@ -120,18 +63,13 @@ class LocalDatabaseConnector {
|
|||
|
||||
destroyAccountDatabase(accountId) {
|
||||
const dbname = `a-${accountId}`;
|
||||
if (process.env.DB_HOSTNAME) {
|
||||
// todo
|
||||
} else {
|
||||
fs.removeFileSync(path.join(STORAGE_DIR, `${dbname}.sqlite`));
|
||||
}
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
_sequelizeForShared() {
|
||||
const sequelize = this._sequelizePoolForDatabase(`shared`);
|
||||
const modelsPath = path.join(__dirname, 'models/shared');
|
||||
const db = this._readModelsInDirectory(sequelize, modelsPath)
|
||||
const db = loadModels(Sequelize, sequelize, 'shared')
|
||||
|
||||
HookAccountCRUD(db, sequelize);
|
||||
|
||||
|
|
Loading…
Reference in a new issue