Skip to content

Open-sourced Lightweight npm package that helps you manage your PostgreSQL database using Node and Express.

Notifications You must be signed in to change notification settings

Leomedina/lightweight-pg-orm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lightweight-pg-orm

lightweight-pg-orm is built on top of pg, Node, and Express to help you easily create CRUD functions on your PostgreSQL database.

Installation

Use npm to install.

npm install lightweight-pg-orm

Usage

This is best used on your Express routes.

const express = require("express");
const Model = require("lightweight-pg-orm");

const app = express();

const company = new Model("companies", "id", ["id",
  "name",
  "num_employees",
  "description",
  "logo_url"]
);

app.get("/", async function (req, res, next) {
  try {
    const results = await company.findAll(req.query);
    return res.status(200).json({ "companies": results });
  } catch (error) {
    return next(error);
  };
});

App.js must include a global variable for the root directory. Example below

global.__basedir = __dirname;

const express = require('express');
const app = express();

/** Routes */
app.get('/', function (req, res, next) {
  return res.status(200).json({
    'status': 200,
    'item': "This is the homepage, nothing to see here.",
  })
});

/** 404 Error Handler */
app.use(function (req, res, next) {
  const error = new ExpressError('Page Not Found', 404);
  return next(error);
});

/** Error Handler */
app.use(function (err, req, res, next) {
  res.status = err.status || 500;

  return res.json({
    status: res.status,
    message: err.message
  });
});

module.exports = app;

BUGS

SQL-Injections: Currently, we are handling SQL-injections on external requests but they are not handled when you create a model/table class.

Versions

(live) 0.5.1 - First working version

(TBD) 1.0.0 - Will better organize the code and handle SQL-injections when creating the classes.

Methods

Class Methods (Use these):

-> get: retrieves single item from database.

-> findAll: retrieves all items from database or items matching query.

-> create: Creates a new item in the database.

-> update: Updates item in the database.

-> delete: Deletes item in the database.

Helper functions:

-> WhereExpressions: creates where expressions depending on query passed.

-> createquery: creates SQL queries.

-> partialupdatequery: creates queries for update

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Errors and bugs

If something is not behaving intuitively, it is a bug and should be reported. Report it here by creating an issue: https://github.com/Leomedina/lightweight-pg-orm/issues.

Help me fix the problem as quickly as possible by following Mozilla's guidelines for reporting bugs.

License

MIT

About

Open-sourced Lightweight npm package that helps you manage your PostgreSQL database using Node and Express.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published