npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2026 โ€“ย Pkg Stats / Ryan Hefner

@maadesh124/pg-orm

v1.1.0

Published

A lightweight, recursive, relational ORM for PostgreSQL with caching support in Node.js. Save, delete, query objects and their relationships in a simple and intuitive way. ๐Ÿš€

Downloads

4

Readme

๐Ÿ“ฆ Postgres ORM

A lightweight, recursive, relational ORM for PostgreSQL with caching support in Node.js. Save, delete, query objects and their relationships in a simple and intuitive way. ๐Ÿš€


๐Ÿ”น Features

  • Automatic mapping between JavaScript classes and PostgreSQL tables.
  • Support for primary and foreign keys.
  • Recursive save and delete for nested objects.
  • Optional caching via entityMap.
  • Execute raw SQL when needed.
  • Convert raw SQL rows to objects using getInstance.

๐Ÿ’ป Installation

npm install @maadesh124/pg-orm

โšก Usage

1. Setup Database Connection

import { Database } from "@maadesh124/pg-orm";

const cred = {
  user: "postgres",
  host: "localhost",
  database: "ORMDB",
  password: "password",
  port: 5432
};

const db = new Database(cred);

2. Define Models

import { Model } from "@maadesh124/pg-orm";

class Subject extends Model {
  static entityMap = new Map();
  static descriptor = {
    table: "SUBJECTS",
    primaryKey: ["sub_id"],
    foreignKey: {},
    id: "sub_id",
    name: "sub_name"
  };
}

class Student extends Model {
  static entityMap = new Map();
  static descriptor = {
    table: "STUDENTS",
    primaryKey: ["id"],
    foreignKey: { sub1: { target: Subject, keys: { sub_id: "sub_id" } } },
    id: "id",
    rollNo: "roll_no"
  };
}

db.loadClasses([Subject, Student]);

3. Create and Save Objects

const sub = new Subject();
sub.id = 6;
sub.name = "Differential Equations";

const stud = new Student();
stud.id = 32;
stud.rollNo = "BDFDSA";
stud.sub1 = sub;

await stud.save(true); // Recursive save

4. Query Objects

const sub1 = await Subject.find({ sub_id: 6 });
console.log(sub1 === sub); // true if cached

5. Delete Objects

await stud.delete(true); // Recursive delete

6. Execute Raw SQL

const res = await db.query("SELECT * FROM students WHERE id = $1", [32]);
console.log(res.rows);

7. Convert Raw SQL Row to Object

const obj = Student.getInstance(res.rows[0]);
console.log(obj instanceof Student); // true

๐Ÿ“ Example Database State

Initial Database:

Students:
[
  { id: 101, roll_no: 'ROLL001', sub_id: 1 },
  { id: 102, roll_no: 'ROLL002', sub_id: 2 },
  { id: 103, roll_no: 'ROLL003', sub_id: 3 },
  { id: 23, roll_no: 'bec1213', sub_id: 5 }
]

Subjects:
[
  { sub_id: 2, sub_name: 'Physics' },
  { sub_id: 3, sub_name: 'Chemistry' },
  { sub_id: 4, sub_name: 'Biology' },
  { sub_id: 1, sub_name: 'Maths' },
  { sub_id: 5, sub_name: 'Diff equations' }
]

After Recursive Save:

Students:
[
  { id: 101, roll_no: 'ROLL001', sub_id: 1 },
  { id: 102, roll_no: 'ROLL002', sub_id: 2 },
  { id: 103, roll_no: 'ROLL003', sub_id: 3 },
  { id: 23, roll_no: 'bec1213', sub_id: 5 },
  { id: 32, roll_no: 'bdfdsa', sub_id: 6 }
]

Subjects:
[
  { sub_id: 2, sub_name: 'Physics' },
  { sub_id: 3, sub_name: 'Chemistry' },
  { sub_id: 4, sub_name: 'Biology' },
  { sub_id: 1, sub_name: 'Maths' },
  { sub_id: 5, sub_name: 'Diff equations' },
  { sub_id: 6, sub_name: 'Differential Equations' }
]

After Recursive Delete:

Students:
[
  { id: 101, roll_no: 'ROLL001', sub_id: 1 },
  { id: 102, roll_no: 'ROLL002', sub_id: 2 },
  { id: 103, roll_no: 'ROLL003', sub_id: 3 },
  { id: 23, roll_no: 'bec1213', sub_id: 5 }
]

Subjects:
[
  { sub_id: 2, sub_name: 'Physics' },
  { sub_id: 3, sub_name: 'Chemistry' },
  { sub_id: 4, sub_name: 'Biology' },
  { sub_id: 1, sub_name: 'Maths' },
  { sub_id: 5, sub_name: 'Diff equations' }
]

๐Ÿง  Caching and Reference Equality

  • Caching: The ORM provides an optional entityMap (Map) to cache instances of models. This ensures that when you query for the same object multiple times, you get the same reference instead of creating a new object.
  • Reference Equality: With caching enabled, Subject.find({ sub_id: 6 }) === sub returns true. This is useful for comparing objects and maintaining object relationships correctly.
  • Optional: If you don't want caching, simply do not override entityMap in your class.

๐Ÿ”ง Notes

  • Recursive save and delete handle nested objects defined in foreignKey.
  • Primary keys are required for both saving and deleting objects.
  • getInstance allows converting raw SQL rows into fully functional model instances.
  • Execute raw SQL directly when needed for complex queries or operations.

Enjoy using pg-orm! ๐Ÿš€