This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
/
client.go
103 lines (79 loc) · 2.7 KB
/
client.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2018 Diego Bernardes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package repository
import (
"context"
"github.com/pkg/errors"
"github.com/diegobernardes/flare"
mongodb "github.com/diegobernardes/flare/provider/mongodb"
)
// Client that implements the repository interface.
type Client struct {
base *mongodb.Client
resource Resource
resourceOptions []func(*Resource)
subscription Subscription
document Document
}
// Resource return a resource repository.
func (c *Client) Resource() flare.ResourceRepositorier {
return &c.resource
}
// Subscription return a subscription repository.
func (c *Client) Subscription() flare.SubscriptionRepositorier {
return &c.subscription
}
// Document return a document repository.
func (c *Client) Document() flare.DocumentRepositorier {
return &c.document
}
// Stop the repository.
func (c *Client) Stop() error { return nil }
// Setup initialize the repository.
func (c *Client) Setup(_ context.Context) error {
if err := c.resource.ensureIndex(); err != nil {
return errors.Wrap(err, "error during resource index initialization")
}
if err := c.document.ensureIndex(); err != nil {
return errors.Wrap(err, "error during document index initialization")
}
if err := c.subscription.ensureIndex(); err != nil {
return errors.Wrap(err, "error during subscription index initialization")
}
return nil
}
// NewClient return a configured client to access the repositories.
func NewClient(options ...func(*Client)) (*Client, error) {
c := &Client{}
for _, option := range options {
option(c)
}
if c.base == nil {
return nil, errors.New("invalid MongoDB client")
}
c.resource.subscriptionRepository = &c.subscription
c.subscription.resourceRepository = &c.resource
c.subscription.documentRepository = &c.document
c.resource.client = c.base
c.subscription.client = c.base
c.document.client = c.base
if err := c.resource.init(c.resourceOptions...); err != nil {
return nil, errors.Wrap(err, "error during resource repository initialization")
}
if err := c.subscription.init(); err != nil {
return nil, errors.Wrap(err, "error during subscription repository initialization")
}
if err := c.document.init(); err != nil {
return nil, errors.Wrap(err, "error during document repository initialization")
}
return c, nil
}
// ClientConnection set the MongoDB client.
func ClientConnection(base *mongodb.Client) func(*Client) {
return func(c *Client) { c.base = base }
}
// ClientResourceOptions set the resource options.
func ClientResourceOptions(options ...func(*Resource)) func(*Client) {
return func(c *Client) { c.resourceOptions = options }
}