https://github.com/randree/gormseeder
A simple go database seeder powered by GROM
https://github.com/randree/gormseeder
Last synced: 5 months ago
JSON representation
A simple go database seeder powered by GROM
- Host: GitHub
- URL: https://github.com/randree/gormseeder
- Owner: randree
- License: mit
- Created: 2021-09-02T12:32:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-02T12:48:47.000Z (almost 5 years ago)
- Last Synced: 2023-07-27T22:33:33.102Z (almost 3 years ago)
- Language: Go
- Size: 27.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: history.go
- License: LICENSE
Awesome Lists containing this project
README
# GORMSeeder - A simple database seeder based on GORM
The GORMSeeder is a lightweight but powerful and flexible seeder tool based on GORM. Especially useful in container environments like Docker.
The goal is to create a build of your seeder setup.
## Steps for deployment
* Create a go build
* Putting build in a `FROM scratch AS bin` container for minimal size
* Deploy
* Start service or container with environment variables to perform a seed on database
## Example
See example under `example/` to see how it works.
The folder looks like
```bash
├── Seeder
│ ├── main.go
│ ├── seed00001_create-mock-user.go
│ ├── seed00002_create-locales.go
│ └── seed00003_create-products.go
...
```
`seed`-files can have any names.
Example for `main.go`:
```golang
package main
import (
"fmt"
gs "github.com/randree/gormseeder"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
db, err := gorm.Open(postgres.Open("host=localhost user=user password=passpass dbname=testdb port=5432 sslmode=disable"))
if err != nil {
fmt.Println(err.Error())
}
gs.InitSeeder(db, "seeders")
}
```
`seed`-files (e.g. `seed001_mock-user-list.go`) looks like:
```golang
package main
import (
gs "github.com/randree/gormseeder"
"gorm.io/gorm"
)
func init() {
gs.Seed(gs.State{
Tag: "",
Perform: func(db *gorm.DB) error {
...
return err
},
})
}
```
## Create and use module
Steps to create a go module:
```console
$ go mod init Seeder
```
To load dependencies:
```console
$ go mod tidy
```
To run a Seeder:
```console
$ TAG= (go run ./... | )
```
With `TAG=all` you can perform all seeds in one go.
To show a Seeder history:
```console
$ HISTORY=1 (go run ./... | )
| DATETIME HISTORY | FILENAME | USER |
| -------------------------------------- | ------------------------- | ---------- |
| 2021-08-31 13:09:47.932619 +0200 CEST | seed003_mock-user-list.go | admin |
| 2021-08-31 13:09:47.908876 +0200 CEST | seed002_mock-products.go | foo |
| 2021-08-31 13:09:47.871357 +0200 CEST | seed001_customers.go | bar |
```
To show version:
```console
$ VERSION=1 (go run ./... | )
Gormseeder version: 0.1.0
```
### Calls
```console
$ TAG= [HISTORY=1] [VERSION=1] (Docker Container | go build | go run ./...)
```
Docker-compose file
```yaml
...
migrator:
image: from_scratch_image
environment:
TAG:
...
```
If you want to seed everything use `TAG=all`.
## References
- [GROM](https://gorm.io/) The GORM project.