forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_model.go
59 lines (50 loc) · 1.54 KB
/
db_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
package core
// Model defines an interface with common methods that all db models should have.
//
// Note: for simplicity composite pk are not supported.
type Model interface {
TableName() string
PK() any
LastSavedPK() any
IsNew() bool
MarkAsNew()
MarkAsNotNew()
}
// BaseModel defines a base struct that is intended to be embedded into other custom models.
type BaseModel struct {
lastSavedPK string
// Id is the primary key of the model.
// It is usually autogenerated by the parent model implementation.
Id string `db:"id" json:"id" form:"id" xml:"id"`
}
// LastSavedPK returns the last saved primary key of the model.
//
// Its value is updated to the latest PK value after MarkAsNotNew() or PostScan() calls.
func (m *BaseModel) LastSavedPK() any {
return m.lastSavedPK
}
func (m *BaseModel) PK() any {
return m.Id
}
// IsNew indicates what type of db query (insert or update)
// should be used with the model instance.
func (m *BaseModel) IsNew() bool {
return m.lastSavedPK == ""
}
// MarkAsNew clears the pk field and marks the current model as "new"
// (aka. forces m.IsNew() to be true).
func (m *BaseModel) MarkAsNew() {
m.lastSavedPK = ""
}
// MarkAsNew set the pk field to the Id value and marks the current model
// as NOT "new" (aka. forces m.IsNew() to be false).
func (m *BaseModel) MarkAsNotNew() {
m.lastSavedPK = m.Id
}
// PostScan implements the [dbx.PostScanner] interface.
//
// It is usually executed right after the model is populated with the db row values.
func (m *BaseModel) PostScan() error {
m.MarkAsNotNew()
return nil
}