Skip to content

mgaebler/drizzle-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

247 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drizzle REST

Transform your database schema into a REST API in seconds.

CI TypeScript License: MIT npm version Alpha Release

A dynamic REST API adapter for Drizzle ORM with JSON-Server compatible query syntax

⚠️ Alpha Release: This project is in active development. APIs may change and features are still being finalized. Use in production at your own risk.

Transform your Drizzle schema into a fully functional REST API with a single function call. Perfect for rapid prototyping, admin panels, and seamless migration from JSON-Server.

✨ Features

  • 🚀 Zero Configuration: Generate REST endpoints from your Drizzle schema instantly
  • 🔍 JSON-Server Compatible: Familiar query syntax for filtering, sorting, and pagination
  • 🎣 Hook System: Authorization, data transformation, and custom logic with beforeOperation and afterOperation hooks
  • 🗄️ PostgreSQL Support: Full PostgreSQL database support

📦 Installation

npm install drizzle-rest-adapter
import express from 'express';
import { createDrizzleRestAdapter } from 'drizzle-rest-adapter';
import { db } from './db/connection'; // Your Drizzle instance
import * as schema from './db/schema'; // Your Drizzle schema

const app = express();
app.use(express.json());

// Create the REST API adapter
const apiRouter = createDrizzleRestAdapter({
  db: db,
  schema: schema,
});

// Mount the generated API
app.use('/api/v1', apiRouter);

app.listen(3000, () => {
  console.log('REST API running on http://localhost:3000/api/v1');
});

Adapter Documentation

That's it! Your API is now available with full CRUD operations for all tables in your schema.

📖 API Usage

Basic CRUD Operations

# Get all users
GET /api/v1/users

# Get user by ID
GET /api/v1/users/123

# Create new user
POST /api/v1/users
Content-Type: application/json
{ "name": "John Doe", "email": "john@example.com" }

# Update user (partial)
PATCH /api/v1/users/123
Content-Type: application/json
{ "name": "Jane Doe" }

# Replace user (complete)
PUT /api/v1/users/123
Content-Type: application/json
{ "name": "Jane Doe", "email": "jane@example.com" }

# Delete user
DELETE /api/v1/users/123

Filtering

# Direct equality
GET /api/v1/users?status=active

# Range filters
GET /api/v1/users?age_gte=18&age_lte=65

# String search (substring)
GET /api/v1/users?name_like=John

# Negation
GET /api/v1/users?status_ne=inactive

# Array membership (multiple IDs)
GET /api/v1/users?id=1&id=2&id=3

Pagination

# Page-based pagination
GET /api/v1/users?_page=2&_per_page=25

# Range-based pagination
GET /api/v1/users?_start=10&_end=20
GET /api/v1/users?_start=10&_limit=10

Sorting

# Single field ascending
GET /api/v1/users?_sort=name

# Single field descending
GET /api/v1/users?_sort=-created_at

Relationships

# Embed related data
GET /api/v1/posts?_embed=author
GET /api/v1/posts?_embed=author,comments

🎯 JSON-Server Migration

Migrating from JSON-Server? The query syntax is 100% compatible:

🗄️ Database Support

Currently supports PostgreSQL databases:

  • PostgreSQL (PGlite and standard PostgreSQL)

🤝 Contributing

We welcome contributions! This is an alpha project, so expect rapid changes and improvements.

Alpha Contributors Welcome: Since this is an alpha release, your feedback and contributions are especially valuable in shaping the final API.

Please see CONTRIBUTING.md for guidelines.

Development Setup

git clone https://github.com/mgaebler/drizzle-rest.git
cd drizzle-rest
npm install
npm run dev

Running Tests

npm test          # Run all tests
npm run test:watch # Watch mode
npm run lint      # Lint code

📄 License

MIT License - see LICENSE file for details.

🙏 Acknowledgments


Made with ❤️ for the Drizzle community

About

A dynamic REST API adapter for Drizzle ORM with JSON-Server compatible query syntax

Resources

License

Contributing

Security policy

Stars

11 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors