pg-connection: using PostgreSQL on NodeJS
Alan Schio
Posted on December 24, 2020
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
Posted on December 24, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 11, 2023
June 22, 2023
November 3, 2022