# pgnode

> PostgresSQL client to Nodejs servers

Latest version **1.1.5** (published 2023-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install pgnode
pnpm add pgnode
yarn add pgnode
bun add pgnode
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2023-12-13 |
| First published | 2021-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Hebert Cisco |
| Maintainers | tecnobert |
| Keywords | pg, postgres, connection, transaction, savepoint, database, db, tx |

## Links

- npm: https://www.npmjs.com/package/pgnode
- Repository: https://github.com/hebertcisco/pgnode
- Homepage: https://github.com/hebertcisco/pgnode#readme
- Issues: https://github.com/hebertcisco/pgnode/issues
- npm.io page: https://npm.io/package/pgnode

## Dependencies (1)

- [pg](https://npm.io/package/pg.md) ^8.7.3

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.1.5 (latest) — 2023-12-13
- 1.1.4 — 2023-07-22
- 1.1.3 — 2023-01-10
- 1.1.2 — 2022-07-25
- 1.1.1 — 2022-07-04
- 1.1.0 — 2022-01-19
- 1.0.9 — 2022-01-17
- 1.0.8 — 2022-01-06
- 1.0.7 — 2022-01-03
- 1.0.6 — 2021-12-28
- 1.0.5 — 2021-12-24
- 1.0.4 — 2021-12-24
- 1.0.3 — 2021-12-21
- 1.0.2 — 2021-12-20
- 1.0.1 — 2021-12-20
- … 10 more at https://npm.io/package/pgnode/versions

## README

<p align="center">
 <img width="100px" src="https://raw.githubusercontent.com/hebertcisco/pgnode/main/.github/images/favicon512x512-postgresql.png" align="center" alt=":package: postgresql" />
 <h2 align="center">:package: pgnode</h2>
 <p align="center">PostgresSQL client to Nodejs servers</p>

  <p align="center">
    <a href="https://github.com/hebertcisco/pgnode/issues">
      <img alt="Issues" src="https://img.shields.io/github/issues/hebertcisco/pgnode?style=flat&color=336791" />
    </a>
    <a href="https://github.com/hebertcisco/pgnode/pulls">
      <img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/hebertcisco/pgnode?style=flat&color=336791" />
    </a>
     <a href="https://github.com/hebertcisco/pgnode">
      <img alt="GitHub Downloads" src="https://img.shields.io/npm/dw/pgnode?style=flat&color=336791" />
    </a>
    <a href="https://github.com/hebertcisco/pgnode">
      <img alt="GitHub Total Downloads" src="https://img.shields.io/npm/dt/pgnode?color=336791&label=Total%20downloads" />
    </a>
 <br />
    <br />
  <a href="https://github.com/hebertcisco/pgnode">
      <img alt="GitHub release" src="https://img.shields.io/github/release/hebertcisco/pgnode.svg?style=flat&color=336791" />
    </a>
  <a href="https://www.npmjs.com/package/pg">
      <img alt="dependency pg" src="https://img.shields.io/github/package-json/dependency-version/hebertcisco/pgnode/pg?style=flat&color=336791" />
    </a>
 <br />
    <br />
    <br />
  <a href="https://github.com/hebertcisco/musiko-app/issues/new/choose">Report Bug</a>
  <a href="https://github.com/hebertcisco/musiko-app/issues/new/choose">Request Feature</a>
  </p>
 <h3 align="center">Systems on which it has been tested.</h3>
 <p align="center">
    <a href="https://ubuntu.com/download">
      <img alt="Ubuntu" src="https://img.shields.io/badge/Ubuntu-E95420?style=for-the-badge&logo=ubuntu&logoColor=white&style=flat" />
    </a>
  <a href="https://alpinelinux.org/">
      <img alt="Alpine_Linux" src="https://img.shields.io/badge/Alpine_Linux-0D597F?style=for-the-badge&logo=alpine-linux&logoColor=white&style=flat" />
    </a>
 <br />
    <br />
  <a href="https://www.debian.org/index.pt.html">
      <img alt="Debian" src="https://img.shields.io/badge/Debian-A81D33?style=for-the-badge&logo=debian&logoColor=white&style=flat" />
    </a>
  <a href="https://www.centos.org/">
      <img alt="CentOS" src="https://img.shields.io/badge/Cent%20OS-262577?style=for-the-badge&logo=CentOS&logoColor=white&style=flat" />
    </a>
 <br />
    <br />
  <a href="https://www.microsoft.com/pt-br/windows/">
      <img alt="Windows" src="https://img.shields.io/badge/Windows-0078D6?style=for-the-badge&logo=windows&logoColor=white&style=flat" />
    </a>
 <a href="https://www.apple.com/br/macos/">
      <img alt="Macos" src="https://img.shields.io/badge/mac%20os-000000?style=for-the-badge&logo=apple&logoColor=white&style=flat" />
    </a>
  </p>
<p align="center">Did you like the project? Please, considerate <a href="https://www.buymeacoffee.com/hebertcisco">a donation</a> to help improve!</p>

<p align="center"><strong>PostgresSQL client to Nodejs servers</strong>✨</p>

<p align="center">Connect your database easily using the pgnode package</p>

# Getting started

## Installation

To install the module in your project just run the command below:

```bash
npm i pgnode
```

or

```bash
yarn add pgnode
```

Now in your project just import the module like this:

```js
const pg = require("pgnode");
```

Or you can use import:

```js
import pg from "pgnode";
```

## Client connection

This is the simplest possible way to connect, query, and disconnect with async/await:

```ts
import pg, { Client, Pool } from "pgnode";

const config = {
  user: process.env.POSTGRES_USER,
  host: process.env.POSTGRES_HOST,
  database: process.env.POSTGRES_DATABASE,
  password: process.env.POSTGRES_PASSWORD,
  port: Number(process.env.POSTGRES_PORT),
};

const client = new pg.Client({ ...config });

function query(sql, params) {
  return client
    .connect()
    .then(() => client.query(sql, params))
    .then((res) => {
      client.end();
      return res;
    });
}
```

# Transactions (tx)

## Usage

```Typescript
import {tx, Client, Pool} from 'pgnode';

const client = new Client({
  user: process.env.POSTGRES_USER,
  host: process.env.POSTGRES_HOST,
  database: process.env.POSTGRES_DATABASE,
  password: process.env.POSTGRES_PASSWORD,
  port: Number(process.env.POSTGRES_PORT)
});

const pool = new Pool({...client});

export async function createTable(){
  return await tx(pool, async (db) => {
    await db.query(`
      CREATE TABLE IF NOT EXISTS test
      (
        id   SERIAL PRIMARY KEY,
        name TEXT NOT NULL
      );`);
  });
}

// or use a generator function to create the transactions

export function* createTableGenerator(){
  yield tx(pool, async (db) => {
    await db.query(`
      CREATE TABLE IF NOT EXISTS test
      (
        id   SERIAL PRIMARY KEY,
        name TEXT NOT NULL
      );`);
  });
  // create another transaction
  yield tx(pool, async (db) => {
    await db.query(`
      INSERT INTO test (name) VALUES ('test');`);
  });
}
```

# Features

- **Pure JavaScript** client and native libpq bindings share the same API
- Support all `tls.connect` options being passed to the client/pool constructor under the `ssl` option.
- Connection pooling
- **Extensible JS** ↔ PostgreSQL data-type coercion
- Supported PostgreSQL features
  - Parameterized queries
  - Named statements with query plan caching
  - Async notifications with `LISTEN/NOTIFY`
  - Bulk import & export with `COPY TO/COPY FROM`
  - Change default database name
  - make pg.Pool an es6 class
  - `pg.Client` and `pg.Pool` are ES6 classes
  - Support for `pg.Client.prototype.query` and `pg.Pool.prototype.query`
  - Support generator functions
  - Support for Nodejs `^v16x`

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