Generator API Reference
The generator is built into the nevr package and provides functions for generating Prisma schemas and OpenAPI specifications from your Nevr entity definitions.
Configuration with defineConfig
The nevr.config.ts file is required for the CLI to work:
// nevr.config.ts
import { defineConfig } from "nevr"
import { user, post } from "./entities"
import { auth } from "nevr/plugins/auth"
export default defineConfig({
database: "postgresql", // "sqlite" | "postgresql" | "mysql"
outDir: "./prisma", // Output directory (default: "./prisma")
entities: [user, post],
plugins: [auth()],
incremental: true, // Enable caching (default: true)
})Then use the CLI:
npx nevr generate # Auto-loads nevr.config.ts
npx nevr db:push # Push schema to databaseConfig file locations (auto-discovered in order):
nevr.config.tsnevr.config.jsnevr.config.mjssrc/nevr.config.tssrc/nevr.config.jssrc/nevr.config.mjslib/nevr.config.tslib/nevr.config.jslib/nevr.config.mjs
Programmatic API
For programmatic use, import directly from nevr/generator:
import { generate, generatePrismaSchema, generateOpenAPI } from "nevr/generator"Primary Functions
generate
Generate a complete Prisma schema from your entities with full support for incremental caching and schema splitting.
import { generate } from "nevr/generator"
import { entities } from "./entities"
const result = generate(entities, {
provider: "postgresql", // sqlite, mysql, postgresql
outDir: "./prisma",
})Options:
| Option | Type | Default | Description |
|---|---|---|---|
provider | "sqlite" | "postgresql" | "mysql" | "sqlite" | Database provider |
outDir | string | "./prisma" | Output directory |
incremental | boolean | true | Enable incremental caching |
force | boolean | false | Force full regeneration |
splitSchema | boolean | false | Enable schema splitting by namespace |
Returns:
{
files: string[] // Generated file paths
schemaPath: string // Main schema path
namespaceFiles?: Record<string, string> // Namespace files (if splitSchema)
cache?: { // Cache info
wasIncremental: boolean
changedEntities: string[]
unchangedEntities: string[]
skipped: boolean
}
}generatePrismaSchema
Generate a Prisma schema string directly (useful for programmatic access).
import { generatePrismaSchema } from "nevr/generator"
const schemaString = generatePrismaSchema(entities, {
provider: "postgresql",
})
// Returns the complete Prisma schema as a stringgeneratePrismaHeader
Generate the header portion of a Prisma schema (generator + datasource blocks).
import { generatePrismaHeader } from "nevr/generator"
const header = generatePrismaHeader({
provider: "postgresql",
splitSchema: false,
})
// Returns string with generator and datasource blocksgeneratePrismaModels
Generate only the model definitions from entities (without header).
import { generatePrismaModels } from "nevr/generator"
const models = generatePrismaModels(entities, allEntities)
// Returns string with all model definitionsgenerateOpenAPI
Generate an OpenAPI 3.0 specification from your entities.
import { generateOpenAPI } from "nevr/generator"
const spec = generateOpenAPI(entities, {
info: {
title: "My API",
version: "1.0.0",
description: "API generated by Nevr",
},
servers: [{ url: "http://localhost:3000" }],
basePath: "/api",
security: true,
outPath: "./docs/openapi.json", // Optional: write to file
})See OpenAPI Guide for more details.
Incremental Caching
The generator uses an incremental caching system to avoid regenerating unchanged entities.
// Cache is stored in .nevr-cache.json in the output directory
{
"version": "1.0.0",
"entities": {
"user": "abc123...", // MD5 hash of entity config
"post": "def456..."
},
"lastGenerated": "2024-01-15T10:30:00Z"
}Disable caching:
generate(entities, { incremental: false })
// or force regeneration
generate(entities, { force: true })Schema Splitting
For large codebases (100+ entities), you can enable schema splitting to organize models by namespace:
// Define entities with namespaces
const user = entity("user", { ... }).namespace("auth")
const post = entity("post", { ... }).namespace("content")
// Generate with split schema
generate(entities, { splitSchema: true })
// Creates:
// prisma/schema/schema.prisma (header + datasource)
// prisma/schema/auth.prisma (auth models)
// prisma/schema/content.prisma (content models)This uses Prisma's prismaSchemaFolder preview feature.
CLI Integration
The generator is typically invoked via the CLI:
npx nevr generate
# or
npx nevr gSee CLI > Code Generation for details.
Type Inference
Note: TypeScript types and API clients are no longer generated by the generator. They are automatically inferred from the server through Nevr's E2E type system.
import { createClient } from "nevr/client"
const client = createClient({ baseURL: "http://localhost:3000" })
// Types are automatically synced from server!
const users = await client.users.list() // TypeScript knows the shapeSee Type Inference for details.
