diff --git a/package.json b/package.json index 672cbaadf..4b3ab2a8b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/nylas-api/routes/auth.js b/packages/nylas-api/routes/auth.js index 35e771915..fd14f4fc5 100644 --- a/packages/nylas-api/routes/auth.js +++ b/packages/nylas-api/routes/auth.js @@ -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, + }) ) ); }); diff --git a/packages/nylas-core/database-connector.js b/packages/nylas-core/database-connector.js index 5d95b16e1..d7459458a 100644 --- a/packages/nylas-core/database-connector.js +++ b/packages/nylas-core/database-connector.js @@ -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)