Skip to content

wowDaiver/postgres-pdo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Postgres Data Object

Pdo wrapper for https://github.com/porsager/postgres

Usage
Install
$ npm i -S postgres-pdo
Connect
const postgres = require('postgres');
const Pdo = require('postgres-pdo');

const options = {
    // Connection parameters
}

const sql = postgres(options);
const pdo = new Pdo(sql);
Select
const result = await pdo
    .select()
    .from('table_name')
    .whereIn('id', [1, 2])
    .orWhere('id', '=', 3)
    .execute();

console.log(result);
Insert
await pdo
    .insert(['name', 'age'])
    .into('table_name')
    .values(['John', 30])
    .execute();
Update
await pdo
    .update({
        name: 'John',
        age: 30
    })
    .table('table_name')
    .where('id', '=', 1)
    .execute();
Delete
await pdo
    .delete('table_name')
    .where('id', '>', 1)
    .execute();
Transactions
await pdo.transaction(async (trxPdo) => {
    const user = await trxPdo
        .select()
        .from('users')
        .where('uid', '=', 1)
        .forUpdate()
        .first()
        .execute();

    await trxPdo
        .update({
            name: 'Updated'
        })
        .table('users')
        .where('uid', '=', user.uid)
        .execute();
});
Row Locks
const row = await pdo
    .select()
    .from('jobs')
    .where('status', '=', 'pending')
    .forUpdate()
    .skipLocked()
    .first()
    .execute();

Available helpers:

  • forUpdate()
  • forShare()
  • skipLocked()
  • noWait()
  • first()
  • returningOne()
Fork / withClient
const trxPdo = pdo.withClient(trx);
const isolatedBuilder = pdo.fork();

About

Postgres Data Object

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors