Add namespacing for users

This commit is contained in:
Bel LaPointe
2020-07-22 22:20:06 -06:00
parent 1a6973b02c
commit 660cd64262
5 changed files with 45 additions and 37 deletions

View File

@@ -19,9 +19,9 @@ func NewGraph() Graph {
}
}
func (g Graph) List(ctx context.Context, from ...string) ([]entity.One, error) {
func (g Graph) List(ctx context.Context, namespace string, from ...string) ([]entity.One, error) {
filter := operator.NewFilterIn("_id", from)
ch, err := g.mongo.Find(ctx, filter)
ch, err := g.mongo.Find(ctx, namespace, filter)
if err != nil {
return nil, err
}
@@ -36,14 +36,14 @@ func (g Graph) List(ctx context.Context, from ...string) ([]entity.One, error) {
return ones, nil
}
func (g Graph) Insert(ctx context.Context, one entity.One) error {
return g.mongo.Insert(ctx, one)
func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error {
return g.mongo.Insert(ctx, namespace, one)
}
func (g Graph) Update(ctx context.Context, one entity.One, modify interface{}) error {
return g.mongo.Update(ctx, one, modify)
func (g Graph) Update(ctx context.Context, namespace string, one entity.One, modify interface{}) error {
return g.mongo.Update(ctx, namespace, one, modify)
}
func (g Graph) Delete(ctx context.Context, filter interface{}) error {
return g.mongo.Delete(ctx, filter)
func (g Graph) Delete(ctx context.Context, namespace string, filter interface{}) error {
return g.mongo.Delete(ctx, namespace, filter)
}

View File

@@ -21,11 +21,10 @@ func TestIntegration(t *testing.T) {
os.Args = os.Args[:1]
graph := NewGraph()
graph.mongo.db = "test-db"
graph.mongo.col = "test-col"
ctx, can := context.WithCancel(context.TODO())
defer can()
clean := func() {
graph.mongo.client.Database(graph.mongo.db).Collection(graph.mongo.col).DeleteMany(ctx, map[string]interface{}{})
graph.mongo.client.Database(graph.mongo.db).Collection("col").DeleteMany(ctx, map[string]interface{}{})
}
clean()
defer clean()
@@ -39,7 +38,7 @@ func TestIntegration(t *testing.T) {
t.Run("graph.Insert(...)", func(t *testing.T) {
for _, one := range ones {
err := graph.Insert(ctx, one)
err := graph.Insert(ctx, "col", one)
if err != nil {
t.Fatal(err)
}
@@ -47,7 +46,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.List", func(t *testing.T) {
all, err := graph.List(ctx)
all, err := graph.List(ctx, "col")
if err != nil {
t.Fatal(err)
}
@@ -58,7 +57,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.List(foo => *)", func(t *testing.T) {
some, err := graph.List(ctx, ones[0].Peers()...)
some, err := graph.List(ctx, "col", ones[0].Peers()...)
if err != nil {
t.Fatal(err)
}
@@ -69,12 +68,12 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
if err != nil {
t.Fatal(err)
}
some, err := graph.List(ctx, ones[0].Name)
some, err := graph.List(ctx, "col", ones[0].Name)
if err != nil {
t.Fatal(err)
}
@@ -92,7 +91,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.Update(foo, +=2); graph.Update(foo, -=1)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]entity.One{
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]entity.One{
"hello": entity.One{Name: "hello", Relationship: ":("},
"world": entity.One{Name: "world", Relationship: ":("},
}})
@@ -100,7 +99,7 @@ func TestIntegration(t *testing.T) {
t.Fatal(err)
}
some1, err := graph.List(ctx, ones[0].Name)
some1, err := graph.List(ctx, "col", ones[0].Name)
if err != nil {
t.Fatal(err)
}
@@ -112,11 +111,11 @@ func TestIntegration(t *testing.T) {
t.Fatal(some1[0].Peers())
}
err = graph.Update(ctx, ones[0].Query(), operator.Unset(fmt.Sprintf("%s.world", entity.Connections)))
err = graph.Update(ctx, "col", ones[0].Query(), operator.Unset(fmt.Sprintf("%s.world", entity.Connections)))
if err != nil {
t.Fatal(err)
}
some2, err := graph.List(ctx, ones[0].Name)
some2, err := graph.List(ctx, "col", ones[0].Name)
if err != nil {
t.Fatal(err)
}

View File

@@ -15,7 +15,6 @@ import (
type Mongo struct {
client *mongo.Client
db string
col string
}
func NewMongo() Mongo {
@@ -35,12 +34,11 @@ func NewMongo() Mongo {
return Mongo{
client: c,
db: config.New().Database,
col: "col",
}
}
func (m Mongo) Find(ctx context.Context, filter interface{}) (chan bson.Raw, error) {
c := m.client.Database(m.db).Collection(m.col)
func (m Mongo) Find(ctx context.Context, namespace string, filter interface{}) (chan bson.Raw, error) {
c := m.client.Database(m.db).Collection(namespace)
cursor, err := c.Find(ctx, filter)
if err != nil {
return nil, err
@@ -60,13 +58,13 @@ func (m Mongo) page(ctx context.Context, cursor *mongo.Cursor) chan bson.Raw {
return ch
}
func (m Mongo) Update(ctx context.Context, filter, apply interface{}) error {
c := m.client.Database(m.db).Collection(m.col)
func (m Mongo) Update(ctx context.Context, namespace string, filter, apply interface{}) error {
c := m.client.Database(m.db).Collection(namespace)
_, err := c.UpdateOne(ctx, filter, apply)
return err
}
func (m Mongo) Insert(ctx context.Context, apply interface{}) error {
func (m Mongo) Insert(ctx context.Context, namespace string, apply interface{}) error {
b, err := bson.Marshal(apply)
if err != nil {
return err
@@ -80,13 +78,13 @@ func (m Mongo) Insert(ctx context.Context, apply interface{}) error {
} else if _, ok := mapp["_id"].(string); !ok {
return errors.New("non-string _id in new object")
}
c := m.client.Database(m.db).Collection(m.col)
c := m.client.Database(m.db).Collection(namespace)
_, err = c.InsertOne(ctx, apply)
return err
}
func (m Mongo) Delete(ctx context.Context, filter interface{}) error {
c := m.client.Database(m.db).Collection(m.col)
func (m Mongo) Delete(ctx context.Context, namespace string, filter interface{}) error {
c := m.client.Database(m.db).Collection(namespace)
_, err := c.DeleteOne(ctx, filter)
return err
}