Categorygithub.com/loghole/database
modulepackage
0.15.0
Repository: https://github.com/loghole/database.git
Documentation: pkg.go.dev

# README

Database

GoDoc Go Report Card Coverage Status

Database is wrapper for sqlx with clear interface, retry func and hooks.
Compatible databases: PostgreSQL, Clickhouse, CockroachDB, SQLite.

Install

go get github.com/loghole/database

Usage

package main

import (
	"context"
	"log"

	"github.com/loghole/database"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	// Make connection config
	cfg := &database.Config{
		Database: ":memory:",
		Type:     database.SQLiteDatabase,
	}

	// Connect to Database with hooks
	db, err := database.New(cfg, database.WithSimplerrHook(), database.WithRetryPolicy(database.RetryPolicy{
		MaxAttempts:       database.DefaultRetryAttempts,
		InitialBackoff:    database.DefaultRetryInitialBackoff,
		MaxBackoff:        database.DefaultRetryMaxBackoff,
		BackoffMultiplier: database.DefaultRetryBackoffMultiplier,
		ErrIsRetryable:    retry,
	}))
	if err != nil {
		panic(err)
	}

	defer db.Close()

	ctx := context.Background()

	// Queries
	db.ExecContext(ctx, "CREATE TABLE t (id INTEGER PRIMARY KEY, text VARCHAR(5))")
	db.ExecContext(ctx, "INSERT into t (id, text) VALUES(?, ?)", 1, "foo")
	// Try 3 times and return error
	if _, err := db.ExecContext(ctx, "INSERT into t (id, text) VALUES(?, ?)", 1, "bar"); err != nil {
		log.Println(err)
	}
}

func retry(err error) bool {
	log.Println(err)

	return true
}

Custom hooks

You can write custom hooks with dbhook and use options database.WithCustomHook(hook)

# Packages

No description provided by the author
No description provided by the author
Package mocks is a generated GoMock package.

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
RetryPolicy defines retry policy for queries.

# Interfaces

nolint:interfacebloat // it's ok.
Option sets options such as hooks, metrics and retry parameters, etc.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author