-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
171 lines (145 loc) · 4.88 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
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
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0
package store
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/claceio/clace/internal/app/apptype"
"github.com/claceio/clace/internal/app/starlark_type"
"go.starlark.net/starlark"
)
const (
ID_FIELD = "_id"
VERSION_FIELD = "_version"
CREATED_BY_FIELD = "_created_by"
UPDATED_BY_FIELD = "_updated_by"
CREATED_AT_FIELD = "_created_at"
UPDATED_AT_FIELD = "_updated_at"
JSON_FIELD = "_json"
)
var RESERVED_FIELDS = map[string]bool{
ID_FIELD: true,
VERSION_FIELD: true,
CREATED_BY_FIELD: true,
UPDATED_BY_FIELD: true,
CREATED_AT_FIELD: true,
UPDATED_AT_FIELD: true,
JSON_FIELD: true,
}
type EntryId int64
type UserId string
type Document map[string]any
type Entry struct {
Id EntryId
Version int64
CreatedBy UserId
UpdatedBy UserId
CreatedAt time.Time
UpdatedAt time.Time
Data Document
}
var _ starlark.Unpacker = (*Entry)(nil)
func (e *Entry) Unpack(value starlark.Value) error {
v, ok := value.(starlark.HasAttrs)
if !ok {
return fmt.Errorf("expected entry, got %s", value.Type())
}
var err error
entryData := make(map[string]any)
for _, attr := range v.AttrNames() {
switch attr {
case ID_FIELD:
id, err := apptype.GetIntAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
e.Id = EntryId(id)
case VERSION_FIELD:
e.Version, err = apptype.GetIntAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
case CREATED_BY_FIELD:
createdBy, err := apptype.GetStringAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
e.CreatedBy = UserId(createdBy)
case UPDATED_BY_FIELD:
updatedBy, err := apptype.GetStringAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
e.UpdatedBy = UserId(updatedBy)
case CREATED_AT_FIELD:
createdAt, err := apptype.GetIntAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
e.CreatedAt = time.UnixMilli(createdAt)
case UPDATED_AT_FIELD:
updatedAt, err := apptype.GetIntAttr(v, attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
e.UpdatedAt = time.UnixMilli(updatedAt)
default:
dataVal, err := v.Attr(attr)
if err != nil {
return fmt.Errorf("error reading %s: %w", attr, err)
}
data, err := starlark_type.UnmarshalStarlark(dataVal)
if err != nil {
return fmt.Errorf("error unmarshalling %s : %w", attr, err)
}
entryData[attr] = data
}
}
e.Data = entryData
return nil
}
// Store is the interface for a Clace document store. These API are exposed by the db plugin
type Store interface {
// Begin starts a new transaction
Begin(ctx context.Context) (*sql.Tx, error)
// Commit commits a transaction
Commit(ctx context.Context, tx *sql.Tx) error
// Rollback rolls back a transaction
Rollback(ctx context.Context, tx *sql.Tx) error
// Insert a new entry in the store
Insert(ctx context.Context, tx *sql.Tx, table string, Entry *Entry) (EntryId, error)
// SelectById returns a single item from the store
SelectById(ctx context.Context, tx *sql.Tx, table string, id EntryId) (*Entry, error)
// SelectOne returns a single item from the store
SelectOne(ctx context.Context, tx *sql.Tx, table string, filter map[string]any) (*Entry, error)
// Select returns the entries matching the filter
Select(ctx context.Context, tx *sql.Tx, thread *starlark.Thread, table string, filter map[string]any, sort []string, offset, limit int64) (starlark.Iterable, error)
// Count returns the count of entries matching the filter
Count(ctx context.Context, tx *sql.Tx, table string, filter map[string]any) (int64, error)
// Update an existing entry in the store
Update(ctx context.Context, tx *sql.Tx, table string, Entry *Entry) (int64, error)
// DeleteById an entry from the store by id
DeleteById(ctx context.Context, tx *sql.Tx, table string, id EntryId) (int64, error)
// Delete entries from the store matching the filter
Delete(ctx context.Context, tx *sql.Tx, table string, filter map[string]any) (int64, error)
}
func CreateType(name string, entry *Entry) (*starlark_type.StarlarkType, error) {
data := make(map[string]starlark.Value)
data[ID_FIELD] = starlark.MakeInt(int(entry.Id))
data[VERSION_FIELD] = starlark.MakeInt(int(entry.Version))
data[CREATED_BY_FIELD] = starlark.String(string(entry.CreatedBy))
data[UPDATED_BY_FIELD] = starlark.String(string(entry.UpdatedBy))
data[CREATED_AT_FIELD] = starlark.MakeInt(int(entry.CreatedAt.UnixMilli()))
data[UPDATED_AT_FIELD] = starlark.MakeInt(int(entry.UpdatedAt.UnixMilli()))
var err error
for k, v := range entry.Data {
data[k], err = starlark_type.MarshalStarlark(v)
if err != nil {
return nil, err
}
// TODO - add missing fields
}
return starlark_type.NewStarlarkType(name, data), nil
}