-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from tonybka/feature/tonybka/sample-custom-gorm…
…-type-uuidv1 Add base gorm model with customised ID value type
- Loading branch information
Showing
15 changed files
with
296 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
GO_CMD=go | ||
GO_TEST=$(GO_CMD) test -count=1 -v -cover | ||
|
||
|
||
.PHONY:test | ||
test: | ||
@-$(GO_TEST) ./... ||: |
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,15 @@ | ||
# Fundamental DDD elements in Golang | ||
Fundamental elements of DDD implementation in Golang microservice | ||
|
||
|
||
|
||
|
||
# Components | ||
## Domain Layer | ||
- Entity | ||
- Value Object | ||
- Domain Event | ||
|
||
## Persistence Layer | ||
- Data Model | ||
- Repository |
2 changes: 1 addition & 1 deletion
2
entity/base_aggregate_root.go → domain/entity/base_aggregate_root.go
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,2 +1,31 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= | ||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= | ||
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= | ||
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= | ||
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= | ||
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0= | ||
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= |
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,15 @@ | ||
package persistence | ||
|
||
import ( | ||
"time" | ||
|
||
customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type BaseModel struct { | ||
ID customgorm.CustomTypeUUIDv1 `gorm:"primarykey;default:(UUID_TO_BIN(UUID()));"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
DeletedAt gorm.DeletedAt `gorm:"index"` | ||
} |
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,59 @@ | ||
package tests | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type SqliteDBConnect struct { | ||
dataDir string | ||
dbConnection *gorm.DB | ||
} | ||
|
||
func NewSqliteDBConnect() (*SqliteDBConnect, error) { | ||
var tempDir = "" | ||
|
||
// Temp file setup | ||
tempDir, err := ioutil.TempDir("", "tests-") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Database setup | ||
tempDir = path.Join(tempDir, "test.sqlite3") | ||
dialector := sqlite.Open(tempDir) | ||
|
||
dbConn, err := gorm.Open(dialector, &gorm.Config{ | ||
SkipDefaultTransaction: true, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sqliteDB, err := dbConn.DB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
sqliteDB.SetMaxOpenConns(1) | ||
dbConn.Debug() | ||
|
||
return &SqliteDBConnect{dataDir: tempDir, dbConnection: dbConn}, nil | ||
} | ||
|
||
func (conn *SqliteDBConnect) Connection() *gorm.DB { | ||
return conn.dbConnection | ||
} | ||
|
||
func (conn *SqliteDBConnect) CleanUp() error { | ||
var err error | ||
|
||
if len(conn.dataDir) > 0 { | ||
err = os.RemoveAll(conn.dataDir) | ||
} | ||
|
||
return err | ||
} |
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,5 @@ | ||
package entity | ||
|
||
// Account is an user account as sample if entity | ||
type Account struct { | ||
} |
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,11 @@ | ||
package account | ||
|
||
import ( | ||
"github.com/tonybka/go-base-ddd/infrastructure/persistence" | ||
) | ||
|
||
type AccountModel struct { | ||
persistence.BaseModel | ||
|
||
AccountName string `gorm:"column:account_name;unique"` | ||
} |
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,42 @@ | ||
package account | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type AccountRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewAccountRepository(db *gorm.DB) *AccountRepository { | ||
return &AccountRepository{db} | ||
} | ||
|
||
func (repo *AccountRepository) Create(dataModel AccountModel) error { | ||
if result := repo.db.Create(&dataModel); result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
func (repo *AccountRepository) FindById(id uuid.UUID) (AccountModel, error) { | ||
var dataModel AccountModel | ||
|
||
if result := repo.db.Where("id = ?", customgorm.CustomTypeUUIDv1FromString(id.String())).First(&dataModel); result.Error != nil { | ||
return AccountModel{}, result.Error | ||
} | ||
|
||
return dataModel, nil | ||
} | ||
|
||
func (repo *AccountRepository) GetAll() ([]AccountModel, error) { | ||
var dataModels []AccountModel | ||
|
||
if result := repo.db.Find(&dataModels); result.Error != nil { | ||
return nil, result.Error | ||
} | ||
|
||
return dataModels, 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,67 @@ | ||
package account | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" | ||
"github.com/tonybka/go-base-ddd/infrastructure/persistence" | ||
"github.com/tonybka/go-base-ddd/infrastructure/tests" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type AccountRepositoryTestSuite struct { | ||
suite.Suite | ||
|
||
sqliteConnect *tests.SqliteDBConnect | ||
dbConn *gorm.DB | ||
|
||
accountRepo *AccountRepository | ||
} | ||
|
||
func (ts *AccountRepositoryTestSuite) SetupSuite() { | ||
sqliteConn, err := tests.NewSqliteDBConnect() | ||
require.NoError(ts.T(), err) | ||
|
||
ts.sqliteConnect = sqliteConn | ||
ts.dbConn = sqliteConn.Connection() | ||
|
||
ts.dbConn.AutoMigrate(&AccountModel{}) | ||
|
||
ts.accountRepo = NewAccountRepository(ts.dbConn) | ||
} | ||
|
||
func (ts *AccountRepositoryTestSuite) TestCreateAccount() { | ||
entityId := customgorm.CustomTypeUUIDv1FromString(uuid.New().String()) | ||
|
||
account := AccountModel{ | ||
BaseModel: persistence.BaseModel{ | ||
ID: entityId, | ||
}, | ||
AccountName: "abc", | ||
} | ||
|
||
err := ts.accountRepo.Create(account) | ||
ts.NoError(err) | ||
|
||
all, err := ts.accountRepo.GetAll() | ||
ts.NoError(err) | ||
ts.Greater(len(all), 0) | ||
|
||
queriedAccount, err := ts.accountRepo.FindById(uuid.UUID(account.ID)) | ||
ts.NoError(err) | ||
ts.Equal(account.AccountName, queriedAccount.AccountName) | ||
ts.Equal(account.ID, queriedAccount.ID) | ||
} | ||
|
||
func (ts *AccountRepositoryTestSuite) TearDownSuite() { | ||
err := ts.sqliteConnect.CleanUp() | ||
ts.NoError(err) | ||
} | ||
|
||
func TestSuiteRunnerAccountRepository(t *testing.T) { | ||
ts := new(AccountRepositoryTestSuite) | ||
suite.Run(t, ts) | ||
} |