database

package
v0.0.44 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 3 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

Implementing Your Own Connector

A new connector backing the DatabaseConnector interface only needs to expose a Module(scope string) fx.Option factory that wires its constructor through database.Module:

func Module(scope string) fx.Option {
    return database.Module(scope, func(p Params) database.DatabaseConnector {
        c := &MyConnector{ /* ... */ }
        p.Lifecycle.Append(fx.Hook{OnStart: c.onStart, OnStop: c.onStop})
        return c
    })
}

database.Module is a thin wrapper around weedbox/fxmodule.InterfaceModule[DatabaseConnector] that handles the shared multi-load wiring: the connector is always registered as name:"<scope>", and the first connector loaded into the process also exposes itself as the unnamed default so existing single-load consumers that inject DatabaseConnector without a tag keep working.

Tests that build multiple fx.Apps in the same process must call fxmodule.ResetClaim[database.DatabaseConnector]() between apps.

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

func Module added in v0.0.44

func Module(scope string, ctor any) fx.Option

Module wires a DatabaseConnector constructor into fx with the standard connector pattern: always register as `name:"<scope>"`, and let the first caller in the process claim the unnamed default slot. Existing single-load consumers that inject DatabaseConnector without a tag continue to work with zero changes.

Intended for use from a connector package's Module(scope) factory — the application composition root still writes `<pkg>.Module("<scope>")`.

In tests that construct multiple fx.Apps in the same process, call fxmodule.ResetClaim[DatabaseConnector]() between apps.

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