Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: expose ID, path, & namespace. fixes #171. #175

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ function Dataset(options) {
*
* You may also specify a configuration object to define a namespace and path.
*
* Once you have created a Key object, all properties (namespace, path, and id)
* are read-only. You can access them from the returned Key object: `key.id`,
* `key.namespace`, and `key.path` will return their respective components.
*
* @example
* var key;
*
Expand Down
18 changes: 17 additions & 1 deletion lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var SIGN_TO_ORDER = {
};

/**
* Build a Datastore Key object.
* Build a Datastore Key object. Keys components should only be specified during
* instantiation. They are read-only via properties on the returned Key object.
*
* @constructor
* @param {object} - Configuration object.
Expand All @@ -74,10 +75,25 @@ var SIGN_TO_ORDER = {
* namespace: 'ns',
* path: ['Company', 123]
* });
*
* // key.id = 123
* // key.namespace = 'ns'
* // key.path = ['Company', 123]

This comment was marked as spam.

*/
function Key(options) {
this.namespace_ = options.namespace;
this.path_ = options.path;
this.id_ = options.path[options.path.length - 1];

This comment was marked as spam.


Object.defineProperties(this,

This comment was marked as spam.

['namespace', 'path', 'id'].reduce(function(acc, key) {
acc[key] = {
value: this[key + '_'],
writable: false
};
return acc;
}.bind(this), {})
);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

module.exports.Key = Key;
Expand Down
22 changes: 22 additions & 0 deletions test/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ var queryFilterProto = {
group_by: []
};

describe('Key', function() {
var path = ['Kind', 23];
var id = path[path.length - 1];
var key = new entity.Key({
path: ['Kind', 23]
});

it('should expose internal Key values', function() {
assert.strictEqual(key.namespace, undefined);
assert.deepEqual(key.path, path);
assert.equal(key.id, id);
});

it('should protect internal Key values', function() {
['namespace', 'path', 'id'].forEach(function(property) {
assert.throws(function() {
key[property] = 'overriden value';
}, /Cannot assign/);
});
});
});

describe('registerKind', function() {
it('should be able to register valid field metadata', function(done) {
entity.registerKind('namespace', 'kind', blogPostMetadata);
Expand Down