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

node-pg-module

v0.1.3

Published

Use PostgreSQL files as JS modules.

Readme

node-pg-module

Use PostgreSQL files as JS modules.

how to use

  • Define your model in a .sql file:
-- @function insert
-- @params data
-- @returns row
INSERT INTO a_table (a_column, another_column)
VALUES ($data.aProp, $data.anotherProp)
RETURNING *;

-- @function getRange
-- @params from, count
-- @returns multiple
SELECT *
  FROM a_table
OFFSET $from
 LIMIT $count;
  • Then load and use it from JavaScript:
import loadPgModule from 'node-pg-module'

const Model = loadPgModule(['path', 'to', 'Model.sql'])

async function test () {
    await Model.insert({ aProp: 'aValue', anotherProp: 'anotherValue' })
    const rows = await Model.getRange(0, 10)
    console.log('Rows retrieved:')
    rows.forEach(row => console.log(row))
}

test().then(() => console.log('Done.'))

configuration

environment

Create a .env file at the root of your project containing the following configuration:

POSTGRESQL_HOST = <host>
POSTGRESQL_PORT = <port>
POSTGRESQL_USER = <user>
POSTGRESQL_PASSWORD = <password>
POSTGRESQL_DATABASE = <database>

annotations

  • @function <name>

Specifies the name under which the procedure will be exported.

  • @params <argument> ...

Specifies the generated function arguments.

  • optional @returns multiple | row | field | void

multiple returns all the rows from the query result, row only returns the first one, field only returns the value of the first property of the first row and void explicitly returns nothing.

tricky cases

  • Inserting a row with default values:
-- @function create
-- @params data
-- @returns row
INSERT INTO users (nickname, gender, permission)
VALUES (
  $data.nickname,
  COALESCE($data.gender, 'robot'),  -- 'robot' will be used if $data.gender evaluates to NULL.
  COALESCE($data.permission, 'minimal')
)
RETURNING *;
  • Updating a row with optionnal values:
-- @function updateById
-- @params id, data
-- @returns row
UPDATE users
   SET nickname = COALESCE($data.nickname, nickname), -- The previous value of nickname will be used if $data.nickname evaluates to NULL.
       gender = COALESCE($data.gender, gender)
 WHERE id = $id
RETURNING *;

current limitations

  • The dollar sign $ is not supported outside of its use as variable prefix, even if within a SQL string literal.
  • Deep property access is supported but only via the dot notation. Bracket notation like $data[0].value will fail.