forked from mailhog/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_multi_tenant.go
121 lines (107 loc) · 3.28 KB
/
mongodb_multi_tenant.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package storage
import (
"fmt"
"log"
"github.com/mailhog/data"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// MongoDB represents MongoDB backed storage backend
type MultiTenantMongoDB struct {
database *mgo.Database
}
// CreateMongoDB creates a MongoDB backed storage backend
func CreateMultiTenantMongoDB(uri, db string) MultiTenantStorage {
log.Printf("Connecting to MongoDB: %s\n", uri)
session, err := mgo.Dial(uri)
if err != nil {
log.Printf("Error connecting to MongoDB: %s", err)
return nil
}
return &MultiTenantMongoDB{
database: session.DB(db),
}
}
// Store stores a message in MongoDB and returns its storage ID
func (m *MultiTenantMongoDB) Store(msg *data.Message, tenant string) (string, error) {
err := m.database.C(tenant).Insert(msg)
if err != nil {
log.Printf("Error inserting message: %s", err)
return "", err
}
return string(msg.ID), nil
}
// Count returns the number of stored messages
func (m *MultiTenantMongoDB) Count(tenant string) int {
c, _ := m.database.C(tenant).Count()
return c
}
// Search finds messages matching the query
func (m *MultiTenantMongoDB) Search(filter interface{}, start, limit int, tenant string) (*data.Messages, int, error) {
//messages := &data.Messages{}
//var count = 0
//var field = "raw.data"
//switch kind {
//case "to":
// field = "raw.to"
//case "from":
// field = "raw.from"
//}
//err := m.database.C(tenant).Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Skip(start).Limit(limit).Sort("-created").Select(bson.M{
// "id": 1,
// "_id": 1,
// "from": 1,
// "to": 1,
// "content.headers": 1,
// "content.size": 1,
// "created": 1,
// "raw": 1,
//}).All(messages)
//if err != nil {
// log.Printf("Error loading messages: %s", err)
// return nil, 0, err
//}
//count, _ = m.database.C(tenant).Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Count()
//
//return messages, count, nil
return nil, 0, fmt.Errorf("error: method not implemented")
}
// List returns a list of messages by index
func (m *MultiTenantMongoDB) List(start int, limit int, tenant string) (*data.Messages, error) {
messages := &data.Messages{}
err := m.database.C(tenant).Find(bson.M{}).Skip(start).Limit(limit).Sort("-created").Select(bson.M{
"id": 1,
"_id": 1,
"from": 1,
"to": 1,
"content.headers": 1,
"content.size": 1,
"created": 1,
"raw": 1,
}).All(messages)
if err != nil {
log.Printf("Error loading messages: %s", err)
return nil, err
}
return messages, nil
}
// DeleteOne deletes an individual message by storage ID
func (m *MultiTenantMongoDB) DeleteOne(id, tenant string) error {
_, err := m.database.C(tenant).RemoveAll(bson.M{"id": id})
return err
}
// DeleteAll deletes all messages stored in MongoDB
func (m *MultiTenantMongoDB) DeleteAll(tenant string) error {
_, err := m.database.C(tenant).RemoveAll(bson.M{})
return err
}
// Load loads an individual message by storage ID
func (m *MultiTenantMongoDB) Load(id, tenant string) (*data.Message, error) {
result := &data.Message{}
err := m.database.C(tenant).Find(bson.M{"id": id}).One(&result)
if err != nil {
log.Printf("Error loading message: %s", err)
return nil, err
}
return result, nil
}