Skip to content

Commit 86ddf2c

Browse files
stephenpluspluscallmehiphop
authored andcommitted
datastore: use a Symbol to get the key from an entity (#1682)
1 parent 9ec415f commit 86ddf2c

File tree

7 files changed

+71
-40
lines changed

7 files changed

+71
-40
lines changed

src/entity.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ var InvalidKeyError = createErrorClass('InvalidKey', function(opts) {
3737
this.message = errorMessages[opts.code];
3838
});
3939

40+
/**
41+
* A symbol to access the Key object from an entity object.
42+
*
43+
* @type {symbol}
44+
*/
45+
entity.KEY_SYMBOL = Symbol('KEY');
46+
4047
/**
4148
* Build a Datastore Double object.
4249
*
@@ -405,10 +412,9 @@ entity.entityToEntityProto = entityToEntityProto;
405412
*/
406413
function formatArray(results) {
407414
return results.map(function(result) {
408-
return {
409-
key: entity.keyFromKeyProto(result.entity.key),
410-
data: entity.entityFromEntityProto(result.entity)
411-
};
415+
var ent = entity.entityFromEntityProto(result.entity);
416+
ent[entity.KEY_SYMBOL] = entity.keyFromKeyProto(result.entity.key);
417+
return ent;
412418
});
413419
}
414420

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,13 @@ Datastore.prototype.int = Datastore.int = function(value) {
378378
return new entity.Int(value);
379379
};
380380

381+
/**
382+
* Access the Key from an Entity object.
383+
*
384+
* @type {symbol}
385+
*/
386+
Datastore.prototype.KEY = Datastore.KEY = entity.KEY_SYMBOL;
387+
381388
/**
382389
* This is one of three values which may be returned from
383390
* {module:datastore#runQuery}, {module:transaction#runQuery}, and

src/query.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Query.prototype.offset = function(n) {
312312
* // unnecessary processing and API requests.
313313
* //-
314314
* query.run()
315-
* .on('data', function (entity) {
315+
* .on('data', function(entity) {
316316
* this.end();
317317
* });
318318
*
@@ -323,8 +323,9 @@ Query.prototype.offset = function(n) {
323323
* query.select('__key__');
324324
*
325325
* query.run(function(err, entities) {
326-
* // entities[].key = Key object
327-
* // entities[].data = Empty object
326+
* var keys = entities.map(function(entity) {
327+
* return entity[datastore.KEY];
328+
* });
328329
* });
329330
*/
330331
Query.prototype.run = function() {

src/request.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
299299
* // Error handling omitted.
300300
* }
301301
*
302-
* entity.data.newValue = true;
302+
* entity.newValue = true;
303303
* datastore.save(entity, function(err) {});
304304
* });
305305
*/
@@ -489,8 +489,9 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
489489
* var keysOnlyQuery = datastore.createQuery('Lion').select('__key__');
490490
*
491491
* datastore.runQuery(keysOnlyQuery, function(err, entities) {
492-
* // entities[].key = Key object
493-
* // entities[].data = Empty object
492+
* var keys = entities.map(function(entity) {
493+
* return entity[datastore.KEY];
494+
* });
494495
* });
495496
*/
496497
DatastoreRequest.prototype.runQuery = function(query, options, callback) {

system-test/datastore.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Datastore', function() {
4848
}
4949

5050
var keys = entities.map(function(entity) {
51-
return entity.key;
51+
return entity[datastore.KEY];
5252
});
5353

5454
datastore.delete(keys, callback);
@@ -91,7 +91,8 @@ describe('Datastore', function() {
9191
datastore.get(postKey, function(err, entity) {
9292
assert.ifError(err);
9393

94-
assert.deepEqual(entity.data, post);
94+
assert.deepEqual(entity, post);
95+
assert.deepEqual(entity[datastore.KEY], postKey);
9596

9697
datastore.delete(postKey, done);
9798
});
@@ -107,7 +108,7 @@ describe('Datastore', function() {
107108
datastore.get(postKey, function(err, entity) {
108109
assert.ifError(err);
109110

110-
assert.deepEqual(entity.data, post);
111+
assert.deepEqual(entity, post);
111112

112113
datastore.delete(postKey, done);
113114
});
@@ -129,7 +130,7 @@ describe('Datastore', function() {
129130
datastore.get(postKey, function(err, entity) {
130131
assert.ifError(err);
131132

132-
assert.deepEqual(entity.data, data);
133+
assert.deepEqual(entity, data);
133134

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

151-
assert.deepEqual(entity.data, post);
152+
assert.deepEqual(entity, post);
152153

153154
datastore.delete(postKey, done);
154155
});
@@ -174,7 +175,7 @@ describe('Datastore', function() {
174175
datastore.get(postKey, function(err, entity) {
175176
assert.ifError(err);
176177

177-
assert.deepEqual(entity.data, post);
178+
assert.deepEqual(entity, post);
178179

179180
datastore.delete(postKey, done);
180181
});
@@ -268,8 +269,8 @@ describe('Datastore', function() {
268269
datastore.runQuery(query, function(err, results) {
269270
assert.ifError(err);
270271

271-
assert.strictEqual(results[0].data.fullName, 'Full name');
272-
assert.deepEqual(results[0].data.linkedTo, personKey);
272+
assert.strictEqual(results[0].fullName, 'Full name');
273+
assert.deepEqual(results[0].linkedTo, personKey);
273274

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

294295
datastore.get(key, function(err, entity) {
295296
assert.ifError(err);
296-
assert.strictEqual(entity.data.year, integerValue);
297+
assert.strictEqual(entity.year, integerValue);
297298
done();
298299
});
299300
});
@@ -315,7 +316,7 @@ describe('Datastore', function() {
315316

316317
datastore.get(key, function(err, entity) {
317318
assert.ifError(err);
318-
assert.strictEqual(entity.data.nines, doubleValue);
319+
assert.strictEqual(entity.nines, doubleValue);
319320
done();
320321
});
321322
});
@@ -340,7 +341,7 @@ describe('Datastore', function() {
340341

341342
datastore.get(key, function(err, entity) {
342343
assert.ifError(err);
343-
assert.deepEqual(entity.data.location, geoPointValue);
344+
assert.deepEqual(entity.location, geoPointValue);
344345
done();
345346
});
346347
});
@@ -553,8 +554,8 @@ describe('Datastore', function() {
553554
datastore.runQuery(q, function(err, entities) {
554555
assert.ifError(err);
555556

556-
assert.strictEqual(entities[0].data.name, characters[0].name);
557-
assert.strictEqual(entities[7].data.name, characters[3].name);
557+
assert.strictEqual(entities[0].name, characters[0].name);
558+
assert.strictEqual(entities[7].name, characters[3].name);
558559

559560
done();
560561
});
@@ -568,12 +569,12 @@ describe('Datastore', function() {
568569
datastore.runQuery(q, function(err, entities) {
569570
assert.ifError(err);
570571

571-
assert.deepEqual(entities[0].data, {
572+
assert.deepEqual(entities[0], {
572573
name: 'Arya',
573574
family: 'Stark'
574575
});
575576

576-
assert.deepEqual(entities[8].data, {
577+
assert.deepEqual(entities[8], {
577578
name: 'Sansa',
578579
family: 'Stark'
579580
});
@@ -593,8 +594,8 @@ describe('Datastore', function() {
593594
assert.ifError(err);
594595

595596
assert.strictEqual(entities.length, 3);
596-
assert.strictEqual(entities[0].data.name, 'Robb');
597-
assert.strictEqual(entities[2].data.name, 'Catelyn');
597+
assert.strictEqual(entities[0].name, 'Robb');
598+
assert.strictEqual(entities[2].name, 'Catelyn');
598599

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

607608
assert.strictEqual(secondEntities.length, 3);
608-
assert.strictEqual(secondEntities[0].data.name, 'Sansa');
609-
assert.strictEqual(secondEntities[2].data.name, 'Arya');
609+
assert.strictEqual(secondEntities[0].name, 'Sansa');
610+
assert.strictEqual(secondEntities[2].name, 'Arya');
610611

611612
done();
612613
});
@@ -632,8 +633,8 @@ describe('Datastore', function() {
632633
assert.ifError(err);
633634

634635
assert.strictEqual(secondEntities.length, 4);
635-
assert.strictEqual(secondEntities[0].data.name, 'Catelyn');
636-
assert.strictEqual(secondEntities[3].data.name, 'Arya');
636+
assert.strictEqual(secondEntities[0].name, 'Catelyn');
637+
assert.strictEqual(secondEntities[3].name, 'Arya');
637638

638639
done();
639640
});
@@ -681,7 +682,7 @@ describe('Datastore', function() {
681682

682683
datastore.get(key, function(err, entity) {
683684
assert.ifError(err);
684-
assert.deepEqual(entity.data, obj);
685+
assert.deepEqual(entity, obj);
685686
done();
686687
});
687688
});
@@ -738,7 +739,7 @@ describe('Datastore', function() {
738739
function(callback) {
739740
datastore.get(key, function(err, entity) {
740741
assert.ifError(err);
741-
assert.strictEqual(entity.data.rating, 10);
742+
assert.strictEqual(entity.rating, 10);
742743
callback();
743744
});
744745
}

test/entity.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ describe('entity', function() {
3333
entity = require('../src/entity.js');
3434
});
3535

36+
describe('KEY_SYMBOL', function() {
37+
it('should export the symbol', function() {
38+
assert.strictEqual(entity.KEY_SYMBOL.toString(), 'Symbol(KEY)');
39+
});
40+
});
41+
3642
describe('Double', function() {
3743
it('should store the value', function() {
3844
var value = 8.3;
@@ -532,12 +538,7 @@ describe('entity', function() {
532538
}
533539
];
534540

535-
var expectedResults = [
536-
{
537-
key: key,
538-
data: entityProto
539-
}
540-
];
541+
var expectedResults = entityProto;
541542

542543
entity.keyFromKeyProto = function(key_) {
543544
assert.strictEqual(key_, key);
@@ -549,7 +550,10 @@ describe('entity', function() {
549550
return entityProto;
550551
};
551552

552-
assert.deepEqual(entity.formatArray(results), expectedResults);
553+
var ent = entity.formatArray(results)[0];
554+
555+
assert.deepEqual(ent, expectedResults);
556+
assert.strictEqual(ent[entity.KEY_SYMBOL], key);
553557
});
554558
});
555559

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var proxyquire = require('proxyquire');
2222
var util = require('@google-cloud/common').util;
2323

2424
var fakeEntity = {
25+
KEY_SYMBOL: Symbol('fake key symbol'),
2526
Int: function(value) {
2627
this.value = value;
2728
},
@@ -198,6 +199,16 @@ describe('Datastore', function() {
198199
});
199200
});
200201

202+
describe('KEY', function() {
203+
it('should expose the KEY symbol', function() {
204+
assert.strictEqual(Datastore.KEY, fakeEntity.KEY_SYMBOL);
205+
});
206+
207+
it('should also be on the prototype', function() {
208+
assert.strictEqual(datastore.KEY, Datastore.KEY);
209+
});
210+
});
211+
201212
describe('MORE_RESULTS_AFTER_CURSOR', function() {
202213
it('should expose a MORE_RESULTS_AFTER_CURSOR helper', function() {
203214
assert.strictEqual(

0 commit comments

Comments
 (0)