UNPKG

@moicky/dynamodb

Version:

Contains a collection of convenience functions for working with AWS DynamoDB

53 lines (52 loc) 2.15 kB
import { GetItemCommandInput } from "@aws-sdk/client-dynamodb"; import { DynamoDBItem } from "../types"; /** * 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 * ``` */ export declare function itemExists(key: DynamoDBItem, args?: Partial<GetItemCommandInput>): Promise<boolean>; /** * 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" * ``` */ export declare function getAscendingId({ length, TableName, ...keySchema }: { length?: number; TableName?: string; [keySchema: string]: any; }): Promise<string>;