You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dboptions.go 2.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // 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. package options
  7. import (
  8. "go.mongodb.org/mongo-driver/bson/bsoncodec"
  9. "go.mongodb.org/mongo-driver/mongo/readconcern"
  10. "go.mongodb.org/mongo-driver/mongo/readpref"
  11. "go.mongodb.org/mongo-driver/mongo/writeconcern"
  12. )
  13. // DatabaseOptions represent all possible options to configure a Database.
  14. type DatabaseOptions struct {
  15. ReadConcern *readconcern.ReadConcern // The read concern for operations in the database.
  16. WriteConcern *writeconcern.WriteConcern // The write concern for operations in the database.
  17. ReadPreference *readpref.ReadPref // The read preference for operations in the database.
  18. Registry *bsoncodec.Registry // The registry to be used to construct BSON encoders and decoders for the database.
  19. }
  20. // Database creates a new DatabaseOptions instance
  21. func Database() *DatabaseOptions {
  22. return &DatabaseOptions{}
  23. }
  24. // SetReadConcern sets the read concern for the database.
  25. func (d *DatabaseOptions) SetReadConcern(rc *readconcern.ReadConcern) *DatabaseOptions {
  26. d.ReadConcern = rc
  27. return d
  28. }
  29. // SetWriteConcern sets the write concern for the database.
  30. func (d *DatabaseOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *DatabaseOptions {
  31. d.WriteConcern = wc
  32. return d
  33. }
  34. // SetReadPreference sets the read preference for the database.
  35. func (d *DatabaseOptions) SetReadPreference(rp *readpref.ReadPref) *DatabaseOptions {
  36. d.ReadPreference = rp
  37. return d
  38. }
  39. // SetRegistry sets the bsoncodec Registry for the database.
  40. func (d *DatabaseOptions) SetRegistry(r *bsoncodec.Registry) *DatabaseOptions {
  41. d.Registry = r
  42. return d
  43. }
  44. // MergeDatabaseOptions combines the *DatabaseOptions arguments into a single *DatabaseOptions in a last one wins
  45. // fashion.
  46. func MergeDatabaseOptions(opts ...*DatabaseOptions) *DatabaseOptions {
  47. d := Database()
  48. for _, opt := range opts {
  49. if opt == nil {
  50. continue
  51. }
  52. if opt.ReadConcern != nil {
  53. d.ReadConcern = opt.ReadConcern
  54. }
  55. if opt.WriteConcern != nil {
  56. d.WriteConcern = opt.WriteConcern
  57. }
  58. if opt.ReadPreference != nil {
  59. d.ReadPreference = opt.ReadPreference
  60. }
  61. if opt.Registry != nil {
  62. d.Registry = opt.Registry
  63. }
  64. }
  65. return d
  66. }