-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
60 lines (49 loc) · 1.48 KB
/
store.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
package postgres
import (
"context"
"database/sql"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/messaging"
)
// todo: This doesn't support TTL expiries, which is fine for now. We can
// manually delete old entries while in an invite-only testing phase.
type store struct {
db *sqlx.DB
}
// New returns a postgres backed messaging.Store.
func New(db *sql.DB) messaging.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Insert implements messaging.Store.Insert.
func (s *store) Insert(ctx context.Context, record *messaging.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
return model.dbSave(ctx, s.db)
}
// Delete implements messaging.Store.Delete.
func (s *store) Delete(ctx context.Context, account string, messageID uuid.UUID) error {
return dbDelete(ctx, s.db, account, messageID.String())
}
// Get implements messaging.Store.Get.
func (s *store) Get(ctx context.Context, account string) ([]*messaging.Record, error) {
models, err := dbGetAllForAccount(ctx, s.db, account)
if err != nil {
return nil, err
}
records := make([]*messaging.Record, len(models))
for i, model := range models {
record, err := fromModel(model)
if err != nil {
// todo(safety): this is the equivalent QoS brick case, although should be less problematic.
// we could have a valve to ignore, and also to delete
return nil, err
}
records[i] = record
}
return records, nil
}