@bitblit/ratchet-aws
Version:
Common tools for use with AWS browser and node
110 lines • 4.15 kB
JavaScript
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet';
export class DynamoDbStorageProvider {
dynamo;
opts;
constructor(dynamo, opts) {
this.dynamo = dynamo;
this.opts = opts;
RequireRatchet.notNullOrUndefined(this.dynamo, 'dynamo');
RequireRatchet.notNullOrUndefined(this.opts, 'opts');
RequireRatchet.notNullOrUndefined(this.opts.tableName, 'opts.tableName');
RequireRatchet.notNullOrUndefined(this.opts.hashKeyName, 'opts.hashKeyName');
RequireRatchet.true(!this.opts.useRangeKeys || (!!this.opts.rangeKeyName && !!this.opts.hashKeyValue), 'invalid range configuration');
}
static createDefaultOptions() {
const rval = {
tableName: 'simple-cache',
useRangeKeys: false,
hashKeyName: 'cache-key',
rangeKeyName: null,
hashKeyValue: null,
};
return rval;
}
createKeyObject(cacheKey) {
const keys = {};
if (this.opts.useRangeKeys) {
keys[this.opts.hashKeyName] = this.opts.hashKeyValue;
keys[this.opts.rangeKeyName] = cacheKey;
}
else {
keys[this.opts.hashKeyName] = cacheKey;
}
return keys;
}
cleanDynamoFieldsFromObjectInPlace(rval) {
if (rval) {
delete rval[this.opts.hashKeyName];
if (this.opts.rangeKeyName) {
delete rval[this.opts.rangeKeyName];
}
if (this.opts.dynamoExpiresColumnName) {
delete rval[this.opts.dynamoExpiresColumnName];
}
}
}
extractKeysFromObject(rval) {
let keys = null;
if (rval) {
keys = {};
if (this.opts.useRangeKeys) {
keys[this.opts.hashKeyName] = this.opts.hashKeyValue;
keys[this.opts.rangeKeyName] = rval.cacheKey;
}
else {
keys[this.opts.hashKeyName] = rval.cacheKey;
}
}
return keys;
}
async readFromCache(cacheKey) {
const dKey = this.createKeyObject(cacheKey);
const rval = await this.dynamo.simpleGet(this.opts.tableName, dKey);
this.cleanDynamoFieldsFromObjectInPlace(rval);
return rval;
}
async storeInCache(value) {
RequireRatchet.notNullOrUndefined(value, 'value');
RequireRatchet.notNullOrUndefined(value.cacheKey, 'value.cacheKey');
const toSave = Object.assign({}, value, this.createKeyObject(value.cacheKey));
if (this.opts.dynamoExpiresColumnName && value.expiresEpochMS) {
toSave[this.opts.dynamoExpiresColumnName] = Math.floor(value.expiresEpochMS / 1000);
}
const wrote = await this.dynamo.simplePut(this.opts.tableName, toSave);
return !!wrote;
}
async removeFromCache(cacheKey) {
await this.dynamo.simpleDelete(this.opts.tableName, this.createKeyObject(cacheKey));
}
async clearCache() {
const allValues = await this.readAll();
const allKeys = allValues.map((a) => this.extractKeysFromObject(a));
const rval = await this.dynamo.deleteAllInBatches(this.opts.tableName, allKeys, 25);
return rval;
}
async readAll() {
let rval = null;
if (this.opts.useRangeKeys) {
const qry = {
TableName: this.opts.tableName,
KeyConditionExpression: '#cacheKey = :cacheKey',
ExpressionAttributeNames: {
'#cacheKey': this.opts.hashKeyName,
},
ExpressionAttributeValues: {
':cacheKey': this.opts.hashKeyValue,
},
};
rval = await this.dynamo.fullyExecuteQuery(qry);
}
else {
const scan = {
TableName: this.opts.tableName,
};
rval = await this.dynamo.fullyExecuteScan(scan);
}
rval.forEach((r) => this.cleanDynamoFieldsFromObjectInPlace(r));
return rval;
}
}
//# sourceMappingURL=dynamo-db-storage-provider.js.map