-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from dongxuny/master
Add database/mysql as supported entry
- Loading branch information
Showing
11 changed files
with
2,213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2021 rookie-ninja | ||
// | ||
// Use of this source code is governed by an Apache-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package rkbootmysql | ||
|
||
import ( | ||
_ "github.com/rookie-ninja/rk-boot" | ||
"github.com/rookie-ninja/rk-db/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// GetMySqlEntry return rkmysql.MySqlEntry with name. | ||
// | ||
// The entryName was specified in boot.yaml file as bellow. | ||
// | ||
// user is the name of entry as function parameter. | ||
// | ||
// mySql: | ||
// - name: user # Required | ||
// enabled: true # Required | ||
// locale: "*::*::*::*" # Required | ||
// addr: "localhost:3306" # Optional, default: localhost:3306 | ||
func GetMySqlEntry(entryName string) *rkmysql.MySqlEntry { | ||
return rkmysql.GetMySqlEntry(entryName) | ||
} | ||
|
||
// GetGormDb return gorm.DB instance with entryName and database name. | ||
// | ||
// rkmysql will init gorm.DB by reading boot.yaml file and validate the connectivity for us. | ||
// | ||
// The entryName and dataBaseName was specified in boot.yaml file as bellow. | ||
// | ||
// entryName: user | ||
// dataBaseName: user-meta | ||
// | ||
// mySql: | ||
// - name: user # Required | ||
// enabled: true # Required | ||
// locale: "*::*::*::*" # Required | ||
// addr: "localhost:3306" # Optional, default: localhost:3306 | ||
// database: | ||
// - name: user-meta # Required | ||
// autoCreate: true # Optional, default: false | ||
func GetGormDb(entryName, dataBaseName string) *gorm.DB { | ||
if entry := GetMySqlEntry(entryName); entry != nil { | ||
return entry.GetDB(dataBaseName) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/rookie-ninja/rk-boot/database/mysql | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/rookie-ninja/rk-boot v1.4.1 | ||
github.com/rookie-ninja/rk-db/mysql v0.0.3 | ||
gorm.io/gorm v1.22.4 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.PHONY: all | ||
all: gomod doctoc fmt | ||
|
||
.PHONY: fmt | ||
fmt: | ||
@echo "[fmt] Formatting go project..." | ||
@gofmt -s -w . 2>&1 | ||
@echo "------------------------------------[Done]" | ||
|
||
.PHONY: doctoc | ||
doctoc: | ||
@echo "[doctoc] Running doctoc..." | ||
@doctoc . 2>&1 | ||
@echo "------------------------------------[Done]" | ||
|
||
.PHONY: gomod | ||
gomod: | ||
@echo "[gomod] Running go mod tidy..." | ||
@go mod tidy | ||
@echo "------------------------------------[Done]" |
Oops, something went wrong.