MCPcopy Create free account
hub / github.com/DoNewsCode/core / provideMongoFactory

Function provideMongoFactory

otmongo/dependency.go:55–92  ·  view source on GitHub ↗

Provide creates Factory and *mongo.Client. It is a valid dependency for package core.

(providerOption *providersOption)

Source from the content-addressed store, hash-verified

53// Provide creates Factory and *mongo.Client. It is a valid dependency for
54// package core.
55func provideMongoFactory(providerOption *providersOption) func(p factoryIn) (Factory, func()) {
56 if providerOption.interceptor == nil {
57 providerOption.interceptor = func(name string, clientOptions *options.ClientOptions) {}
58 }
59 return func(p factoryIn) (Factory, func()) {
60 factory := di.NewFactory(func(name string) (di.Pair, error) {
61 var conf struct{ URI string }
62 if err := p.Conf.Unmarshal(fmt.Sprintf("mongo.%s", name), &conf); err != nil {
63 return di.Pair{}, fmt.Errorf("mongo configuration %s not valid: %w", name, err)
64 }
65 if conf.URI == "" {
66 conf.URI = "mongodb://127.0.0.1:27017"
67 }
68
69 opts := options.Client()
70 opts.ApplyURI(conf.URI)
71 if p.Tracer != nil {
72 opts.Monitor = NewMonitor(p.Tracer)
73 }
74 providerOption.interceptor(name, opts)
75 client, err := mongo.Connect(context.Background(), opts)
76 if err != nil {
77 return di.Pair{}, err
78 }
79 return di.Pair{
80 Conn: client,
81 Closer: func() {
82 _ = client.Disconnect(context.Background())
83 },
84 }, nil
85 })
86 f := Factory{factory}
87 if providerOption.reloadable {
88 f.SubscribeReloadEventFrom(p.Dispatcher)
89 }
90 return f, f.Close
91 }
92}
93
94func provideDefaultClient(maker Maker) (*mongo.Client, error) {
95 return maker.Make("default")

Callers 2

TestNewMongoFactoryFunction · 0.85
ProvidersFunction · 0.85

Calls 5

NewFactoryFunction · 0.92
NewMonitorFunction · 0.85
UnmarshalMethod · 0.65
SprintfMethod · 0.65

Tested by 1

TestNewMongoFactoryFunction · 0.68