UNPKG

@sarawebs/sb-bin

Version:

CLI tools for SaraWebs dev workflow

155 lines (101 loc) 2.87 kB
## CRUD Generator Script A simple Bash script to generate RESTful CRUD route and controller files for an Express + PostgreSQL project, and automatically register the new routes in `src/routes/index.js`. --- ## Features - Creates route, controller, and query files for a given model name. - Uses pg client (`pool.js`) for database operations. - Automatically updates (or creates): - `src/routes/index.js` to import/register the route - `src/db/db.js` to register the model - Prevents overwriting existing files. - Cleanly removes all generated files and related registrations. - Uses ESM style imports throughout. --- ## Requirements - Node.js project using **Express** and **pg** - Project directory structure: ``` src/ controllers/ db/ db.js pool.js queries/ routes/ index.js ``` - Bash shell environment. --- ## Usage ### ✅ Create CRUD ```bash sb-gen-api <ModelName> ``` **Example:** ```bash sb-gen-api User ``` This will create: - `src/routes/users.js` - `src/controllers/userController.js` - `src/db/queries/user.js` - Auto-register in: - `src/routes/index.js` (under `/api/v1/users`) - `src/db/db.js` (as `db.user`) --- ### 🗑️ Remove CRUD ```bash sb-gen-api remove <ModelName> ``` **Example:** ```bash sb-gen-api remove User ``` This will: - Delete: - `src/routes/users.js` - `src/controllers/userController.js` - `src/db/queries/user.js` - Cleanly remove: - Import and usage from `src/routes/index.js` - Import and reference from `src/db/db.js` - Prompt for confirmation before deletion --- ## What it generates ### Routes file (`src/routes/<modelPlural>.js`) Express router with: - `GET /` Get all items - `GET /:id` Get item by ID - `POST /` Create item - `PUT /:id` Update item - `DELETE /:id` Delete item ### Controller file (`src/controllers/<modelName>Controller.js`) CRUD handlers using `db.<model>.getAll()`, etc. ### Query file (`src/db/queries/<modelName>.js`) SQL queries and logic for database interaction. ### Routes index (`src/routes/index.js`) Imports and registers all routes under `/api/v1/<modelPlural>` paths. --- ## Notes - Model name is case-insensitive; it's normalized and pluralized by adding an `s`. - If files for the model already exist, the script will abort. - Uses API version `v1` (you can change in `index.js`). - Make sure your main `src/index.js` imports and uses the route register function. --- ## Example `src/index.js` usage ```js import express from 'express'; import registerRoutes from './routes/index.js'; import dotenv from 'dotenv'; dotenv.config(); const app = express(); const port = process.env.PORT || 3000; app.use(express.json()); registerRoutes(app, 'v1'); app.listen(port, () => { console.log(\`Server running on port \${port}\`); }); ``` --- ## License MIT License