/
save.go
43 lines (34 loc) · 942 Bytes
/
save.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
package db
import (
"context"
"github.com/rs/zerolog/log"
)
func (db *DB) DropAndSave(ctx context.Context, objects []interface{}) error {
collection := db.getCollectionSemesterFromContext(ctx)
err := collection.Drop(ctx)
if err != nil {
log.Error().Err(err).
Str("collectionName", ctx.Value("collectionName").(string)).
Msg("cannot drop collection")
return err
}
_, err = collection.InsertMany(ctx, objects)
if err != nil {
log.Error().Err(err).
Str("collectionName", ctx.Value("collectionName").(string)).
Msg("cannot insert objects")
return err
}
return nil
}
func (db *DB) Save(ctx context.Context, objects []interface{}) error {
collection := db.getCollectionSemesterFromContext(ctx)
_, err := collection.InsertMany(ctx, objects)
if err != nil {
log.Error().Err(err).
Str("collectionName", ctx.Value("collectionName").(string)).
Msg("cannot insert objects")
return err
}
return nil
}