blob: 989cb1303dceb6d5fbf16b09babe8f8a5f673be2 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package options
8
9import (
10 "github.com/mongodb/mongo-go-driver/bson/bsoncodec"
11 "github.com/mongodb/mongo-go-driver/mongo/readconcern"
12 "github.com/mongodb/mongo-go-driver/mongo/readpref"
13 "github.com/mongodb/mongo-go-driver/mongo/writeconcern"
14)
15
16// DatabaseOptions represent all possible options to configure a Database.
17type DatabaseOptions struct {
18 ReadConcern *readconcern.ReadConcern // The read concern for operations in the database.
19 WriteConcern *writeconcern.WriteConcern // The write concern for operations in the database.
20 ReadPreference *readpref.ReadPref // The read preference for operations in the database.
21 Registry *bsoncodec.Registry // The registry to be used to construct BSON encoders and decoders for the database.
22}
23
24// Database creates a new DatabaseOptions instance
25func Database() *DatabaseOptions {
26 return &DatabaseOptions{}
27}
28
29// SetReadConcern sets the read concern for the database.
30func (d *DatabaseOptions) SetReadConcern(rc *readconcern.ReadConcern) *DatabaseOptions {
31 d.ReadConcern = rc
32 return d
33}
34
35// SetWriteConcern sets the write concern for the database.
36func (d *DatabaseOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *DatabaseOptions {
37 d.WriteConcern = wc
38 return d
39}
40
41// SetReadPreference sets the read preference for the database.
42func (d *DatabaseOptions) SetReadPreference(rp *readpref.ReadPref) *DatabaseOptions {
43 d.ReadPreference = rp
44 return d
45}
46
47// SetRegistry sets the bsoncodec Registry for the database.
48func (d *DatabaseOptions) SetRegistry(r *bsoncodec.Registry) *DatabaseOptions {
49 d.Registry = r
50 return d
51}
52
53// MergeDatabaseOptions combines the *DatabaseOptions arguments into a single *DatabaseOptions in a last one wins
54// fashion.
55func MergeDatabaseOptions(opts ...*DatabaseOptions) *DatabaseOptions {
56 d := Database()
57
58 for _, opt := range opts {
59 if opt == nil {
60 continue
61 }
62 if opt.ReadConcern != nil {
63 d.ReadConcern = opt.ReadConcern
64 }
65 if opt.WriteConcern != nil {
66 d.WriteConcern = opt.WriteConcern
67 }
68 if opt.ReadPreference != nil {
69 d.ReadPreference = opt.ReadPreference
70 }
71 if opt.Registry != nil {
72 d.Registry = opt.Registry
73 }
74 }
75
76 return d
77}