https://github.com/spa5k/fastify-slonik
Fastify Slonik Plugin to help access slonik from anywhere in a fastify app.
https://github.com/spa5k/fastify-slonik
fastify fastify-plugin hacktoberfest postgres postgresql slonik typescript
Last synced: 3 months ago
JSON representation
Fastify Slonik Plugin to help access slonik from anywhere in a fastify app.
- Host: GitHub
- URL: https://github.com/spa5k/fastify-slonik
- Owner: spa5k
- License: mit
- Created: 2021-07-30T16:47:06.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2026-02-24T01:41:55.000Z (4 months ago)
- Last Synced: 2026-02-24T08:15:07.251Z (4 months ago)
- Topics: fastify, fastify-plugin, hacktoberfest, postgres, postgresql, slonik, typescript
- Language: TypeScript
- Homepage:
- Size: 543 KB
- Stars: 7
- Watchers: 0
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify Slonik
A [Fastify](https://www.fastify.io/) plugin that uses the PostgreSQL client, [Slonik](https://www.npmjs.com/package/slonik). Slonik abstracts repeating code patterns, protects against unsafe connection handling and value interpolation, and provides a rich debugging experience.
For fastify v4, use latest version, for fastify v3, use v1.4x.
[](https://www.npmjs.com/package/fastify-slonik)
[](https://www.npmjs.com/package/fastify-slonik)

## Usage
Yarn
```sh
yarn add fastify-slonik
```
NPM
```sh
npm i fastify-slonik
```
PNPM
```sh
pnpm i fastify-slonik
```
## Example:
### Import the Plugin
Both default export and named export option is available.
```js
// index.js
const { fastifySlonik } = require("fastify-slonik");
// Or
const fastifySlonik = require("fastify-slonik");
```
or
```js
import { fastifySlonik } from "fastify-slonik";
// or
import fastifySlonik from "fastify-slonik";
```
### Register the Plugin
```ts
// register fastify-slonik
try {
await app.register(fastifySlonik, {
connectionString: process.env.DATABASE_URL,
});
} catch (err) {
console.log("🔴 Failed to connect, check your Connection string");
throw new Error(err);
}
```
### Using the plugin through decorators
FastifyInstance (this) and FastifyRequest (request) have been decorated with slonik and sql.
Use it the way you want.
```ts
// setup test route
// The decorated Fastify server is bound to this in route route handlers:
fastify.get('/users', async function (this, request, reply) {
const { sql, slonik } = this;
const queryText = sql`SELECT * FROM users WHERE user_id = 1`
const user = await slonik.query(queryText)
reply.send(user)
}
```
### Another way to access the Slonik and SQL decorator is through the request object-
```ts
fastify.get('/users', async function (request, reply) {
const { sql, slonik } = request
const queryText = sql`SELECT * FROM users WHERE user_id = 1`
const user = await slonik.query(queryText)
reply.send(user)
}
```
[Docs for This](https://www.fastify.io/docs/latest/Reference/Decorators/#decoratename-value-dependencies)
View [Slonik API](https://github.com/gajus/slonik#slonik-usage-api) for details.
## Development and Testing
[Tap](https://node-tap.org/) is used for testing. Use `pnpm test` command to run tests.
### Docker approach
```
$ docker-compose up
```
To run the tests:
- Create .env `cp .env.example .env`
```
$ yarn test
```
### Custom Postgres approach
1. Set up a database of your choice in a postgres server of your choice
2. Create the required table using
```sql
CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);
```
3. Create .env `cp .env.example .env` and update environment variables accordingly
## License
Licensed under [MIT](./LICENSE).