database

package
v0.0.40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 1 Imported by: 4

README

Database Module

A database abstraction interface for GORM, providing a common interface for different database connectors.

Features

  • Common interface for database operations
  • GORM integration
  • Swappable database backends (PostgreSQL, SQLite, etc.)

Installation

go get github.com/weedbox/common-modules/database

Interface

type DatabaseConnector interface {
    GetDB() *gorm.DB
}

Available Implementations

Usage

Injecting Database Connector
package repository

import (
    "github.com/weedbox/common-modules/database"
    "go.uber.org/fx"
)

type Params struct {
    fx.In

    Database database.DatabaseConnector
}

func (r *Repository) GetUsers() ([]User, error) {
    var users []User
    err := r.params.Database.GetDB().Find(&users).Error
    return users, err
}
Switching Database Backends

The DatabaseConnector interface allows you to switch between different database implementations without changing your application code:

package main

import (
    "github.com/weedbox/common-modules/postgres_connector"
    // or
    // "github.com/weedbox/common-modules/sqlite_connector"
    "go.uber.org/fx"
)

func main() {
    fx.New(
        // Use PostgreSQL
        postgres_connector.Module("database"),

        // Or use SQLite
        // sqlite_connector.Module("database"),

        // Your application modules that depend on database.DatabaseConnector
        // will work with either implementation
    ).Run()
}

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseConnector

type DatabaseConnector interface {
	GetDB() *gorm.DB
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL