lightweight-pg-orm is built on top of pg, Node, and Express to help you easily create CRUD functions on your PostgreSQL database.
Use npm to install.
npm install lightweight-pg-orm
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;
SQL-Injections: Currently, we are handling SQL-injections on external requests but they are not handled when you create a model/table class.
(live) 0.5.1 - First working version
(TBD) 1.0.0 - Will better organize the code and handle SQL-injections when creating the classes.
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
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.
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.