PostgresSQL client to Nodejs servers
npm install pgnode
![]()
PostgresSQL client to Nodejs servers
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
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";
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;
});
}
`
`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'););`
});
}
- 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.LISTEN/NOTIFY
- Connection pooling
- Extensible JS ↔ PostgreSQL data-type coercion
- Supported PostgreSQL features
- Parameterized queries
- Named statements with query plan caching
- Async notifications with COPY TO/COPY FROM
- Bulk import & export with pg.Client
- Change default database name
- make pg.Pool an es6 class
- and pg.Pool are ES6 classespg.Client.prototype.query
- Support for and pg.Pool.prototype.query^v16x`
- Support generator functions
- Support for Nodejs