Skip to content

A tool for generating dynamic APIs in Go that focuses on the service layer, helping developers create clean, modular, and maintainable APIs efficiently

Notifications You must be signed in to change notification settings

abdulaziz-go/go-gen-apis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Generated APIs

Auto-generate RESTFULL CRUD APIs for any PostgreSQL table without writing code.

Installation

go get github.com/abdulaziz-go/go-gen-apis

Quick Setup

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")
}

Database Example

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
);

API Endpoints

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

Usage Examples

Create Users

# 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}
    ]
  }'

Get Users

# 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"

Update User

curl -X PUT http://localhost:8080/api/v1/items/users/1 \
  -H "Content-Type: application/json" \
  -d '{
    "data": {
      "name": "John Smith",
      "age": 31
    }
  }'

Delete User

curl -X DELETE "http://localhost:8080/api/v1/items/users/1"

Working with Posts

# 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"

Query Parameters

  • limit - Items per page (default: 50, max: 1000)
  • offset - Skip items (default: 0)
  • order_by - Sort column
  • sort - Sort direction (asc/desc)
  • Any column name - Filter by value

Filtering Examples

# 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

Response Format

Success

{
  "success": true,
  "message": "Items retrieved successfully",
  "data": [...],
  "meta": {
    "total": 100,
    "limit": 10,
    "offset": 0
  }
}

Error

{
  "success": false,
  "message": "Item not found",
  "error": "..."
}

About

A tool for generating dynamic APIs in Go that focuses on the service layer, helping developers create clean, modular, and maintainable APIs efficiently

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages