npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-at-mysql

v0.4.0

Published

Fastify MySQL to prevent SQL injection attacks

Downloads

50

Readme

fastify-at-mysql

Continuous Integration

Fastify MySQL alternative plugin.

Installation

npm install fastify-at-mysql

Getting Started

The fastify-at-mysql plugin is a wrapper around the @databases/mysql package. It exposes the mysql property on the Fastify instance.

const Fastify = require('fastify')
const fatsifyMysql = require('fastify-at-mysql')

const fastify = fastify()
fastify.register(fatsifyMysql, {
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
})

fastify.get('/', async (request, reply) => {
  const result = await fastify.mysql.query('SELECT * FROM contributors')  
  reply.send(result)
})

Instance

const db = {
  query,        // use this to create queries in a simple way
  transaction,  // use this to create transactions
  task,         // use this to create tasks
  sql,          // method to create queries in a safe-way
  db,           // database object
}

Query

The query property automatically wraps the sql method. It gives you a powerful and flexible way of creating queries without opening yourself to SQL Injection attacks. Read more here

const result = await fastify.mysql.query(sql`SELECT * FROM contributors`)

You can also specify the type of the result between: raw, iterator, stream:

Raw

const result = await fastify.mysql.query(sql`SELECT * FROM contributors`, { type: 'raw' }) // default
console.log(result) // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]

Iterator

for await (const row of fastify.mysql.query(sql`SELECT * FROM contributors`, { type: 'iterator' })) {
  console.log(row) // { id: 1, name: 'John' }
}

Stream

const { Transform } = require('node:stream')
const stringify = new Transform({
  writableObjectMode: true,
  transform (chunk, _, callback) {
    this.push(JSON.stringify(chunk) + '\n')
    callback()
  }
})

const stream = fastify.mysql.query(sql`SELECT * FROM contributors`, { type: 'stream' })
stream.pipe(stringify).pipe(process.stdout) // { id: 1, name: 'John' }

Transaction

The transaction function is used to execute multiple queries in a single transaction. Read more here

const txs = [
  (db) => db.query(fastify.mysql.sql`INSERT INTO contributors (name) VALUES ('John')`),
  (db) => db.query(fastify.mysql.sql`INSERT INTO contributors (name) VALUES ('Jane')`),
]

const result = await fastify.mysql.transaction(txs)

Task

The task function is used to execute a single set of operations as a single task. Read more here

const task = (db) => {
  return db.query(fastify.mysql.sql`INSERT INTO contributors (name) VALUES ('John')`)
}

const result = await fastify.mysql.task(task)

Options

The plugin accepts the following options:

  • host - The hostname of the database you are connecting to. (Default: localhost)
  • port - The port number to connect to. (Default: 3306)
  • user - The MySQL user to authenticate as.
  • password - The password of that MySQL user.
  • database - Name of the database to use for this connection (Optional).
  • connectionString - A connection string to use instead of the connection options. (Optional)
  • name - Name of the database instance if you want to use multiple databases. (Optional)

License

fastify-at-mysql is licensed under the MIT license.