Skip to content

Commit 2c9803c

Browse files
googleapis#41 : _action_All methods removed.
1 parent 994ff61 commit 2c9803c

File tree

4 files changed

+90
-144
lines changed

4 files changed

+90
-144
lines changed

README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -97,45 +97,49 @@ TODO
9797
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.
9898

9999
~~~~ js
100-
ds.get(['Company', 123], function(err, entities) {
100+
ds.get(['Company', 123], function(err, entity) {});
101101

102-
});
103102
// alternatively, you can retrieve multiple entities at once.
104-
ds.getAll([key1, key2, ...], function(err, entities) {
105-
106-
});
103+
ds.get([
104+
['Company', 123],
105+
['Product', 'Computer']
106+
], function(err, entities) {});
107107
~~~~
108108

109109
You can insert arbitrary objects by providing an incomplete key during saving. If the key is not incomplete, the existing entity is updated or inserted with the provided key.
110110

111111
To learn more about keys and incomplete keys, skip to the Keys section.
112112

113113
~~~~ js
114-
ds.save(['Company', null], obj, function(err, key) {
114+
ds.save({
115+
key: ['Company', null],
116+
data: {/*...*/}
117+
}, function(err, key) {
115118
// First arg is an incomplete key for Company kind.
116119
// console.log(key) will output ['Company', 599900452312].
117120
});
118121
// alternatively, you can save multiple entities at once.
119-
ds.saveAll([key1, key2, key3], [obj1, obj2, obj3], function(err, keys) {
120-
// if key1 was incomplete, keys[0] will return the generated key.
122+
ds.save([
123+
{ key: ['Company', 123], data: {/*...*/} },
124+
{ key: ['Product', 'Computer'], data: {/*...*/} }
125+
], function(err, keys) {
126+
// if the first key was incomplete, keys[0] will return the generated key.
121127
});
122128
~~~~
123129

124130
Deletion requires the key of the entity to be deleted.
125131

126132
~~~~ js
127-
ds.del(['Company', 599900452312], function(err) {
133+
ds.delete(['Company', 599900452312], function(err) {});
128134

129-
});
130135
// alternatively, you can delete multiple entities of different
131136
// kinds at once.
132-
ds.delAll([
137+
ds.delete([
133138
['Company', 599900452312],
134139
['Company', 599900452315],
135140
['Office', 'mtv'],
136-
['Company', 123, 'Employee', 'jbd']], function(err) {
137-
138-
});
141+
['Company', 123, 'Employee', 'jbd']
142+
], function(err) {});
139143
~~~~
140144

141145
#### Querying

lib/datastore/index.js

+41-102
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,18 @@ Transaction.prototype.finalize = function(callback) {
9393
};
9494

9595
/**
96-
* Get retrieves the objects identified with the specified key in the
96+
* Get retrieves the objects identified with the specified key(s) in the
9797
* current transaction.
98-
* @param {Array} key
98+
* @param {Array} keys
9999
* @param {Function} callback
100100
*/
101-
Transaction.prototype.get = function(key, callback) {
102-
this.getAll([key], function(err, results) {
103-
if (err) {
104-
return callback(err);
105-
}
106-
return callback(null, results[0]);
107-
});
108-
};
109-
110-
/**
111-
* Gets all objects identified with the specified list of keys
112-
* in the current transaction.
113-
* @param {Array<Array>} keys
114-
* @param {Function} callback
115-
*/
116-
Transaction.prototype.getAll = function(keys, callback) {
101+
Transaction.prototype.get = function(keys, callback) {
102+
var isMultipleRequest = Array.isArray(keys[0]);
103+
keys = isMultipleRequest ? keys : [keys];
117104
callback = callback || util.noop;
118-
keysProto = [];
119-
keys.forEach(function(k) {
120-
keysProto.push(entity.keyToKeyProto(this.id, k));
121-
});
122-
var req = { keys: keysProto };
105+
var req = {
106+
keys: keys.map(entity.keyToKeyProto.bind(null, this.id))
107+
};
123108
if (this.id) {
124109
req.transaction = this.id;
125110
}
@@ -128,112 +113,78 @@ Transaction.prototype.getAll = function(keys, callback) {
128113
if (err) {
129114
return callback(err);
130115
}
131-
132-
callback(null, entity.formatArray(resp.found));
116+
var response = entity.formatArray(resp.found);
117+
callback(null, isMultipleRequest ? response : response[0]);
133118
});
134119
};
135120

136-
/**
137-
* Inserts or updates the specified the object in the current
138-
* transaction. If the provided key is incomplete, inserts the object
139-
* and returns the generated identifier.
140-
* @param {Array} key
141-
* @param {Object} obj
142-
* @param {Function} callback
143-
*/
144-
Transaction.prototype.save = function(key, obj, callback) {
145-
this.saveAll([key], [obj], function(err, keys) {
146-
if (err || !keys) {
147-
return callback(err);
148-
}
149-
if (keys[0]) {
150-
return callback(err, keys[0]);
151-
}
152-
callback();
153-
});
154-
};
155121

156122
/**
157-
* Inserts or upates the specified objects in the current transaction.
123+
* Inserts or upates the specified object(s) in the current transaction.
158124
* If a key is incomplete, its associated object is inserted and
159125
* generated identifier is returned.
160-
* @param {Array<Array>} keys
161-
* @param {Array<Object>} objs
126+
* @param {Array<Array>} entities
162127
* @param {Function} callback
163128
*/
164-
Transaction.prototype.saveAll = function(keys, objs, callback) {
165-
if (keys.length != objs.length) {
166-
throw new Error('The length of the keys don\'t match the length of the objects');
167-
}
129+
Transaction.prototype.save = function(entities, callback) {
130+
var isMultipleRequest = Array.isArray(entities);
131+
entities = isMultipleRequest ? entities : [entities];
168132
var insertIndexes = [];
133+
var keys = entities.map(function(entityObject) {
134+
return entityObject.key;
135+
});
169136
var req = {
170137
mode: MODE_NON_TRANSACTIONAL,
171-
mutation: {
172-
upsert: [],
173-
insertAutoId: []
174-
}
138+
mutation: entities.reduce(function(acc, entityObject, index) {
139+
var ent = entity.entityToEntityProto(entityObject.data);
140+
ent.key = entity.keyToKeyProto(this.datasetId, entityObject.key);
141+
if (entity.isKeyComplete(entityObject.key)) {
142+
acc.upsert.push(ent);
143+
} else {
144+
insertIndexes.push(index);
145+
acc.insertAutoId.push(ent);
146+
}
147+
return acc;
148+
}, { upsert: [], insertAutoId: [] })
175149
};
176150
if (this.id) {
177151
req.transaction = this.id;
178152
req.mode = MODE_TRANSACTIONAL;
179153
}
180-
for (var i = 0; i < keys.length; i++) {
181-
var e = entity.entityToEntityProto(objs[i]);
182-
e.key = entity.keyToKeyProto(this.datasetId, keys[i]);
183-
if (entity.isKeyComplete(keys[i])) {
184-
req.mutation.upsert.push(e);
185-
} else {
186-
insertIndexes.push(i);
187-
req.mutation.insertAutoId.push(e);
188-
}
189-
}
190154
this.makeReq(
191155
'commit', req, function(err, resp) {
192156
if (err || !resp) {
193157
return callback(err);
194158
}
195-
resp.mutationResult.insertAutoIdKeys = resp.mutationResult.insertAutoIdKeys || [];
196-
var i = 0;
197-
resp.mutationResult.insertAutoIdKeys.forEach(function(item) {
198-
keys[insertIndexes[i++]] = entity.keyFromKeyProto(item);
159+
(resp.mutationResult.insertAutoIdKeys || []).forEach(function(key, index) {
160+
keys[insertIndexes[index]] = entity.keyFromKeyProto(key);
199161
});
200-
callback(null, keys);
162+
callback(null, isMultipleRequest ? keys : keys[0]);
201163
});
202164
};
203165

204166
/**
205-
* Deletes the entitiy identified with the specified key in the
206-
* current transaction.
207-
* @param {Array} key
208-
* @param {Function} callback
209-
*/
210-
Transaction.prototype.del = function(key, callback) {
211-
this.delAll([key], callback);
212-
};
213-
214-
/**
215-
* Deletes all entities identified with the specified list of keys
167+
* Deletes all entities identified with the specified list of key(s)
216168
* in the current transaction.
217169
* @param {Array<Array>} keys
218170
* @param {Function} callback
219171
*/
220-
Transaction.prototype.delAll = function(keys, callback) {
221-
keysProto = [];
222-
keys.forEach(function(k) {
223-
keysProto.push(entity.keyToKeyProto(this.id, k));
224-
});
172+
Transaction.prototype.delete = function(keys, callback) {
173+
var isMultipleRequest = Array.isArray(keys[0]);
174+
keys = isMultipleRequest ? keys : [keys];
175+
callback = callback || util.noop;
225176
var req = {
226177
mode: MODE_NON_TRANSACTIONAL,
227178
mutation: {
228-
'delete': keysProto
179+
delete: keys.map(entity.keyToKeyProto.bind(null, this.id))
229180
}
230181
};
231182
if (this.id) {
232183
req.transaction = this.id;
233184
req.mode = MODE_TRANSACTIONAL;
234185
}
235186
this.makeReq('commit', req, function(err, resp) {
236-
callback && callback(err);
187+
callback(err);
237188
});
238189
};
239190

@@ -350,24 +301,12 @@ Dataset.prototype.get = function(key, callback) {
350301
this.transaction.get(key, callback);
351302
};
352303

353-
Dataset.prototype.getAll = function(keys, callback) {
354-
this.transaction.getAll(keys, callback);
355-
};
356-
357304
Dataset.prototype.save = function(key, obj, callback) {
358305
this.transaction.save(key, obj, callback);
359306
};
360307

361-
Dataset.prototype.saveAll = function(keys, objs, callback) {
362-
this.transaction.saveAll(keys, objs, callback);
363-
};
364-
365-
Dataset.prototype.del = function(key, callback) {
366-
this.transaction.del(key, callback);
367-
};
368-
369-
Dataset.prototype.delAll = function(keys, callback) {
370-
this.transaction.delAll(keys, callback);
308+
Dataset.prototype.delete = function(key, callback) {
309+
this.transaction.delete(key, callback);
371310
};
372311

373312
Dataset.prototype.runQuery = function(q, callback) {

regression/datastore.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ describe('datastore', function() {
3636
};
3737
var postKeyName = 'post1';
3838

39-
ds.save(['Post', postKeyName], post, function(err, key) {
39+
ds.save({ key: ['Post', postKeyName], data: post }, function(err, key) {
4040
if (err) return done(err);
4141
assert.equal(key[1], postKeyName);
4242
ds.get(['Post', postKeyName], function(err, entity) {
4343
if (err) return done(err);
4444
assert.deepEqual(entity.data, post);
45-
ds.del(['Post', postKeyName], function(err) {
45+
ds.delete(['Post', postKeyName], function(err) {
4646
if (err) return done(err);
4747
done();
4848
});
@@ -62,13 +62,13 @@ describe('datastore', function() {
6262
};
6363
var postKeyId = '123456789';
6464

65-
ds.save(['Post', postKeyId], post, function(err, key) {
65+
ds.save({ key: ['Post', postKeyId], data: post }, function(err, key) {
6666
if (err) return done(err);
6767
assert.equal(key[1], postKeyId);
6868
ds.get(['Post', postKeyId], function(err, entity) {
6969
if (err) return done(err);
7070
assert.deepEqual(entity.data, post);
71-
ds.del(['Post', postKeyId], function(err) {
71+
ds.delete(['Post', postKeyId], function(err) {
7272
if (err) return done(err);
7373
done();
7474
});
@@ -86,14 +86,14 @@ describe('datastore', function() {
8686
wordCount: 400,
8787
rating: 5.0,
8888
};
89-
ds.save(['Post', null], post, function(err, key) {
89+
ds.save({ key: ['Post', null], data: post }, function(err, key) {
9090
if (err) return done(err);
9191
assert(key[1]);
9292
var assignedId = key[1];
9393
ds.get(['Post', assignedId], function(err, entity) {
9494
if (err) return done(err);
9595
assert.deepEqual(entity.data, post);
96-
ds.del(['Post', assignedId], function(err) {
96+
ds.delete(['Post', assignedId], function(err) {
9797
if (err) return done(err);
9898
done();
9999
});
@@ -121,15 +121,18 @@ describe('datastore', function() {
121121
rating: 4.5,
122122
};
123123
var key = ['Post', null];
124-
ds.saveAll([key, key], [post1, post2], function(err, keys) {
124+
ds.save([
125+
{ key: key, data: post1 },
126+
{ key: key, data: post2 }
127+
], function(err, keys) {
125128
if (err) return done(err);
126129
assert.equal(keys.length,2);
127130
var firstKey = ['Post', keys[0][1]],
128131
secondKey = ['Post', keys[1][1]];
129-
ds.getAll([firstKey, secondKey], function(err, entities) {
132+
ds.get([firstKey, secondKey], function(err, entities) {
130133
if (err) return done(err);
131134
assert.equal(entities.length, 2);
132-
ds.delAll([firstKey, secondKey], function(err) {
135+
ds.delete([firstKey, secondKey], function(err) {
133136
if (err) return done(err);
134137
done();
135138
});
@@ -196,7 +199,12 @@ describe('datastore', function() {
196199

197200
before(function(done) {
198201

199-
ds.saveAll(keys, characters, function(err, keys) {
202+
ds.save(keys.map(function(key, index) {
203+
return {
204+
key: key,
205+
data: characters[index]
206+
};
207+
}), function(err, keys) {
200208
if (err) return done(err);
201209
done();
202210
});
@@ -343,7 +351,7 @@ describe('datastore', function() {
343351

344352
after(function(done) {
345353

346-
ds.delAll(keys, function(err) {
354+
ds.delete(keys, function(err) {
347355
if (err) return done(err);
348356
done();
349357
});
@@ -366,7 +374,7 @@ describe('datastore', function() {
366374
tDone();
367375
return;
368376
} else {
369-
ds.save(key, obj, function(err, keyRes) {
377+
ds.save({ key: key, data: obj }, function(err, keyRes) {
370378
if (err) console.log(err);
371379
tDone();
372380
return;
@@ -378,7 +386,7 @@ describe('datastore', function() {
378386
ds.get(key, function(err, entity) {
379387
if (err) return done(err);
380388
assert.deepEqual(entity.data, obj);
381-
ds.del(entity.key, function(err) {
389+
ds.delete(entity.key, function(err) {
382390
if (err) return done(err);
383391
done();
384392
})

0 commit comments

Comments
 (0)