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 🙏

© 2024 – Pkg Stats / Ryan Hefner

postorm

v0.1.4

Published

Node js ORM for PostgreSQL database.

Downloads

9

Readme

postorm

Node js ORM for PostgreSQL database. The whole perpose of that package is to make sql requests easier to write with JS, to create "SELECT someStaff FROM staffWareHouse" by writing

staffWareHouse.select("someStaff");

etc.

How to use it?

So what do we have?

const { PgObj, compileModelsByScripts, PgUtils, PgGlob } = require("postorm")

Lets understand what is that and how it works.

PgObj

PgObj is a class that implements table object. To create it you will need a name of table in your database, db connecter(pgConnecter) and model of your table

now we don't have implementation of DB interfaces, so we use pg-promise. Btw, thanx to it's author.

let pgconnection = {
  host: "localhost",
  user: "postgres",
  database: "BotDb",
  password: "123",
  port: 5432,
  connectionTimeoutMillis: 20000,
};

const db = pgp(pgconnection);
tableObj = new PgObj("table", db, tableModel);

That might be clear. But what is model in parameters? Model is a dictionary where keys are the names of columns of real table and values are their types converted to Node.js types. For example:

let userModel = { id: Number, gender: String, isHeOrSheAwesome: Boolean };

compileModelsByScripts

Another way to create model is to use table creation script. Here compileModelsByScripts() goes.

Imagine that you have a file with sql script like this in ./scripts directory:

CREATE TABLE IF NOT EXISTS public.user
(
  	id SERIAL,
    another_id integer NOT NULL,
	  mode VARCHAR DEFAULT 'unauthorized',
	  eyes_nubmer integer DEFAULT 2,
    CONSTRAINT users_pkey PRIMARY KEY (id),
    CONSTRAINT users_user_id_key UNIQUE (user_id, bot_instance)
);

than if you write

let models = compileModelsByScripts("./scripts");

//model - {user : {model}}

let yourScriptModel = models["user"];

//yourScriptModel - {another_id: null, creationScript: theWholeScriptText, eyes_nubmer:2, mode:'unauthorized'}

Using this modules you can make things easily

 const { PgObj, compileModelsByScripts, PgUtils} = require("postorm")

 let models = compileModelsByScripts('./scripts')

 const connectionStr = 'postgres://john:pass123@localhost:5432/products'
 const db = pgp(connectionStr);

 let userModel = models['user']


 let User = new PgObj('user', db, userModel)

PgUtils

PgUtils is a class to add SQL functions to your code For examle

Users.select(PgUtils.count("another_id"))
  .where(Users.eyes_nubmer.eq(2))
  .toStr();
//returns: SELECT * FROM users WHERE users.eyes_nubmer=2;
Users.select(PgUtils.count("another_id")).where(Users.eyes_nubmer.eq(2)).exec();
//executes this request

PgGlob

PgGlob is a class to create nested request such as "select from select"

let Glob = new PgGlob(db);

Glob.select()
  .from(
    Users.select(PgUtils.count(PgUtils.distinct(Users.user_id)))
      .fullJoin(Requests, ["user_id", "user_id"])
      .limit(1)
  )
  .toStr();
//returns: SELECT  FROM (SELECT COUNT(DISTINCT users.user_id )  FROM users  FULL JOIN requests ON users.user_id=requests.user_id  LIMIT 1 ) t1 ;
Glob.select()
  .from(
    Users.select(PgUtils.count(PgUtils.distinct(Users.user_id)))
      .fullJoin(Requests, ["user_id", "user_id"])
      .limit(1)
  )
  .exec();
//executes this request

To find more functions check PgUtils file. Good luck! And you check an example