/
models.go
78 lines (67 loc) · 1.55 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
package db
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
)
type JSON json.RawMessage
// Scan scan value into Jsonb, implements sql.Scanner interface
func (j *JSON) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
result := json.RawMessage{}
err := json.Unmarshal(bytes, &result)
*j = JSON(result)
return err
}
// Value return json value, implement driver.Valuer interface
func (j JSON) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return json.RawMessage(j).MarshalJSON()
}
// Model Definitions
type User struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
IsActive bool `gorm:"default:true"`
Name string `gorm:"not null"`
Email string `gorm:"not null;unique"`
}
type Conversation struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Name string
UserID uint
User User
}
type Query struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Query string `gorm:"not null"`
Context int32 `gorm:"default:0"`
ConversationID uint
Response JSON `gorm:"type:jsonb;default:'{}'"`
Conversation Conversation
}
type Template struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Name string `gorm:"not null"`
Properties JSON `gorm:"type:jsonb;default:'{}'"`
UserID uint
User User
}
type TemplateProperties struct {
Parts []string `json:"parts"`
Params []string `json:"params"`
}