Skip to content

A simple library to reduce boilerplate code when connecting to a database.

License

Notifications You must be signed in to change notification settings

tsawler/persist

Repository files navigation

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() {
	// You can specify your connection string.
	// dsn := "host=localhost port=5433 user=postgres password=password dbname=foo sslmode=disable"

	// Alternatively, build a connection string like this:
	connectionData := persist.ConnectionData{
		DBType:   "pg",
		UserName: "postgres",
		Password: "password",
		Host:     "localhost",
		Database: "foo",
		SSL:      "disable",
		Port:     5432,
	}
	dsn, err := persist.BuildConnectionString(connectionData)

	// 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.")
}

About

A simple library to reduce boilerplate code when connecting to a database.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages