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

jsonbadger

v0.6.2

Published

JSONB query/model library for PostgreSQL

Readme

JsonBadger

JsonBadger is a Node.js library for working with complex JSON data in PostgreSQL through a schema-driven model API. Instead of building nested JSONB queries by hand, you define schemas and use document-style models, query operators, and persistence helpers.

Why did I create this utility?

I originally built my personal systems and small business client projects using MongoDB and Mongoose. Over time, that setup was no longer viable or cost-effective on shared hosting. I needed a solution that was more contained and budget-friendly. Since most shared hosting providers offer free, robust PostgreSQL databases, I created this utility to leverage PostgreSQL's jsonb fields as an alternative for document-based storage.

Table of Contents

Install

Requirements:

  • Node.js >=20
  • PostgreSQL
npm install jsonbadger

Examples (Quick Start)

import JsonBadger from 'jsonbadger';

// 1. Connect to database
const db_uri = 'postgresql://user:pass@localhost:5432/dbname';
const options = {
	debug: false,
	max: 10,
	ssl: false
};
const connection = await JsonBadger.connect(db_uri, options);

// 2. Define schema (FieldType format)
const user_schema = new JsonBadger.Schema({
	name: {type: String, required: true, index: true},
	age: {type: Number, min: 0, index: -1},
	type: {type: String},
	email: {type: String, index: {unique: true, name: 'idx_users_email_unique'}}
}, {
	id_strategy: JsonBadger.ID_STRATEGY.bigserial,
	auto_index: true
});

// 3. Declare indexes on schema (path-level + schema-level compound index)
// Descriptor-form create_index(...) is how you define explicit index declarations.
user_schema.create_index({using: 'btree', paths: {name: 1, type: -1}});

// 4. Create model
const User = connection.model({
	name: 'User',
	schema: user_schema,
	table_name: 'users'
});

// 5. Run migrations manually
await User.ensure_table();
await User.ensure_indexes();

// 6. Save a document
const saved_user = await User.from({
	name: 'john',
	age: 30,
	type: 'admin'
}).save();
// returns: User document instance
// saved_user.to_json() -> { id, name, age, type, created_at, updated_at }

// 7. Find a document
const found_user = await User.find_one({
	name: 'john'
}).exec();
// returns: User document instance or null
// found_user?.to_json() -> { id, name, age, type, created_at, updated_at }

When using ID_STRATEGY.uuidv7, declare it on the schema. JsonBadger checks support automatically during Model.ensure_table() using capability data captured at connect(...).

Examples Cheat Sheet

For a complete copy-paste operator and runtime cheat sheet (queries, updates, and document methods), see docs/examples.md.

For the document state flow from new Model(...) through save(), hydration, dirty tracking, and serialization, see docs/lifecycle.md.

Core Features

  • JSONB-first: Model API designed specifically for PostgreSQL JSONB.
  • Validation: FieldType-based schema validation built in.
  • Querying: Mongo-style query operators such as $gt, $regex, $contains, and $elem_match.
  • Runtime document APIs: get, set, alias virtuals, serialization helpers, mark_modified/dirty tracking, and immutable enforcement after save.
  • JSON updates: update_one(...) supports implicit keys, $set, $unset, and $replace_roots.
  • Migrations: Helpers for ensure_table, ensure_indexes, and ensure_model.
  • Configurable row IDs: use ID_STRATEGY.bigserial and ID_STRATEGY.uuidv7.
  • ID create semantics: for uuidv7, caller-provided id is used on create when present; otherwise PostgreSQL generates it. For bigserial, create ignores caller-provided id and PostgreSQL generates it.
  • Timestamp helper semantics: created_at and updated_at are helper fields. Create keeps caller values when provided, otherwise auto-fills both. Updates keep caller updated_at when provided, otherwise auto-refresh updated_at.
  • Configurable index lifecycle: auto_index is a schema option that controls whether ensure_model() also ensures indexes.
  • UUIDv7 compatibility checks: JsonBadger checks support automatically and fails early when unavailable.
  • Document instance returns: data-returning methods return document instances with top-level base fields (id, created_at, updated_at), and .to_json() / .$serialize() for plain-object snapshots.
  • Modern: Native ESM package.

Documentation

Author

Carlos López

License

MIT. See LICENSE.