Skip to content

Commit

Permalink
datastore: refactor privatizing of Key values.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 5, 2014
1 parent 9f7fd8a commit 9667465
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
18 changes: 13 additions & 5 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ var SIGN_TO_ORDER = {
* });
*/
function Key(options) {
this.namespace_ = options.namespace;
this.path_ = options.path;
Object.defineProperties(this, {
namespace: {
enumerable: true,
value: options.namespace
},
path: {
enumerable: true,
value: options.path
}
});
}

module.exports.Key = Key;
Expand Down Expand Up @@ -219,7 +227,7 @@ module.exports.keyFromKeyProto = keyFromKeyProto;
* // }
*/
function keyToKeyProto(key) {
var keyPath = key.path_;
var keyPath = key.path;
if (keyPath.length < 2) {
throw new Error('A key should contain at least a kind and an identifier.');
}
Expand All @@ -240,9 +248,9 @@ function keyToKeyProto(key) {
var proto = {
path_element: path
};
if (key.namespace_) {
if (key.namespace) {
proto.partition_id = {
namespace: key.namespace_
namespace: key.namespace
};
}
return proto;
Expand Down
14 changes: 7 additions & 7 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('Dataset', function() {
it('should return a key scoped by namespace', function() {
var ds = new datastore.Dataset({ projectId: 'test', namespace: 'my-ns' });
var key = ds.key('Company', 1);
assert.equal(key.namespace_, 'my-ns');
assert.deepEqual(key.path_, ['Company', 1]);
assert.equal(key.namespace, 'my-ns');
assert.deepEqual(key.path, ['Company', 1]);
});

it('should allow namespace specification when creating a key', function() {
Expand All @@ -39,8 +39,8 @@ describe('Dataset', function() {
namespace: 'custom-ns',
path: ['Company', 1]
});
assert.equal(key.namespace_, 'custom-ns');
assert.deepEqual(key.path_, ['Company', 1]);
assert.equal(key.namespace, 'custom-ns');
assert.deepEqual(key.path, ['Company', 1]);
});

it('should get by key', function(done) {
Expand All @@ -52,7 +52,7 @@ describe('Dataset', function() {
};
ds.get(ds.key('Kind', 123), function(err, entity) {
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
Expand All @@ -71,7 +71,7 @@ describe('Dataset', function() {
ds.get([key], function(err, entities) {
var entity = entities[0];
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('Dataset', function() {

ds.runQuery(query, function (err, entities) {
assert.ifError(err);
assert.deepEqual(entities[0].key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entities[0].key.path, ['Kind', 5732568548769792]);

var data = entities[0].data;
assert.strictEqual(data.author, 'Silvano');
Expand Down

0 comments on commit 9667465

Please sign in to comment.