Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/herrmannplatz/fastify-postgresjs
Fastify postgres.js plugin
https://github.com/herrmannplatz/fastify-postgresjs
database fastify-plugin postgresql
Last synced: 2 months ago
JSON representation
Fastify postgres.js plugin
- Host: GitHub
- URL: https://github.com/herrmannplatz/fastify-postgresjs
- Owner: herrmannplatz
- Created: 2020-02-02T14:15:35.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T06:17:32.000Z (about 2 years ago)
- Last Synced: 2024-11-09T03:02:40.735Z (3 months ago)
- Topics: database, fastify-plugin, postgresql
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fastify-postgresjs
- Size: 2.4 MB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fastify-postgresjs
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/fastify-postgres.svg?branch=master)](https://travis-ci.org/herrmannplatz/fastify-postgresjs)
Fastify PostgreSQL connection plugin, based on [postgres](https://github.com/porsager/postgres).
## Install
```
npm i postgres fastify-postgresjs --save
```## Usage
Add it to you project with `register` and you are done!
This plugin will add the `sql` namespace in your Fastify instance.Example:
```js
const fastify = require('fastify')()const url = 'postgres://postgres@localhost/postgres'
const options = { /* postgres.js options */ }
fastify.register(require('fastify-postgresjs'), {
url, ...options
})fastify.get('/users/:id', async (req, reply) => {
const users = await fastify.sql`
select * from users
where id = ${req.params.id}
`
return users
})fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```## Development and Testing
First, start postgres with:
```
$ docker run --rm -d -p 5432:5432 --name fastify-postgresjs postgres:11-alpine
```Run the tests.
```
$ npm test
```