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

najm-database

v2.2.0

Published

Database and transaction handling plugin for Najm API framework

Readme

najm-database

Database connections and transactions plugin for Najm, powered by Drizzle ORM.

Install

bun add najm-database

Peer dependencies: najm-core, reflect-metadata, drizzle-orm, zod.

Usage

Register Database

import { Server } from 'najm-core';
import { database } from 'najm-database';
import { drizzle } from 'drizzle-orm/bun-sqlite';
import { Database } from 'bun:sqlite';

const sqlite = new Database('./app.db');
const db = drizzle(sqlite, { schema });

await new Server()
  .use(database({ default: db }))
  .load(MyController)
  .listen(3000);

Repository Pattern

import { Repository } from 'najm-core';
import { DB } from 'najm-database';

@Repository('default')
class UserRepository {
  @DB() db!: ReturnType<typeof drizzle>;

  async findById(id: string) {
    return this.db.select().from(usersTable).where(eq(usersTable.id, id));
  }

  async create(data: typeof usersTable.$inferInsert) {
    return this.db.insert(usersTable).values(data).returning();
  }
}

Transactions

import { Service } from 'najm-core';
import { Transaction } from 'najm-database';

@Service()
class OrderService {
  constructor(
    private orderRepo: OrderRepository,
    private inventoryRepo: InventoryRepository,
  ) {}

  @Transaction({ retries: 2 })
  async createOrder(data: any) {
    const order = await this.orderRepo.create(data);
    await this.inventoryRepo.decrementStock(data.items);
    return order;
  }
}

Seeding

import { SeedService } from 'najm-database';

@Service()
class SetupService {
  constructor(private seeder: SeedService) {}

  async seed() {
    const report = await this.seeder.run({
      users: {
        by: ['id'],
        rows: [{ id: '1', name: 'Alice' }],
      },
    }, { verbose: true, onConflict: 'skip' });
  }
}

Closing Connections

server.stop() leaves database connections open by default (close: false), so existing apps and shared pools keep their caller-managed lifecycle. Opt in when this server owns the connection:

// Default: the caller manages the connection
database(db)

// Let this server close its connections on stop()
database({ default: db, close: true })

// Driver-specific cleanup, e.g. a postgres-js shutdown timeout
database({ default: db, close: (db) => db.$client.end({ timeout: 5 }) })

With close: true, the plugin calls the database's disconnect(), or its driver client's end() (postgres-js/node-postgres) or close() (SQLite). Clients without a recognized close method remain open; provide a callback for those drivers. Inline close is an option when it is a boolean or callback; a database object named close remains a connection. The existing second argument also works: database(db, { close: true }). Explicit close: false is still supported, but shared pools do not need it.

The database service boots at plugin order -100. When cleanup is enabled, it closes after app services and plugins with higher orders; their onDestroy hooks can still query. Aliases sharing one client are closed once, including with a custom callback. All clients are attempted before stop() rejects for any closure failures. For a custom callback, the database and name come from the first alias in alphabetical order. Automatic cleanup uses the driver's own shutdown timing; provide a callback when you need driver-specific timeouts.

The plugin supports Core 2.1.1 and Core 3.0.2 or later within those majors. App-service onDestroy hooks require Core 3; Core 2 retains its existing plugin-only teardown behavior.

Production Notes

  • drizzle-orm is a peer dependency — install your preferred driver (better-sqlite3, postgres, mysql2)
  • Use @DB('name') to target a specific database when multiple are registered
  • @Transaction decorator requires database plugin to be registered first
  • Schema should use authSchema from najm-auth via spread, never duplicate auth tables