-
Notifications
You must be signed in to change notification settings - Fork 2
/
datastore.go
226 lines (181 loc) · 4.75 KB
/
datastore.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package schemaless
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/satori/go.uuid"
)
// Relation ...
type Relation struct {
On string
Collection string
LocalKey string
RemoteKey string
}
// Datastore ...
type Datastore struct {
name string
db *sqlx.DB
}
// NewDatastore ...
func NewDatastore(name string, conn *sql.DB) (*Datastore, error) {
db := sqlx.NewDb(conn, "postgres")
if err := db.Ping(); err != nil {
return nil, err
}
s := new(Datastore)
s.name = name
s.db = db
if err := s.boot(); err != nil {
return nil, err
}
return s, nil
}
// Create - insert a new document
func (s *Datastore) Create(doc *Document) error {
id, err := uuid.NewV4()
if err != nil {
return err
}
now := time.Now().UnixNano()
sql := `
INSERT INTO ` + (s.name) + `(uuid, collection, data, created_at, updated_at, deleted_at)
VALUES($1, $2, $3, $4, $5, $6)
`
doc.UUID = id.String()
doc.CreatedAt = now
doc.UpdatedAt = now
if _, err := s.db.Exec(sql, id, doc.Collection, doc.Data, doc.CreatedAt, doc.UpdatedAt, 0); err != nil {
return err
}
return nil
}
// Update a document using its uuid, also you can merge/override its content
func (s *Datastore) Update(uuid string, data SQLObject, replace bool) (*Document, error) {
now := time.Now().UnixNano()
dataSQL := `data || $1`
if replace {
dataSQL = `$1`
}
if _, err := s.db.Exec(`UPDATE `+(s.name)+` SET data = `+(dataSQL)+`, updated_at = $2 WHERE uuid = $3`, data.JSON(), now, uuid); err != nil {
return nil, err
}
return s.Get(uuid)
}
// Get - fetches the document at the specified uuid
func (s *Datastore) Get(uuid string) (*Document, error) {
var doc Document
err := s.db.QueryRowx(`SELECT * FROM `+(s.name)+` WHERE uuid = $1`, uuid).StructScan(&doc)
if err != nil {
return nil, err
}
return &doc, err
}
// Filter - fetch all documents using the specified options
func (s *Datastore) Filter(opts *FilterOpts) (*Result, error) {
result := &Result{}
result.Hits = []*Document{}
if opts == nil {
opts = &FilterOpts{}
}
opts.Where = strings.TrimSpace(opts.Where)
if opts.Where != "" {
opts.Where = (opts.Where) + ` `
}
if opts.Limit < 1 {
opts.Limit = 10
}
rows, err := s.db.NamedQuery(`SELECT count(uuid) as totals FROM `+(s.name)+` `+opts.Where, opts.Args)
if err != nil {
return nil, err
}
defer rows.Close()
rows.Next()
rows.Scan(&result.Total)
sorter := ""
if len(opts.Order) > 0 {
sorter = " ORDER BY "
sorts := []string{}
for k, v := range opts.Order {
if !DocumentReservedKeys[k] {
k = "data->>'" + (k) + "'"
}
sorts = append(sorts, fmt.Sprintf("%s %s", k, strings.ToUpper(v)))
}
sorter += strings.Join(sorts, ", ")
}
sql := `SELECT * FROM ` + (s.name) + ` ` + opts.Where + sorter
if opts.Paginate {
sql += ` OFFSET ` + strconv.FormatInt(opts.Offset, 10) + ` LIMIT ` + strconv.FormatInt(opts.Limit, 10)
}
rows, err = s.db.NamedQuery(sql, opts.Args)
if err != nil {
return nil, err
}
for rows.Next() {
var doc Document
if err := rows.StructScan(&doc); err != nil {
return nil, err
}
result.Hits = append(result.Hits, &doc)
}
if opts.Paginate {
s.pagerify(opts, result)
}
return result, nil
}
// DB - returns the underlying sqlx.db
func (s *Datastore) DB() *sqlx.DB {
return s.db
}
// Name - returns the datastore name
func (s *Datastore) Name() string {
return s.name
}
// pagerify add the pagination info to the result
func (s *Datastore) pagerify(o *FilterOpts, r *Result) {
if o.Limit < 1 {
o.Limit = 10
}
pages := (r.Total / uint64(o.Limit)) + 1
currentpage := (o.Offset / o.Limit) + 1
next := currentpage + 1
prev := currentpage - 1
if uint64(next) > pages {
next = -1
}
if prev < 1 {
prev = -1
}
if r.Total < 1 {
pages = 0
}
r.Pager = Pager{
Pages: pages,
Next: next,
Prev: prev,
Current: currentpage,
}
}
// boot create the missing tables/indexes
func (s *Datastore) boot() error {
_, err := s.db.Exec(`
CREATE TABLE IF NOT EXISTS ` + (s.name) + ` (
uuid varchar not null primary key,
collection varchar DEFAULT 'default',
data jsonb DEFAULT null,
created_at bigint DEFAULT '0',
updated_at bigint DEFAULT '0',
deleted_at bigint DEFAULT '0'
);
CREATE INDEX IF NOT EXISTS ` + (s.name) + `_index_collection on ` + (s.name) + ` (collection);
CREATE INDEX IF NOT EXISTS ` + (s.name) + `_index_collection_updated_at on ` + (s.name) + ` (collection, updated_at);
CREATE INDEX IF NOT EXISTS ` + (s.name) + `_index_collection_created_at on ` + (s.name) + ` (collection, created_at);
CREATE INDEX IF NOT EXISTS ` + (s.name) + `_index_collection_deletd_at on ` + (s.name) + ` (collection, deleted_at);
CREATE INDEX IF NOT EXISTS ` + (s.name) + `_index_gin_data on ` + (s.name) + ` using gin(data);
`)
return err
}