/** * Table.ts contains the classes used to model a DynamoDB local and global secondary indexes. * @packageDocumentation */ import { DocumentClient } from 'aws-sdk/clients/dynamodb'; import { Table } from './Table'; /** * Represents either Global Secondary Index (GSI) or Local Secondary Index (LSI) for a table. GSI and LSI can be * validated using {@link validateIndex} or {@link validateIndexes}. * * If you are using TypeScript you can use {@link Index.createIndex} to create an Index with strong typing for the primary key. * This provides strong types for the {@link Index.keySchema} property, {@link Index.queryParams} and {@link Index.scan} methods. * * @example [examples/Index.ts]{@link https://github.com/jasoncraftscode/dynamodb-datamodel/tree/main/examples/Index.ts} * ```typescript * [[include:Index.ts]] * ``` * * See {@link Table} for how to include Indexes into a Table. * @public */ export declare class Index { /** * Name of the table's secondary index, used to set the IndexName for dynamodb scan and query actions. */ name: string; /** * Schema map for the Secondary Index's primary key, in the form of \{ \: \{ keyType: 'HASH' \} \}. */ keySchema: Table.PrimaryKey.KeyTypesMap; /** * Defines how the other attributes for an entity are projected to the index. */ projection: { /** * Only relevant when type is 'INCLUDE', list of the attributes to project to the secondary index. */ attributes?: string[]; /** * Defines what general set of attributes are projected into the secondary index. */ type: Table.ProjectionType; }; /** * The table this index is associated with. Used in {@link queryParams}, {@link scanParams}, {@link query}, and {@link scan}. */ table: Table; /** * The type of this secondary index. */ type: Index.Type; /** * @param params - Initialize the Index's name, keySchema and projection properties. */ constructor(params: Index.IndexParams); /** * Gets the partition key name for the Index. * @returns The name of the primary (or HASH) key. */ getPartitionKey(): string; /** * Gets the sort key name for the Index. * @returns The name of the sort (or RANGE) key, or an empty string if one doesn't exists. */ getSortKey(): string; /** * Add the IndexName to query options. * @param options - Options to add IndexName to. * @returns Query options with the IndexName set to the {@link Index.name}. */ getQueryOptions(options?: Table.QueryOptions): Table.QueryOptions; /** * Add the IndexName to scan options. * @param options - Options to add IndexName to. * @returns Scan options with the IndexName set to the {@link Index.name}. */ getScanOptions(options?: Table.ScanOptions): Table.ScanOptions; /** * Creates the params that can be used when calling the [DocumentClient.query]{@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property} method. * @param key - Primary key with optional KeyCondition to query the secondary index with. * @param options - Used in building the query params. * @returns DynamoDB query method params containing the table, index, key and options. */ queryParams(key: Table.PrimaryKey.KeyQueryMap, options?: Table.QueryOptions): DocumentClient.QueryInput; /** * Creates the params that can be used when calling the [DocumentClient.scan]{@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property} method. * @param options - Used in building the scan params. * @returns DocumentClient scan method's params containing the table, index and options. */ scanParams(options?: Table.ScanOptions): DocumentClient.ScanInput; /** * Wrapper around [DocumentClient.query]{@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property} * method that uses the index and table properties with the key and options params. * @param key - Primary key with optional KeyCondition to query the secondary index with. * @param options - Used in building the query params. * @returns Promise with the query results, including items fetched. */ query(key: Table.PrimaryKey.KeyQueryMap, options?: Table.QueryOptions): Promise; /** * Wrapper around [DocumentClient.scan]{@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property} * method that uses the index and table properties with the options param. * @param options - Used in building the scan params. * @returns Promise with the scan results, including items fetched. */ scan(options?: Table.ScanOptions): Promise; } /** * Is also a namespace for scoping Index based interfaces and types. * @public */ export declare namespace Index { /** * Type of secondary index. */ type Type = 'GLOBAL' | 'LOCAL'; /** * Used in {@link Index."constructor"}. */ interface IndexParams { /** * Name of the table's secondary index, used to set the IndexName for dynamodb scan and query actions. */ name: string; /** * Schema map for the Secondary Index's primary key, in the form of \{ \: \{ keyType: 'HASH' \} \}. */ keySchema: Table.PrimaryKey.KeyTypesMap; /** * Defines how the other attributes for an entity are projected to the index. */ projection: { /** * Only relevant when type is 'INCLUDE', list of the attributes to project to the secondary index. */ attributes?: string[]; /** * Defines what general set of attributes are projected into the secondary index. */ type: Table.ProjectionType; }; /** * The table this index is associated with. Used in {@link queryParams}, {@link scanParams}, {@link query}, and {@link scan}. */ table: Table; /** * The type of this secondary index. */ type: Index.Type; } /** * Default and Example global secondary index primary key with the generalized compact format of. */ interface DefaultGlobalIndexKey { /** * Partition key: G#P which represents G = Global + # = index number + P = Partition key. */ G0P: Table.PrimaryKey.PartitionString; /** * Sort key: G#S which represents G = Global + # = index number + S = Sort key. The sort key is optional * to support the sort key as being option for queryParams and query methods. */ G0S?: Table.PrimaryKey.SortString; } /** * Default and Example local secondary index primary key with the generalized compact format of. */ interface DefaultLocalIndexKey { /** * Partition key: P which is the Table partition key since local secondary indexes are stored in the * same partition as the main table. */ P: Table.PrimaryKey.PartitionString; /** * Sort key: L#S which represents L = Local + # = index number + S = Sort key. The sort key is optional * to support the sort key as being option for queryParams and query methods. */ L0S?: Table.PrimaryKey.SortString; } /** * Index constructor param for the generic form of {@link IndexParams}. * @param KEY - The interface of the index's primary key. */ interface IndexParamsT extends IndexParams { /** * Generic form of {@link IndexParam.keySchema}. */ keySchema: Table.PrimaryKey.KeyTypesMapT; } /** * Generic form of {@link Index}. * @param KEY - The interface of the index's primary key. */ interface IndexT extends Index { /** * Generic form of {@link Index.keySchema}. */ keySchema: Table.PrimaryKey.KeyTypesMapT; /** * See Generic form of {@link Index.queryParams}. */ queryParams(key: Table.PrimaryKey.KeyQueryMapT, options?: Table.QueryOptions): DocumentClient.QueryInput; /** * Generic form of {@link Index.query}. */ query(key: Table.PrimaryKey.KeyQueryMapT, options?: Table.QueryOptions): Promise; } /** * Creates the generic form of Index used in TypeScript to get strong typing. * * See {@link Table.createTable} reasoning for having a createTable over support 'new TableT'. * @param params - Index constructor params. */ function createIndex(params: IndexParamsT): IndexT; }