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: Introduce Key type #101

Merged
merged 6 commits into from
Aug 8, 2014
Merged
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
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ If you're running this client on Google Compute Engine, you need to construct a

~~~~ js
var gcloud = require('gcloud'),
ds = new gcloud.datastore.Dataset({ projectId: YOUR_PROJECT_ID });
datastore = gcloud.datastore,
ds = new datastore.Dataset({ projectId: YOUR_PROJECT_ID });
~~~~

Elsewhere, initiate with project ID and private key downloaded from Developer's Console.
Expand All @@ -97,12 +98,12 @@ TODO
Get operations require a valid key to retrieve the key identified entity from Datastore. Skip to the "Querying" section if you'd like to learn more about querying against Datastore.

~~~~ js
ds.get(['Company', 123], function(err, entity) {});
ds.get(datastore.key('Company', 123), function(err, entity) {});

// alternatively, you can retrieve multiple entities at once.
ds.get([
['Company', 123],
['Product', 'Computer']
datastore.key('Company', 123),
datastore.key('Product', 'Computer')
], function(err, entities) {});
~~~~

Expand All @@ -111,14 +112,16 @@ You can insert arbitrary objects by providing an incomplete key during saving. I
To learn more about keys and incomplete keys, skip to the Keys section.

~~~~ js
ds.save({ key: ['Company', null], data: {/*...*/} }, function(err, key) {
ds.save({
key: datastore.key('Company', null), data: {/*...*/}
}, function(err, key) {
// First arg is an incomplete key for Company kind.
// console.log(key) will output ['Company', 599900452312].
});
// alternatively, you can save multiple entities at once.
ds.save([
{ key: ['Company', 123], data: {/*...*/} },
{ key: ['Product', 'Computer'], data: {/*...*/} }
{ key: datastore.key('Company', 123), data: {/*...*/} },
{ key: datastore.key('Product', 'Computer'), data: {/*...*/} }
], function(err, keys) {
// if the first key was incomplete, keys[0] will return the generated key.
});
Expand All @@ -132,10 +135,10 @@ ds.delete(['Company', 599900452312], function(err) {});
// alternatively, you can delete multiple entities of different
// kinds at once.
ds.delete([
['Company', 599900452312],
['Company', 599900452315],
['Office', 'mtv'],
['Company', 123, 'Employee', 'jbd']
datastore.key('Company', 599900452312),
datastore.key('Company', 599900452315),
datastore.key('Office', 'mtv'),
datastore.key('Company', 123, 'Employee', 'jbd')
], function(err) {});
~~~~

Expand Down Expand Up @@ -174,13 +177,14 @@ stored as properties is not currently supported.

~~~~ js
var q = ds.createQuery('Company')
.filter('__key__ =', ['Company', 'Google'])
.filter('__key__ =', datastore.key('Company', 'Google'))
~~~~

In order to filter by ancestors, use `hasAncestor` helper.

~~~ js
var q = ds.createQuery('Child').hasAncestor(['Parent', 123]);
var q = ds.createQuery('Child')
.hasAncestor(datastore.key('Parent', 123));
~~~

##### Sorting
Expand Down Expand Up @@ -221,7 +225,7 @@ You can generate IDs without creating entities. The following call will create
100 new IDs from the Company kind which exists under the default namespace.

~~~~ js
ds.allocateIds(['Company', null], 100, function(err, keys) {
ds.allocateIds(datastore.key('Company', null), 100, function(err, keys) {

});
~~~~
Expand All @@ -232,8 +236,8 @@ call below will create 100 new IDs, but from the Company kind that exists
under the "ns-test" namespace.

~~~~ js
ds.allocateIds(['ns-test', 'Company', null], 100, function(err, keys) {

var incompleteKey = datastore.key('ns-test', 'Company', null);
ds.allocateIds(incompleteKey, 100, function(err, keys) {
});
~~~~

Expand Down
29 changes: 18 additions & 11 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var signToOrderDict = {
'+': 'ASCENDING'
};

function Key(path) {
this.path_ = path;

This comment was marked as spam.

This comment was marked as spam.

}

module.exports.Key = Key;

function Int(val) {
this.val = val;
}
Expand Down Expand Up @@ -78,34 +84,35 @@ var entityFromEntityProto = function(proto) {
module.exports.entityFromEntityProto = entityFromEntityProto;

var keyFromKeyProto = function(proto) {
var key = [];
var path = [];
if (proto.partitionId.namespace) {
key.push(proto.partitionId.namespace);
path.push(proto.partitionId.namespace);
}
proto.path.forEach(function(p) {
key.push(p.kind);
key.push(Number(p.id) || p.name || null);
path.push(p.kind);
path.push(Number(p.id) || p.name || null);
});
return key;
return new Key(path);
};

module.exports.keyFromKeyProto = keyFromKeyProto;

var keyToKeyProto = function(key) {
if (key.length < 2) {
var keyPath = key.path_;
if (keyPath.length < 2) {
throw new Error('A key should contain at least a kind and an identifier.');
}
var namespace = null;
var start = 0;
if (key.length % 2 === 1) {
if (keyPath.length % 2 === 1) {
// the first item is the namespace
namespace = key[0];
namespace = keyPath[0];
start = 1;
}
var path = [];
for (var i = start; i < (key.length - start); i += 2) {
var p = { kind: key[i] };
var val = key[i+1];
for (var i = start; i < (keyPath.length - start); i += 2) {
var p = { kind: keyPath[i] };
var val = keyPath[i+1];
if (val) {
// if not numeric, set key name.
if (isNaN(val)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var entity = require('./entity');

module.exports = {
Dataset: require('./dataset'),
key: function() {
// TODO(jbd): Handle arguments in entity.Key constructor.
return new entity.Key([].slice.call(arguments));
},

This comment was marked as spam.

This comment was marked as spam.

Int: function(value) {
return new entity.Int(value);
},
Expand Down
5 changes: 3 additions & 2 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Transaction.prototype.finalize = function(callback) {
* @param {Function} callback
*/
Transaction.prototype.get = function(keys, callback) {
var isMultipleRequest = Array.isArray(keys[0]);
var isMultipleRequest = Array.isArray(keys);
keys = isMultipleRequest ? keys : [keys];
callback = callback || util.noop;
var req = {
Expand Down Expand Up @@ -170,9 +170,10 @@ Transaction.prototype.save = function(entities, callback) {
* @param {Function} callback
*/
Transaction.prototype.delete = function(keys, callback) {
var isMultipleRequest = Array.isArray(keys[0]);
var isMultipleRequest = Array.isArray(keys);
keys = isMultipleRequest ? keys : [keys];
callback = callback || util.noop;

var req = {
mode: MODE_NON_TRANSACTIONAL,
mutation: {
Expand Down
67 changes: 36 additions & 31 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ describe('datastore', function() {
};

it('should save/get/delete with a key name', function(done) {
var postKeyName = 'post1';

ds.save({ key: ['Post', postKeyName], data: post }, function(err, key) {
var postKey = datastore.key('Post', 'post1');
ds.save({ key: postKey, data: post }, function(err, key) {
assert.ifError(err);
assert.equal(key[1], postKeyName);
ds.get(['Post', postKeyName], function(err, entity) {
assert.equal(key.path_[1], 'post1');
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', postKeyName], function(err) {
ds.delete(key, function(err) {
assert.ifError(err);
done();
});
Expand All @@ -54,15 +53,17 @@ describe('datastore', function() {
});

it('should save/get/delete with a numeric key id', function(done) {
var postKeyId = '123456789';

ds.save({ key: ['Post', postKeyId], data: post }, function(err, key) {
var postKey = datastore.key('Post', 123456789);
ds.save({
key: postKey,
data: post
}, function(err, key) {
assert.ifError(err);
assert.equal(key[1], postKeyId);
ds.get(['Post', postKeyId], function(err, entity) {
assert.equal(key.path_[1], 123456789);
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', postKeyId], function(err) {
ds.delete(key, function(err) {
assert.ifError(err);
done();
});
Expand All @@ -71,14 +72,17 @@ describe('datastore', function() {
});

it('should save/get/delete with a generated key id', function(done) {
ds.save({ key: ['Post', null], data: post }, function(err, key) {
ds.save({
key: datastore.key('Post', null),
data: post
}, function(err, key) {
assert.ifError(err);
assert(key[1]);
var assignedId = key[1];
ds.get(['Post', assignedId], function(err, entity) {
var assignedId = key.path_[1];
assert(assignedId);
ds.get(datastore.key('Post', assignedId), function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', assignedId], function(err) {
ds.delete(datastore.key('Post', assignedId), function(err) {
assert.ifError(err);
done();
});
Expand All @@ -96,15 +100,15 @@ describe('datastore', function() {
wordCount: 450,
rating: 4.5,
};
var key = ['Post', null];
var key = datastore.key('Post', null);
ds.save([
{ key: key, data: post },
{ key: key, data: post2 }
], function(err, keys) {
assert.ifError(err);
assert.equal(keys.length,2);
var firstKey = ['Post', keys[0][1]];
var secondKey = ['Post', keys[1][1]];
var firstKey = datastore.key('Post', keys[0].path_[1]);
var secondKey = datastore.key('Post', keys[1].path_[1]);
ds.get([firstKey, secondKey], function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 2);
Expand All @@ -121,14 +125,14 @@ describe('datastore', function() {
describe('querying the datastore', function() {

var keys = [
['Character', 'Rickard'],
['Character', 'Rickard', 'Character', 'Eddard'],
['Character', 'Catelyn'],
['Character', 'Eddard', 'Character', 'Arya'],
['Character', 'Eddard', 'Character', 'Sansa'],
['Character', 'Eddard', 'Character', 'Robb'],
['Character', 'Eddard', 'Character', 'Bran'],
['Character', 'Eddard', 'Character', 'Jon Snow']
datastore.key('Character', 'Rickard'),
datastore.key('Character', 'Rickard', 'Character', 'Eddard'),
datastore.key('Character', 'Catelyn'),
datastore.key('Character', 'Eddard', 'Character', 'Arya'),
datastore.key('Character', 'Eddard', 'Character', 'Sansa'),
datastore.key('Character', 'Eddard', 'Character', 'Robb'),
datastore.key('Character', 'Eddard', 'Character', 'Bran'),
datastore.key('Character', 'Eddard', 'Character', 'Jon Snow')
];

var characters = [{
Expand Down Expand Up @@ -227,7 +231,8 @@ describe('datastore', function() {
});

it('should filter by ancestor', function(done) {
var q = ds.createQuery('Character').hasAncestor(['Character', 'Eddard']);
var q = ds.createQuery('Character')
.hasAncestor(datastore.key('Character', 'Eddard'));
ds.runQuery(q, function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 5);
Expand All @@ -237,7 +242,7 @@ describe('datastore', function() {

it('should filter by key', function(done) {
var q = ds.createQuery('Character')
.filter('__key__ =', ['Character', 'Rickard']);
.filter('__key__ =', datastore.key('Character', 'Rickard'));
ds.runQuery(q, function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 1);
Expand Down Expand Up @@ -332,7 +337,7 @@ describe('datastore', function() {
describe('transactions', function() {

it('should run in a transaction', function(done) {
var key = ['Company', 'Google'];
var key = datastore.key('Company', 'Google');
var obj = {
url: 'www.google.com'
};
Expand Down
Loading