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

@triadjs/drizzle

v0.2.2

Published

Drizzle ORM bridge — schema codegen and migration generation for Triad

Downloads

308

Readme

@triadjs/drizzle

Drizzle ORM bridge for Triad — schema codegen, validation bridge, and migration tools.

Triad keeps the API contract (t.model()) separate from the storage contract (pgTable() / sqliteTable()). This package connects the two with type helpers, runtime validation at the repository boundary, portable error introspection, and a codegen pipeline that turns .storage() hints into Drizzle table definitions.

Install

npm install @triadjs/drizzle

Peer dependency: drizzle-orm

Quick start

1. Annotate your model with .storage() hints

import { t } from '@triadjs/core';

const Pet = t.model('Pet', {
  id:   t.string().format('uuid').storage({ primaryKey: true }),
  name: t.string().storage({ columnName: 'pet_name' }),
  age:  t.integer().optional(),
});

2. Validate rows at the repository boundary

import { validateAgainst } from '@triadjs/drizzle';
import { Pet } from './schemas/pet.js';
import { pets } from './db/schema.js';

async function findById(db, id: string) {
  const row = await db.select().from(pets).where(eq(pets.id, id)).get();
  if (!row) return null;
  return validateAgainst(Pet, rowToApi(row));
}

validateAgainst parses the mapped row through the Triad model. If the DB drifts from the API schema, the mismatch surfaces immediately instead of sending malformed data to clients.

A non-throwing variant is also available:

import { validateAgainstSafe } from '@triadjs/drizzle';

const result = validateAgainstSafe(Pet, row);
if (!result.success) console.error(result.errors);

Type helpers

import type { InferRow, InferInsert } from '@triadjs/drizzle';
import { pets } from './db/schema.js';

type PetRow    = InferRow<typeof pets>;    // DB row
type PetInsert = InferInsert<typeof pets>; // insert shape

Schema codegen

triad db generate walks the router, reads .storage() hints, and emits Drizzle table definitions for SQLite, Postgres, or MySQL.

triad db generate --dialect postgres --output src/db/schema.ts

Programmatic usage:

import { generateDrizzleSchema } from '@triadjs/drizzle';

const { source, tables } = generateDrizzleSchema(router, {
  dialect: 'postgres',
});

The pipeline is two-stage — walkRouter() produces dialect-neutral TableDescriptor[], then emitForDialect() renders TypeScript source — so tooling can consume or transform the intermediate representation.

Migration codegen

Diff two router snapshots and emit SQL migration files:

import { generateMigration } from '@triadjs/drizzle';

Or via the CLI:

triad db migrate --dialect postgres --dir ./migrations

Helpers

findPrimaryKey(model)

Returns the field name marked with .storage({ primaryKey: true }), or undefined if none is set.

import { findPrimaryKey } from '@triadjs/drizzle';

findPrimaryKey(Pet); // "id"

isUniqueViolation(err)

Detect unique-constraint violations portably across better-sqlite3, node-postgres, and mysql2. Returns a DbError descriptor ({ table?, column?, constraint? }) when matched, or null otherwise.

import { isUniqueViolation } from '@triadjs/drizzle';

try {
  await db.insert(users).values(input).returning().get();
} catch (err) {
  const conflict = isUniqueViolation(err);
  if (conflict) throw new DuplicateEmailError(input.email);
  throw err;
}

Links