/databases/connection.go
https://github.com/bigfile/bigfile · Go · 60 lines · 40 code · 10 blank · 10 comment · 10 complexity · 7015d35ffc6e2a4633d7beb4dbcc7cb2 MD5 · raw file
- // Copyright 2019 The bigfile Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
- // Package databases provides capacity to interact with database
- package databases
- import (
- "time"
- "github.com/bigfile/bigfile/config"
- "github.com/jinzhu/gorm"
- // import mysql database driver
- _ "github.com/jinzhu/gorm/dialects/mysql"
- // import sqlite database driver
- _ "github.com/jinzhu/gorm/dialects/sqlite"
- )
- var connection *gorm.DB
- // NewConnection will initialize a connection to database. But, if connection
- // has already existed, it will be used.
- func NewConnection(dbConfig *config.Database) (*gorm.DB, error) {
- var (
- err error
- dsn string
- )
- if dbConfig == nil {
- dbConfig = &config.DefaultConfig.Database
- }
- if connection == nil {
- if dsn, err = dbConfig.DSN(); err != nil {
- return nil, err
- }
- if connection, err = gorm.Open(dbConfig.Driver, dsn); err != nil {
- return nil, err
- }
- connection.DB().SetConnMaxLifetime(time.Hour)
- connection.DB().SetMaxOpenConns(500)
- connection.DB().SetMaxIdleConns(0)
- }
- return connection, err
- }
- // MustNewConnection just call NewConnection, but, when something goes wrong, it will
- // raise a panic
- func MustNewConnection(dbConfig *config.Database) *gorm.DB {
- var (
- conn *gorm.DB
- err error
- )
- if conn, err = NewConnection(dbConfig); err != nil {
- panic(err)
- }
- return conn
- }