@setho/dynamodb-repository
Version:
DynamoDB repository for hash-key and hash-key/range indexed tables. Designed for Lambda use. Handles nice-to-haves like created and updated timestamps and default id creation.
26 lines • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDynamoDbKey = exports.createId = exports.parseCursor = exports.createCursor = void 0;
const ulid_1 = require("ulid");
const http_errors_1 = require("http-errors");
const createCursor = (lastEvaluatedKey) => Buffer.from(JSON.stringify(lastEvaluatedKey)).toString('base64');
exports.createCursor = createCursor;
const parseCursor = (cursor) => {
let result;
try {
result = JSON.parse(Buffer.from(cursor, 'base64').toString('utf8'));
}
catch (err) {
throw new http_errors_1.BadRequest('Bad Request: cursor is not valid');
}
return result;
};
exports.parseCursor = parseCursor;
const createId = async ({ prefix = '' } = {}) => `${prefix}${(0, ulid_1.ulid)()}`;
exports.createId = createId;
const createDynamoDbKey = (input) => {
const { keyName, keyValue } = input;
return { [keyName]: keyValue };
};
exports.createDynamoDbKey = createDynamoDbKey;
//# sourceMappingURL=utils.js.map