npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postgresql-node

v0.1.0

Published

PostgreSQL interface for Node.js with TypeScript to simplify common database interactions and error handling.

Downloads

12

Readme

postgresql-node

IMPORTANT: This repository is work in progress.

PostgreSQL interface for Node.js with TypeScript to simplify common database interactions and error handling. Built on top of the awesome pg-promise package.

Setup

Install package.

npm install postgresql-node

Import package and initialize database object with connection details and options.

import PostgresClient from 'postgresql-node';

const db = new PostgresClient({
    host: 'host',
    port: 5432,
    database: 'database',
    user: 'user',
    password: 'password'
});

Queries

Running a query against the database consists of two methods.

  1. Constructing the query
  2. Executing the constructed query and returning results

Both methods can be chained together. But it is important to know that the query won't be excecuted until one of the Query methods, e.g. .one(), .many() or .none() is called.

await db.query.run('SELECT NOW()').none();

RUN

db.query.add() can be used to run any query.

const users = await db.query.run('SELECT * FROM users').none();

FIND

The db.query.find() method is itended to simplify SELECT queries.

const users = await db.query.find('SELECT * FROM users').many();

This method provides additional helpers that come in handy with SELECT queries.

Filter

Pagination

Ordering

Parameters

UPDATE

db.query.update() method is itended to simplify UPDATE queries.

const data = {
    name: 'testUser';
    email: '[email protected]';
};
const users = await db.query.update(data).one();

This method provides additional helpers that come in handy with UPDATE queries.

Filter

Returning Results

Limit columns that are allowed to be updated

ADD

db.query.add() method is itended to simplify INSERT queries.

const data = {
    name: 'testUser';
    email: '[email protected]';
};
await db.query.add(data).none();

BATCH

All of the above query methods use a single connection pool that is closed after the query is executed.

const orders = await db.query.batch(async (t) => {
    const user = await t.find('SELECT * FROM USERS', { filter: 1 }).one();
    const orders = await t.find('SELECT * FROM orders', { filter: 1 }).many();
    return orders;
});

TRANSACTION

A transaction is all-or-nothing method which either runs all specified queries and committ them to the database or none and a rollback is executed. the .transaction() method automatically applied BEGIN and COMMIT or ROLLBACK commands.

const bb = await db.query.transaction(async (t) => {
    console.log(t.run('SELECT NOW()'));

    const a = await t.run('SELECT * FROM USERS where id =1').oneOrNone();
    await t.add({ name: 'd', rank, email: `${rank}@mail.com` }, 'users').none();
});

Repositories

Repositories provide further simplification to interact with one table in a database. They allow to centralize queries for the that table and specify parameters and return values as well as filters, columns and types that can be used with a query.

const db = new PostgresClient(connection);

const repos = db.addRepositories({
    user: UserRepo
});

type UserModel = {
    id: number;
    name: string;
    email: string;
};

class UserRepo extends DatabaseRepository<UserModel> {
    // specify table name for this repository
    table = 'users';
    // specify directory that contains SQL files for this repo
    sqlFilesDir = [__dirname, 'sqlFiles'];
    // specify filterSet that can be referenced within the Repo
    filterSet = this.filterSet({ id: 'INCLUDES' });
    // specify columnSet that can be referenced within the Repo
    columns = this.columnSet(['name', 'name', 'rank']);
    // specify columnSet that can be referenced within the Repo
    // if sqlFilesDir is specified, it is included
    queries = this.sqlFile('sqlFile.sql');

    find(id: number) {
        return this.query
            .find('SELECT * FROM users WHERE id = $<id>', { id })
            .one<UserModel>();
    }

    add(data: UserModel) {
        return this.query.add(data).one<UserModel>();
    }
}