UNPKG

@rapidd/build

Version:

Dynamic code generator that transforms Prisma schemas into Express.js CRUD APIs with PostgreSQL RLS-to-JavaScript translation

161 lines (121 loc) β€’ 3.97 kB
# @rapidd/build Dynamic code generator that transforms Prisma schemas into complete Express.js CRUD APIs with intelligent PostgreSQL RLS-to-JavaScript translation. ## Features - πŸš€ **Automatic CRUD API Generation** - Creates Express.js routes from Prisma models - πŸ”’ **RLS Translation** - Converts PostgreSQL Row-Level Security policies to JavaScript/Prisma filters (ACL: Access Control Layer) - 🎯 **Dynamic & Schema-Aware** - Zero hardcoding, adapts to any database structure - πŸ”— **Relationship Handling** - Supports 1:1, 1:n, n:m including junction tables - πŸ‘₯ **Role-Based Access Control** - Properly handles role checks in filters - πŸ“Š **Model Generation** - Creates CRUD model classes with capitalized filenames - πŸ—ΊοΈ **Relationships JSON** - Generates complete relationship mappings with foreign keys - ⚑ **Selective Generation** - Update only specific models or components ## Installation ```bash npm install @rapidd/build ``` ## Quick Start ```bash # Generate everything in current directory (default) npx rapidd build # Generate in specific directory npx rapidd build --output ./generated # Generate only specific model npx rapidd build --model user # Generate only specific component npx rapidd build --only model npx rapidd build --only route npx rapidd build --only acl npx rapidd build --only relationship # Combine model and component filters npx rapidd build --model account --only route # Specify custom user table npx rapidd build --user-table accounts ``` ## CLI Options - `-o, --output <path>` - Output directory (default: `./`) - `-s, --schema <path>` - Prisma schema file (default: `./prisma/schema.prisma`) - `-m, --model <name>` - Generate/update only specific model (e.g., "account", "user") - `--only <component>` - Generate only specific component: "model", "route", "acl", or "relationship" - `--user-table <name>` - User table name for ACL (default: auto-detected) ## Selective Generation ### Update Single Model ```bash # Update only the account model across all components npx rapidd build --model account ``` This will: - Generate/update `src/Model/Account.js` - Generate/update `routes/api/v1/account.js` - Update the `account` entry in `rapidd/relationships.json` - Update the `account` entry in `rapidd/acl.js` ### Update Single Component ```bash # Regenerate all routes npx rapidd build --only route # Regenerate all ACL configs npx rapidd build --only acl # Regenerate all models npx rapidd build --only model # Regenerate relationships npx rapidd build --only relationship ``` ### Combine Filters ```bash # Update only the route for a specific model npx rapidd build --model user --only route # Update ACL for account model npx rapidd build --model account --only acl ``` ## Generated Structure ``` ./ β”œβ”€β”€ src/Model/ β”‚ β”œβ”€β”€ User.js β”‚ β”œβ”€β”€ Post.js β”‚ └── ... β”œβ”€β”€ routes/ β”‚ β”œβ”€β”€ user.js β”‚ β”œβ”€β”€ post.js β”‚ └── ... └── rapidd/ β”œβ”€β”€ acl.js β”œβ”€β”€ relationships.json └── rapidd.js ``` ## ACL Translation Example **PostgreSQL Policy:** ```sql CREATE POLICY user_policy ON posts FOR SELECT USING (author_id = current_user_id() OR current_user_role() IN ('admin', 'moderator')); ``` **Generated JavaScript:** ```javascript getAccessFilter: (user) => { if (['admin', 'moderator'].includes(user?.role)) return {}; return { author_id: user?.id }; } ``` ## Use Cases ### During Development ```bash # After adding a new model to schema npx rapidd build --model newModel # After changing relationships npx rapidd build --only relationship # After updating RLS policies npx rapidd build --only acl ``` ### Continuous Integration ```bash # Full rebuild for CI/CD npx rapidd build --output ./generated ``` ### Incremental Updates ```bash # Update specific model after schema changes npx rapidd build --model user --only model npx rapidd build --model user --only acl ``` ## License MIT