Skip to content

jimmyolo/fastify-mariadb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fastify MariaDB Pool plugin

js-standard-style CI npm version npm downloads

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.

v2.x

  • min requirements
    • node >= 10, fastify >= 3, mariadb >= 2.4.2

v3.x

  • min requirements
    • node >= 14, fastify >= 4, mariadb >= 3.1

Install

npm install fastify-mariadb --save

Usage

Add it to your project with register and you are done! This plugin will add the mariadb namespace in your Fastify instance, with the following properties:

pool: the pool instance
query: an utility to perform a query without a transaction
getConnection: get a connection from the pool

Example:

const fastify = require('fastify')()

fastify.register(require('fastify-mariadb'), {
  host: 'localhost',
  user: 'root',
  database: 'mysql',
  connectionLimit: 5
})

fastify.get('/user/:id', (req, reply) => {
  // `pool.getConnection` -> `conn.query` -> `conn.release`
  fastify.mariadb.getConnection((err, conn) => {
    if (err) return reply.send(err)
    conn.query('SELECT username FROM users WHERE id=?', [req.params.id], (err, result) => {
      conn.release()
      reply.send(err || result)
    })
  })
})

fastify.get('/mariadb/time', (req, reply) => {
  // `pool.query`
  fastify.mariadb.query('SELECT now()', (err, result) => {
    reply.send(err || result)
  })
})

fastify.listen({ port: 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.

Async await is supported, when register promise option is true:

const fastify = require('fastify')()

fastify.register(require('fastify-mariadb'), {
  promise: true,
  connectionString: 'mariadb://root@localhost/mysql'
})

fastify.get('/user/:id', async (req, reply) => {
  const mariadb = fastify.mariadb
  const connection = await mariadb.getConnection()
  const result = await mariadb.query('SELECT username FROM users WHERE id=?', [req.params.id])
  connection.release()
  return result[0]
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

options

  • promise - Boolean (optional, if not present will use callback style pool)
  • connectionString - String (optional, url string) For example: mariadb://user:pass@host/db?debug=true
  • Pool options - Pool options includes connection options that will be used when creating new connections.

MariaDB connector/Node.js most options are similar to mysql/mysql2 driver with more features and performant. More usage, please see mariadb-connector-nodejs

Acknowledgements

License

Licensed under MIT.