Auto-generate RESTFULL CRUD APIs for any PostgreSQL table without writing code.
go get github.com/abdulaziz-go/go-gen-apis
package main
import (
"github.com/abdulaziz-go/go-gen-apis"
"github.com/abdulaziz-go/go-gen-apis/config"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
api := r.Group("/api/v1")
cfg := &config.GenApiConfig{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "password",
DBName: "mydb",
SSLMode: "disable",
}
genapis.SetUpAutoGeneratedApis(cfg, api)
r.Run(":8080")
}
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE,
age INTEGER
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT,
user_id INTEGER,
published BOOLEAN DEFAULT false
);
All endpoints follow this pattern: /api/v1/items/{table_name}
Method | Endpoint | Description |
---|---|---|
POST | /items/users |
Create user(s) |
GET | /items/users |
Get all users |
GET | /items/users/1 |
Get user by ID |
PUT | /items/users/1 |
Update user |
DELETE | /items/users/1 |
Delete user |
# Single user
curl -X POST http://localhost:8080/api/v1/items/users \
-H "Content-Type: application/json" \
-d '{
"data": [{
"name": "John Doe",
"email": "[email protected]",
"age": 30
}]
}'
# Multiple users
curl -X POST http://localhost:8080/api/v1/items/users \
-H "Content-Type: application/json" \
-d '{
"data": [
{"name": "Alice", "email": "[email protected]", "age": 25},
{"name": "Bob", "email": "[email protected]", "age": 35}
]
}'
# All users
curl "http://localhost:8080/api/v1/items/users"
# With pagination
curl "http://localhost:8080/api/v1/items/users?limit=10&offset=0"
# Filter by single column
curl "http://localhost:8080/api/v1/items/users?age=30"
# Filter by multiple columns
curl "http://localhost:8080/api/v1/items/users?age=30&name=John"
# Multiple values for same column (IN operator)
curl "http://localhost:8080/api/v1/items/users?age=25&age=30&age=35"
# With sorting
curl "http://localhost:8080/api/v1/items/users?order_by=name&sort=asc"
# Complex filtering with sorting
curl "http://localhost:8080/api/v1/items/users?age=30&order_by=name&sort=desc&limit=5"
# Get specific user
curl "http://localhost:8080/api/v1/items/users/1"
curl -X PUT http://localhost:8080/api/v1/items/users/1 \
-H "Content-Type: application/json" \
-d '{
"data": {
"name": "John Smith",
"age": 31
}
}'
curl -X DELETE "http://localhost:8080/api/v1/items/users/1"
# Create post
curl -X POST http://localhost:8080/api/v1/items/posts \
-H "Content-Type: application/json" \
-d '{
"data": [{
"title": "My First Post",
"content": "Hello World!",
"user_id": 1,
"published": true
}]
}'
# Get published posts by specific user
curl "http://localhost:8080/api/v1/items/posts?published=true&user_id=1"
# Get posts with multiple user IDs
curl "http://localhost:8080/api/v1/items/posts?user_id=1&user_id=2&user_id=3"
# Filter by multiple columns with sorting
curl "http://localhost:8080/api/v1/items/posts?published=true&user_id=1&order_by=id&sort=desc"
limit
- Items per page (default: 50, max: 1000)offset
- Skip items (default: 0)order_by
- Sort columnsort
- Sort direction (asc
/desc
)- Any column name - Filter by value
# Single filter
?age=30
# Multiple columns (AND condition)
?age=30&name=John&published=true
# Multiple values for same column (IN condition)
?user_id=1&user_id=2&user_id=3
# Combined with pagination and sorting
?age=30&published=true&limit=10&order_by=created_at&sort=desc
{
"success": true,
"message": "Items retrieved successfully",
"data": [...],
"meta": {
"total": 100,
"limit": 10,
"offset": 0
}
}
{
"success": false,
"message": "Item not found",
"error": "..."
}