Skip to content

Commit

Permalink
docs: add using View instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh committed May 8, 2024
1 parent bee8793 commit 28183e9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ env "gorm" {
}
```

### Supported Features
#### View
To create a view, you can define the `ViewDef` method on a struct:

```go
type User struct {
gorm.Model
Name string
Age int
}

type WorkingAgedUsers struct {}

func (WorkingAgedUsers) ViewDef(db *gorm.DB) gorm.ViewOption {
return gorm.ViewOption{
Query: db.Table("users").Select("name", "age").Where("age > ?", 18),
}
}
```
Then using `WithViews` option to load the view:

```go
stmts, err := gormschema.New("mysql",
gormschema.WithViews(&models.WorkingAgedUsers{}),
).Load(&models.User{})
```
Name of the view will be the struct name with snake_case format. For example, the view name for `WorkingAgedUsers` struct will be `working_aged_users`.
### Additional Configuration

To supply custom `gorm.Config{}` object to the provider use the [Go Program Mode](#as-go-file) with
Expand Down

0 comments on commit 28183e9

Please sign in to comment.