Socket
Book a DemoInstallSign in
Socket

zenstack-crudzod

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zenstack-crudzod

Generate CRUD Zod schemas from Zenstack models

0.2.1
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

ZenStack CRUD Zod Plugin

ZenStack CRUD Zod Plugin is a standard plugin for ZenStack that generate Zod schemas for all CRUD operations (Prisma Client API) for each model in your ZModel schema.

This is particularly useful for building AI agent, where you want LLM to be able to operate on your database safely per user's request using Prisma Client API like findMany, create, and update.

Example

For models like:

model User {
    id       String  @id @default(cuid())
    name     String
    todo     Todo[]
    list     List[]
}

model Todo {
    id        String   @id @default(uuid())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    owner     User     @relation(fields: [ownerId], references: [id], onDelete: Cascade)
    ownerId   String
    title     String
    completed Boolean  @default(false)
}

model List {
    id        String   @id @default(uuid())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    owner     User     @relation(fields: [ownerId], references: [id], onDelete: Cascade)
    ownerId   String
    title     String
    private   Boolean  @default(false)
    todos     Todo[]
}

Here are the generated object in the schema file that you will use in your AI agent:

  • 3 Zod schemas for each model. For example, for the User model, it generates:

    export const UserFindManyArgsSchema = z
        .object({
            where: UserWhereInput.optional(),
            include: UserInclude.optional(),
        })
        .describe('Prisma client API `findMany` function args for User model');
    
    export const UserCreateArgsSchema = z
        .object({
            data: UserCreateInput,
        })
        .describe('Prisma client API `create` function args for User model');
    
    export const UserUpdateArgsSchema = z
        .object({
            data: UserUpdateInputSchema,
            where: UserWhereInput,
        })
        .describe('Prisma client API `update` function args for User model');
    
  • A complete allSchemas object that contains all the generated schemas for all models:

    export const allSchemas = {
        list: {
            findMany: ListFindManyArgsSchema,
            update: ListUpdateArgsSchema,
            create: ListCreateArgsSchema,
        },
        todo: {
            findMany: TodoFindManyArgsSchema,
            update: TodoUpdateArgsSchema,
            create: TodoCreateArgsSchema,
        },
        user: {
            findMany: UserFindManyArgsSchema,
            update: UserUpdateArgsSchema,
            create: UserCreateArgsSchema,
        },
    };
    
  • A GetSystemPrompt function that returns a system prompt for the AI agent to use:

    export const GetSystemPrompt = (userId: string):string
    

Setup

  • Install the package:
npm install -D zenstack-crudzod
  • Add the plugin to your ZModel schema file:
plugin zenstack_crud_zod {
    provider = 'zenstack-crudzod'
    // optional: specify output path
    output = '.'
}
  • Generate the Zod schemas:
npx zenstack generate

After running the command, you'll see a crud-zod.ts file generated in the specified output directory (or the current directory if not specified).

Options

NameTypeDescriptionRequiredDefault
outputStringOutput directory path (relative to the path of ZModel)Nocurrent directory

Local Development

Install

npm install

Build

npm run build

After building, the plugin will be generated in the dist folder. Then you can use it in your existing ZModel schema by setting the provider to this dist folder:

plugin zenstack_crud_zod {
    provider = '../path/to/zenstack-crudzod/dist'
}

License

MIT

FAQs

Package last updated on 28 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.