1.0.1 • Published 1 year ago

@schirrel/simple-connection v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

simple-connection

Simple PostgreSQL and MySQL query builder/ORM-ish lib for using databases with nodejs

Install

npm install @schirrel/simple-connection --save

Config

Uses .env to acquire credentials.

postgres

PropRequiredDefaultDescription
PG_USERRequired
PG_URLRequired
PG_DATABASERequired
PG_PASSWORDRequired
PG_PORTOptional5432
PG_SSLOptionalfalse
PG_REJECT_UNHAUTHORIZEDOptional
PG_LOGOptionalfalse

mysql

PropRequiredDefaultDescription
CON_USERRequired
CON_URLRequired
CON_DATABASERequired
CON_PASSWORDRequired
CON_PORTOptional3306
CON_SSLOptionalfalse
CON_REJECT_UNHAUTHORIZEDOptional
CON_LOGOptionalfalse
CON_LIMITOptional10

Example

Using in 3 Steps

  1. .env
PG_USER=postgres
PG_URL=localhost
PG_DATABASE=postgres
PG_PASSWORD=postgres
PG_SCHEMA=mercado_alencar
PG_LOG=true
  1. Model
const Model = require('@schirrel/simple-connection/postgres/Model');
class User extends Model{
	constructor(args = {}){
	super("USER", {
		'primaryKey' : 'ID'
	});
	this.addColumn('email', 'EMAIL');
	this.addColumn('name', 'NAME');
	this.addColumn('password', 'PASSWORD');
	this.addColumn('active', 'ACTIVE', true);
	this.setValues(args);
	}
}

module.exports = User;
  1. Repository
const Repository = require('@schirrel/simple-connection/postgres/Repository');
const User = require('../models/User');

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

module.exports = UserRepository;
  1. Your main file
const Database = require("@schirrel/simple-connection/mysql/Database");
await Database.connect();
const userRepository = new UserRepository();

const user = await userRepository.get(123);

const newUser = new User({
	email: 'abc@email.com',
	name: 'someone',
	password: '1234123'
});

await userRepository.create(newUser);

And thats it.

TL;DR

Model

  • Used as extends Model at your model class
  • Call super("TABLE_NAME") with your table name
  • To add a columns this.addColumn('email', 'EMAIL');, it accepts a 3rd parameter as the default value.
  • To set values of your constructor use this.setValues(args);

Repository

  • Used as extends Repository at your repo class
  • Call super(YourClass); with your class reference
  • it already have built in: get(id), create(model), update(model),delete(id), list(), search(options)
1.0.1

1 year ago

1.0.0

1 year ago