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
/
models.go
105 lines (90 loc) · 3.39 KB
/
models.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package bux
import (
"context"
"reflect"
"time"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/utils"
)
var defaultPageSize = 25
// Model is the generic model field(s) and interface(s)
//
// gorm: https://gorm.io/docs/models.html
type Model struct {
// ModelInterface `json:"-" toml:"-" yaml:"-" gorm:"-"` (@mrz: not needed, all models implement all methods)
// ID string `json:"id" toml:"id" yaml:"id" gorm:"primaryKey"` (@mrz: custom per table)
CreatedAt time.Time `json:"created_at" toml:"created_at" yaml:"created_at" gorm:"comment:The time that the record was originally created" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" toml:"updated_at" yaml:"updated_at" gorm:"comment:The time that the record was last updated" bson:"updated_at,omitempty"`
Metadata Metadata `gorm:"type:json;comment:The JSON metadata for the record" json:"metadata,omitempty" bson:"metadata,omitempty"`
// https://gorm.io/docs/indexes.html
// DeletedAt gorm.DeletedAt `json:"deleted_at" toml:"deleted_at" yaml:"deleted_at" (@mrz: this was the original type)
DeletedAt utils.NullTime `json:"deleted_at" toml:"deleted_at" yaml:"deleted_at" gorm:"index;comment:The time the record was marked as deleted" bson:"deleted_at,omitempty"`
// Private fields
client ClientInterface // Interface of the parent Client that loaded this bux model
encryptionKey string // Use for sensitive values that required encryption (IE: paymail public xpub)
name ModelName // Name of model (table name)
newRecord bool // Determine if the record is new (create vs update)
pageSize int // Number of items per page to get if being used in for method getModels
rawXpubKey string // Used on "CREATE" on some models
}
// ModelInterface is the interface that all models share
type ModelInterface interface {
AfterCreated(ctx context.Context) (err error)
AfterDeleted(ctx context.Context) (err error)
AfterUpdated(ctx context.Context) (err error)
BeforeCreating(ctx context.Context) (err error)
BeforeUpdating(ctx context.Context) (err error)
ChildModels() []ModelInterface
Client() ClientInterface
DebugLog(text string)
Display() interface{}
GetID() string
GetModelName() string
GetModelTableName() string
GetOptions(isNewRecord bool) (opts []ModelOps)
IsNew() bool
Migrate(client datastore.ClientInterface) error
Name() string
New()
NotNew()
RawXpub() string
RegisterTasks() error
Save(ctx context.Context) (err error)
SetOptions(opts ...ModelOps)
SetRecordTime(bool)
UpdateMetadata(metadata Metadata)
}
// ModelName is the model name type
type ModelName string
// NewBaseModel create an empty base model
func NewBaseModel(name ModelName, opts ...ModelOps) (m *Model) {
m = &Model{name: name}
m.SetOptions(opts...)
return
}
// DisplayModels process the (slice) of model(s) for display
func DisplayModels(models interface{}) interface{} {
if models == nil {
return nil
}
s := reflect.ValueOf(models)
if s.IsNil() {
return nil
}
if s.Kind() == reflect.Slice {
for i := 0; i < s.Len(); i++ {
s.Index(i).MethodByName("Display").Call([]reflect.Value{})
}
} else {
s.MethodByName("Display").Call([]reflect.Value{})
}
return models
}
// String is the string version of the name
func (n ModelName) String() string {
return string(n)
}
// IsEmpty tests if the model name is empty
func (n ModelName) IsEmpty() bool {
return n == ModelNameEmpty
}