| 1 |
define([ |
| 2 |
'dojo/_base/declare', |
| 3 |
'dojo/Stateful', |
| 4 |
'dojo/request', |
| 5 |
'dojo/store/util/QueryResults', |
| 6 |
'dojo/store/util/SimpleQueryEngine', |
| 7 |
'dojo/_base/lang', |
| 8 |
'dojo/_base/array', |
| 9 |
'dojo/errors/RequestError', |
| 10 |
'dojo/Deferred', |
| 11 |
'../encoding/digests/SHA256', |
| 12 |
'../encoding/digests/_base', |
| 13 |
'dojo/has!host-node?dojo/node!url' |
| 14 |
], function ( |
| 15 |
declare, |
| 16 |
Stateful, |
| 17 |
request, |
| 18 |
createQueryResults, |
| 19 |
SimpleQueryEngine, |
| 20 |
lang, |
| 21 |
arrayUtil, |
| 22 |
RequestError, |
| 23 |
Deferred, |
| 24 |
hash, |
| 25 |
digests, |
| 26 |
nodeUrl |
| 27 |
) { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
function hmac(key, data, outputType) { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
outputType = outputType || digests.outputTypes.String; |
| 56 |
return hash._hmac(data, key, outputType); |
| 57 |
} |
| 58 |
|
| 59 |
function getSigningKey(shortDate, secretKey, region) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
this._signingDate = shortDate; |
| 65 |
var kDate = hmac('AWS4' + secretKey, shortDate); |
| 66 |
var kRegion = hmac(kDate, region); |
| 67 |
var kService = hmac(kRegion, 'dynamodb'); |
| 68 |
return hmac(kService, 'aws4_request'); |
| 69 |
} |
| 70 |
|
| 71 |
function toUTCString(date) { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
function twoDigit(value) { |
| 77 |
value = String(value); |
| 78 |
return value.length === 1 ? '0' + value : value; |
| 79 |
} |
| 80 |
var year = String(date.getUTCFullYear()); |
| 81 |
var month = twoDigit(date.getUTCMonth() + 1); |
| 82 |
var day = twoDigit(date.getUTCDate()); |
| 83 |
var hour = twoDigit(date.getUTCHours()); |
| 84 |
var minute = twoDigit(date.getUTCMinutes()); |
| 85 |
var second = twoDigit(date.getUTCSeconds()); |
| 86 |
return year + month + day + 'T' + hour + minute + second + 'Z'; |
| 87 |
} |
| 88 |
|
| 89 |
function getHostName(url) { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (typeof document !== 'undefined') { |
| 95 |
var a = document.createElement('a'); |
| 96 |
a.href = url; |
| 97 |
return a.hostname; |
| 98 |
} |
| 99 |
else { |
| 100 |
url = nodeUrl.parse(url); |
| 101 |
return url.hostname; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
function getDynamoType(value) { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (value == null) { |
| 111 |
|
| 112 |
return 'NULL'; |
| 113 |
} |
| 114 |
|
| 115 |
var type = typeof value; |
| 116 |
|
| 117 |
if (type === 'string') { |
| 118 |
return 'S'; |
| 119 |
} |
| 120 |
|
| 121 |
if (type === 'boolean') { |
| 122 |
return 'BOOL'; |
| 123 |
} |
| 124 |
|
| 125 |
if (type === 'number') { |
| 126 |
return 'N'; |
| 127 |
} |
| 128 |
|
| 129 |
if (value instanceof Array) { |
| 130 |
|
| 131 |
return 'L'; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
return 'M'; |
| 136 |
} |
| 137 |
|
| 138 |
function getDynamoValue(value) { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
var type = getDynamoType(value); |
| 144 |
var dynamoValue = {}; |
| 145 |
var returnValue; |
| 146 |
|
| 147 |
switch (type) { |
| 148 |
case 'BOOL': |
| 149 |
case 'S': |
| 150 |
returnValue = value; |
| 151 |
break; |
| 152 |
case 'N': |
| 153 |
returnValue = String(value); |
| 154 |
break; |
| 155 |
case 'NULL': |
| 156 |
returnValue = true; |
| 157 |
break; |
| 158 |
case 'M': |
| 159 |
returnValue = {}; |
| 160 |
for (var key in value) { |
| 161 |
returnValue[key] = getDynamoValue(value[key]); |
| 162 |
} |
| 163 |
break; |
| 164 |
case 'L': |
| 165 |
returnValue = []; |
| 166 |
for (var i = 0; i < value.length; i++) { |
| 167 |
returnValue[i] = getDynamoValue(value[i]); |
| 168 |
} |
| 169 |
break; |
| 170 |
default: |
| 171 |
throw new Error('Unknown Dynamo type: ' + type); |
| 172 |
} |
| 173 |
|
| 174 |
dynamoValue[type] = returnValue; |
| 175 |
return dynamoValue; |
| 176 |
} |
| 177 |
|
| 178 |
function getNativeValue(dynamoValue) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
var type; |
| 184 |
var value; |
| 185 |
|
| 186 |
|
| 187 |
for (type in dynamoValue) { |
| 188 |
value = dynamoValue[type]; |
| 189 |
} |
| 190 |
|
| 191 |
function parse(value) { |
| 192 |
if (type === 'N') { |
| 193 |
return Number(value); |
| 194 |
} |
| 195 |
if (type === 'NULL') { |
| 196 |
return null; |
| 197 |
} |
| 198 |
if (type === 'L') { |
| 199 |
return arrayUtil.map(value, function (value) { |
| 200 |
return getNativeValue(value); |
| 201 |
}); |
| 202 |
} |
| 203 |
if (type === 'M') { |
| 204 |
var returnValue = {}; |
| 205 |
for (var key in value) { |
| 206 |
returnValue[key] = getNativeValue(value[key]); |
| 207 |
} |
| 208 |
return returnValue; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
return value; |
| 213 |
} |
| 214 |
|
| 215 |
if (type.charAt(1) === 'S') { |
| 216 |
|
| 217 |
|
| 218 |
type = type.charAt(0); |
| 219 |
return arrayUtil.map(value, parse); |
| 220 |
} |
| 221 |
|
| 222 |
return parse(value); |
| 223 |
} |
| 224 |
|
| 225 |
function toDynamoObject(object) { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
var dynamoObject = {}; |
| 231 |
for (var k in object) { |
| 232 |
dynamoObject[k] = getDynamoValue(object[k]); |
| 233 |
} |
| 234 |
return dynamoObject; |
| 235 |
} |
| 236 |
|
| 237 |
function fromDynamoObject(dynamoObject) { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if (!dynamoObject) { |
| 243 |
return null; |
| 244 |
} |
| 245 |
|
| 246 |
var object = {}; |
| 247 |
for (var k in dynamoObject) { |
| 248 |
object[k] = getNativeValue(dynamoObject[k]); |
| 249 |
} |
| 250 |
|
| 251 |
return object; |
| 252 |
} |
| 253 |
|
| 254 |
return declare(Stateful, { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
tableName: '', |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
attributesToGet: null, |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
consistentRead: false, |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
maxRetries: 5, |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
idProperty: 'id', |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
queryEngine: SimpleQueryEngine, |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
region: null, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
endpointUrl: null, |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
credentials: null, |
| 310 |
|
| 311 |
_signRequest: function ( request, date) { |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
date = toUTCString(date || new Date()); |
| 329 |
var shortDate = date.slice(0, 8); |
| 330 |
var secretKey = this.credentials.SecretAccessKey; |
| 331 |
var accessKey = this.credentials.AccessKeyId; |
| 332 |
var sessionToken = this.credentials.SessionToken; |
| 333 |
var tokenHeader = ''; |
| 334 |
var tokenTag = ''; |
| 335 |
|
| 336 |
request.headers['x-amz-date'] = date; |
| 337 |
|
| 338 |
if (sessionToken) { |
| 339 |
tokenHeader = 'x-amz-security-token:' + sessionToken + '\n'; |
| 340 |
tokenTag = ';x-amz-security-token'; |
| 341 |
request.headers['x-amz-security-token'] = sessionToken; |
| 342 |
} |
| 343 |
|
| 344 |
var canonicalRequest = request.method + '\n/\n\n' + |
| 345 |
'host:' + request.host + '\n' + |
| 346 |
'x-amz-date:' + date + '\n' + |
| 347 |
tokenHeader + |
| 348 |
'x-amz-target:' + request.headers['x-amz-target'] + '\n' + |
| 349 |
'\n' + |
| 350 |
'host;x-amz-date' + tokenTag + |
| 351 |
';x-amz-target\n' + hash(request.body, digests.outputTypes.Hex); |
| 352 |
|
| 353 |
var key = getSigningKey(shortDate, secretKey, this.region); |
| 354 |
|
| 355 |
var stringToSign = 'AWS4-HMAC-SHA256\n' + |
| 356 |
date + '\n' + |
| 357 |
shortDate + '/' + this.region + '/dynamodb/aws4_request\n' + |
| 358 |
hash(canonicalRequest, digests.outputTypes.Hex); |
| 359 |
|
| 360 |
var signature = hmac(key, stringToSign, digests.outputTypes.Hex); |
| 361 |
|
| 362 |
request.headers.authorization = 'AWS4-HMAC-SHA256 Credential=' + accessKey + '/' + shortDate + '/' + |
| 363 |
this.region + '/dynamodb/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target' + tokenTag + |
| 364 |
',Signature=' + signature; |
| 365 |
}, |
| 366 |
|
| 367 |
_rpc: function ( action, data) { |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
var timeoutId; |
| 380 |
var requestPromise; |
| 381 |
var dfd = new Deferred(function () { |
| 382 |
requestPromise && requestPromise.cancel.apply(request, arguments); |
| 383 |
clearTimeout(timeoutId); |
| 384 |
}); |
| 385 |
var headers = { |
| 386 |
'Content-Type': 'application/x-amz-json-1.0', |
| 387 |
'x-amz-target': 'DynamoDB_20120810.' + action |
| 388 |
}; |
| 389 |
|
| 390 |
data.TableName = this.tableName; |
| 391 |
data = JSON.stringify(data); |
| 392 |
|
| 393 |
|
| 394 |
if (!this.endpointUrl) { |
| 395 |
this.endpointUrl = 'https://dynamodb.' + this.region + '.amazonaws.com'; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if (this.credentials) { |
| 400 |
this._signRequest({ |
| 401 |
body: data, |
| 402 |
host: getHostName(this.endpointUrl), |
| 403 |
method: 'POST', |
| 404 |
headers: headers |
| 405 |
}); |
| 406 |
} |
| 407 |
|
| 408 |
var endpointUrl = this.endpointUrl; |
| 409 |
var maxRetries = this.maxRetries; |
| 410 |
var currentRetry = 0; |
| 411 |
|
| 412 |
(function sendRequest() { |
| 413 |
requestPromise = request.post(endpointUrl, { |
| 414 |
data: data, |
| 415 |
headers: headers, |
| 416 |
handleAs: 'json' |
| 417 |
}).then(lang.hitch(dfd, 'resolve'), function (error) { |
| 418 |
var response = error.response; |
| 419 |
|
| 420 |
if (++currentRetry === maxRetries) { |
| 421 |
dfd.reject(error); |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
if (response.status >= 500 || response.status < 400 || |
| 426 |
(response.status === 400 && response.data && response.data.__type && |
| 427 |
(response.data.__type.indexOf('#ProvisionedThroughputExceededException') > -1 || |
| 428 |
response.data.__type.indexOf('#ThrottlingException') > -1)) |
| 429 |
) { |
| 430 |
timeoutId = setTimeout(sendRequest, Math.pow(2, currentRetry) * 50); |
| 431 |
} |
| 432 |
else { |
| 433 |
dfd.reject(error); |
| 434 |
} |
| 435 |
}); |
| 436 |
})(); |
| 437 |
|
| 438 |
return dfd.promise.then(function (response) { |
| 439 |
return response; |
| 440 |
}, function (error) { |
| 441 |
var errorInfo = error.response && error.response.data; |
| 442 |
if (errorInfo) { |
| 443 |
throw new RequestError(errorInfo.__type + ': ' + errorInfo.message, error.response); |
| 444 |
} |
| 445 |
|
| 446 |
throw error; |
| 447 |
}); |
| 448 |
}, |
| 449 |
|
| 450 |
_getKeyFromId: function ( id) { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
var key = {}; |
| 459 |
|
| 460 |
if (this.idProperty instanceof Array) { |
| 461 |
if (typeof id === 'string') { |
| 462 |
id = arrayUtil.map(id.split('/'), function (value) { |
| 463 |
var type = value.charAt(0); |
| 464 |
value = value.slice(1); |
| 465 |
|
| 466 |
if (type === 'N') { |
| 467 |
value = +value; |
| 468 |
} |
| 469 |
|
| 470 |
return value; |
| 471 |
}); |
| 472 |
} |
| 473 |
|
| 474 |
arrayUtil.forEach(id, function (value, index) { |
| 475 |
key[this.idProperty[index]] = getDynamoValue(value); |
| 476 |
}, this); |
| 477 |
} |
| 478 |
else { |
| 479 |
key[this.idProperty] = getDynamoValue(id); |
| 480 |
} |
| 481 |
|
| 482 |
return key; |
| 483 |
}, |
| 484 |
|
| 485 |
get: function ( id) { |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
var data = { |
| 497 |
Key: this._getKeyFromId(id), |
| 498 |
ConsistentRead: this.consistentRead |
| 499 |
}; |
| 500 |
|
| 501 |
if (this.attributesToGet) { |
| 502 |
data.AttributesToGet = this.attributesToGet; |
| 503 |
} |
| 504 |
|
| 505 |
return this._rpc('GetItem', data).then(function (object) { |
| 506 |
return fromDynamoObject(object.Item); |
| 507 |
}); |
| 508 |
}, |
| 509 |
|
| 510 |
getIdentity: function ( object) { |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
var id; |
| 518 |
if (this.idProperty instanceof Array) { |
| 519 |
id = arrayUtil.map(this.idProperty, function (property) { |
| 520 |
return getDynamoType(object[property]) + object[property]; |
| 521 |
}).join('/'); |
| 522 |
} |
| 523 |
else { |
| 524 |
id = object[this.idProperty]; |
| 525 |
} |
| 526 |
|
| 527 |
return id; |
| 528 |
}, |
| 529 |
|
| 530 |
query: function ( query, options) { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
function copyFilterMap(map) { |
| 583 |
var values = {}; |
| 584 |
for (var key in map) { |
| 585 |
values[key] = getDynamoValue(map[key]); |
| 586 |
} |
| 587 |
return values; |
| 588 |
} |
| 589 |
|
| 590 |
options = options || {}; |
| 591 |
|
| 592 |
var data = { |
| 593 |
KeyConditions: {}, |
| 594 |
ConsistentRead: this.consistentRead |
| 595 |
}; |
| 596 |
|
| 597 |
if (this.attributesToGet) { |
| 598 |
data.AttributesToGet = this.attributesToGet; |
| 599 |
} |
| 600 |
|
| 601 |
if (options.indexName) { |
| 602 |
data.IndexName = options.indexName; |
| 603 |
} |
| 604 |
|
| 605 |
if (options.sort && options.sort.length) { |
| 606 |
if (options.sort.length > 1) { |
| 607 |
throw new Error('Cannot sort by more than one dimension'); |
| 608 |
} |
| 609 |
|
| 610 |
if (!data.IndexName) { |
| 611 |
data.IndexName = options.sort[0].attribute; |
| 612 |
} |
| 613 |
|
| 614 |
data.ScanIndexForward = !options.sort[0].descending; |
| 615 |
} |
| 616 |
|
| 617 |
for (var k in query) { |
| 618 |
var value = query[k]; |
| 619 |
|
| 620 |
data.KeyConditions[k] = { |
| 621 |
AttributeValueList: value instanceof Array ? arrayUtil.map(value, getDynamoValue) : |
| 622 |
[ getDynamoValue(value) ], |
| 623 |
ComparisonOperator: 'EQ' |
| 624 |
}; |
| 625 |
} |
| 626 |
|
| 627 |
if (options.filter) { |
| 628 |
var filter = options.filter; |
| 629 |
data.FilterExpression = filter.FilterExpression; |
| 630 |
|
| 631 |
if (filter.ExpressionAttributeValues) { |
| 632 |
data.ExpressionAttributeValues = copyFilterMap(filter.ExpressionAttributeValues); |
| 633 |
} |
| 634 |
|
| 635 |
if (filter.ExpressionAttributeNames) { |
| 636 |
data.ExpressionAttributeNames = filter.ExpressionAttributeNames; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
var dfd = new Deferred(function () { |
| 641 |
request && request.cancel.apply(request, arguments); |
| 642 |
}); |
| 643 |
var response = createQueryResults(dfd.promise); |
| 644 |
|
| 645 |
if (options.fetchTotal !== false) { |
| 646 |
response.total = this._rpc('Query', lang.mixin({}, data, { |
| 647 |
Select: 'COUNT' |
| 648 |
})).then(function (response) { |
| 649 |
return response.Count; |
| 650 |
}); |
| 651 |
} |
| 652 |
|
| 653 |
var self = this; |
| 654 |
var result = []; |
| 655 |
var skipRecords = typeof options.start === 'number' ? options.start : 0; |
| 656 |
var recordsToRetrieve = (skipRecords + (options.count || 0)) || Infinity; |
| 657 |
var request; |
| 658 |
|
| 659 |
(function nextQuery(nextStartKey) { |
| 660 |
data.Limit = recordsToRetrieve < Infinity ? recordsToRetrieve : undefined; |
| 661 |
data.ExclusiveStartKey = nextStartKey; |
| 662 |
|
| 663 |
request = self._rpc('Query', data).then(function (response) { |
| 664 |
if (response.Items.length) { |
| 665 |
var newData = arrayUtil.map(response.Items.slice(skipRecords), fromDynamoObject); |
| 666 |
|
| 667 |
if (skipRecords > 0) { |
| 668 |
skipRecords = Math.max(skipRecords - newData.length, 0); |
| 669 |
} |
| 670 |
|
| 671 |
recordsToRetrieve -= newData.length; |
| 672 |
result = result.concat(newData); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
if (recordsToRetrieve > 0 && response.LastEvaluatedKey) { |
| 679 |
nextQuery(response.LastEvaluatedKey); |
| 680 |
} |
| 681 |
else { |
| 682 |
dfd.resolve(result); |
| 683 |
} |
| 684 |
}, lang.hitch(dfd, 'reject')); |
| 685 |
})(typeof options.start === 'object' ? toDynamoObject(options.start) : undefined); |
| 686 |
|
| 687 |
return response; |
| 688 |
}, |
| 689 |
|
| 690 |
remove: function ( id, options) { |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
options = options || {}; |
| 704 |
|
| 705 |
var data = { |
| 706 |
Key: this._getKeyFromId(id), |
| 707 |
ReturnValues: 'ALL_OLD' |
| 708 |
}; |
| 709 |
|
| 710 |
if (options.expected) { |
| 711 |
data.Expected = {}; |
| 712 |
for (var k in options.expected) { |
| 713 |
data.Expected[k] = { |
| 714 |
Value: getDynamoValue(options.expected[k]) |
| 715 |
}; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
return this._rpc('DeleteItem', data).then(function (object) { |
| 720 |
return fromDynamoObject(object.Attributes); |
| 721 |
}); |
| 722 |
}, |
| 723 |
|
| 724 |
put: function ( object, options) { |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
options = options || {}; |
| 741 |
|
| 742 |
var data = { |
| 743 |
Item: toDynamoObject(object) |
| 744 |
}; |
| 745 |
|
| 746 |
if (options.id) { |
| 747 |
lang.mixin(data.Item, this._getKeyFromId(options.id)); |
| 748 |
} |
| 749 |
|
| 750 |
if (options.overwrite === false) { |
| 751 |
data.Expected = {}; |
| 752 |
var idProperties = this.idProperty instanceof Array ? this.idProperty : [ this.idProperty ]; |
| 753 |
arrayUtil.forEach(idProperties, function (property) { |
| 754 |
data.Expected[property] = { Exists: false }; |
| 755 |
}); |
| 756 |
} |
| 757 |
else if (options.expected) { |
| 758 |
data.Expected = {}; |
| 759 |
for (var k in options.expected) { |
| 760 |
data.Expected[k] = { |
| 761 |
Value: getDynamoValue(options.expected[k]) |
| 762 |
}; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
return this._rpc('PutItem', data).then(function () { |
| 767 |
|
| 768 |
|
| 769 |
}); |
| 770 |
}, |
| 771 |
|
| 772 |
add: function ( object, options) { |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
options = options || {}; |
| 780 |
options.overwrite = false; |
| 781 |
return this.put(object, options); |
| 782 |
} |
| 783 |
}); |
| 784 |
}); |