Options
All
  • Public
  • Public/Protected
  • All
Menu

Represents either Global Secondary Index (GSI) or Local Secondary Index (LSI) for a table. GSI and LSI can be validated using validateIndex or validateIndexes.

If you are using TypeScript you can use Index.createIndex to create an Index with strong typing for the primary key. This provides strong types for the Index.keySchema property, Index.queryParams and Index.scan methods.

example

examples/Index.ts

import { Index, Table } from 'dynamodb-datamodel';
import { table } from './Table';

// Define a Global Secondary Index (GSI) key interface for GSI0.
export interface GSI0Key {
G0P: Table.PrimaryKey.PartitionString;
G0S?: Table.PrimaryKey.SortString;
}

// Create an Index object for GSI0 based on GSI0Key, and project all attributes.
export const gsi0 = Index.createIndex<GSI0Key>({
name: 'GSI0',
// Defines the key type ('HASH' or 'RANGE') for the GSI primary keys.
keySchema: {
G0P: Table.PrimaryKey.PartitionKeyType,
G0S: Table.PrimaryKey.SortKeyType,
},
projection: { type: 'ALL' },
table: table as Table,
type: 'GLOBAL',
});

// Define a Local Secondary Index (LSI) key interface for LSI0, partition key must be same as the table's
export interface LSI0Key {
P: Table.PrimaryKey.PartitionString;
L0S?: Table.PrimaryKey.SortNumber;
}

// Create an Index object for LSI0 based on LSI0Key, and project all attributes.
export const lsi0 = Index.createIndex<LSI0Key>({
name: 'LSI0',
// Defines the key type ('HASH' or 'RANGE') for the LSI primary keys.
keySchema: {
P: Table.PrimaryKey.PartitionKeyType,
L0S: Table.PrimaryKey.SortKeyType,
},
projection: { type: 'ALL' },
table: table as Table,
type: 'LOCAL',
});

See Table for how to include Indexes into a Table.

Hierarchy

Index

Constructors

Properties

keySchema: KeyTypesMap

Schema map for the Secondary Index's primary key, in the form of { <partition key name>: { keyType: 'HASH' } }.

name: string

Name of the table's secondary index, used to set the IndexName for dynamodb scan and query actions.

projection: { attributes?: string[]; type: ProjectionType }

Defines how the other attributes for an entity are projected to the index.

Type declaration

  • Optional attributes?: string[]

    Only relevant when type is 'INCLUDE', list of the attributes to project to the secondary index.

  • type: ProjectionType

    Defines what general set of attributes are projected into the secondary index.

table: Table

The table this index is associated with. Used in queryParams, scanParams, query, and scan.

type: Type

The type of this secondary index.

Methods

  • getPartitionKey(): string
  • Gets the partition key name for the Index.

    Returns string

    The name of the primary (or HASH) key.

  • getSortKey(): string
  • Gets the sort key name for the Index.

    Returns string

    The name of the sort (or RANGE) key, or an empty string if one doesn't exists.

  • Wrapper around DocumentClient.query method that uses the index and table properties with the key and options params.

    Parameters

    • key: KeyQueryMap

      Primary key with optional KeyCondition to query the secondary index with.

    • Optional options: QueryOptions

      Used in building the query params.

    Returns Promise<QueryOutput>

    Promise with the query results, including items fetched.

  • Creates the params that can be used when calling the DocumentClient.query method.

    Parameters

    • key: KeyQueryMap

      Primary key with optional KeyCondition to query the secondary index with.

    • Optional options: QueryOptions

      Used in building the query params.

    Returns QueryInput

    DynamoDB query method params containing the table, index, key and options.

  • Wrapper around DocumentClient.scan method that uses the index and table properties with the options param.

    Parameters

    • Optional options: ScanOptions

      Used in building the scan params.

    Returns Promise<ScanOutput>

    Promise with the scan results, including items fetched.

  • Creates the params that can be used when calling the DocumentClient.scan method.

    Parameters

    • Optional options: ScanOptions

      Used in building the scan params.

    Returns ScanInput

    DocumentClient scan method's params containing the table, index and options.

Generated using TypeDoc