UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

45 lines (44 loc) 3.36 kB
import type { BatchGetCommandInput, BatchGetCommandOutput, DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; import type { FormattedItem } from '../../../entity/index.js'; import type { CapacityOption } from '../../../options/capacity.js'; import type { Paths } from '../../../schema/index.js'; import type { DocumentClientOptions } from '../../../types/documentClientOptions.js'; import { BatchGetCommand } from './batchGetCommand.js'; import type { BatchGetCommandOptions, IBatchGetRequest } from './batchGetCommand.js'; import { $options, $requests } from './constants.js'; export interface ExecuteBatchGetOptions extends DocumentClientOptions { capacity?: CapacityOption; documentClient?: DynamoDBDocumentClient; maxAttempts?: number; } export type ExecuteBatchGetInput = BatchGetCommand[] | [ExecuteBatchGetOptions, ...BatchGetCommand[]]; type ExecuteBatchGet = <COMMANDS extends ExecuteBatchGetInput>(..._commands: COMMANDS) => Promise<ExecuteBatchGetResponses<COMMANDS>>; export type ExecuteBatchGetResponses<COMMANDS extends ExecuteBatchGetInput> = COMMANDS extends BatchGetCommand[] ? ExecuteBatchGetResponse<COMMANDS> : COMMANDS extends [ExecuteBatchGetOptions, ...infer REQUESTS_TAIL] ? REQUESTS_TAIL extends BatchGetCommand[] ? ExecuteBatchGetResponse<REQUESTS_TAIL> : never : never; type ExecuteBatchGetResponse<COMMAND extends BatchGetCommand[]> = Omit<BatchGetCommandOutput, 'Responses'> & { Responses: BatchGetResponses<COMMAND>; }; type BatchGetResponses<COMMANDS extends BatchGetCommand[], RESPONSES extends unknown[] = []> = COMMANDS extends [infer COMMANDS_HEAD, ...infer COMMANDS_TAILS] ? COMMANDS_HEAD extends BatchGetCommand ? COMMANDS_TAILS extends BatchGetCommand[] ? BatchGetResponses<COMMANDS_TAILS, [ ...RESPONSES, BatchGetRequestResponses<NonNullable<COMMANDS_HEAD[$requests]>, COMMANDS_HEAD[$options]> ]> : never : never : number extends COMMANDS['length'] ? [ ...RESPONSES, ...(COMMANDS[number] extends infer TABLE_COMMAND ? TABLE_COMMAND extends BatchGetCommand ? BatchGetRequestResponses<NonNullable<TABLE_COMMAND[$requests]>, TABLE_COMMAND[$options]> : never : never)[] ] : RESPONSES; type BatchGetRequestResponses<REQUESTS extends IBatchGetRequest[], OPTIONS extends BatchGetCommandOptions, ITEMS extends unknown[] = []> = REQUESTS extends [infer REQUESTS_HEAD, ...infer REQUESTS_TAIL] ? REQUESTS_HEAD extends IBatchGetRequest ? REQUESTS_TAIL extends IBatchGetRequest[] ? BatchGetRequestResponses<REQUESTS_TAIL, OPTIONS, [ ...ITEMS, (FormattedItem<REQUESTS_HEAD['entity'], { attributes: OPTIONS extends { attributes: string[]; } ? Extract<OPTIONS['attributes'][number], Paths<REQUESTS_HEAD['entity']['schema']>> : undefined; }> | undefined) ]> : never : never : number extends REQUESTS['length'] ? [ ...ITEMS, ...((REQUESTS[number] extends infer ENTITY_REQUEST ? ENTITY_REQUEST extends IBatchGetRequest ? FormattedItem<ENTITY_REQUEST['entity'], { attributes: OPTIONS extends { attributes: Paths<ENTITY_REQUEST['entity']['schema']>[]; } ? OPTIONS['attributes'][number] : undefined; }> : never : never) | undefined)[] ] : ITEMS; export declare const execute: ExecuteBatchGet; export declare const getCommandInput: (commands: BatchGetCommand[], options?: ExecuteBatchGetOptions) => BatchGetCommandInput; export {};