-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
37 lines (33 loc) · 821 Bytes
/
writer.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
package writer
import (
"context"
"reflect"
"github.com/apache/cassandra-gocql-driver"
c "github.com/core-go/cassandra"
)
type Writer[T any] struct {
db *gocql.ClusterConfig
table string
Map func(T)
schema *c.Schema
VersionIndex int
}
func NewWriter[T any](session *gocql.ClusterConfig, table string, modelType reflect.Type, options ...func(T)) *Writer[T] {
var mp func(T)
if len(options) >= 1 {
mp = options[0]
}
schema := c.CreateSchema(modelType)
return &Writer[T]{db: session, table: table, Map: mp, schema: schema}
}
func (w *Writer[T]) Write(ctx context.Context, model T) error {
if w.Map != nil {
w.Map(model)
}
session, er0 := w.db.CreateSession()
if er0 != nil {
return er0
}
defer session.Close()
return c.Save(session, w.table, model, w.schema)
}