@codification/cutwater-aws
Version:
A library providing general functionality for TypeScript based AWS projects.
51 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompoundKey = void 0;
const CompoundValue_1 = require("../CompoundValue");
const DynamoItem_1 = require("../DynamoItem");
const CompoundItemId_1 = require("./CompoundItemId");
class CompoundKey {
constructor(itemType, compoundItemId) {
this.itemType = itemType;
this.compoundItemId = compoundItemId;
}
static fromItemId(itemType, itemId) {
return new CompoundKey(itemType, CompoundItemId_1.CompoundItemId.fromItemId(itemId));
}
static fromAttributeMap(map, partitionKey = 'pk', sortKey = 'sk') {
const dynamoItem = new DynamoItem_1.DynamoItem(map);
return new CompoundKey(dynamoItem.toStringPart(sortKey, 0), CompoundItemId_1.CompoundItemId.fromKeys(dynamoItem.toString(partitionKey), dynamoItem.toString(sortKey)));
}
get partitionKey() {
return this.hasParent()
? CompoundValue_1.CompoundValue.create(...this.compoundItemId.parentIdParts).value
: CompoundKey.DEFAULT_PARENT;
}
get sortKey() {
return CompoundValue_1.CompoundValue.create(this.itemType, this.compoundItemId.name).value;
}
toKey(partitionKey = 'pk', sortKey = 'sk') {
return {
[partitionKey]: {
S: this.partitionKey,
},
[sortKey]: {
S: this.sortKey,
},
};
}
hasParent() {
return !!this.compoundItemId.parentId;
}
static toPartitionKey(parentId) {
return parentId
? CompoundValue_1.CompoundValue.create(...CompoundItemId_1.CompoundItemId.fromItemId(parentId).idParts).value
: CompoundKey.DEFAULT_PARENT;
}
static toSortKey(itemType, itemId) {
return CompoundValue_1.CompoundValue.create(itemType, CompoundItemId_1.CompoundItemId.toName(itemId)).value;
}
}
exports.CompoundKey = CompoundKey;
CompoundKey.DEFAULT_PARENT = 'CMPD_ROOT';
//# sourceMappingURL=CompoundKey.js.map