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: scope Key creation to include NS. fixes #116. #124

Merged
merged 7 commits into from
Aug 22, 2014
Prev Previous commit
Next Next commit
datastore: support overriding namespace.
  • Loading branch information
stephenplusplus committed Aug 21, 2014
commit 8f356c97ee27e3d30fd97b3c644fc7833ed23441
52 changes: 52 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,55 @@ function handleResp(err, resp, body, callback) {
}

module.exports.handleResp = handleResp;

/**
* Get the type of a value.
*
* @private
*
* @return {string}
*/
function getType(value) {
return Object.prototype.toString.call(value).match(/\s(\w+)\]/)[1];
}

/**
* Check if an object is of the given type.
*
* @param {*} value - Value to compare to.
* @param {string} type - Type to check against object's value.
* @return {boolean}
*
* @example
* ```js
* is([1, 2, 3], 'array');
* // true
* ```
*/
function is(value, type) {
return getType(value).toLowerCase() === type.toLowerCase();
}

module.exports.is = is;

/**
* Convert an object into an array.
*
* @param {object} object - Object to convert to an array.
* @return {array}
*
* @example
* ```js
* function aFunction() {
* return toArray(arguments);
* }
*
* aFunction(1, 2, 3);
* // [1, 2, 3]
* ```
*/
function toArray(object) {
return [].slice.call(object);
}

module.exports.toArray = toArray;
28 changes: 21 additions & 7 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,32 @@ function Dataset(options) {
}

/**
* Helper to create a Key object, scoped to the namespace if present.
* Helper to create a Key object, scoped to the dataset's namespace by default.
*
* You may also specify a configuration object to define a namespace and path.
*
* @example
* ```js
* var key = dataset.key('Company', 123);
* var key;
*
* // Create a key from the dataset's namespace.
* key = dataset.key('Company', 123);
*
* // Create a key from a provided namespace and path.
* key = dataset.key({
* namespace: 'My-NS',
* path: ['Company', 123]
* });
* ```
*/
Dataset.prototype.key = function() {
return new entity.Key({
namespace: this.ns,
path: [].slice.call(arguments)
});
Dataset.prototype.key = function(keyConfig) {
if (!util.is(keyConfig, 'object')) {
keyConfig = {
namespace: this.ns,
path: util.toArray(arguments)
};
}
return new entity.Key(keyConfig);
};


Expand Down
7 changes: 1 addition & 6 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ var SIGN_TO_ORDER = {
*/
function Key(options) {
this.namespace_ = options.namespace;

if (options.path.length > 1) {
this.path_ = [].slice.call(options.path);
} else {
this.path_ = options.path[0];
}
this.path_ = options.path;
}

module.exports.Key = Key;
Expand Down
11 changes: 11 additions & 0 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('Dataset', 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]);
});

it('should allow namespace specification when creating a key', function() {
var ds = new datastore.Dataset({ projectId: 'test' });
var key = ds.key({
namespace: 'custom-ns',
path: ['Company', 1]
});
assert.equal(key.namespace_, 'custom-ns');
assert.deepEqual(key.path_, ['Company', 1])
});

it('should get by key', function(done) {
Expand Down