https://github.com/hebertcisco/pgnode
PostgresSQL client to Nodejs servers
https://github.com/hebertcisco/pgnode
database javascript nodejs pgsql-connection-pooling postgres postgresql postgressql typescript
Last synced: 6 months ago
JSON representation
PostgresSQL client to Nodejs servers
- Host: GitHub
- URL: https://github.com/hebertcisco/pgnode
- Owner: hebertcisco
- License: mit
- Created: 2021-06-15T00:42:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-24T00:50:41.000Z (over 1 year ago)
- Last Synced: 2025-05-15T19:42:38.057Z (8 months ago)
- Topics: database, javascript, nodejs, pgsql-connection-pooling, postgres, postgresql, postgressql, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/pgnode
- Size: 1.08 MB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
:package: pgnode
PostgresSQL client to Nodejs servers
Systems on which it has been tested.
Did you like the project? Please, considerate a donation to help improve!
PostgresSQL client to Nodejs servers✨
Connect your database easily using the pgnode package
# 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`