|
| 1 | +# ORM Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/orm-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/orm) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/orm) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Form) |
| 7 | +[](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. |
0 commit comments