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

zenstack-orpc

v0.2.1

Published

ZenStack plugin for generating oRPC routers with automatic Row Level Security support

Downloads

8

Readme

zenstack-orpc

ZenStack plugin for generating oRPC routers with automatic Row Level Security support.

🎯 Features

  • ✅ Generate oRPC routers directly from ZenStack schema
  • ✅ Full Row Level Security (RLS) policy support
  • ✅ Automatic validation via Zod schemas
  • ✅ Type-safe API with complete type inference
  • ✅ Access error handling (P2004)
  • ✅ Support for all Prisma CRUD operations
  • ✅ Simplified architecture (no adapter layer needed)

📦 Installation

npm install -D zenstack-orpc
# or
pnpm add -D zenstack-orpc
# or
yarn add -D zenstack-orpc

🚀 Usage

1. Add plugin to schema.zmodel

plugin orpc {
  provider = 'zenstack-orpc'
  output = 'zenstack/orpc'
}

2. Run generation

npx zenstack generate

3. Use generated routers

import { appRouter } from './zenstack/orpc/routers';
import { createORPCHandler } from '@orpc/server';
import { PrismaClient } from '@prisma/client';

const handler = createORPCHandler({
  router: appRouter,
  context: async (req) => ({
    prisma: new PrismaClient(),
    user: await getUserFromRequest(req),
  }),
});

📁 Generated Structure

zenstack/orpc/
├── routers/
│   ├── index.ts         # Main router (appRouter)
│   ├── User.router.ts
│   ├── Post.router.ts
│   └── ...              # Router for each model
└── helper.ts            # Utilities and context

🔧 Configuration

Plugin Options

plugin orpc {
  provider = 'zenstack-orpc'
  output = 'zenstack/orpc'                     // Output path
  generateModels = ['User', 'Post']            // Generate only specified models
  generateModelActions = ['findMany', 'create'] // Generate only specified operations
  zodSchemasImport = '../../zod'               // Path to Zod schemas import
  baseImport = '../base'                       // Import base context from custom path
}

Custom Base Context

By default, the plugin generates a basic context. You can provide your own:

// base.ts
import { os } from '@orpc/server';
import type { PrismaClient } from '@prisma/client';

export const base = os.$context<{
  prisma: PrismaClient;
  user?: {
    id: string;
    role: string;
    // Add your custom fields
  };
}>();

Then use baseImport option:

plugin orpc {
  provider = 'zenstack-orpc'
  output = 'zenstack/orpc'
  baseImport = '../base'
}

📚 API

Context

The plugin generates a typed context:

type Context = {
  prisma: PrismaClient;
  user?: {
    id: string;
    role: string;
  };
};

Operations

For each model, the following operations are generated:

Queries (8 operations):

  • aggregate - aggregate data
  • count - count records
  • findFirst - find first record
  • findFirstOrThrow - find first record (throws if not found)
  • findMany - find multiple records
  • findUnique - find unique record
  • findUniqueOrThrow - find unique record (throws if not found)
  • groupBy - group data

Mutations (7 operations):

  • create - create record
  • createMany - create multiple records
  • delete - delete record
  • deleteMany - delete multiple records
  • update - update record
  • updateMany - update multiple records
  • upsert - create or update record

Helper Functions

// Check read operations
export async function checkRead<T>(promise: Promise<T>): Promise<T>

// Check write operations
export async function checkMutate<T>(promise: Promise<T>): Promise<T | undefined>

🔒 Row Level Security (RLS)

The plugin automatically integrates with ZenStack RLS policies:

model Post {
  id        String   @id @default(cuid())
  title     String
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  String

  // Anyone can read published posts
  @@allow('read', published)

  // Only authenticated users can read all posts
  @@allow('read', auth() != null)

  // Only author can update/delete their posts
  @@allow('update,delete', auth() == author)

  // Admins have full access
  @@allow('all', auth().role == 'admin')
}

Access errors are automatically handled:

// Prisma error P2004 → "Access denied"
// Prisma error P2025 → "Not found"

🆚 Comparison with tRPC

| Feature | tRPC | oRPC | |---------|------|------| | Type Safety | ✅ | ✅ | | RLS Support | Via ZenStack | Via ZenStack | | Bundle Size | Larger | Smaller | | Learning Curve | Steeper | Gentler | | Middleware | Complex | Simple |

🏗️ Architecture

Generated Router for Model

// Generated code
export const PostRouter = {
  findMany: base
    .input($Schema.PostInputSchema.findMany)
    .handler(async ({ context, input }) =>
      checkRead(context.prisma.post.findMany(input))
    ),

  create: base
    .input($Schema.PostInputSchema.create)
    .handler(async ({ context, input }) =>
      checkMutate(context.prisma.post.create(input))
    ),

  // ... +13 operations
};

Main Router

export const appRouter = {
  user: UserRouter,
  post: PostRouter,
  // ... all models
};

export type AppRouter = typeof appRouter;

📖 Examples

Simple Query

const posts = await orpcClient.post.findMany({
  where: { published: true },
  take: 10,
});

Mutation with RLS

// RLS automatically checks permissions
const post = await orpcClient.post.create({
  data: {
    title: 'Hello World',
    published: false,
    authorId: userId,
  },
});

Error Handling

try {
  await orpcClient.post.delete({
    where: { id: 'post-id' }
  });
} catch (err) {
  if (err.message === 'Access denied') {
    // RLS denied the operation
  }
}

🤝 Contributing

Contributions are welcome! Please open an issue or PR.

📝 License

MIT

🔗 Links