Skip to content
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
14 changes: 10 additions & 4 deletions packages/datastore/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ var InvalidKeyError = createErrorClass('InvalidKey', function(opts) {
this.message = errorMessages[opts.code];
});

/**
* A symbol to access the Key object from an entity object.
*
* @type {symbol}
*/
entity.KEY_SYMBOL = Symbol('KEY');

/**
* Build a Datastore Double object.
*
Expand Down Expand Up @@ -405,10 +412,9 @@ entity.entityToEntityProto = entityToEntityProto;
*/
function formatArray(results) {
return results.map(function(result) {
return {
key: entity.keyFromKeyProto(result.entity.key),
data: entity.entityFromEntityProto(result.entity)
};
var ent = entity.entityFromEntityProto(result.entity);
ent[entity.KEY_SYMBOL] = entity.keyFromKeyProto(result.entity.key);
return ent;
});
}

Expand Down
7 changes: 7 additions & 0 deletions packages/datastore/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ Datastore.prototype.int = Datastore.int = function(value) {
return new entity.Int(value);
};

/**
* Access the Key from an Entity object.
*
* @type {symbol}
*/
Datastore.prototype.KEY = Datastore.KEY = entity.KEY_SYMBOL;

/**
* This is one of three values which may be returned from
* {module:datastore#runQuery}, {module:transaction#runQuery}, and
Expand Down
7 changes: 4 additions & 3 deletions packages/datastore/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Query.prototype.offset = function(n) {
* // unnecessary processing and API requests.
* //-
* query.run()
* .on('data', function (entity) {
* .on('data', function(entity) {
* this.end();
* });
*
Expand All @@ -323,8 +323,9 @@ Query.prototype.offset = function(n) {
* query.select('__key__');
*
* query.run(function(err, entities) {
* // entities[].key = Key object
* // entities[].data = Empty object
* var keys = entities.map(function(entity) {
* return entity[datastore.KEY];
* });
* });
*/
Query.prototype.run = function() {
Expand Down
7 changes: 4 additions & 3 deletions packages/datastore/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
* // Error handling omitted.
* }
*
* entity.data.newValue = true;
* entity.newValue = true;
* datastore.save(entity, function(err) {});
* });
*/
Expand Down Expand Up @@ -489,8 +489,9 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
* var keysOnlyQuery = datastore.createQuery('Lion').select('__key__');
*
* datastore.runQuery(keysOnlyQuery, function(err, entities) {
* // entities[].key = Key object
* // entities[].data = Empty object
* var keys = entities.map(function(entity) {
* return entity[datastore.KEY];
* });
* });
*/
DatastoreRequest.prototype.runQuery = function(query, options, callback) {
Expand Down
47 changes: 24 additions & 23 deletions packages/datastore/system-test/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Datastore', function() {
}

var keys = entities.map(function(entity) {
return entity.key;
return entity[datastore.KEY];
});

datastore.delete(keys, callback);
Expand Down Expand Up @@ -91,7 +91,8 @@ describe('Datastore', function() {
datastore.get(postKey, function(err, entity) {
assert.ifError(err);

assert.deepEqual(entity.data, post);
assert.deepEqual(entity, post);
assert.deepEqual(entity[datastore.KEY], postKey);

datastore.delete(postKey, done);
});
Expand All @@ -107,7 +108,7 @@ describe('Datastore', function() {
datastore.get(postKey, function(err, entity) {
assert.ifError(err);

assert.deepEqual(entity.data, post);
assert.deepEqual(entity, post);

datastore.delete(postKey, done);
});
Expand All @@ -129,7 +130,7 @@ describe('Datastore', function() {
datastore.get(postKey, function(err, entity) {
assert.ifError(err);

assert.deepEqual(entity.data, data);
assert.deepEqual(entity, data);

datastore.delete(datastore.key(['Post', assignedId]), done);
});
Expand All @@ -148,7 +149,7 @@ describe('Datastore', function() {
datastore.get(postKey, function(err, entity) {
assert.ifError(err);

assert.deepEqual(entity.data, post);
assert.deepEqual(entity, post);

datastore.delete(postKey, done);
});
Expand All @@ -174,7 +175,7 @@ describe('Datastore', function() {
datastore.get(postKey, function(err, entity) {
assert.ifError(err);

assert.deepEqual(entity.data, post);
assert.deepEqual(entity, post);

datastore.delete(postKey, done);
});
Expand Down Expand Up @@ -268,8 +269,8 @@ describe('Datastore', function() {
datastore.runQuery(query, function(err, results) {
assert.ifError(err);

assert.strictEqual(results[0].data.fullName, 'Full name');
assert.deepEqual(results[0].data.linkedTo, personKey);
assert.strictEqual(results[0].fullName, 'Full name');
assert.deepEqual(results[0].linkedTo, personKey);

datastore.delete(personKey, done);
});
Expand All @@ -293,7 +294,7 @@ describe('Datastore', function() {

datastore.get(key, function(err, entity) {
assert.ifError(err);
assert.strictEqual(entity.data.year, integerValue);
assert.strictEqual(entity.year, integerValue);
done();
});
});
Expand All @@ -315,7 +316,7 @@ describe('Datastore', function() {

datastore.get(key, function(err, entity) {
assert.ifError(err);
assert.strictEqual(entity.data.nines, doubleValue);
assert.strictEqual(entity.nines, doubleValue);
done();
});
});
Expand All @@ -340,7 +341,7 @@ describe('Datastore', function() {

datastore.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data.location, geoPointValue);
assert.deepEqual(entity.location, geoPointValue);
done();
});
});
Expand Down Expand Up @@ -553,8 +554,8 @@ describe('Datastore', function() {
datastore.runQuery(q, function(err, entities) {
assert.ifError(err);

assert.strictEqual(entities[0].data.name, characters[0].name);
assert.strictEqual(entities[7].data.name, characters[3].name);
assert.strictEqual(entities[0].name, characters[0].name);
assert.strictEqual(entities[7].name, characters[3].name);

done();
});
Expand All @@ -568,12 +569,12 @@ describe('Datastore', function() {
datastore.runQuery(q, function(err, entities) {
assert.ifError(err);

assert.deepEqual(entities[0].data, {
assert.deepEqual(entities[0], {
name: 'Arya',
family: 'Stark'
});

assert.deepEqual(entities[8].data, {
assert.deepEqual(entities[8], {
name: 'Sansa',
family: 'Stark'
});
Expand All @@ -593,8 +594,8 @@ describe('Datastore', function() {
assert.ifError(err);

assert.strictEqual(entities.length, 3);
assert.strictEqual(entities[0].data.name, 'Robb');
assert.strictEqual(entities[2].data.name, 'Catelyn');
assert.strictEqual(entities[0].name, 'Robb');
assert.strictEqual(entities[2].name, 'Catelyn');

var secondQ = datastore.createQuery('Character')
.hasAncestor(ancestor)
Expand All @@ -605,8 +606,8 @@ describe('Datastore', function() {
assert.ifError(err);

assert.strictEqual(secondEntities.length, 3);
assert.strictEqual(secondEntities[0].data.name, 'Sansa');
assert.strictEqual(secondEntities[2].data.name, 'Arya');
assert.strictEqual(secondEntities[0].name, 'Sansa');
assert.strictEqual(secondEntities[2].name, 'Arya');

done();
});
Expand All @@ -632,8 +633,8 @@ describe('Datastore', function() {
assert.ifError(err);

assert.strictEqual(secondEntities.length, 4);
assert.strictEqual(secondEntities[0].data.name, 'Catelyn');
assert.strictEqual(secondEntities[3].data.name, 'Arya');
assert.strictEqual(secondEntities[0].name, 'Catelyn');
assert.strictEqual(secondEntities[3].name, 'Arya');

done();
});
Expand Down Expand Up @@ -681,7 +682,7 @@ describe('Datastore', function() {

datastore.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, obj);
assert.deepEqual(entity, obj);
done();
});
});
Expand Down Expand Up @@ -738,7 +739,7 @@ describe('Datastore', function() {
function(callback) {
datastore.get(key, function(err, entity) {
assert.ifError(err);
assert.strictEqual(entity.data.rating, 10);
assert.strictEqual(entity.rating, 10);
callback();
});
}
Expand Down
18 changes: 11 additions & 7 deletions packages/datastore/test/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('entity', function() {
entity = require('../src/entity.js');
});

describe('KEY_SYMBOL', function() {
it('should export the symbol', function() {
assert.strictEqual(entity.KEY_SYMBOL.toString(), 'Symbol(KEY)');
});
});

describe('Double', function() {
it('should store the value', function() {
var value = 8.3;
Expand Down Expand Up @@ -532,12 +538,7 @@ describe('entity', function() {
}
];

var expectedResults = [
{
key: key,
data: entityProto
}
];
var expectedResults = entityProto;

entity.keyFromKeyProto = function(key_) {
assert.strictEqual(key_, key);
Expand All @@ -549,7 +550,10 @@ describe('entity', function() {
return entityProto;
};

assert.deepEqual(entity.formatArray(results), expectedResults);
var ent = entity.formatArray(results)[0];

assert.deepEqual(ent, expectedResults);
assert.strictEqual(ent[entity.KEY_SYMBOL], key);
});
});

Expand Down
11 changes: 11 additions & 0 deletions packages/datastore/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var proxyquire = require('proxyquire');
var util = require('@google-cloud/common').util;

var fakeEntity = {
KEY_SYMBOL: Symbol('fake key symbol'),
Int: function(value) {
this.value = value;
},
Expand Down Expand Up @@ -198,6 +199,16 @@ describe('Datastore', function() {
});
});

describe('KEY', function() {
it('should expose the KEY symbol', function() {
assert.strictEqual(Datastore.KEY, fakeEntity.KEY_SYMBOL);
});

it('should also be on the prototype', function() {
assert.strictEqual(datastore.KEY, Datastore.KEY);
});
});

describe('MORE_RESULTS_AFTER_CURSOR', function() {
it('should expose a MORE_RESULTS_AFTER_CURSOR helper', function() {
assert.strictEqual(
Expand Down