项目作者: SbstnErhrdt

项目描述 :
generic sql connector for the grom framework
高级语言: Go
项目地址: git://github.com/SbstnErhrdt/go-gorm-all-sql.git
创建时间: 2020-09-04T15:52:43Z
项目社区:https://github.com/SbstnErhrdt/go-gorm-all-sql

开源协议:MIT License

下载


Gorm - Connect to all databases (Work in Progress)

This is a generic connector interface for the following databases:

  • MySQL
  • Postgres
  • SQLite

With this connector, it is easy to run your sql based golang software, written with the gorm.io framework, on different
databases, by only changing one environment variable.

Just set SQL_TYPE to either MYSQL, POSTGRES, SQLITE and you are good to go.

Installation

  1. go get github.com/SbstnErhrdt/go-gorm-all-sql

In a development setting you can make use of the env package to load variables from a .env file into the ENV.

  1. go get github.com/SbstnErhrdt/env

Examples

  1. package main
  2. import (
  3. "github.com/SbstnErhrdt/go-gorm-all-sql/pkg/sql"
  4. )
  5. func main() {
  6. // load env
  7. env.LoadEnvFiles() // reads the .env file in the working directory
  8. // connect to DB
  9. db, err := sql.ConnectToDatabase()
  10. }

Todo

  • Tests
  • Docker compose environment

MySQL

Env variables

  1. SQL_TYPE=MYSQL
  2. SQL_HOST=mysql.server.com
  3. SQL_PORT=3306
  4. SQL_USER=sql_user
  5. SQL_PASSWORD=xxxxxx
  6. SQL_DATABASE=test

Postgres

  1. SQL_TYPE=POSTGRES
  2. SQL_HOST=postgres.server.com
  3. SQL_PORT=5432
  4. SQL_USER=sql_user
  5. SQL_PASSWORD=xxxxxx
  6. SQL_DATABASE=test
  7. SQL_SSL= // optional

SQLite

  1. SQL_TYPE=SQLITE
  2. SQL_DATABASE=data.db

Logger

to add the logrus logger, add the following environment flag

  1. SQL_LOGGER=LOGRUS

Functionality

  • Works with different SQL dialects
  • Checks if necessary environment variables are present
  • Retries multiple times, if there is no connection available

Test

MySQL

  1. docker-compose run --rm