/
rel_test.go
143 lines (117 loc) · 2.58 KB
/
rel_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package rel
import (
"encoding/json"
"time"
)
type Status string
type extendedUser struct {
ID int
Password []byte
Metadata json.RawMessage
CreatedAt time.Time
UpdatedAt time.Time
}
type User struct {
ID int
Name string
Age int
Transactions []Transaction `ref:"id" fk:"user_id"`
Address Address `autosave:"true"`
WorkAddress *Address
UserRoles []UserRole `autosave:"true"`
Emails []Email `autosave:"true"`
// many to many
// user:id <- user_id:user_roles:role_id -> role:id
Roles []Role `through:"user_roles"`
// self-referencing needs two intermediate reference to be set up.
Follows []Follow `ref:"id" fk:"following_id"`
Followeds []Follow `ref:"id" fk:"follower_id"`
// association through
Followings []User `through:"follows"`
Followers []User `through:"followeds"`
CreatedAt time.Time
UpdatedAt time.Time
}
type Follow struct {
FollowerID int `db:",primary"`
FollowingID int `db:",primary"`
Accepted bool // this way, it may contains additional data
}
type Email struct {
ID int
Email string
UserID int
}
type Transaction struct {
ID int
Item string
Status Status
BuyerID int `db:"user_id"`
Buyer User `ref:"user_id" fk:"id" autoload:"true"`
AddressID int
Address Address `ref:"address_id" fk:"id"`
Histories *[]History `ref:"id" fk:"transaction_id"`
}
type VersionedTransaction struct {
Transaction
LockVersion int
}
type SoftDelVersionedTransaction struct {
Transaction
LockVersion int
Deleted bool
}
func (t VersionedTransaction) Table() string {
return "transactions"
}
func (t SoftDelVersionedTransaction) Table() string {
return "transactions"
}
type History struct {
ID int
TransactionID int
}
type Notes string
func (n Notes) Equal(other any) bool {
if o, ok := other.(Notes); ok {
return n == o
}
return false
}
type Address struct {
ID int
UserID *int
User *User
Street string
Notes Notes
DeletedAt *time.Time
}
func (a Address) Table() string {
return "user_addresses"
}
type Profile struct {
ID int
Name string
User *User `autosave:"true"`
UserID *int
}
type Role struct {
ID int
Name string
UserRoles []UserRole
// explicit many to many declaration:
// role:id <- role_id:user_roles:user_id -> user:id
Users []User `through:"user_roles"`
}
type UserRole struct {
UserID int `db:",primary"`
RoleID int `db:",primary"`
}
type UserRepository struct {
ID int
Name string
User *User `autosave:"true"`
UserID *int
UpdatedAt *time.Time
Deleted bool
}