Permalink
Browse files

added namespace awareness

  • Loading branch information...
Alex Crowe
Alex Crowe committed Feb 9, 2017
1 parent 99657cf commit 0fb8fe0eb1ddf2e8a08eac53986be4af537fd055
Showing with 8 additions and 4 deletions.
  1. +5 −2 datastore.go
  2. +3 −2 lookup.go
View
@@ -141,6 +141,7 @@ func (d *Handler) Insert(ctx context.Context, items []*resource.Item) error {
for i, item := range items {
mKeys[i] = datastore.NameKey(d.entity, item.ID.(string), nil)
mKeys[i].Namespace = d.namespace
mEntities[i] = d.newEntity(item)
}
_, err := d.client.PutMulti(ctx, mKeys, mEntities)
@@ -156,6 +157,7 @@ func (d *Handler) Update(ctx context.Context, item *resource.Item, original *res
tx := func(tx *datastore.Transaction) error {
// Create a key for our current Entity
key := datastore.NameKey(d.entity, original.ID.(string), nil)
key.Namespace = d.namespace
var current Entity
// Attempt to get the existing Entity
@@ -183,6 +185,7 @@ func (d *Handler) Delete(ctx context.Context, item *resource.Item) error {
tx := func(tx *datastore.Transaction) error {
// Create a key for our target Entity
key := datastore.NameKey(d.entity, item.ID.(string), nil)
key.Namespace = d.namespace
var e Entity
// Attempt to get the existing Entity
@@ -205,7 +208,7 @@ func (d *Handler) Delete(ctx context.Context, item *resource.Item) error {
// Clear clears all entities matching the lookup from the Datastore
func (d *Handler) Clear(ctx context.Context, lookup *resource.Lookup) (int, error) {
q, err := getQuery(d.entity, lookup)
q, err := getQuery(d.entity, d.namespace, lookup)
if err != nil {
return 0, err
}
@@ -236,7 +239,7 @@ func (d *Handler) Clear(ctx context.Context, lookup *resource.Lookup) (int, erro
// Find entities matching the provided lookup from the Datastore
func (d *Handler) Find(ctx context.Context, lookup *resource.Lookup, offset, limit int) (*resource.ItemList, error) {
q, err := getQuery(d.entity, lookup)
q, err := getQuery(d.entity, d.namespace, lookup)
if err != nil {
return nil, err
}
View
@@ -28,13 +28,14 @@ func getField(f string) string {
}
// getQuery transform a resource.Lookup into a Google Datastore query
func getQuery(e string, l *resource.Lookup) (*datastore.Query, error) {
func getQuery(e string, ns string, l *resource.Lookup) (*datastore.Query, error) {
query, err := translateQuery(datastore.NewQuery(e), l.Filter())
// if lookup specifies sorting add this to our query
if len(l.Sort()) > 0 {
query = getSort(query, l.Sort())
}
// Set namespace for this query
query = query.Namespace(ns)
return query, err
}

0 comments on commit 0fb8fe0

Please sign in to comment.