Mailspring/packages/nylas-core/database-connector.js

116 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-06-19 18:02:32 +08:00
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
2016-06-24 07:28:51 +08:00
const HookTransactionLog = require('./hook-transaction-log');
const HookAccountCRUD = require('./hook-account-crud');
const HookIncrementVersionOnSave = require('./hook-increment-version-on-save');
2016-06-19 18:02:32 +08:00
require('./database-extensions'); // Extends Sequelize on require
const STORAGE_DIR = path.join(__dirname, '..', '..', 'storage');
2016-06-19 18:02:32 +08:00
if (!fs.existsSync(STORAGE_DIR)) {
fs.mkdirSync(STORAGE_DIR);
}
class DatabaseConnector {
2016-06-19 18:02:32 +08:00
constructor() {
this._pools = {};
}
_readModelsInDirectory(sequelize, dirname) {
const db = {};
for (const filename of fs.readdirSync(dirname)) {
if (filename.endsWith('.js')) {
const model = sequelize.import(path.join(dirname, filename));
db[model.name[0].toUpperCase() + model.name.substr(1)] = model;
2016-06-19 18:02:32 +08:00
}
}
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",
logging: false,
});
}
2016-07-08 06:25:45 +08:00
return new Sequelize(dbname, '', '', {
storage: path.join(STORAGE_DIR, `${dbname}.sqlite`),
2016-06-19 18:02:32 +08:00
dialect: "sqlite",
logging: false,
})
}
2016-06-19 18:02:32 +08:00
_sequelizeForAccount(accountId) {
if (!accountId) {
return Promise.reject(new Error(`You need to pass an accountId to init the database!`))
}
const sequelize = this._sequelizePoolForDatabase(`a-${accountId}`);
2016-06-19 18:02:32 +08:00
const modelsPath = path.join(__dirname, 'models/account');
const db = this._readModelsInDirectory(sequelize, modelsPath)
HookTransactionLog(db, sequelize);
HookIncrementVersionOnSave(db, sequelize);
2016-06-19 18:02:32 +08:00
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.accountId = accountId;
2016-06-19 18:02:32 +08:00
return sequelize.authenticate().then(() =>
sequelize.sync()
).thenReturn(db);
}
forAccount(accountId) {
this._pools[accountId] = this._pools[accountId] || this._sequelizeForAccount(accountId);
return this._pools[accountId];
}
prepareAccountDatabase(accountId) {
const dbname = `a-${accountId}`;
if (process.env.DB_HOSTNAME) {
const sequelize = new Sequelize(null, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: process.env.DB_HOSTNAME,
dialect: "mysql",
logging: false,
})
return sequelize.authenticate().then(() =>
sequelize.query(`CREATE DATABASE \`${dbname}\``)
);
}
return Promise.resolve()
}
2016-06-19 18:02:32 +08:00
_sequelizeForShared() {
const sequelize = this._sequelizePoolForDatabase(`shared`);
2016-06-19 18:02:32 +08:00
const modelsPath = path.join(__dirname, 'models/shared');
const db = this._readModelsInDirectory(sequelize, modelsPath)
HookAccountCRUD(db, sequelize);
2016-06-19 18:02:32 +08:00
db.sequelize = sequelize;
db.Sequelize = Sequelize;
return sequelize.authenticate().then(() =>
sequelize.sync()
).thenReturn(db);
}
forShared() {
this._pools.shared = this._pools.shared || this._sequelizeForShared();
return this._pools.shared;
}
}
module.exports = new DatabaseConnector()