UNPKG

@moicky/dynamodb

Version:

Contains a collection of convenience functions for working with AWS DynamoDB

88 lines (87 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAscendingId = exports.itemExists = void 0; const client_dynamodb_1 = require("@aws-sdk/client-dynamodb"); const lib_1 = require("../lib"); const query_1 = require("./query"); /** * Check if an item exists in the DynamoDB table using its key schema. * @param key - The item with at least the partition key and the sort key (if applicable) of the item to check. * @param args - The additional arguments to override or specify for {@link GetItemCommandInput} * @returns A promise that resolves to a boolean indicating whether the item exists or not. * * @example * Check if an item exists * ```typescript * const exists = await itemExists({ PK: "User/1", SK: "Book/1" }); * console.log(exists); // true / false * ``` */ async function itemExists(key, args = {}) { args = (0, lib_1.withDefaults)(args, "itemExists"); return (0, lib_1.getClient)() .send(new client_dynamodb_1.GetItemCommand({ Key: (0, lib_1.stripKey)(key, args), ...args, TableName: args?.TableName || (0, lib_1.getDefaultTable)(), })) .then((res) => !!res?.Item); } exports.itemExists = itemExists; /** * Generate an ascending ID for an item in the DynamoDB table using the key schema. * @param params - An object containing key schema information and optional ID length and TableName * @returns A promise that resolves to a string representing the new ascending ID. * * @example * Generate ascending ID * ```typescript * // Example Structure 1: PK: "User/1", SK: "{{ ASCENDING_ID }}" * // Last item: { PK: "User/1", SK: "00000009" } * const id1 = await getAscendingId({ PK: "User/1" }); * console.log(id1); // "00000010" * * // Example Structure 2: PK: "User/1", SK: "Book/{{ ASCENDING_ID }}" * // Last item: { PK: "User/1", SK: "Book/00000009" } * const id2 = await getAscendingId({ PK: "User/1", SK: "Book" }); * console.log(id2); // "00000010" * * // Specify length of ID * const id3 = await getAscendingId({ PK: "User/1", SK: "Book", length: 4 }); * console.log(id3); // "0010" * * // Example Structure 3: someKeySchemaHash: "User/1", SK: "Book/{{ ASCENDING_ID }}" * // Last item: { someKeySchemaHash: "User/1", SK: "Book/00000009" } * const id4 = await getAscendingId({ * someKeySchemaHash: "User/1", * SK: "Book", * }); * console.log(id4); // "00000010" * ``` */ async function getAscendingId({ length = 8, TableName, ...keySchema }) { // Assumes that you are the incrementing ID inside or as the keySchema range key const table = TableName || (0, lib_1.getDefaultTable)(); const { hash, range } = (0, lib_1.getTableSchema)(table); const keySchemaHash = keySchema[hash]; const keySchemaRange = keySchema[range]; if (!keySchemaHash) { throw new Error(`[@moicky/dynamodb]: Cannot generate new ID: keySchemaHash is missing, expected '${hash}'`); } let lastId = "0"; if (!keySchemaRange) { const lastItem = (await (0, query_1.queryItems)(`#${hash} = :${hash}`, { [hash]: keySchemaHash }, { Limit: 1, ScanIndexForward: false, TableName: table }))?.[0]; const parts = lastItem?.[range]?.split("/") || []; lastId = parts?.[parts.length - 1] || "0"; } else { const formattedSK = keySchemaRange + (!keySchemaRange.endsWith("/") ? "/" : ""); const lastItem = (await (0, query_1.queryItems)(`#${hash} = :${hash} and begins_with(#${range}, :${range})`, { [hash]: keySchemaHash, [range]: formattedSK }, { Limit: 1, ScanIndexForward: false, TableName: table }))?.[0]; const parts = lastItem?.[range]?.split("/") || []; lastId = parts?.[formattedSK.split("/").length - 1] || "0"; } const newId = parseInt(lastId) + 1 + ""; const withPadding = newId.padStart(length || 0, "0"); return withPadding; } exports.getAscendingId = getAscendingId;