-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
89 lines (64 loc) · 2.3 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dbm
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DatabaseOptions = options.DatabaseOptions
func NewDatabaseOptions() *DatabaseOptions {
return options.Database()
}
type Database interface {
Client() Client
Database() *mongo.Database
Name() string
Drop(ctx context.Context) error
Collection(name string, opts ...*CollectionOptions) Collection
UseSession(ctx context.Context, fn func(SessionContext) error) error
UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(SessionContext) error) error
StartSession(opts ...*SessionOptions) (Session, error)
BeginTx(ctx context.Context, opts ...*TransactionOptions) (Tx, error)
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*ChangeStream, error)
}
type database struct {
database *mongo.Database
client Client
}
func (db *database) Client() Client {
return db.client
}
func (db *database) Database() *mongo.Database {
return db.database
}
func (db *database) Name() string {
return db.database.Name()
}
func (db *database) Drop(ctx context.Context) error {
return db.database.Drop(ctx)
}
func (db *database) Collection(name string, opts ...*CollectionOptions) Collection {
return &collection{collection: db.database.Collection(name, opts...), database: db}
}
func (db *database) Aggregate(ctx context.Context, pipeline interface{}) Aggregate {
var a = &aggregate{}
a.pipeline = pipeline
a.ctx = ctx
a.opts = options.Aggregate()
a.aggregator = db.database
return a
}
func (db *database) UseSession(ctx context.Context, fn func(SessionContext) error) error {
return db.client.UseSession(ctx, fn)
}
func (db *database) UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(SessionContext) error) error {
return db.client.UseSessionWithOptions(ctx, opts, fn)
}
func (db *database) StartSession(opts ...*SessionOptions) (Session, error) {
return db.client.StartSession(opts...)
}
func (db *database) BeginTx(ctx context.Context, opts ...*TransactionOptions) (Tx, error) {
return db.client.BeginTx(ctx, opts...)
}
func (db *database) Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*ChangeStream, error) {
return db.database.Watch(ctx, pipeline, opts...)
}