| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 |
1
1
1
1
1
1
1
29
29
29
1
1
504
1
1
3
3
1
451
1
1
1
1
1
2
1
1
1
1
1
462
462
460
458
456
456
456
1
455
3
3
452
1
454
29
425
1
453
453
1
2
1
4
4
2
2
4
1
| //
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Module dependencies.
var extend = require('extend');
var azureCommon = require('./../../common/common');
var SR = azureCommon.SR;
var validate = azureCommon.validate;
var Constants = azureCommon.Constants;
var TableConstants = Constants.TableConstants;
/**
* Creates a new TableBatch.
*
* @constructor
*/
function TableBatch() {
this.operations = [];
this.pk = null;
this.retrieve = false;
}
/**
* Removes all of the operations from the batch.
*
* @return {undefined}
*/
TableBatch.prototype.clear = function () {
this.operations = [];
};
/**
* Returns a boolean value indicating weather there are operations in the batch.
*
* @return {Boolean} True if there are operations queued up; false otherwise.
*/
TableBatch.prototype.hasOperations = function () {
return this.operations.length > 0;
};
/**
* Returns the number of operations in the batch.
*
* @return {number} The number of operations in the batch.
*/
TableBatch.prototype.size = function () {
return this.operations.length;
};
/**
* Adds a retrieve operation to the batch. Note that this must be the only operation in the batch.
*
* @param {string} partitionKey The partition key.
* @param {string} rowKey The row key.
* @param {object} [options] The request options.
* @param {string} [options.payloadFormat] The payload format to use for the request.
* @param {TableService~propertyResolver} [options.propertyResolver] The property resolver. Given the partition key, row key, property name, property value,
* and the property Edm type if given by the service, returns the Edm type of the property.
* @param {Function(entity)} [options.entityResolver] The entity resolver. Given the single entity returned by the query, returns a modified object.
* @return {undefined}
*/
TableBatch.prototype.retrieveEntity = function (partitionKey, rowKey, options) {
var entity = { PartitionKey: {_: partitionKey, $: 'Edm.String'},
RowKey: {_: rowKey, $: 'Edm.String'},
};
this.addOperation(TableConstants.Operations.RETRIEVE, entity, options);
};
/**
* Adds an insert operation to the batch.
*
* @param {object} entity The entity.
* @param {object} [options] The request options.
* @param {string} [options.echoContent] Whether or not to return the entity upon a successful insert. Inserts only, default to false.
* @param {string} [options.payloadFormat] The payload format to use for the request.
* @param {TableService~propertyResolver} [options.propertyResolver] The property resolver. Only applied if echoContent is true. Given the partition key, row key, property name,
* property value, and the property Edm type if given by the service, returns the Edm type of the property.
* @param {Function(entity)} [options.entityResolver] The entity resolver. Only applied if echoContent is true. Given the single entity returned by the insert, returns
* a modified object.
* @return {undefined}
*/
TableBatch.prototype.insertEntity = function (entity, options) {
this.addOperation(TableConstants.Operations.INSERT, entity, options);
};
/**
* Adds a delete operation to the batch.
*
* @param {object} entity The entity.
* @return {undefined}
*/
TableBatch.prototype.deleteEntity = function (entity) {
this.addOperation(TableConstants.Operations.DELETE, entity);
};
/**
* Adds a merge operation to the batch.
*
* @param {object} entity The entity.
* @return {undefined}
*/
TableBatch.prototype.mergeEntity = function (entity) {
this.addOperation(TableConstants.Operations.MERGE, entity);
};
/**
* Adds an update operation to the batch.
*
* @param {object} entity The entity.
* @return {undefined}
*/
TableBatch.prototype.updateEntity = function (entity) {
this.addOperation(TableConstants.Operations.UPDATE, entity);
};
/**
* Adds an insert or replace operation to the batch.
*
* @param {object} entity The entity.
* @return {undefined}
*/
TableBatch.prototype.insertOrReplaceEntity = function (entity) {
this.addOperation(TableConstants.Operations.INSERT_OR_REPLACE, entity);
};
/**
* Adds an insert or merge operation to the batch.
*
* @param {object} entity The entity.
* @return {undefined}
*/
TableBatch.prototype.insertOrMergeEntity = function (entity) {
this.addOperation(TableConstants.Operations.INSERT_OR_MERGE, entity);
};
/**
* Adds an operation to the batch after performing checks.
*
* @param {string} operationType The type of operation to perform. See Constants.TableConstants.Operations
* @param {object} entity The entity.
* @param {object} [options] The request options.
* @return {undefined}
*/
TableBatch.prototype.addOperation = function (operationType, entity, options) {
validate.validateArgs('addOperation', function (v) {
v.object(entity, 'entity');
v.object(entity.PartitionKey, 'entity.PartitionKey');
v.object(entity.RowKey, 'entity.RowKey');
v.stringAllowEmpty(entity.PartitionKey._, 'entity.PartitionKey._');
v.stringAllowEmpty(entity.RowKey._, 'entity.RowKey._');
});
if(this.operations.length >= 100) {
throw new Error(SR.BATCH_TOO_LARGE);
}
if (operationType === TableConstants.Operations.RETRIEVE) {
Iif(this.hasOperations()) {
throw new Error(SR.BATCH_ONE_RETRIEVE);
} else {
this.retrieve = true;
}
} else if (this.retrieve) {
throw new Error(SR.BATCH_ONE_RETRIEVE);
}
if (!this.hasOperations()) {
this.pk = entity.PartitionKey._;
} else if (entity.PartitionKey._ !== this.pk) {
throw new Error(SR.BATCH_ONE_PARTITION_KEY);
}
var copiedOptions = extend(true, {}, options);
this.operations.push({type: operationType, entity: entity, options: copiedOptions});
};
/**
* Gets an operation from the batch. Returns null if the index does not exist.
*
* @param {number} index The index in the operations array at which to remove an element.
* @return {object} The removed operation.
*/
TableBatch.prototype.getOperation = function (index) {
return this.operations[index];
};
/**
* Removes an operation from the batch. Returns null if the index does not exist.
*
* @param {number} index The index in the operations array at which to remove an element.
* @return {object} The removed operation.
*/
TableBatch.prototype.removeOperation = function (index) {
var operation = this.operations.splice(index, 1)[0];
// if the array is empty, unlock the partition key
if (!this.hasOperations()) {
this.pk = null;
this.retrieve = false;
}
return operation;
};
module.exports = TableBatch;
|