forked from goadesign/gorma
/
relationalstore.go
78 lines (72 loc) · 2.45 KB
/
relationalstore.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
package dsl
import (
"github.com/goadesign/goa/dslengine"
"github.com/kaizenplatform/gorma"
)
// Store represents a database. Gorma lets you specify
// a database type, but it's currently not used for any generation
// logic.
func Store(name string, storeType gorma.RelationalStorageType, dsl func()) {
if name == "" || len(name) == 0 {
dslengine.ReportError("Relational Store requires a name.")
return
}
if len(storeType) == 0 {
dslengine.ReportError("Relational Store requires a RelationalStoreType.")
return
}
if dsl == nil {
dslengine.ReportError("Relational Store requires a dsl.")
return
}
if s, ok := storageGroupDefinition(true); ok {
if s.RelationalStores == nil {
s.RelationalStores = make(map[string]*gorma.RelationalStoreDefinition)
}
store, ok := s.RelationalStores[name]
if !ok {
store = &gorma.RelationalStoreDefinition{
Name: name,
DefinitionDSL: dsl,
Parent: s,
Type: storeType,
RelationalModels: make(map[string]*gorma.RelationalModelDefinition),
}
} else {
dslengine.ReportError("Relational Store %s can only be declared once.", name)
}
s.RelationalStores[name] = store
}
}
// NoAutomaticIDFields applies to a `Store` or `Model` type. It allows you
// to turn off the default behavior that will automatically create
// an ID/int Primary Key for each model.
func NoAutomaticIDFields() {
if s, ok := relationalStoreDefinition(false); ok {
s.NoAutoIDFields = true
} else if m, ok := relationalModelDefinition(false); ok {
delete(m.RelationalFields, "ID")
}
}
// NoAutomaticTimestamps applies to a `Store` or `Model` type. It allows you
// to turn off the default behavior that will automatically create
// an `CreatedAt` and `UpdatedAt` fields for each model.
func NoAutomaticTimestamps() {
if s, ok := relationalStoreDefinition(false); ok {
s.NoAutoTimestamps = true
} else if m, ok := relationalModelDefinition(false); ok {
delete(m.RelationalFields, "CreatedAt")
delete(m.RelationalFields, "UpdatedAt")
}
}
// NoAutomaticSoftDelete applies to a `Store` or `Model` type. It allows
// you to turn off the default behavior that will automatically
// create a `DeletedAt` field (*time.Time) that acts as a
// soft-delete filter for your models.
func NoAutomaticSoftDelete() {
if s, ok := relationalStoreDefinition(false); ok {
s.NoAutoSoftDelete = true
} else if m, ok := relationalModelDefinition(false); ok {
delete(m.RelationalFields, "DeletedAt")
}
}