Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DBYOC - Database Bring Your Own Connection

License: MIT Go Version Release

dbyoc logo

DBYOC is a Go module that simplifies database connections by providing a unified interface for both SQL and NoSQL databases. This module comes with flexible configuration, automatic retry mechanisms, connection pooling, migration support, logging, and metrics tracking.

Features

  • One-Line Setup: Connect with just environment variable
  • Unified Interface: Consistent interface for various database types
  • Flexible Configuration: Load configuration from environment variables, JSON, or YAML
  • Auto Retry & Reconnect: Handle transient errors with automatic retry logic
  • Connection Pooling: Efficient database connection management
  • Built-in Migration: Manage database schema changes easily
  • Integrated Logging: Automatic logging for queries and errors using Logrus
  • Metrics Tracking: Monitor connection and query performance

Installation

go get github.com/yoockh/dbyoc

Super Quick Start (Recommended)

MongoDB - One Line Setup

export MONGO_URI="mongodb://localhost:27017/mydb"
package main

import (
    "context"
    "log"
    
    "github.com/yoockh/dbyoc/config"
    "github.com/yoockh/dbyoc/db/nosql"
    "github.com/yoockh/dbyoc/logger"
    "go.mongodb.org/mongo-driver/bson"
)

func main() {
    // Load config from MONGO_URI
    cfg, err := nosql.QuickMongo()
    if err != nil {
        log.Fatal(err)
    }
    
    // Validate config
    if err := cfg.Validate(); err != nil {
        log.Fatal("Config validation error:", err)
    }
    
    // Initialize logger
    logger.Init(cfg.Logger.Level)
    
    // Connect with database and collection names
    client, err := nosql.NewMongoDBClient(cfg.MongoDB.URI, "planetsdb", "planet")
    if err != nil {
        log.Fatal("Failed to connect to db:", err)
    }
    defer client.Close()
    
    // Insert
    client.Insert(bson.M{"name": "Earth", "type": "Terrestrial"})
    
    // Find
    cursor, _ := client.Find(bson.M{})
    defer cursor.Close(context.Background())
    
    for cursor.Next(context.Background()) {
        var result bson.M
        cursor.Decode(&result)
        log.Printf("Planet: %+v", result)
    }
}

PostgreSQL - One Line Setup

export DATABASE_URL="postgres://user:pass@localhost:5432/mydb?sslmode=disable"
package main

import (
    "log"
    "github.com/yoockh/dbyoc/db/sql"
)

func main() {
    // ONE LINE!
    db, err := sql.QuickPostgres()
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    rows, _ := db.Query("SELECT id, name FROM users")
    defer rows.Close()
    
    for rows.Next() {
        var id int
        var name string
        rows.Scan(&id, &name)
        log.Printf("User: %d - %s", id, name)
    }
}

Redis - One Line Setup

export REDIS_URL="redis://:password@localhost:6379/0"
package main

import (
    "context"
    "log"
    "time"
    
    "github.com/yoockh/dbyoc/db/nosql"
)

func main() {
    // ONE LINE!
    redis, err := nosql.QuickRedis()
    if err != nil {
        log.Fatal(err)
    }
    defer redis.Close()
    
    ctx := context.Background()
    redis.Set(ctx, "key", "value", 5*time.Minute)
    
    val, _ := redis.Get(ctx, "key")
    log.Printf("Value: %s", val)
}

Environment Variables

Minimal (Recommended)

# MongoDB
export MONGO_URI="mongodb://localhost:27017/mydb"

# PostgreSQL
export DATABASE_URL="postgres://user:pass@localhost:5432/mydb"

# Redis
export REDIS_URL="redis://:password@localhost:6379/0"

Optional Settings

# MongoDB
export MONGO_DATABASE="mydb"
export MONGO_TIMEOUT=30

# PostgreSQL
export DATABASE_MAX_RETRIES=3
export DATABASE_MAX_POOL_SIZE=10

# Redis
export REDIS_PASSWORD="secret"
export REDIS_DB=0

Advanced Usage (Optional)

If you need more control, use config file:

database:
  url: "postgres://user:pass@localhost:5432/mydb"
  max_pool_size: 10

mongodb:
  uri: mongodb://localhost:27017
  database: mydb

redis:
  url: "redis://:password@localhost:6379/0"
cfg, err := config.LoadConfig()

Project Structure

dbyoc/
├── config/          # Configuration management
├── db/             
│   ├── sql/        # PostgreSQL, MySQL
│   └── nosql/      # MongoDB, Redis
├── migration/      # Database migrations
├── logger/         # Logging
├── metrics/        # Metrics tracking
└── utils/          # Utilities

External Dependencies

Library Purpose Repository
Logrus Structured logging github.com/sirupsen/logrus
Viper Configuration management github.com/spf13/viper
Mapstructure Map to struct conversion github.com/mitchellh/mapstructure
PostgreSQL Driver PostgreSQL connectivity github.com/lib/pq
MySQL Driver MySQL connectivity github.com/go-sql-driver/mysql
MongoDB Driver MongoDB connectivity go.mongodb.org/mongo-driver
Redis Client Redis connectivity github.com/go-redis/redis

Changelog

v1.3.7 (Latest)

  • Added Quick Start helpers: QuickMongo(), QuickPostgres(), QuickRedis()
  • MongoDB works with just MONGO_URI environment variable
  • PostgreSQL works with just DATABASE_URL environment variable
  • Redis works with just REDIS_URL environment variable
  • No complex configuration needed for basic usage
  • Backward compatible with existing configuration methods

v1.3.6

  • Fixed module import resolution issues
  • Improved graceful shutdown flow
  • Corrected timeout handling

Contributing

Contributions are welcome. To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Author

Aisiya Qutwatunnada


Note: DBYOC is designed to simplify database connections. Just set one environment variable and you're ready to go.

About

DBYOC is a Go module that simplifies database connections by providing a unified interface for both SQL and NoSQL databases. This module comes with flexible configuration, automatic retry mechanisms, connection pooling, migration support, logging, and metrics tracking.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages