UNPKG

@payburner/keyburner-sidewinder-core

Version:
102 lines 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AWSDynamoEnvironmentBlockService = void 0; class AWSDynamoEnvironmentBlockService { constructor(docClient) { this.docClient = null; this.docClient = docClient; } getLatestEnvironmentBlock(environment) { const self = this; return new Promise((resolve, reject) => { const params = { TableName: "sidewinder_environment_latest_block", KeyConditionExpression: "environment = :environment", ExpressionAttributeValues: { ":environment": environment } }; const t0 = new Date().getTime(); self.docClient.query(params, function (err, data) { console.log('Query Time Get Processed Transaction: ' + (new Date().getTime() - t0)); if (err) { console.error("Get Latest Environment Block :: Unable to query. Error:", JSON.stringify(err, null, 2)); reject(err); } else { if (data.Items.length === 0) { resolve({ block_id: "NONE", block: "", environment: environment, sequence: 0, count: 0, last_block_id: "NONE", timestamp: new Date().getTime() }); } else { resolve(data.Items[0]); } } }); }); } saveSignedBlock(transactions, signedBlock) { const self = this; return new Promise((resolve) => { const items = []; items.push({ Put: { TableName: 'sidewinder_environment_latest_block', Item: signedBlock, ConditionExpression: "attribute_not_exists(environment) OR (environment = :environment AND #seq = :last_sequence AND block_id = :last_block_id)", ExpressionAttributeNames: { "#seq": "sequence" }, ExpressionAttributeValues: { ":environment": signedBlock.environment, ":last_sequence": signedBlock.sequence - 1, ":last_block_id": signedBlock.last_block_id } } }); items.push({ Put: { TableName: 'sidewinder_environment_blocks', Item: signedBlock, ConditionExpression: "attribute_not_exists(block_id)" } }); transactions.forEach((txn) => { items.push({ Update: { TableName: 'sidewinder_transactions', Key: { environment: signedBlock.environment, id: txn.id }, UpdateExpression: "set processing_stage = :new_processing_stage, environment_processing_stage = :environment_processing_stage", ConditionExpression: "processing_stage = :old_processing_stage", ExpressionAttributeValues: { ":new_processing_stage": "PROCESSING", ":old_processing_stage": "PENDING", ":environment_processing_stage": signedBlock.environment + '_PROCESSING' } } }); }); console.log('Items:' + JSON.stringify(items, null, 2)); self.docClient.transactWrite({ TransactItems: items }, function (err, data) { if (err) { console.log('AWS Transactional Write Error:' + err); console.log('AWS Transactional Write Error:' + JSON.stringify(err, null, 2)); resolve(false); } else { resolve(true); } }); }); } } exports.AWSDynamoEnvironmentBlockService = AWSDynamoEnvironmentBlockService; //# sourceMappingURL=AWSDynamoEnvironmentBlockService.js.map