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

zodgres

v0.4.6

Published

Postgres.js + Zod

Readme

TypeScript-first database collections with static type inference and automatic migrations. Built on top of Postgres.js and Zod.

  • 🔒 Type-safe - Full TypeScript support with Zod schema validation
  • 🚀 Simple API - Collection-based interface for common database operations
  • 📦 Flexible - Works with Postgres or in-memory PGLite for testing
  • SQL Templates - Use template literals for complex queries
  • 🔄 Auto-migration - Automatic table creation from Zod schemas

⚠️ Disclaimer: PGLite support is currently not working. See issue #1 for more details.

Installation

npm install zodgres

Quick Start

import { connect, z } from 'zodgres';

// Set-up database connection
const db = connect('postgres://user:password@localhost:5432/mydb');

// Define a collection with Zod schema
const users = db.collection('users', {
  id: z.number().optional(), // auto-incrementing
  name: z.string().max(100),
  age: z.number().min(0).max(100).optional(),
});

// Open the connection and run collection migrations
await db.open();

// Create records
const user = await users.create({ name: 'John Doe', age: 30 });
// Result: { id: 1, name: 'John Doe', age: 30 }

// Create multiple records
const newUsers = await users.create([
  { name: 'Alice' },
  { name: 'Bob', age: 25 }
]);
// Result: [{ id: 2, name: 'Alice' }, { id: 3, name: 'Bob', age: 25 }]

// Query records
const allUsers = await users.select(); // or users.select`*`
const adults = await users.select`* WHERE age >= ${18}`;

// Close connection
await db.close();

API Overview

Database Connection

connect(uri, options?)

Connect to a Postgres database or use in-memory storage for testing:

// Connect to Postgres
const db = connect('postgres://user:password@localhost:5432/mydb');

// Use in-memory database (great for testing)
const testDb = connect(':memory:');

Important: After defining all your collections, you must call await db.open() to establish the database connection and run any necessary migrations. This ensures your database schema matches your collection definitions before performing any operations.

Collection Definition

db.collection(name, schema, params?)

Create a type-safe collection with Zod schema validation:

const items = db.collection('items', {
  id: z.number().optional(),          // auto-incrementing primary key
  name: z.string().max(100),          // required string with max length
  price: z.number().positive(),       // required positive number
  description: z.string().optional(), // optional string
});

Collection Operations

create(data) / create(data[])

Create single or multiple records:

// Single record
const item = await items.create({
  name: 'Widget',
  price: 19.99
});

// Multiple records
const newItems = await items.create([
  { name: 'Gadget', price: 29.99 },
  { name: 'Tool', price: 39.99, description: 'Useful tool' }
]);

select() / select``query `

Query records using SQL template literals:

// Select all records
const all = await items.select();

// Select with conditions
const expensive = await items.select`* WHERE price > ${25}`;
const byName = await items.select`* WHERE name = ${'Widget'}`;

// Complex queries
const recent = await items.select`
  name, price
  WHERE created_at > ${new Date('2024-01-01')}
  ORDER BY price DESC
  LIMIT ${10}
`;

Testing

The library supports in-memory databases for fast testing:

⚠️ PGLite support is currently not working. See issue #1 for more details.

import { connect, z } from 'zodgres';

describe('My tests', () => {
  let db;

  before(async () => {
    db = await connect(':memory:').open(); // Uses PGLite
  });

  after(async () => {
    await db.close();
  });

  it('should create users', async () => {
    const users = db.collection('users', {
      id: z.number().optional(),
      name: z.string(),
    });

    const user = await users.create({ name: 'Test User' });
    assert.deepStrictEqual(user, { id: 1, name: 'Test User' });
  });
});

Schema Validation

All data is validated using Zod schemas before database operations:

const products = db.collection('products', {
  id: z.number().optional(),
  name: z.string().min(1).max(100),
  price: z.number().positive(),
  category: z.enum(['electronics', 'books', 'clothing']),
  tags: z.array(z.string()).optional(),
  metadata: z.record(z.any()).optional(),
});

// This will throw validation error
await products.create({
  name: '', // too short
  price: -10, // not positive
  category: 'invalid' // not in enum
});

License

MIT