component-dbs-core
Version:
DTO objects shared among device backend
104 lines • 4.87 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionDb = void 0;
/**
* Transaction db
* used to access transaction database table
*/
class TransactionDb {
constructor(tableName, dynamodbService, transactionGsi, accessTokenGsi, entityUuidGsi, siteUuidGsi) {
this.tableName = tableName;
this.dynamodbService = dynamodbService;
this.transactionGsi = transactionGsi;
this.accessTokenGsi = accessTokenGsi;
this.entityUuidGsi = entityUuidGsi;
this.siteUuidGsi = siteUuidGsi;
this.type = 'transaction';
this.getTransaction = (transactionUuid) => {
const params = {
TableName: this.tableName,
IndexName: this.transactionGsi,
KeyConditionExpression: 'transactionUuid = :transactionUuid',
ExpressionAttributeValues: {
':transactionUuid': transactionUuid,
},
};
return this.dynamodbService.documentClient.query(params).promise();
};
this.getIndexName = (deviceUuid, siteUuid) => {
if (!deviceUuid && !siteUuid) {
return this.entityUuidGsi;
}
if (siteUuid) {
return this.siteUuidGsi;
}
// query the main table
return undefined;
};
this.getTransactionsMultiQueries = (limit, timestampFilter, { deviceUuid, siteUuid, entityUuid, }, nextToken, filter, prev) => __awaiter(this, void 0, void 0, function* () {
let values = {};
const attributeNames = { '#type': 'type' };
let keyExpression = '';
if (entityUuid) {
values = { ':entityUuid': entityUuid };
keyExpression = 'entityUuid = :entityUuid';
}
if (siteUuid) {
values = { ':siteUuid': siteUuid };
keyExpression = 'siteUuid = :siteUuid';
}
if (deviceUuid) {
values = { ':deviceUuid': deviceUuid };
keyExpression = 'id = :deviceUuid';
}
const params = {
TableName: this.tableName,
IndexName: this.getIndexName(deviceUuid, siteUuid),
ExpressionAttributeValues: values,
KeyConditionExpression: keyExpression,
ExpressionAttributeNames: attributeNames,
Limit: limit,
ScanIndexForward: false,
};
const queryOutput = yield this.dynamodbService.getQueryOutput(params, this.type, timestampFilter, filter, nextToken);
let combinedItems = prev && prev.Items ? prev.Items : [];
combinedItems = queryOutput.Items
? [...combinedItems, ...queryOutput.Items]
: combinedItems;
const outputItems = {
Items: combinedItems,
Count: combinedItems.length,
LastEvaluatedKey: queryOutput.LastEvaluatedKey,
};
if (typeof queryOutput.Items !== 'undefined' &&
queryOutput.Items.length < limit &&
queryOutput.LastEvaluatedKey) {
console.log(`try another query ${combinedItems.length} ${limit}`);
return this.getTransactionsMultiQueries(limit - queryOutput.Items.length, timestampFilter, { deviceUuid, siteUuid, entityUuid }, queryOutput.LastEvaluatedKey, filter, outputItems);
}
return outputItems;
});
this.getTransactions = (event, entityUuid) => {
const { deviceUuid, siteUuid, limit, nextToken, filter, timestampFilter, } = event;
let tFilter;
if (!timestampFilter) {
tFilter = this.dynamodbService.utils.createLastYearQuery();
}
else {
tFilter = timestampFilter;
}
return this.getTransactionsMultiQueries(limit, tFilter, { deviceUuid, siteUuid, entityUuid }, nextToken, filter);
};
}
}
exports.TransactionDb = TransactionDb;
//# sourceMappingURL=transactionDb.js.map