@@ -93,33 +93,18 @@ Transaction.prototype.finalize = function(callback) {
93
93
} ;
94
94
95
95
/**
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
97
97
* current transaction.
98
- * @param {Array } key
98
+ * @param {Array } keys
99
99
* @param {Function } callback
100
100
*/
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 ] ;
117
104
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
+ } ;
123
108
if ( this . id ) {
124
109
req . transaction = this . id ;
125
110
}
@@ -128,112 +113,78 @@ Transaction.prototype.getAll = function(keys, callback) {
128
113
if ( err ) {
129
114
return callback ( err ) ;
130
115
}
131
-
132
- callback ( null , entity . formatArray ( resp . found ) ) ;
116
+ var response = entity . formatArray ( resp . found ) ;
117
+ callback ( null , isMultipleRequest ? response : response [ 0 ] ) ;
133
118
} ) ;
134
119
} ;
135
120
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
- } ;
155
121
156
122
/**
157
- * Inserts or upates the specified objects in the current transaction.
123
+ * Inserts or upates the specified object(s) in the current transaction.
158
124
* If a key is incomplete, its associated object is inserted and
159
125
* generated identifier is returned.
160
- * @param {Array<Array> } keys
161
- * @param {Array<Object> } objs
126
+ * @param {Array<Array> } entities
162
127
* @param {Function } callback
163
128
*/
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 ] ;
168
132
var insertIndexes = [ ] ;
133
+ var keys = entities . map ( function ( entityObject ) {
134
+ return entityObject . key ;
135
+ } ) ;
169
136
var req = {
170
137
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 : [ ] } )
175
149
} ;
176
150
if ( this . id ) {
177
151
req . transaction = this . id ;
178
152
req . mode = MODE_TRANSACTIONAL ;
179
153
}
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
- }
190
154
this . makeReq (
191
155
'commit' , req , function ( err , resp ) {
192
156
if ( err || ! resp ) {
193
157
return callback ( err ) ;
194
158
}
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 ) ;
199
161
} ) ;
200
- callback ( null , keys ) ;
162
+ callback ( null , isMultipleRequest ? keys : keys [ 0 ] ) ;
201
163
} ) ;
202
164
} ;
203
165
204
166
/**
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)
216
168
* in the current transaction.
217
169
* @param {Array<Array> } keys
218
170
* @param {Function } callback
219
171
*/
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 ;
225
176
var req = {
226
177
mode : MODE_NON_TRANSACTIONAL ,
227
178
mutation : {
228
- ' delete' : keysProto
179
+ delete : keys . map ( entity . keyToKeyProto . bind ( null , this . id ) )
229
180
}
230
181
} ;
231
182
if ( this . id ) {
232
183
req . transaction = this . id ;
233
184
req . mode = MODE_TRANSACTIONAL ;
234
185
}
235
186
this . makeReq ( 'commit' , req , function ( err , resp ) {
236
- callback && callback ( err ) ;
187
+ callback ( err ) ;
237
188
} ) ;
238
189
} ;
239
190
@@ -350,24 +301,12 @@ Dataset.prototype.get = function(key, callback) {
350
301
this . transaction . get ( key , callback ) ;
351
302
} ;
352
303
353
- Dataset . prototype . getAll = function ( keys , callback ) {
354
- this . transaction . getAll ( keys , callback ) ;
355
- } ;
356
-
357
304
Dataset . prototype . save = function ( key , obj , callback ) {
358
305
this . transaction . save ( key , obj , callback ) ;
359
306
} ;
360
307
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 ) ;
371
310
} ;
372
311
373
312
Dataset . prototype . runQuery = function ( q , callback ) {
0 commit comments