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 🙏

© 2025 – Pkg Stats / Ryan Hefner

peng-orm

v1.0.0

Published

Micro ORM for Node's native SQLite module

Readme

Peng ORM

Micro ORM for Node's native SQLite module.

Setup

To create a database context, only two arguments are required by the constructor: the path to the database file and a migration array with sql instructions.

import PengORM from 'peng-orm';

const todos = `
  CREATE TABLE IF NOT EXISTS todos (
    id INTEGER NOT NULL PRIMARY KEY,
    task TEXT NOT NULL
  );
`;

const alterTodos = `ALTER TABLE todos ADD COLUMN userId INTEGER NOT NULL DEFAULT 0;`;

const users = `
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
  );
`;

const migrations: string[][] = [[todos], [users, alterTodos]];

const dbContext = new PengORM('./data.db', migrations);

export default dbContext;

Migrations

In the example above, we can see that the migration array has two items, it means our database will be in its version 2. The statements of version one (index 0) will be executed and then the statements of version two, always in order that they were provided.

If more alterations are made to the database schema, a new array of statements should be added to the migrations array. In this way we can ensure that databases in production are updated to the latest version without conflicts.

Logging

The ORM also accepts a custom logging function ((stm: string) => void), like this example:

const myLogger = (stm: string) => console.log(`[MyDB]: ${stm}`);

const dbContext = new PengORM('./data.db', [], myLogger);

Usage

Query

Use query to retrieve multiple rows. Pass positional parameters as an array; the method handles statement preparation and execution for you.

type Todo = { id: number; task: string; userId: number };

const todos = dbContext.query<Todo>(
  'SELECT id, task, userId FROM todos WHERE userId = ? ORDER BY id',
  [currentUserId],
);

// With a transformer you can cast values or rename fields.
const simpleTodos = dbContext.query('SELECT id, task FROM todos', [], (row) => ({
  id: Number(row.id),
  task: String(row.task),
}));

Get

Use get when you expect at most one row. A transformer can shape the result, and null is returned if nothing matches.

const todo = dbContext.get(
  'SELECT id, task FROM todos WHERE id = ?',
  [todoId],
  (row) => ({ id: Number(row.id), task: String(row.task) }),
);

if (!todo) {
  throw new Error('Todo not found');
}

Exec

Use exec for statements that do not return rows, such as inserts, updates, or schema changes.

dbContext.exec('INSERT INTO todos (task, userId) VALUES (?, ?)', [
  'Write documentation',
  currentUserId,
]);

dbContext.exec('DELETE FROM todos WHERE id = ?', [completedTodoId]);

API Summary

| Method | Parameters | Returns | Notes | | ---------------------------------------- | ---------------------------------------------------------------- | ------------------ | --------------------------------------------------------------------- | | new PengORM(path, migrations, logger?) | path: string, migrations: string[][], optional logger(stm) | PengORM instance | Automatically opens the database on first use and applies migrations. | | query(stmt, args?, transform?) | SQL string, optional positional args array, optional row mapper | T[] | Use for multi-row reads; mapper lets you coerce values. | | get(stmt, args?, transform?) | SQL string, optional positional args array, optional row mapper | T \| null | Returns first row or null; mapper shapes the single row result. | | exec(stmt, args?) | SQL string, optional positional args array | void | Runs statements without results (INSERT/UPDATE/DDL). |

Note: getDb is internal to the ORM. Interact through the exported methods above.

Testing

Run the Jest suite in watch mode:

yarn test

The command starts Jest in watch mode, so it reruns relevant tests as files change. Use q inside the watcher to quit when finished.