From c0346f2e32411e6167201378ebf326ff5e03e880 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 17:17:26 -0800 Subject: [PATCH] Added iredmail class and used-quota model --- bin/iredmail.js | 45 +++++++++++++++++++++++++++++++++++ bin/server.js | 39 ++++++++---------------------- config/index.js | 10 +++++++- postgres/models/used-quota.js | 30 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 bin/iredmail.js create mode 100644 postgres/models/used-quota.js diff --git a/bin/iredmail.js b/bin/iredmail.js new file mode 100644 index 0000000..ccca338 --- /dev/null +++ b/bin/iredmail.js @@ -0,0 +1,45 @@ +/* + A class for connecting to and controlling the postgres database of an iredmail + email server. +*/ + +'use strict' + +const Sequelize = require('sequelize') +const config = require('../config') + +// Models +const UsedQuota = require('../postgres/models/used-quota') + +class IRedMail { + constructor () { + // Instantiate and initialize sequelize. + this.sequelize = this.initSequelize() + + // Load the models + this.models = { + usedQuota: new UsedQuota(this.sequelize) + } + } + + // Returns an instance of the sequelize class, configured for the Postgres + // database defined in the config object. + initSequelize () { + return new Sequelize('vmail', config.postgresUser, config.postgresPass, { + host: config.postgresHost, + dialect: 'postgres', + + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000 + }, + + // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators + operatorsAliases: false + }) + } +} + +module.exports = IRedMail diff --git a/bin/server.js b/bin/server.js index 53c6175..ddaa3f9 100644 --- a/bin/server.js +++ b/bin/server.js @@ -8,7 +8,8 @@ const passport = require('koa-passport') const mount = require('koa-mount') const serve = require('koa-static') const cors = require('kcors') -const Sequelize = require('sequelize') + +const IRedMail = require('./iredmail') const config = require('../config') const errorMiddleware = require('../src/middleware') @@ -53,41 +54,20 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) - runTest() + const iRedMail = new IRedMail() + await iRedMail.sequelize.authenticate() + console.log('Connection to the iRedMail postgres database has been established successfully.') + + // Attach to iRedMail instantiation to the context object. + app.context.iRedMail = iRedMail return app } // startServer() +/* async function runTest () { try { - const sequelize = new Sequelize('vmail', config.postgresUser, config.postgresPass, { - host: config.postgresHost, - dialect: 'postgres', - - pool: { - max: 5, - min: 0, - acquire: 30000, - idle: 10000 - }, - - // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators - operatorsAliases: false - }) - - const UsedQuota = sequelize.define( - 'used_quota', - { - username: { type: Sequelize.STRING, primaryKey: true }, - bytes: Sequelize.BIGINT, - messages: Sequelize.BIGINT, - domain: Sequelize.STRING - }, - { - timestamps: false - } - ) await sequelize.authenticate() @@ -102,6 +82,7 @@ async function runTest () { console.log(`Error: `, err) } } +*/ // export default app // module.exports = app diff --git a/config/index.js b/config/index.js index db7fcb9..395a90d 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,14 @@ -const common = require('./env/common') +// Attempt to include a common.js config file, if it exists. +let common +try { + common = require('./env/common') +} catch (err) {} +// Default to development environment. const env = process.env.NODE_ENV || 'development' + +// Load the config object for the selected environment. const config = require(`./env/${env}`) +// Merge into a single object and export. module.exports = Object.assign({}, common, config) diff --git a/postgres/models/used-quota.js b/postgres/models/used-quota.js new file mode 100644 index 0000000..b8185b7 --- /dev/null +++ b/postgres/models/used-quota.js @@ -0,0 +1,30 @@ +/* + UsedQuota table in the iRedMail postgres database. + + The constructor cxpects an instantiation of the sequalize class. Returns a + Sequelize model. +*/ + +'use strict' + +const Sequelize = require('sequelize') + +class UsedQuota { + // Expects an instantiation of the sequalize class. Returns a Sequelize model. + constructor (sequelize) { + return sequelize.define( + 'used_quota', + { + username: { type: Sequelize.STRING, primaryKey: true }, + bytes: Sequelize.BIGINT, + messages: Sequelize.BIGINT, + domain: Sequelize.STRING + }, + { + timestamps: false + } + ) + } +} + +module.exports = UsedQuota