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

@relayerjs/next

v0.1.2

Published

Next.js App Router integration for Relayer - type-safe CRUD route handlers with validation and hooks

Readme

@relayerjs/next

Next.js App Router CRUD integration for Relayer. Type-safe route handlers with hooks, validation, and SSR support.

Installation

npm install @relayerjs/next @relayerjs/drizzle drizzle-orm next

Setup

1. Define your schema and entity models

// db/schema.ts
import { relations } from 'drizzle-orm';
import { integer, pgTable, serial, text } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull(),
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  authorId: integer('author_id').references(() => users.id),
});

export const usersRelations = relations(users, ({ many }) => ({ posts: many(posts) }));
export const postsRelations = relations(posts, ({ one }) => ({
  author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));

Extend models with computed/derived fields if needed:

// db/entities.ts
import { createRelayerEntity } from '@relayerjs/drizzle';

import * as schema from './schema';

const UserEntity = createRelayerEntity(schema, 'users');
const { computed, derived } = UserEntity;

export class User extends UserEntity {
  @computed({ resolve: ({ table, sql }) => sql`upper(${table.name})` })
  displayName!: string;

  @derived({
    query: ({ db, schema: s, sql, field }) =>
      db
        .select({ [field()]: sql`count(*)::int`, authorId: s.posts.authorId })
        .from(s.posts)
        .groupBy(s.posts.authorId),
    on: ({ parent, derived: d, eq }) => eq(parent.id, d.authorId),
  })
  postsCount!: number;
}

// No computed/derived needed? Just extend without decorators:
export class Post extends createRelayerEntity(schema, 'posts') {}

// Or simply omit this step, Relayer will use Drizzle schemas directly

2. Initialize Relayer

// lib/relayer.ts
import { db } from '@/db';
import { Post, User } from '@/db/entities';
import * as schema from '@/db/schema';
import { createRelayerDrizzle } from '@relayerjs/drizzle';

export const r = createRelayerDrizzle({
  db,
  schema,
  entities: { users: User, posts: Post },
  defaultRelationLimit: 20, // cap many-type relations at 20 rows
});

3. Create routes

// lib/routes.ts
import { createRelayerRoute } from '@relayerjs/next';

import { r } from './relayer';

export const userRoutes = createRelayerRoute(r, 'users', {
  allowSelect: { posts: { $limit: 10, title: true } }, // $limit caps relation rows
  allowWhere: { email: { operators: ['eq', 'contains'] } },
  allowOrderBy: ['name', 'createdAt', 'postsCount'],
  maxLimit: 100,
  defaultLimit: 20,
});

4. Wire up API routes

// app/api/users/route.ts
import { userRoutes } from '@/lib/routes';

export const GET = userRoutes.list({
  defaultSelect: { id: true, name: true, email: true, postsCount: true },
  defaultOrderBy: { field: 'createdAt', order: 'desc' },
});

export const POST = userRoutes.create({
  beforeCreate: async (data, ctx) => {
    return { ...data, createdAt: new Date().toISOString() };
  },
});
// app/api/users/[id]/route.ts
import { userRoutes } from '@/lib/routes';

export const { GET, PATCH, DELETE } = userRoutes.detailHandlers();

5. Query with filters, sort, and pagination

GET /api/users?where={"email":{"contains":"@gmail.com"}}&orderBy={"field":"postsCount","order":"desc"}&limit=10
GET /api/users/1
POST /api/users  { "name": "John", "email": "[email protected]" }
PATCH /api/users/1  { "name": "Updated" }
DELETE /api/users/1

Response:

{
  "data": [{ "id": 1, "name": "John", "postsCount": 5 }],
  "meta": { "total": 42, "limit": 10, "offset": 0 }
}

Hooks

Hooks can intercept any step. Return data to continue, false to cancel, or a Response for full control:

export const POST = userRoutes.create({
  beforeCreate: async (data, ctx) => {
    if (!ctx.user) return new Response(null, { status: 401 });
    return { ...data, createdBy: ctx.user.id };
  },
  afterCreate: async (created, ctx) => {
    await sendWelcomeEmail(created.email);
    return created;
  },
});

SSR Direct Call

Use .query() in Server Components, same config and hooks, no HTTP:

const { data: users, meta } = await listHandler.query({
  where: { role: 'admin' },
  limit: 10,
});

Documentation

Full docs at relayerjs.vercel.app/next/getting-started

License

MIT