UNPKG

dynolink

Version:
112 lines (111 loc) 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOrCreateEntityMeta = getOrCreateEntityMeta; exports.setEntityMetadata = setEntityMetadata; exports.getEntityMetadata = getEntityMetadata; exports.Table = Table; exports.Column = Column; exports.PartitionKey = PartitionKey; exports.SortKey = SortKey; exports.GSI = GSI; exports.LSI = LSI; require("reflect-metadata"); // Storage const ENTITY_METADATA = new Map(); /** * Gets or creates the entity metadata for the given target. * @param target */ function getOrCreateEntityMeta(target) { const constructor = target.constructor; let meta = ENTITY_METADATA.get(constructor); if (!meta) { meta = { tableName: '', attributes: new Map(), }; ENTITY_METADATA.set(constructor, meta); } return meta; } function setEntityMetadata(target, metadata) { ENTITY_METADATA.set(target, metadata); } function getEntityMetadata(constructor) { return ENTITY_METADATA.get(constructor); } // Decorators /** * Marks a class as a DynamoDB entity. * @param name * @constructor */ function Table(name) { return (target) => { const metadata = getOrCreateEntityMeta(target.prototype); metadata.tableName = name; setEntityMetadata(target, metadata); }; } /** * Marks a property as a column in the entity. * @param options * @constructor */ function Column(options = {}) { return (target, propertyKey) => { const meta = getOrCreateEntityMeta(target); meta.attributes.set(propertyKey, { ...(meta.attributes.get(propertyKey) || {}), ...options, }); }; } /** * Marks a property as the partition key for the entity. * @constructor */ function PartitionKey() { return (target, propertyKey) => { const meta = getOrCreateEntityMeta(target); const attr = meta.attributes.get(propertyKey) || {}; meta.attributes.set(propertyKey, { ...attr, isPartitionKey: true }); }; } /** * Marks a property as the sort key for the entity. * @constructor */ function SortKey() { return (target, propertyKey) => { const meta = getOrCreateEntityMeta(target); const attr = meta.attributes.get(propertyKey) || {}; meta.attributes.set(propertyKey, { ...attr, isSortKey: true }); }; } /** * Marks a property as a global secondary index (GSI). * @param options * @constructor */ function GSI(options) { return (target, propertyKey) => { const meta = getOrCreateEntityMeta(target); const attr = meta.attributes.get(propertyKey) || {}; attr.gsi = options; meta.attributes.set(propertyKey, attr); }; } /** * Marks a property as a local secondary index (LSI). * @param options * @constructor */ function LSI(options) { return (target, propertyKey) => { const meta = getOrCreateEntityMeta(target); const attr = meta.attributes.get(propertyKey) || {}; attr.lsi = options; meta.attributes.set(propertyKey, attr); }; }