Move fastify decorators to before start

This commit is contained in:
Robbie Antenesse 2020-06-14 13:20:27 -06:00
parent 9004388f0a
commit e94cb890f5
3 changed files with 14 additions and 9 deletions

View File

@ -26,11 +26,13 @@ const fastifyNodemailer = (fastify, options, next) => {
try {
transporter = createTransport(options);
} catch (err) {
fastify.decorate('canEmail', false);
return next(err);
}
fastify
.decorate('nodemailer', transporter)
.decorate('canEmail', true)
.addHook('onClose', close);
next();

View File

@ -21,13 +21,14 @@ const fp = require('fastify-plugin');
const Sequelize = require('sequelize');
function plugin (fastify, options) {
const instance = options.instance || 'sequelize';
const autoConnect = options.autoConnect || true;
const { config, registerModels } = options;
const instance = config.instance || 'sequelize';
const autoConnect = config.autoConnect || true;
delete options.instance;
delete options.autoConnect;
delete config.instance;
delete config.autoConnect;
const sequelize = new Sequelize(options);
const sequelize = new Sequelize(config);
if (autoConnect) {
return sequelize.authenticate().then(decorate);
@ -39,6 +40,7 @@ function plugin (fastify, options) {
function decorate () {
fastify.decorate(instance, sequelize);
fastify.decorate('models', registerModels(sequelize));
fastify.addHook('onClose', (fastifyInstance, done) => {
sequelize.close()
.then(done)

View File

@ -45,10 +45,14 @@ switch (fastify.siteConfig.db_engine) {
sequelizeConfig.password = fastify.siteConfig.db_password;
}
}
fastify.register(require('./fastify-plugins/fastify-sequelize'), sequelizeConfig);
fastify.register(require('./fastify-plugins/fastify-sequelize'), {
config: sequelizeConfig,
registerModels: require('./sequelize/models'),
});
if (!fastify.siteConfig.email_host || !fastify.siteConfig.email_username) {
console.warn('###\nNo email server set up. You will not be able to send emails without entering your email configuration.\n###');
fastify.decorate('canEmail', false);
} else {
fastify.register(require('./fastify-plugins/fastify-nodemailer'), {
pool: true,
@ -103,7 +107,4 @@ fastify.listen(fastify.siteConfig.port, function (err, address) {
fastify.log.error(err);
process.exit(1);
}
fastify.decorate('canEmail', typeof fastify.nodemailer !== 'undefined');
fastify.decorate('models', require('./sequelize/models')(fastify.sequelize));
});