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

@try-catch-80/pgres

v1.0.9

Published

Just another ORM

Downloads

2

Readme

PGres

GitHub package.json version GitHub code size in bytes

Introduction

Pgres is just another ORM used for querying postgres databases. It is made to make the setup process very easy. This package depends on the package pg-promise.

NOTE: Pgres can only be used to do CRUD operations on the database. It does not support any schema migration.

Usage

Make a connection object as below:

import { DBConnection } from "@try-catch-80/pgres";

const connection = new DBConnection({
  host: 'localhost',
  port: 5432,
  user: '<your_username>',
  password: '<your_password>',
  database: '<your_dbname>'
});

*** For NextJS applications, you might get a warning in your console,

  WARNING: Creating a duplicate database object for the same connection.

In this case use DBConnectionSingleton instead of DBConnection.


Make models according to your database by extending the model class from BaseModel:

import { BaseModel } from "@try-catch-80/pgres";

export class YourModel extends BaseModel {
  constructor({
    name = '', // these are just named parameters. implement this as you like
    email = '',
    description = ''
  } = {}) {
    const data = {
      name,
      email,
      description
    };

    super({ table: 'users', data, connection }); // You have to call super() to pass the necessary data to BaseModel
    
    /*
      super takes 3 named parameters:
      table (The name of your db table),
      data (The model data),
      connection (The DBConnection object)
    */
  }
}

You have some methods out-of-the-box, such as:
list: gets all the data for a particular model
findById: gets data by primary key which should be spelled 'id' in the database. Takes parameter id
save: doesn't take any parameter. Saves data to the database after initialization
update: takes model object as parameter. updates the object
delete: takes model object as parameter. deletes the object

Eample:

Code for list:

import { YourModel } from 'YourModel.js';

const model = new YourModel();
const result = await model.list(); // gives you a list back in json format


Code for save:

import { YourModel } from 'YourModel.js';

const model = new YourModel({
  name: 'Jon Doe',
  email: '[email protected]',
  description: 'A software developer'
});

await model.save();


Code for findByEmail, update, delete:

import { YourModel } from 'YourModel.js';

const model = new YourModel();
const result = await model.findById(id);

// Change the result as neccessary for update
result.description = 'An open-source software enthusiast';

await model.update(result);


// Delete function is similar
await model.delete(result);

Advanced Usage

To make custom queries to the database, you can add methods to your extended model class. For example:

export class YourModel extends BaseModel {
  // ...rest of the code

  customQuery() {
    return this.connection.db.any(`SELECT * FROM db_table`);
  }
}

To use the connection.db instance, refer to the pg-promise documentaion.