Skip to content

Commit 98c089d

Browse files
authored
feat(orm): Provided module (#20)
1 parent d43a58d commit 98c089d

File tree

20 files changed

+1997
-0
lines changed

20 files changed

+1997
-0
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
- "httpclient"
2828
- "httpserver"
2929
- "log"
30+
- "orm"
3031
- "trace"
3132
steps:
3233
- name: Checkout

.github/workflows/orm-ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "orm-ci"
2+
3+
on:
4+
push:
5+
branches:
6+
- "feat**"
7+
- "fix**"
8+
- "hotfix**"
9+
- "chore**"
10+
paths:
11+
- "orm/**.go"
12+
- "orm/go.mod"
13+
- "orm/go.sum"
14+
pull_request:
15+
types:
16+
- opened
17+
- synchronize
18+
- reopened
19+
branches:
20+
- main
21+
paths:
22+
- "orm/**.go"
23+
- "orm/go.mod"
24+
- "orm/go.sum"
25+
26+
jobs:
27+
ci:
28+
uses: ./.github/workflows/common-ci.yml
29+
secrets: inherit
30+
with:
31+
module: "orm"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Yokai's documentation will be available soon.
2424
| [httpclient](httpclient) | Http client module based on [net/http](https://pkg.go.dev/net/http) |
2525
| [httpserver](httpserver) | Http server module based on [Echo](https://echo.labstack.com/) |
2626
| [log](log) | Logging module based on [Zerolog](https://github.com/rs/zerolog) |
27+
| [orm](orm) | ORM module based on [Gorm](https://gorm.io/) |
2728
| [trace](trace) | Tracing module based on [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-go) |
2829

2930
## Contributing

orm/.golangci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
run:
2+
timeout: 5m
3+
concurrency: 8
4+
5+
linters:
6+
enable:
7+
- asasalint
8+
- asciicheck
9+
- bidichk
10+
- bodyclose
11+
- containedctx
12+
- contextcheck
13+
- decorder
14+
- dogsled
15+
- durationcheck
16+
- errcheck
17+
- errchkjson
18+
- errname
19+
- errorlint
20+
- forbidigo
21+
- forcetypeassert
22+
- gocognit
23+
- goconst
24+
- gocritic
25+
- gocyclo
26+
- godot
27+
- godox
28+
- gofmt
29+
- goheader
30+
- gomoddirectives
31+
- gomodguard
32+
- goprintffuncname
33+
- gosec
34+
- gosimple
35+
- govet
36+
- grouper
37+
- importas
38+
- ineffassign
39+
- interfacebloat
40+
- logrlint
41+
- maintidx
42+
- makezero
43+
- misspell
44+
- nestif
45+
- nilerr
46+
- nilnil
47+
- nlreturn
48+
- nolintlint
49+
- nosprintfhostport
50+
- prealloc
51+
- predeclared
52+
- promlinter
53+
- reassign
54+
- staticcheck
55+
- tenv
56+
- thelper
57+
- tparallel
58+
- typecheck
59+
- unconvert
60+
- unparam
61+
- unused
62+
- usestdlibvars
63+
- whitespace

orm/README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# ORM Module
2+
3+
[![ci](https://github.com/ankorstore/yokai/actions/workflows/orm-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/orm-ci.yml)
4+
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/orm)](https://goreportcard.com/report/github.com/ankorstore/yokai/orm)
5+
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=orm)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/orm)
6+
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Form)
7+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/orm)](https://pkg.go.dev/github.com/ankorstore/yokai/orm)
8+
9+
> ORM module based on [Gorm](https://gorm.io/).
10+
11+
<!-- TOC -->
12+
* [Installation](#installation)
13+
* [Documentation](#documentation)
14+
* [Usage](#usage)
15+
* [Add-ons](#add-ons)
16+
* [Logger](#logger)
17+
* [Tracer](#tracer)
18+
* [Healthcheck](#healthcheck)
19+
<!-- TOC -->
20+
21+
## Installation
22+
23+
```shell
24+
go get github.com/ankorstore/yokai/orm
25+
```
26+
27+
## Documentation
28+
29+
### Usage
30+
31+
This module provides a [OrmFactory](factory.go), allowing to build an `gorm.DB` instance.
32+
33+
The following database drivers are [supported](https://gorm.io/docs/connecting_to_the_database.html):
34+
35+
- `SQLite`
36+
- `MySQL`
37+
- `PostgreSQL`
38+
- `SQL Server`
39+
40+
```go
41+
package main
42+
43+
import (
44+
"github.com/ankorstore/yokai/orm"
45+
)
46+
47+
// with MySQL driver
48+
var db, _ = orm.NewDefaultOrmFactory().Create(
49+
orm.WithDriver(orm.Mysql),
50+
orm.WithDsn("user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True"),
51+
)
52+
53+
// or with SQLite driver
54+
var db, _ = orm.NewDefaultOrmFactory().Create(
55+
orm.WithDriver(orm.Sqlite),
56+
orm.WithDsn("file::memory:?cache=shared"),
57+
)
58+
```
59+
60+
See [Gorm documentation](https://gorm.io/docs/) for more details.
61+
62+
### Add-ons
63+
64+
This module provides several add-ons ready to use to enrich your ORM.
65+
66+
#### Logger
67+
68+
This module provides an [CtxOrmLogger](logger.go), compatible with
69+
the [log module](https://github.com/ankorstore/yokai/tree/main/log):
70+
71+
```go
72+
package main
73+
74+
import (
75+
"github.com/ankorstore/yokai/orm"
76+
"gorm.io/gorm"
77+
"gorm.io/gorm/logger"
78+
)
79+
80+
func main() {
81+
ormLogger := orm.NewCtxOrmLogger(logger.Info, false)
82+
83+
db, _ := orm.NewDefaultOrmFactory().Create(
84+
orm.WithConfig(gorm.Config{
85+
Logger: ormLogger,
86+
}),
87+
)
88+
}
89+
```
90+
91+
If needed, you can set the parameter `withValues` to `true` to append SQL query parameter values in the log records:
92+
93+
```go
94+
ormLogger := orm.NewCtxOrmLogger(logger.Info, true)
95+
```
96+
97+
#### Tracer
98+
99+
This module provides an [OrmTracerPlugin](plugin/trace.go), compatible with
100+
the [trace module](https://github.com/ankorstore/yokai/tree/main/trace):
101+
102+
```go
103+
package main
104+
105+
import (
106+
"github.com/ankorstore/yokai/orm"
107+
"github.com/ankorstore/yokai/orm/plugin"
108+
"github.com/ankorstore/yokai/trace"
109+
)
110+
111+
func main() {
112+
tracerProvider, _ := trace.NewDefaultTracerProviderFactory().Create()
113+
114+
db, _ := orm.NewDefaultOrmFactory().Create()
115+
116+
db.Use(plugin.NewOrmTracerPlugin(tracerProvider, false))
117+
}
118+
```
119+
120+
If needed, you can set the parameter `withValues` to `true` to append SQL query parameter values in the trace spans:
121+
122+
```go
123+
db.Use(plugin.NewOrmTracerPlugin(tracerProvider, true))
124+
```
125+
126+
#### Healthcheck
127+
128+
This module provides an [OrmProbe](healthcheck/probe.go), compatible with
129+
the [healthcheck module](https://github.com/ankorstore/yokai/tree/main/healthcheck):
130+
131+
```go
132+
package main
133+
134+
import (
135+
"context"
136+
137+
hc "github.com/ankorstore/yokai/healthcheck"
138+
"github.com/ankorstore/yokai/orm"
139+
"github.com/ankorstore/yokai/orm/healthcheck"
140+
)
141+
142+
func main() {
143+
db, _ := orm.NewDefaultOrmFactory().Create()
144+
145+
checker, _ := hc.NewDefaultCheckerFactory().Create(
146+
hc.WithProbe(healthcheck.NewOrmProbe(db)),
147+
)
148+
149+
checker.Check(context.Background(), hc.Readiness)
150+
}
151+
```
152+
153+
This probe performs a `ping` to the configured database connection.

orm/enum.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package orm
2+
3+
import (
4+
"strings"
5+
6+
"gorm.io/gorm/logger"
7+
)
8+
9+
// Driver is an enum for the supported database drivers.
10+
type Driver int
11+
12+
const (
13+
Unknown Driver = iota
14+
Sqlite
15+
Mysql
16+
Postgres
17+
SqlServer
18+
)
19+
20+
// String returns a string representation of the [Driver].
21+
func (d Driver) String() string {
22+
switch d {
23+
case Sqlite:
24+
return "sqlite"
25+
case Mysql:
26+
return "mysql"
27+
case Postgres:
28+
return "postgres"
29+
case SqlServer:
30+
return "sqlserver"
31+
default:
32+
return "unknown"
33+
}
34+
}
35+
36+
// FetchDriver returns a [Driver] for a given value.
37+
func FetchDriver(driver string) Driver {
38+
switch strings.ToLower(driver) {
39+
case "sqlite":
40+
return Sqlite
41+
case "mysql":
42+
return Mysql
43+
case "postgres":
44+
return Postgres
45+
case "sqlserver":
46+
return SqlServer
47+
default:
48+
return Unknown
49+
}
50+
}
51+
52+
// FetchLogLevel returns a [logger.LogLevel] for a given value.
53+
func FetchLogLevel(level string) logger.LogLevel {
54+
switch strings.ToLower(level) {
55+
case "silent":
56+
return logger.Silent
57+
case "info":
58+
return logger.Info
59+
case "warn":
60+
return logger.Warn
61+
case "error":
62+
return logger.Error
63+
default:
64+
return logger.Silent
65+
}
66+
}

0 commit comments

Comments
 (0)