2020-01-05 03:11:57 +01:00
|
|
|
/* Copied from https://github.com/lyquocnam/fastify-sequelize/blob/master/index.js
|
|
|
|
* Copied so custom Sequelize version could be used without needing to wait for non-updated
|
|
|
|
* project to update version in NPM.
|
|
|
|
* Example Usage:
|
|
|
|
* const sequelizeConfig = {
|
|
|
|
instance: 'sequelize', // the name of fastify plugin instance. Accessed via fastify.sequelize by default
|
|
|
|
autoConnect: true, // auto authentication and test connection on first run
|
|
|
|
|
|
|
|
// other sequelize config goes here
|
|
|
|
dialect: 'sqlite',
|
|
|
|
|
|
|
|
// SQLite only
|
|
|
|
storage: 'path/to/db.sqlite'
|
|
|
|
}
|
|
|
|
* instance: (optional) the name of instance will be mapped to fastify, default is sequelize
|
|
|
|
* autoConnect: default: true auto authentication and test connection on first run.
|
|
|
|
* sequelizeConfig: all sequelize configurations, you can see [here](http://docs.sequelizejs.com/manual/installation/getting-started.html#setting-up-a-connection).
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fp = require('fastify-plugin');
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
|
|
|
function plugin (fastify, options) {
|
2020-06-14 21:20:27 +02:00
|
|
|
const { config, registerModels } = options;
|
|
|
|
const instance = config.instance || 'sequelize';
|
|
|
|
const autoConnect = config.autoConnect || true;
|
2020-01-05 03:11:57 +01:00
|
|
|
|
2020-06-14 21:20:27 +02:00
|
|
|
delete config.instance;
|
|
|
|
delete config.autoConnect;
|
2020-01-05 03:11:57 +01:00
|
|
|
|
2020-06-14 21:20:27 +02:00
|
|
|
const sequelize = new Sequelize(config);
|
2020-01-05 03:11:57 +01:00
|
|
|
|
|
|
|
if (autoConnect) {
|
|
|
|
return sequelize.authenticate().then(decorate);
|
|
|
|
}
|
|
|
|
|
|
|
|
decorate();
|
|
|
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
|
|
|
function decorate () {
|
|
|
|
fastify.decorate(instance, sequelize);
|
2020-06-14 21:20:27 +02:00
|
|
|
fastify.decorate('models', registerModels(sequelize));
|
2020-01-05 03:11:57 +01:00
|
|
|
fastify.addHook('onClose', (fastifyInstance, done) => {
|
|
|
|
sequelize.close()
|
|
|
|
.then(done)
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = fp(plugin);
|