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 { try {
transporter = createTransport(options); transporter = createTransport(options);
} catch (err) { } catch (err) {
fastify.decorate('canEmail', false);
return next(err); return next(err);
} }
fastify fastify
.decorate('nodemailer', transporter) .decorate('nodemailer', transporter)
.decorate('canEmail', true)
.addHook('onClose', close); .addHook('onClose', close);
next(); next();

View File

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

View File

@ -45,10 +45,14 @@ switch (fastify.siteConfig.db_engine) {
sequelizeConfig.password = fastify.siteConfig.db_password; 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) { 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###'); 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 { } else {
fastify.register(require('./fastify-plugins/fastify-nodemailer'), { fastify.register(require('./fastify-plugins/fastify-nodemailer'), {
pool: true, pool: true,
@ -103,7 +107,4 @@ fastify.listen(fastify.siteConfig.port, function (err, address) {
fastify.log.error(err); fastify.log.error(err);
process.exit(1); process.exit(1);
} }
fastify.decorate('canEmail', typeof fastify.nodemailer !== 'undefined');
fastify.decorate('models', require('./sequelize/models')(fastify.sequelize));
}); });