# fastify-pg

> Fastify PostgreSQL Plugin

Latest version **0.2.0** (published 2021-03-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-pg
pnpm add fastify-pg
yarn add fastify-pg
bun add fastify-pg
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-03-24 |
| First published | 2020-01-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Artem Tolstykh |
| Maintainers | arttolstykh |
| Keywords | fastify, fastify-plugin, plugin, pg, postgres, postgresql, database, sql |

## Links

- npm: https://www.npmjs.com/package/fastify-pg
- Repository: https://github.com/arttolstykh/fastify-pg
- Homepage: https://github.com/arttolstykh/fastify-pg#readme
- Issues: https://github.com/arttolstykh/fastify-pg/issues
- npm.io page: https://npm.io/package/fastify-pg

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2021-03-24
- 0.1.10 — 2020-12-21
- 0.1.5 — 2020-11-19
- 0.1.4 — 2020-03-17
- 0.1.3 — 2020-01-24
- 0.1.2 — 2020-01-22
- 0.1.1 — 2020-01-22
- 0.1.0 — 2020-01-21

## README

```
      ##          ##
        ##      ##
      ##############
    ####  ######  ####
  ######################
  ##  ##############  ##
  ##  ##          ##  ##
        ####  ####
```

# fastify-pg 

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Fastify PostgreSQL plugin inspired by [fastify-postgres](https://github.com/fastify/fastify-postgres) and [typeorm](https://github.com/typeorm/typeorm).

Dependencies:
 - [fastify](https://github.com/fastify/fastify)
 - [fastify-plugin](https://github.com/fastify/fastify-plugin)
 - [pg](https://github.com/brianc/node-postgres)

# Install

```
npm i fastify fastify-plugin fastify-pg pg --save
```

# Usage

```js
const pg = require('fastify-pg')
```

or

```js
import pg from 'fastify-pg'
```

Register plugin

```js
fastify.register(pg, options)
```

This plugin will add the `pg` namespace in your Fastify instance, with the following properties:

```js
const {
  Client, // client constructor for a single query
  pool, // pool instance
  connect, // function to get a connection from the pool
  query, // utility to perform a query WITHOUT a transaction
  transaction, // utility to perform multiple queries WITH a transaction
  QueryBuilder // query builder
} = fastify.pg
```

For examples, please see the [fastify-postgres](https://github.com/fastify/fastify-postgres/blob/master/README.md) and [node-postgres](https://node-postgres.com/) documentation.

# QueryBuilder

```js
const { QueryBuilder, transaction } = fastify.pg
```

## .table(tableName)

```js
await QueryBuilder
  .of(fastify.pg)
  .table('users')
  .execute()
```

```
TABLE users;
```

## .insert()

```js
await QueryBuilder
  .of(fastify.pg)
  .insert()
  .into('users')
  .defaultValues()
  .returning(['*'])
  .execute()
```

```
INSERT INTO users DEFAULT VALUES RETURNING *;
```

## .select(elements)

```js
await QueryBuilder
  .of(fastify.pg)
  .select(['*'])
  .from('users')
  .where('users.id = :id', {id: 1})
  .andWhere('users.is_active = :is_active', {is_active: true})
  .orWhere('users.deleted = :deleted', {deleted: false})
  .getMany()
```

```
SELECT * FROM users WHERE users.id = 1 AND users.is_active = true OR users.deleted = false;
```

```js
await QueryBuilder
  .of(fastify.pg)
  .insert()
  .into('users')
  .columns(['first_name', 'last_name'])
  .values([[`'Artem'`, `'Tolstykh'`], [`'Maksym'`, `'Bezruchko'`]])
  .returning(['*'])
  .execute()
```

```
INSERT INTO users (first_name, last_name) VALUES ('Artem', 'Tolstykh'), ('Maksym', 'Bezruchko') RETURNING *;
```

## .update(tableName)

```js
await QueryBuilder
  .of(fastify.pg)
  .update('users')
  .set({first_name: `'Artem'`, last_name: `'Tolstykh'`})
  .where('id = :id', {id: 1})
  .execute()
```

```
UPDATE users SET first_name = 'Artem', last_name = 'Tolstykh' WHERE id = 1;
```

## .delete()

```js
await QueryBuilder
  .of(fastify.pg)
  .delete()
  .from('users')
  .execute()
```

```
DELETE FROM users;
```

```js
await QueryBuilder
  .of(fastify.pg)
  .delete()
  .from('users')
  .where('id = :id', {id: 1})
  .returning(['*'])
  .execute()
```

```
DELETE FROM users WHERE id = 1 RETURNING *;
```

## .innerJoin(tableName, expression)

```js
await QueryBuilder
  .of(fastify.pg)
  .select(['users.*', 'photos.*'])
  .from('users')
  .innerJoin('photos', 'photos.user = users.id')
  .andWhere('photos.isRemoved = :isRemoved', {isRemoved: false})
  .where('users.name = :username', {username: `'arttolstykh'`})
  .getOne()
```

```
SELECT users.*, photos.* FROM users INNER JOIN photos ON photos.user = users.id AND photos.isRemoved = false WHERE users.name = 'arttolstykh';
```

## transaction

```js
transaction(async client => {
    const id 
        = await QueryBuilder
        .of(client, ['arttolstykh'])
        .insert()
        .into('users')
        .columns(['username'])
        .values([['$1']])
        .returning(['id'])
        .execute()
    // etc
    return id
})
```

```
BEGIN;
INSERT INTO users (username) VALUES ('arttolstykh') RETURNING id;
-- etc
COMMIT;
```

## QueryBuilder API

- **of**
- **with**
- **withRecursive**
- **insert**
- **table**
- **select**
- **update**
- **delete**
- **into**
- **from**
- **innerJoin**
- **set**
- **where**
- **andWhere**
- **orWhere**
- **columns**
- **values**
- **defaultValues**
- **limit**
- **offset**
- **groupBy**
- **orderBy**
- **onConflict**
- **returning**
- **getQuery**
- **execute**
- **getOne**
- **getMany**

# License

Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/fastify-pg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
