2.0.0 • Published 1 year ago

fastify-mariadb v2.0.0

Weekly downloads
44
License
MIT
Repository
github
Last release
1 year ago

Fastify MariaDB Pool plugin

Build Status codecov Greenkeeper badge 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 offical MariaDB Node.js connector is used, the options that you pass to register will be passed to the MariaDB pool builder.

Install

npm install fastify-mariadb --save

Usage

Add it to you 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

# synchronous SQL format and escape for MySQL/MariaDB
sqlstring: {
  format: an utility to generate SQL string
  escape: an utility to escape query values
  escapeId: an utility to escape query identifiers
}

Example:

const fastify = require('fastify');

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

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

  // or, just use `pool.query`
  fastify.get('/ping/mariadb', (req, reply) => {
  fastify.mariadb.query('SELECT now()', function onResult (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.

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 connection = await fastify.mariadb.getConnection();
  const result = await connection.query(
    'SELECT id, username FROM users WHERE id=?', [req.params.id],
  );
  connection.release();
  return result[0];
});

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

options

Acknowledgements

Most of code is copy from fastify-mysql. Since some MariaDB connetor/Node.js is not compatible to mysql or mysql2.

License

Licensed under MIT.

3.0.0-beta.1

1 year ago

2.0.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0-rc2

5 years ago

1.0.0-rc1

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago