Skip to content

Commit e8b17a2

Browse files
committed
Merge pull request #387 from ryanseys/datastore-method
Allow explicit set of method on Dataset#save
2 parents b52b28b + 30bd7be commit e8b17a2

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

lib/datastore/request.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ DatastoreRequest.prototype.get = function(keys, callback) {
166166
*
167167
* @param {object|object[]} entities - Datastore key object(s).
168168
* @param {Key} entities.key - Datastore key object.
169+
* @param {string=} entities.method - Optional method to explicity use for save.
170+
* The choices include 'insert', 'update', 'upsert' and 'auto_insert_id'.
169171
* @param {object|object[]} entities.data - Data to save with the provided key.
170172
* If you provide an array of objects, you must use the explicit syntax:
171173
* `name` for the name of the property and `value` for its value. You may
@@ -209,7 +211,7 @@ DatastoreRequest.prototype.get = function(keys, callback) {
209211
* //-
210212
* var companyKey = dataset.key(['Company', 123]);
211213
* var productKey = dataset.key(['Product', 'Computer']);
212-
*
214+
*
213215
* dataset.save([
214216
* {
215217
* key: companyKey,
@@ -224,6 +226,21 @@ DatastoreRequest.prototype.get = function(keys, callback) {
224226
* }
225227
* }
226228
* ], function(err) {});
229+
*
230+
* //-
231+
* // Explicitly attempt to 'insert' a specific entity.
232+
* //-
233+
* var userKey = dataset.key(['User', 'chilts']);
234+
*
235+
* dataset.save([
236+
* {
237+
* key: userKey,
238+
* method: 'insert', // force the method to 'insert'
239+
* data: {
240+
* fullName: 'Andrew Chilton'
241+
* }
242+
* }
243+
* ], function(err) {});
227244
*/
228245
DatastoreRequest.prototype.save = function(entities, callback) {
229246
var isMultipleRequest = Array.isArray(entities);
@@ -234,6 +251,8 @@ DatastoreRequest.prototype.save = function(entities, callback) {
234251
var req = {
235252
mutation: entities.reduce(function(acc, entityObject, index) {
236253
var ent = {};
254+
var method = entityObject.method;
255+
delete entityObject.method;
237256

238257
if (Array.isArray(entityObject.data)) {
239258
ent.property = entityObject.data.map(function(data) {
@@ -252,15 +271,35 @@ DatastoreRequest.prototype.save = function(entities, callback) {
252271

253272
ent.key = entity.keyToKeyProto(entityObject.key);
254273

255-
if (entity.isKeyComplete(entityObject.key)) {
256-
acc.upsert.push(ent);
274+
if (method) {
275+
switch (method) {
276+
case 'insert':
277+
acc.insert.push(ent);
278+
break;
279+
case 'update':
280+
acc.update.push(ent);
281+
break;
282+
case 'upsert':
283+
acc.upsert.push(ent);
284+
break;
285+
case 'insert_auto_id':
286+
insertIndexes.push(index);
287+
acc.insert_auto_id.push(ent);
288+
break;
289+
}
257290
} else {
258-
insertIndexes.push(index);
259-
acc.insert_auto_id.push(ent);
291+
if (entity.isKeyComplete(entityObject.key)) {
292+
acc.upsert.push(ent);
293+
} else {
294+
insertIndexes.push(index);
295+
acc.insert_auto_id.push(ent);
296+
}
260297
}
261298

262299
return acc;
263300
}, {
301+
insert: [],
302+
update: [],
264303
upsert: [],
265304
insert_auto_id: []
266305
})

regression/datastore.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ describe('datastore', function() {
119119
});
120120
});
121121

122+
it('should fail explicitly set second insert on save', function(done) {
123+
var postKey = ds.key('Post');
124+
125+
ds.save({ key: postKey, data: post }, function(err) {
126+
assert.ifError(err);
127+
128+
// The key's path should now be complete.
129+
assert(postKey.path[1]);
130+
131+
ds.save({ key: postKey, method: 'insert', data: post }, function(err) {
132+
assert.notEqual(err, null); // should fail insert
133+
134+
ds.get(postKey, function(err, entity) {
135+
assert.ifError(err);
136+
137+
assert.deepEqual(entity.data, post);
138+
139+
ds.delete(postKey, done);
140+
});
141+
});
142+
});
143+
});
144+
145+
it('should fail explicitly set first update on save', function(done) {
146+
var postKey = ds.key('Post');
147+
148+
ds.save({ key: postKey, method: 'update', data: post }, function(err) {
149+
assert.notEqual(err, null);
150+
done();
151+
});
152+
});
153+
122154
it('should save/get/delete multiple entities at once', function(done) {
123155
var post2 = {
124156
title: 'How to make the perfect homemade pasta',

test/datastore/request.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,25 @@ describe('Request', function() {
205205
], done);
206206
});
207207

208+
it('should save with specific method', function(done) {
209+
request.makeReq_ = function(method, req, callback) {
210+
assert.equal(method, 'commit');
211+
assert.equal(req.mutation.insert.length, 1);
212+
assert.equal(req.mutation.update.length, 1);
213+
assert.equal(req.mutation.insert[0].property[0].name, 'k');
214+
assert.equal(
215+
req.mutation.insert[0].property[0].value.string_value, 'v');
216+
assert.equal(req.mutation.update[0].property[0].name, 'k2');
217+
assert.equal(
218+
req.mutation.update[0].property[0].value.string_value, 'v2');
219+
callback();
220+
};
221+
request.save([
222+
{ key: key, method: 'insert', data: { k: 'v' } },
223+
{ key: key, method: 'update', data: { k2: 'v2' } }
224+
], done);
225+
});
226+
208227
it('should not set an indexed value by default', function(done) {
209228
request.makeReq_ = function(method, req) {
210229
var property = req.mutation.upsert[0].property[0];

0 commit comments

Comments
 (0)