Registry / database / fastify-at-mysql

fastify-at-mysql

JSON →
library0.4.0jsnpmunverified

Fastify MySQL plugin using @databases/mysql to prevent SQL injection attacks. Version 0.4.0 provides a wrapper that exposes mysql property on Fastify instance, supporting queries, transactions, and tasks with built-in SQL injection protection via tagged template literals. It offers result types: raw (default), iterator, and stream. The plugin is actively maintained, ships TypeScript types, and requires Fastify v4+ and @databases/mysql as peer dependencies.

npm install fastify-at-mysql
INSTALL
IMPORT
SIG · FASTIFY-AT-MYSQL
F
fastify-at-mysql
databasejavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import fastifyMysql from 'fastify-at-mysql'
const fastifyMysql = require('fastify-at-mysql').default
Package ships TypeScript types and supports both ESM and CJS.
fastifyMysql (CommonJS)
const fastifyMysql = require('fastify-at-mysql')
const { default: fastifyMysql } = require('fastify-at-mysql')
In CommonJS, require returns the default export directly.

Registers the MySQL plugin, then uses tagged template literals to safely query users.

import Fastify from 'fastify'; import fastifyMysql from 'fastify-at-mysql'; const app = Fastify(); app.register(fastifyMysql, { host: process.env.DB_HOST ?? 'localhost', port: parseInt(process.env.DB_PORT ?? '3306'), user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? 'test', }); app.get('/users', async (request, reply) => { const { rows } = await app.mysql.query(app.mysql.sql`SELECT * FROM users`); return rows; }); await app.listen({ port: 3000 });
Debug
Known issues
gotchaEnsure you use the `sql` tagged template from `fastify.mysql.sql` to prevent SQL injection, not raw strings.
fix
Use `fastify.mysql.sql` for all queries.
affects: >=0.1.0
breakingThe plugin expects Fastify v4+; using with Fastify v3 may cause register issues.
fix
Upgrade Fastify to v4+ or use an older version of the plugin if needed.
affects: >=0.4.0
deprecatedThe `query` method with `{ type: 'raw' }` is deprecated in favor of default behavior (raw).
fix
Remove `{ type: 'raw' }` option; raw is now default.
affects: >=0.3.0
gotchaWhen using `transaction`, each callback must be an async function or return a promise.
fix
Ensure transaction callbacks are async or return a promise.
affects: >=0.1.0
gotchaDo not import `mysql` directly from `@databases/mysql`; always use `fastify.mysql` from the plugin.
fix
Access MySQL through the Fastify instance: `fastify.mysql`.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: fastify.mysql is not a function
Plugin not registered or registration order wrong.
fix
Call `app.register(fastifyMysql, options)` before using `fastify.mysql`.
Error: connect ECONNREFUSED ::1:3306
MySQL not running or host/port incorrect.
fix
Ensure MySQL is running and accessible; check environment variables for DB_HOST and DB_PORT.
Cannot find module '@databases/mysql'
Missing peer dependency @databases/mysql.
fix
Run `npm install @databases/mysql`.
SyntaxError: Unexpected token using 'sql' tag
Using `sql` without importing or misusing as a regular function.
fix
Always use `fastify.mysql.sql` tagged template: `fastify.mysql.sql`SELECT * FROM contributors``.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
fastifyrequiredPeer dependency: plugin registers on Fastify instance
@databases/mysqlrequiredUnderlying MySQL client for safe queries
Agent activity
5 hits · last 30 days
node
4
Resources
fastify-at-mysql — npm install fastify-at-mysql · libregistry