DEV Community

Alan Schio
Alan Schio

Posted on • Edited on

3 4

pg-connection: using PostgreSQL on NodeJS

You dont like the ways 'ORM' are on node for pg?
Do you wish it was simpler?

I never quite liked the ones ready at the moment so i started to use in my projects a structure tha i realized could become a project, and so i made the pg-connection.

A small postgres wrapper to simplify its use you devopment.

Requirements

Requires .env for configuration

How to use

It has basic 3 steps to use:

  • Add the following props to your .env: PG_USER,PG_URL, PG_DATABASE, PG_PASSWORD, PG_SCHEMA; If you like or need you cand add PG_SSL, if not set the value is false, PG_PORT if not set the value is 5432.
  • At your Model, Entity, Table, or whatever you like to call it, extends @schirrel/pg-connection/Model and:
    • call super with the table name;
    • Use `super.addColumn('propName', 'COLUMN_NAME');
  • At your data layer, i call it repositories but you can call whatever you like, extends @schirrel/pg-connection/Repository and call the super with your model reference.

Example

.env

PG_USER=postgres
PG_URL=localhost
PG_DATABASE=postgres
PG_PASSWORD=postgres
PG_SCHEMA=mercado_alencar
PG_SSL=true

Model

`const Model = require('@schirrel/pg-connection/Model');
class User extends Model{
constructor(args = {}){
super("USER");
this.addColumn('email', 'EMAIL');
this.addColumn('name', 'NAME');
this.addColumn('password', 'PASSWORD');
this.addColumn('active', 'ACTIVE', true);
this.setValues(args);
}
}

module.exports = User;`

Repository

`const Repository = require('@schirrel/pg-connection/Repository');
const User = require('../models/User');

class UserRepository extends Repository{
constructor(){
super(User);
}
}

module.exports = UserRepository;`

It already have built in: get(id), create(model), update(model),delete(id), list(), search(options)


The project is only on the start, i will be glad if you could share points of improvement and bugs

Here is the repo
https://github.com/schirrel/pg-connection

You can finde usage at
https://github.com/schirrel/my-movies

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay