@@ -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 */
228245DatastoreRequest . 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 } )
0 commit comments