Fastify Knex connection plugin, this plugin will allow you to use many different relational database wrappers from Knex.
List of supported databases wrapper:
- MySQL
- PostgreSQL
- SQLite
- OracleDB
- Maria
- MS SQL
Under the hood the knex is used, the options that you pass to register
will be passed to the Knex object.
npm i fastify-knex --save
Add it to you project with register
and you are done!
This plugin will add the knex
namespace in your Fastify instance.
function registerPlugins(fastify, opts, next) {
fastify.register(require('fastify-knex'), {
client: 'pg',
debug: true // Knex debug SQL
connection: 'psql://angel:Som3p@ss@localhost:5432/angel'
});
fastify.register(registerRoutes);
done();
}
// Now in your routes
function registerRoutes(fastify, opts, next) {
fastify.get('/', async req => {
// Get Knex object here..
const users = await fastify.knex.select().from('users');
return users;
});
}
- Add test cases
- Add examples
Licensed under MIT.