-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
70 lines (59 loc) · 1.71 KB
/
database.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
package dalgo2firestore
import (
"cloud.google.com/go/firestore"
"context"
"github.com/dal-go/dalgo/dal"
)
// NewDatabase creates new instance of dalgo interface to Firestore
func NewDatabase(id string, client *firestore.Client) (db dal.DB) {
if id == "" {
panic("id is a required field, got empty string")
}
if client == nil {
panic("client is a required field, got nil")
}
var getReader = func(c context.Context, query dal.Query) (reader dal.Reader, err error) {
return newFirestoreReader(c, client, query)
}
return &database{
id: id,
client: client,
QueryExecutor: dal.NewQueryExecutor(getReader),
}
}
var _ dal.DB = database{}
var _ dal.DB = (*database)(nil)
// database implements dal.Database
type database struct {
id string
client *firestore.Client
dal.QueryExecutor
}
func (db database) ID() string {
return db.id
}
func (db database) Adapter() dal.Adapter {
return dal.NewAdapter("firestore", "v1.9.0")
}
var _ dal.DB = (*database)(nil)
func (db database) Upsert(_ context.Context, _ dal.Record) error {
panic("implement me")
}
func (db database) keyToDocRef(key *dal.Key) *firestore.DocumentRef {
return keyToDocRef(key, db.client)
}
func (db database) Insert(ctx context.Context, record dal.Record, opts ...dal.InsertOption) error {
options := dal.NewInsertOptions(opts...)
generateID := options.IDGenerator()
if generateID != nil {
if err := generateID(ctx, record); err != nil {
return err
}
}
_, err := insert(ctx, db, record, createNonTransactional)
return err
}
func (db database) InsertMulti(ctx context.Context, records []dal.Record, opts ...dal.InsertOption) (err error) {
_, err = insertMulti(ctx, db, records, createNonTransactional, opts...)
return err
}