FMDB ~ Fastify MariaDB plugin
Fastify MariaDB connection Pool plugin, with this you can share the same MariaDB connection pool in every part of your server.
Under the hood the official MariaDB Node.js connector is used, the options that you pass to register
will be passed to the MariaDB pool builder.
Install
npm i @adminy/fmdb
Usage
Add it to your project with register
and you are done!
This plugin will add the db
namespace in your Fastify requests, with the following properties:
query: an utility to perform a query without a transaction
format: a way to swap out ? for variables
escape: unwrap variables in sql statements to just fields by type
escapeId: put quotes around named field ids
Example:
const fastify = require('fastify')()
fastify.register(require('@adminy/fmdb'), {
host: 'localhost',
user: 'root',
database: 'mysql',
connectionLimit: 5
})
fastify.get('/user/:id', (req, reply) => {
// `pool.getConnection` -> `conn.query` -> `conn.release`
req.db.query('SELECT username FROM users WHERE id=?', [req.params.id], (err, result) => {
reply.send(err || result)
})
})
fastify.listen(3000, (err) => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
As you can see there is no need to close the client, since is done internally.
const fastify = require('fastify')()
fastify.register(require('@adminy/fmdb'), {
promise: true,
connectionString: 'mariadb://root@localhost/mysql'
})
fastify.get('/user/:id', async req => {
return await req.db.query('SELECT username FROM users WHERE id=?', [req.params.id])
})
fastify.listen(3000, (err) => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
options
Either a connection String e.g: mariadb://user:pass@host/db?debug=true
or Pool options.
MariaDB connector/Node.js
most options are similar to mysql/mysql2
driver with more features and performant.
More usage, please see mariadb-connector-nodejs