elysia-crud-plugin
v0.1.1
Published
Generate REST endpoints for your Drizzle tables without writing boilerplate. Inspired by Django Rest Framework's simplicity.
Maintainers
Readme
Elysia CRUD Plugin
Generate REST endpoints for your Drizzle tables without writing boilerplate. Inspired by Django Rest Framework's simplicity, this plugin creates full CRUD operations from your table definitions.
What it does
Pass a Drizzle table to the crud function, and you get five REST endpoints:
GET /{tableName}- List all recordsGET /{tableName}/:id- Get one recordPOST /{tableName}- Create a recordPUT /{tableName}/:id- Update a recordDELETE /{tableName}/:id- Delete a record
The plugin automatically detects your primary key, handles errors, and returns appropriate HTTP status codes.
Installation
bun add elysia-crud-pluginYou also need Elysia and Drizzle ORM:
bun add elysia drizzle-ormQuick start
import { Elysia } from "elysia";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { sqliteTable, integer, text } from "drizzle-orm/sqlite-core";
import { Database } from "bun:sqlite";
import { crud } from "elysia-crud-plugin";
// Define your table
const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
email: text("email").notNull(),
});
// Set up database
const sqlite = new Database("example.db");
const db = drizzle(sqlite);
// Create your app
const app = new Elysia().decorate("db", db).use(crud(users)).listen(3000);That's it. You now have working CRUD endpoints for users.
How it works
The plugin reads your Drizzle table definition to:
- Extract the table name for route generation
- Find the primary key column automatically
- Generate type-safe endpoints based on your table schema
You don't write route handlers, validation logic, or error handling. The plugin handles it.
Field selection
Control which fields appear in responses using the fields option:
app.use(crud(users, { fields: ["id", "name", "email"] }));This returns only the specified fields in all responses. Useful for hiding sensitive data like passwords or internal fields.
Field selection affects responses only. All fields are still validated on create and update operations.
Database setup
The plugin expects a database instance decorated on your Elysia app. Use Elysia's decorate method:
const app = new Elysia().decorate("db", db).use(crud(users));The plugin works with any database Drizzle supports: SQLite, PostgreSQL, MySQL, and others.
Error handling
The plugin handles common errors:
404 Not Found- Record doesn't exist (GET, PUT, DELETE by ID)500 Internal Server Error- Database not configured or other server errors
Error responses include a JSON body with an error field:
{
"error": "Resource not found"
}Limitations
The current version focuses on core CRUD operations. It doesn't include:
- Pagination for list endpoints
- Filtering or sorting
- Query parameter field selection
- Lifecycle hooks
- Custom validation beyond Drizzle schema
These may be added in future versions. For now, the plugin keeps things simple and focused.
