persist

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Built with GoLang License Go Report Card

Persist

Persist is a simple package that reduces boilerplate code when getting a pool of connections for a database.

This project does nothing particularly spectacular; it exists simply to save me writing the same 100 lines of code every time I need to connect to a database.

Currently, this project supports Postgres, MariaDB/MySQL, and sqlite.

Example usage

package main

import (
	"github.com/tsawler/persist"
	"log"
)

func main() {
	// Specify your connection string.
	dsn := "host=localhost port=5433 user=postgres password=password dbname=foo sslmode=disable"

	// Get a pool of connections. The first parameter can be "postgres", "mariadb", "mysql", or "sqlite".
	conn, err := persist.New("postgres", dsn, nil)
	if err != nil {
		log.Panic(err)
	}
	defer conn.Close()

	log.Println("Connected to db successfully.")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxOpenConns    = 12              // Maximum number of open connections in our pool.
	MaxIdleConns    = 6               // Maximum idle connections in our pool.
	ConnMaxLifetime = 0 * time.Second // Max lifetime for a connection (how long before it expires). 0 is forever.
)

Functions

func BuildConnectionString added in v1.1.0

func BuildConnectionString(cd ConnectionData) (string, error)

BuildConnectionString takes information stored in a ConnectionData variable and builds a connection string for postgres/mysql.

func New

func New(db, dsn string, ops *Options) (*sql.DB, error)

New is a factory method which takes a db type, a DSN and options and attempts to open a connection to the database and return a pool of connections.

func NewMariaDB

func NewMariaDB(dsn string, ops *Options) (*sql.DB, error)

NewMariaDB is a convenience function for getting a pool of MariaDB/MySQL connections.

func NewPostgres

func NewPostgres(dsn string, ops *Options) (*sql.DB, error)

NewPostgres is a convenience function for getting a pool of Postgres connections.

func NewSQLite

func NewSQLite(dsn string, ops *Options) (*sql.DB, error)

NewSQLite is a convenience function for getting a pool of sqlite connections.

Types

type ConnectionData added in v1.1.1

type ConnectionData struct {
	DBType   string
	UserName string
	Password string
	Host     string
	Database string
	SSL      string
	Port     int
}

ConnectionData is the type used to hold information required to build a connection string.

type Options

type Options struct {
	MaxOpen     int
	MaxIdle     int
	MaxLifetime time.Duration
}

Options holds useful options for a pool of connections.

Jump to

Keyboard shortcuts

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