Allow usage with mysql in addition to SQLite

This commit is contained in:
Ben Gotow 2016-07-07 11:37:55 -07:00
parent 40ab07cfdf
commit 822b8e54e6
3 changed files with 40 additions and 18 deletions

View file

@ -5,6 +5,7 @@
"main": "",
"dependencies": {
"bluebird": "3.x.x",
"mysql": "^2.11.1",
"redis": "2.x.x",
"rx": "4.x.x",
"sequelize": "3.x.x",

View file

@ -9,8 +9,6 @@ const {
DatabaseConnector,
SyncPolicy,
Provider,
PubsubConnector,
MessageTypes,
} = require('nylas-core');
const {GMAIL_CLIENT_ID, GMAIL_CLIENT_SECRET, GMAIL_REDIRECT_URL} = process.env;
@ -56,10 +54,11 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => {
account.setCredentials(credentials);
return account.save().then((saved) =>
AccountToken.create({
accountId: saved.id,
}).then((token) =>
Promise.resolve({account: saved, token: token})
AccountToken.create({accountId: saved.id}).then((token) =>
DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({
account: saved,
token: token,
})
)
);
});

View file

@ -34,16 +34,27 @@ class DatabaseConnector {
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,
});
}
return Sequelize(dbname, '', '', {
storage: path.join(STORAGE_DIR, `${dbname}.sqlite`),
dialect: "sqlite",
logging: false,
})
}
_sequelizeForAccount(accountId) {
if (!accountId) {
return Promise.reject(new Error(`You need to pass an accountId to init the database!`))
}
const sequelize = new Sequelize(accountId, '', '', {
storage: path.join(STORAGE_DIR, `a-${accountId}.sqlite`),
dialect: "sqlite",
logging: false,
});
const sequelize = this._sequelizePoolForDatabase(`a-${accountId}`);
const modelsPath = path.join(__dirname, 'models/account');
const db = this._readModelsInDirectory(sequelize, modelsPath)
@ -64,13 +75,24 @@ class DatabaseConnector {
return this._pools[accountId];
}
_sequelizeForShared() {
const sequelize = new Sequelize('shared', '', '', {
storage: path.join(STORAGE_DIR, 'shared.sqlite'),
dialect: "sqlite",
logging: false,
});
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()
}
_sequelizeForShared() {
const sequelize = this._sequelizePoolForDatabase(`shared`);
const modelsPath = path.join(__dirname, 'models/shared');
const db = this._readModelsInDirectory(sequelize, modelsPath)