GoBase is a powerful, Django-inspired ORM library for Go that provides elegant database operations with automatic migrations, user management, and CLI tools.
go get github.com/AIGamer28100/gobase
package main
import (
"log"
"github.com/AIGamer28100/gobase"
)
// Define your model
type Article struct {
gobase.BaseModel
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
Status string `json:"status" gorm:"default:draft"`
}
func main() {
// Initialize database connection
connection, err := gobase.InitDB()
if err != nil {
log.Fatal(err)
}
defer connection.Close()
// Create accessor for database operations
accessor := gobase.NewAccessor(connection)
// Migrate database schema
err = accessor.Migrate(&Article{})
if err != nil {
log.Fatal(err)
}
// Create a new article
article := &Article{
Title: "Getting Started with GoBase",
Content: "GoBase makes database operations simple...",
Author: "Developer",
Status: "published",
}
err = accessor.Create(article)
if err != nil {
log.Fatal(err)
}
}
Create a .env file in your project root:
DB_TYPE=sqlite
DB_NAME=myapp.db
# For PostgreSQL:
# DB_TYPE=postgres
# DB_HOST=localhost
# DB_PORT=5432
# DB_USER=username
# DB_PASSWORD=password
# DB_NAME=mydb
// Create
article := &Article{Title: "New Article", Content: "Content here"}
err := accessor.Create(article)
// Get by ID
var article Article
err := accessor.Get(&article, 1)
// Get all records
var articles []Article
err := accessor.All(&articles)
// Filter records
var publishedArticles []Article
err := accessor.Filter(&publishedArticles, "status = ?", "published")
// Update
article.Status = "archived"
err := accessor.Update(&article)
// Delete (soft delete)
err := accessor.Delete(&article)
// Create a superuser
user := &gobase.User{
Username: "admin",
Email: "admin@example.com",
}
err := user.SetPassword("SecurePassword123!")
if err != nil {
log.Fatal(err)
}
err = gobase.CreateSuperuser(accessor, user)
if err != nil {
log.Fatal(err)
}
// Verify password
isValid := user.CheckPassword("SecurePassword123!")
GoBase provides a powerful CLI tool for common database operations.
go install github.com/AIGamer28100/gobase/cmd/gobase@latest
# Run database migration
gobase -migrate
# Create a superuser
gobase -createsuperuser -username admin -email admin@example.com
# Preload data from JSON files
gobase -preload -files articles.json,users.json
# Get help
gobase -help
Create JSON files with data to preload. The filename (without extension) should match your model name:
articles.json:
[
{
"id": 1,
"title": "Sample Article",
"content": "This is sample content...",
"author": "Author Name",
"status": "published"
}
]
type CustomModel struct {
gobase.BaseModel
ID string `gorm:"primarykey;size:36" json:"id"` // Override default ID
Name string `json:"name"`
}
modelRegistry := map[string]interface{}{
"articles": &Article{},
"users": &gobase.User{},
}
err := accessor.Preload(modelRegistry, "data.json")
config := gobase.DatabaseConfig{
Type: "postgres",
Host: "localhost",
Port: 5432,
User: "username",
Password: "password",
Database: "mydb",
}
connection, err := gobase.InitDBWithConfig(config)
Run the test suite:
cd gobase
go test -v
git clone https://github.com/AIGamer28100/gobase.git
cd gobase
go mod download
make install-hooks
The Git hooks will automatically run before each commit to ensure:
go vet# Build the project
make build
# Run tests
make test
# Run linter
make lint
# Run security scan
make security
# Run all checks (like CI)
make test lint security
# Install Git hooks
make install-hooks
# Clean build artifacts
make clean
If you prefer to install hooks manually:
./scripts/install-hooks.sh
To bypass hooks for a commit (not recommended):
git commit --no-verify
MIT License - see LICENSE file for details.