This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
example_model.go
67 lines (53 loc) · 1.92 KB
/
example_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-datastore"
)
// Example is an example model
type Example struct {
bux.Model `bson:",inline"` // Base bux model
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the unique record id" bson:"_id"` // Unique identifier
ExampleField string `json:"example_field" toml:"example_field" yaml:"example_field" gorm:"<-:create;type:varchar(64);comment:This is an example string field" bson:"example_field"` // Example string field
}
// ModelExample is an example model
const ModelExample = "example"
const tableExamples = "examples"
// NewExample create new example model
func NewExample(exampleString string, opts ...bux.ModelOps) *Example {
id, _ := utils.RandomHex(32)
// Standardize and sanitize!
return &Example{
Model: *bux.NewBaseModel(ModelExample, opts...),
ExampleField: exampleString,
ID: id,
}
}
// GetModelName returns the model name
func (e *Example) GetModelName() string {
return ModelExample
}
// GetModelTableName returns the model db table name
func (e *Example) GetModelTableName() string {
return tableExamples
}
// Save the model
func (e *Example) Save(ctx context.Context) (err error) {
return bux.Save(ctx, e)
}
// GetID will get the ID
func (e *Example) GetID() string {
return e.ID
}
// BeforeCreating is called before the model is saved to the DB
func (e *Example) BeforeCreating(_ context.Context) (err error) {
e.DebugLog("starting: " + e.Name() + " BeforeCreating hook...")
// Do something here!
e.DebugLog("end: " + e.Name() + " BeforeCreating hook")
return
}
// Migrate model specific migration
func (e *Example) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableExamples), bux.ModelMetadata.String())
}