@eduardomagaldi/crudy
v1.0.2
Published
A CRUD generator for Express.js with PostgreSQL - automatically detects schemas and handles relations
Maintainers
Readme
Crudy
A CRUD generator for Express.js with PostgreSQL that automatically detects table schemas and handles relations.
Features
- 🚀 Auto-detect schemas from PostgreSQL database (lazy loading - no database calls on creation)
- 🔗 Automatic relation detection from foreign keys
- 📦 Multi-table support - generate CRUD for multiple tables at once
- 🔍 Related data inclusion - GET requests automatically include related records
- 🛡️ Smart delete protection - shows which records are blocking deletions
- ⚡ Zero configuration - just pass table names
Installation
npm install github:eduardomagaldi/crudy
npm install pg expressQuick Start
const express = require('express')
const { Pool } = require('pg')
const createCrudyMulti = require('crudy')
const pool = new Pool({
user: 'your_user',
host: 'localhost',
database: 'your_database',
password: 'your_password',
port: 5432,
})
const app = express()
app.use(express.json())
// ⚡ No await needed! createCrudyMulti is synchronous
// Schemas are loaded lazily on first request
const handlers = createCrudyMulti(['users', 'addresses', 'purchases'], pool)
// Response middleware
const sendResponse = (req, res) => {
const status = res.locals.crudyStatus || 200
const result = res.locals.crudyResult
if (status === 204) return res.status(204).send()
res.status(status).json(result)
}
// Set up routes
app.get('/users', handlers.users.handleGetAll, sendResponse)
app.get('/users/:id', handlers.users.handleGet, sendResponse)
app.post('/users', handlers.users.handleCreate, sendResponse)
app.put('/users/:id', handlers.users.handleUpdate, sendResponse)
app.delete('/users/:id', handlers.users.handleDelete, sendResponse)
// Repeat for other tables: handlers.addresses.*, handlers.purchases.*
app.listen(3000, () => {
console.log('Server running on http://localhost:3000')
})How It Works
Lazy Schema Loading
No database calls on creation! Schemas are automatically loaded and cached in memory on the first request. This means:
- ✅ Instant handler creation (synchronous, no await)
- ✅ Schemas cached as singleton (one query per table)
- ✅ Fast subsequent requests
Schema Auto-Detection
Crudy automatically detects:
- Column types, primary keys, foreign keys, auto-increment, required/optional fields
Relation Detection
Relations are automatically detected from:
- Foreign key constraints
- Column patterns (e.g.,
user_id→userstable)
Relation Types:
- belongsTo - Included as singular property:
address.user - hasMany - Included as plural property:
user.addresses
API
createCrudyMulti(tableNames, db)
Creates CRUD handlers for multiple tables. Synchronous - no await needed!
Parameters:
tableNames(string[]) - Array of table namesdb(pg.Pool) - PostgreSQL connection pool
Returns: Object with table names as keys, each containing:
handleGetAll(req, res, next)- Get all recordshandleGet(req, res, next)- Get single record by IDhandleCreate(req, res, next)- Create new recordhandleUpdate(req, res, next)- Update record by IDhandleDelete(req, res, next)- Delete record by IDtableName- Table nameschema- Schema object (available after first request)
Response Format
GET with Relations
{
"id": 1,
"email": "[email protected]",
"addresses": [{ "id": 1, "street": "123 Main St", "user_id": 1 }],
"user": { "id": 1, "email": "[email protected]" }
}DELETE with Relations
If deleting a record referenced by others:
{
"error": "Cannot delete: this record is referenced by other records",
"details": "2 record(s) in 'addresses' table (IDs: 1, 2)"
}Status: 409 Conflict
Query Parameters
Filter results using query parameters:
GET /[email protected]&is_active=trueError Handling
Handlers set response data in res.locals:
res.locals.crudyStatus- HTTP status coderes.locals.crudyResult- Response data or error object
Status codes: 200 (Success), 201 (Created), 204 (No Content), 404 (Not Found), 409 (Conflict), 500 (Server Error)
Requirements
- Node.js 14+
- PostgreSQL database
pgpackage (peer dependency)expresspackage
License
ISC
