Implement namespace optional arg

This commit is contained in:
Bel LaPointe
2019-03-20 09:54:26 -06:00
parent b79cb97ba6
commit b7a231feaf
10 changed files with 101 additions and 45 deletions

View File

@@ -53,10 +53,11 @@ func NewMongo(addr string, auth ...string) (*Mongo, error) {
return &Mongo{db: db}, nil
}
func (mg *Mongo) Get(key string) ([]byte, error) {
func (mg *Mongo) Get(key string, ns ...string) ([]byte, error) {
namespace := resolveNamespace(ns)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
collection := mg.db.Database(DefaultNamespace).Collection(DefaultNamespace)
collection := mg.db.Database(DefaultNamespace).Collection(namespace)
filter := bson.M{"_id": key}
cursor, err := collection.Find(ctx, filter)
if err != nil {
@@ -82,10 +83,11 @@ func (mg *Mongo) Get(key string) ([]byte, error) {
return b, nil
}
func (mg *Mongo) Set(key string, value []byte) error {
func (mg *Mongo) Set(key string, value []byte, ns ...string) error {
namespace := resolveNamespace(ns)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
collection := mg.db.Database(DefaultNamespace).Collection(DefaultNamespace)
collection := mg.db.Database(DefaultNamespace).Collection(namespace)
filter := bson.M{"_id": key}
document := bson.M{"value": value}
_, err := collection.ReplaceOne(ctx, filter, document, options.Replace().SetUpsert(true))