@tgraph/backend-generator
v0.1.1
Published
CLI toolkit for generating NestJS APIs, DTOs, and React Admin dashboards from Prisma schemas.
Maintainers
Readme
TGraph Backend Generator
Transform your Prisma schema into production-ready NestJS APIs and React Admin dashboards with a single command.
Features
- Full-Stack Generation: Generate NestJS controllers, services, DTOs, and React Admin pages from Prisma schemas
- Type Safety: Carry TypeScript types from database to frontend, including Swagger-based API clients
- Smart Introspection: Automatically discovers your project structure and preserves custom code
- Unique Field Getters: Auto-generated getters for every Prisma
@uniquefield - Relation Support: React Admin resources and DTOs understand Prisma relations out of the box
- Static Infrastructure: Generate guards, interceptors, decorators, and utilities
- Field Directives: Fine-grained control with
@tg_format,@tg_upload, and@tg_readonly - Safe Regeneration: Generated files live beside your code and can be overwritten safely
- Convention Over Configuration: Works out of the box with sensible defaults
- Interactive Wizard: Guided setup with
tgraph init
Quick Start
# Install
npm install --save-dev @tgraph/backend-generator
# Initialize configuration
npx tgraph init
# Mark your Prisma models
// @tg_form()
model User {
id String @id @default(uuid())
name String
email String @unique
createdAt DateTime @default(now())
}
# Generate everything
npx tgraph allResult: Complete CRUD API plus an admin dashboard in seconds.
What Gets Generated
Backend (NestJS)
- REST controllers with pagination and search
- Services with CRUD operations, unique field getters, and Prisma integration
- DTOs with class-validator decorators (including arrays and enums)
- Automatic AppModule updates
- Optional static modules (guards, DTOs, interceptors, utilities)
Frontend (React Admin)
- List/Edit/Create/Show pages for every model
- Studio pages for spreadsheet-style editing
- Type-appropriate inputs (dates, uploads, references, JSON)
- Relation handling with autocomplete widgets
- Type-safe API client generated from Swagger
Example
Input (Prisma):
// @tg_form()
model Post {
id String @id @default(uuid())
title String // @min(5) @max(200)
content String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
}Output: Instant admin system with a working API and dashboard.
Installation
# Project dependency (recommended)
npm install --save-dev @tgraph/backend-generator
# Global installation
npm install -g @tgraph/backend-generatorUsage
# Generate everything
tgraph all
# Preview pending changes without writing files
tgraph preflight
# Generate only API files
tgraph api
# Generate only dashboard resources
tgraph dashboard
# Generate only DTOs
tgraph dtos
# With options
tgraph all --config tgraph.admin.config.ts --non-interactive
# List available static modules
tgraph static --list
# Generate selected static modules
tgraph static --include admin.guard,pagination.interceptor
# Generate dashboard API types from swagger.json
tgraph typesRequirements
- Node.js 18.0.0 or newer
- NestJS project with Prisma
- React Admin dashboard (optional, only required for dashboard generation)
Documentation
- Complete Documentation - Browse every guide and reference
Getting Started
- Getting Started - Installation and setup
- Quick Start Tutorial - Build a blog in 5 minutes
- CLI Reference - All commands and options
Guides
- Prisma Setup - Configure your schema
- Field Directives - Control generation behavior
- Authentication Guards - Configure authentication
- Static Files Generation - Infrastructure generation
- Dashboard Types Generation - Type-safe API clients
Advanced
- Recipes - Practical examples
- SDK Reference - Programmatic usage
- Troubleshooting - Common issues
AI Integration
- LLM Integration Guide - Complete guide for AI assistants
Configuration
Initialize a configuration file in your project:
npx tgraph initThis creates tgraph.config.ts with typed defaults:
import type { Config } from '@tgraph/backend-generator';
export const config: Config = {
input: {
prisma: {
schemaPath: 'prisma/schema.prisma',
servicePath: 'src/infrastructure/database/prisma.service.ts',
},
dashboard: {
components: { form: {}, display: {} },
},
},
output: {
backend: {
root: 'src/features',
dtosPath: 'src/dtos/generated',
modulesPaths: ['src/features', 'src/modules', 'src'],
guardsPath: 'src/guards',
decoratorsPath: 'src/decorators',
interceptorsPath: 'src/interceptors',
utilsPath: 'src/utils',
appModulePath: 'src/app.module.ts',
},
dashboard: {
enabled: true,
updateDataProvider: true,
root: 'src/dashboard/src',
resourcesPath: 'src/dashboard/src/resources',
swaggerJsonPath: 'src/dashboard/src/types/swagger.json',
apiPath: 'src/dashboard/src/types/api.ts',
appComponentPath: 'src/dashboard/src/App.tsx',
dataProviderPath: 'src/dashboard/src/providers/dataProvider.ts',
},
},
api: {
suffix: '',
prefix: 'tg-api',
authenticationEnabled: true,
requireAdmin: true,
guards: [{ name: 'JwtAuthGuard', importPath: '@/guards/jwt-auth.guard' }],
adminGuards: [{ name: 'AdminGuard', importPath: '@/guards/admin.guard' }],
},
behavior: {
nonInteractive: false,
},
};The structured configuration makes it easy to run multiple variants (--config tgraph.public.config.ts), customize component imports, and control every output path.
Public vs Admin Controllers
- Set
api.authenticationEnabled = false(or pass--public) to generate public controllers without guards. - Set
api.requireAdmin = trueto add the@IsAdmin()decorator and admin guards.
Route Prefix
api.prefix controls the base route for generated controllers (for example tg-api, public-api). Use different prefixes per config file.
Static Assets Generation
- During
tgraph api, you'll be prompted to generate static files. Use--yesfor non-interactive generation ortgraph static --include <names>for a targeted run. - Available names include:
admin.guard,feature-flag.guard,is-admin.decorator,paginated-search-query.dto,paginated-search-result.dto,api-response.dto,pagination.interceptor,audit.interceptor,paginated-search.decorator,paginated-search.util.
Dashboard API Types
tgraph types reads the file defined by output.dashboard.swaggerJsonPath (default src/dashboard/src/types/swagger.json) and generates the client at output.dashboard.apiPath. Ensure swagger.json is fresh before running.
Programmatic Usage
import { ApiGenerator, DashboardGenerator, DtoGenerator } from '@tgraph/backend-generator';
import { config } from './tgraph.config';
const api = new ApiGenerator(config);
await api.generate();
const dashboard = new DashboardGenerator(config);
await dashboard.generate();
const dtos = new DtoGenerator(config);
await dtos.generate();See the SDK Reference for every method and option.
Project Structure
your-project/
|- prisma/
| |- schema.prisma
|
|- src/
| |- app.module.ts
| |- features/
| |- user/
| |- user.tg.controller.ts # Generated
| |- user.tg.service.ts # Generated
| |- create-user.tg.dto.ts # Generated
| |- user.service.ts # Your custom logic
|
|- dashboard/src/
|- App.tsx
|- resources/
|- users/
|- UserList.tsx # Generated
|- UserEdit.tsx # Generated
|- ...Field Directives
Control generation directly from your Prisma schema:
model User {
/// @tg_format(email)
email String @unique
/// @tg_upload(image)
avatar String?
/// @tg_readonly
ipAddress String?
}See the Field Directives Guide for every directive.
Philosophy
- Convention over Configuration: Works out of the box
- Generate, Don't Replace: Generated files live beside your code
- Progressive Disclosure: Start simple, add complexity when needed
- Type Safety First: Catch errors at compile time
Contributing
Contributions are welcome! See the Contributing Guide.
Publishing
For maintainers: see the Publishing Guide for the release process.
License
ISC
Links
Made with love by the TruGraph team.
