I'm using Sequelize and fastify to do an API and would like to not have to log in for every request. So for that I had the idea of connecting when my web server (fastify) starts. But I can't recover the connection with the models etc to use my database in the other files.
Code for connection:
// Start the server
try {
fastify.listen(config.fastify.server.port, config.fastify.server.host).then(() => {
console.log(`Server listening on http://${config.fastify.server.host}:${config.fastify.server.port}`)
// Connection to the database
this.sequelize = new Sequelize(config.database.name, config.database.user, config.database.password, {
host: config.database.host,
dialect: config.database.dialect,
port: config.database.port,
connectionLimit: 10,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define: {
timestamps: false
}
});
this.sequelize.define('User', models.User);
})
} catch (err) {
console.log(err)
process.exit(1)
}