Skip to content

Commit

Permalink
Merge pull request #106 from rakyll/key-entity
Browse files Browse the repository at this point in the history
datastore: Adding support for embedding keys and filtering by keys
  • Loading branch information
Burcu Dogan committed Aug 8, 2014
2 parents 61f09a2 + cb194ab commit 007da63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,13 @@ function valueToProperty(v) {
});
return p;
}
if (v instanceof Key) {
p.keyValue = keyToKeyProto(v);
return p;
}
if (v instanceof Object && Object.keys(v).length > 0) {
var properties = {};
Object.keys(v).forEach(function(k) {
// TODO: Detect keys?
properties[k] = valueToProperty(v[k]);
});
p.entityValue = { properties: properties };
Expand Down
22 changes: 22 additions & 0 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ describe('datastore', function() {

});

it('should be able to save keys as a part of entity and query by key',
function(done) {
var personKey = datastore.key('Person', 'name');
ds.save({
key: personKey,
data: {
fullName: 'Full name',
linkedTo: personKey // himself
}
}, function(err) {
assert.ifError(err);
var q = ds.createQuery('Person')
.filter('linkedTo =', personKey);
ds.runQuery(q, function(err, results) {
assert.ifError(err);
assert.strictEqual(results[0].data.fullName, 'Full name');
assert.deepEqual(results[0].data.linkedTo, personKey);
ds.delete(personKey, done);
});
});
});

describe('querying the datastore', function() {

var keys = [
Expand Down

0 comments on commit 007da63

Please sign in to comment.