Added iredmail class and used-quota model

This commit is contained in:
Chris Troutner
2019-01-21 17:17:26 -08:00
parent 04ea33cd96
commit c0346f2e32
4 changed files with 94 additions and 30 deletions
+45
View File
@@ -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
+10 -29
View File
@@ -8,7 +8,8 @@ const passport = require('koa-passport')
const mount = require('koa-mount') const mount = require('koa-mount')
const serve = require('koa-static') const serve = require('koa-static')
const cors = require('kcors') const cors = require('kcors')
const Sequelize = require('sequelize')
const IRedMail = require('./iredmail')
const config = require('../config') const config = require('../config')
const errorMiddleware = require('../src/middleware') const errorMiddleware = require('../src/middleware')
@@ -53,41 +54,20 @@ async function startServer () {
await app.listen(config.port) await app.listen(config.port)
console.log(`Server started on ${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 return app
} }
// startServer() // startServer()
/*
async function runTest () { async function runTest () {
try { 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() await sequelize.authenticate()
@@ -102,6 +82,7 @@ async function runTest () {
console.log(`Error: `, err) console.log(`Error: `, err)
} }
} }
*/
// export default app // export default app
// module.exports = app // module.exports = app
+9 -1
View File
@@ -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' const env = process.env.NODE_ENV || 'development'
// Load the config object for the selected environment.
const config = require(`./env/${env}`) const config = require(`./env/${env}`)
// Merge into a single object and export.
module.exports = Object.assign({}, common, config) module.exports = Object.assign({}, common, config)
+30
View File
@@ -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