Giter VIP home page Giter VIP logo

go-sql's Introduction

go-sql

Getting Started:

Installing

go get -u github.com/joematpal/go-sql@latest

Supported CLI flags

The cli flags are found in the flags/ folder. TLS is not supported yet.

Building

Build flags are required; mysql,postgres

go build -tag=postgres main.go

Example

package main

import (
    sqlp "github.com/joematpal/go-sql"
    sqlf "github.com/joematpal/go-sql/flags"
)

type DB interface{
    DBX() (*sqlx.DB, error)
}

func NewDBFromContext(c *cli.Context) (DB, error)
	db, err := sqlp.New(
        // This is the Anant way... Very very bad way.
        // sql.DB{
        //     Host:        c.String(sqlf.DBHost),
        //     DBname:      c.String(sqlf.DBName),
        //     User:        c.String(sqlf.DBUser),
        //     Password:    c.String(sqlf.DBPass),
        //     DriverName:  c.String(sqlf.DBType),
        //     Port:        c.String(sqlf.DBPort),
        //     Migrate:     c.Bool(sqlf.Migrate),
        //     MigratePath: c.String(sqlf.MigratePath),
        // }
        sqlp.WithHost(c.String(sqlf.DBHost)),
        sqlp.WithDBName(c.String(sqlf.DBName)),
        sqlp.WithUser(c.String(sqlf.DBUser)),
        sqlp.WithPassword(c.String(sqlf.DBPass)),
        // These one DB have defaults, and are not needed it you want to use the defaults
        // sqlp.WithType(c.String(sqlf.DBType)),
        // sqlp.WithPort(c.String(sqlf.DBPort)),
        // sqlp.WithMigrate(c.Bool(sqlf.Migrate)),
        // sqlp.WithMigratePath(c.String(sqlf.MigratePath)),
        }
    )
	if err != nil {
		return nil, fmt.Errorf("new db: %v", err)
	}
    return db, nil

Notes:


Migration

When a DBX() interface is called it will pull from a list of connections. This list of connections is create for testing purposes. It helps speed up testing by not create unnecessary db connections. So when using it from a testing perspective don't ever send "different" migrations paths because the subsequent New() calls will return an already existing connection.

docker-compose -f ./sidecars/docker-compose.yaml up postgres mysql cassandra

go-sql's People

Contributors

joematpal avatar ben-r-teng avatar

Stargazers

Eric Raio avatar Roman avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.