Options
All
  • Public
  • Public/Protected
  • All
Menu

Object that represents the DynamoDB table.

In most single table designs secondary indexes will be used like in the following example:

example

examples/Table.ts (imports: examples/Index.ts)

import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { Table } from 'dynamodb-datamodel';
import { GSI0Key, gsi0, LSI0Key, lsi0 } from './Index';
export { GSI0Key, gsi0, LSI0Key, lsi0 }; // export from here to consolidate imports

// Good practice to covert empty values to null
export const client = new DocumentClient({ convertEmptyValues: true });

// Define the table primary key interface.
export interface TableKey {
P: Table.PrimaryKey.PartitionString;
S?: Table.PrimaryKey.SortString;
}

// Define the combined primary keys across the table and secondary indexes.
interface KeyAttributes extends TableKey, GSI0Key, LSI0Key {}

// Create the table object with global and local secondary indexes.
// name, keyAttributes and keySchema should match the DynamoDB's table CloudFormation resource.
export const table = Table.createTable<TableKey, KeyAttributes>({
client,
name: 'ExampleTable',
// Defines the attribute type ('S', 'N', 'B') for all primary keys, table and indexes.
keyAttributes: {
P: Table.PrimaryKey.StringType,
S: Table.PrimaryKey.StringType,
G0P: Table.PrimaryKey.StringType,
G0S: Table.PrimaryKey.StringType,
L0S: Table.PrimaryKey.NumberType,
},
// Defines the key type ('HASH' or 'RANGE') for the table primary keys.
keySchema: {
P: Table.PrimaryKey.PartitionKeyType,
S: Table.PrimaryKey.SortKeyType,
},
});

Hierarchy

Index

Constructors

  • Parameters

    • params: TableParams

      Initialize the Table's name, attributes, keySchema and index properties.

    Returns Table

Properties

keyAttributes: AttributeTypesMap

Definition of the attribute types required for table and index primary key and for index projected attributes. These need to be defined at the table level since the attributes are table wide concept.

keySchema: KeyTypesMap

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

name: string

Name of the DynamoDB table, used to set the TableName when calling DynamoDB methods.

onError: (msg: string) => void = ...

Type declaration

    • (msg: string): void
    • Determines how errors should be handled. The default is to throw on any errors.

      Parameters

      • msg: string

      Returns void

Accessors

  • get client(): DocumentClient
  • Gets the DocumentClient associated with this Table, which may mean creating one.

    Returns DocumentClient

    The DocumentClient used for all Table operations.

Methods

  • createBinarySet(list: binaryType[], options?: CreateSetOptions): BinarySet
  • Create a binary set from a binary array.

    Parameters

    • list: binaryType[]

      Binary array to create set from.

    • Optional options: CreateSetOptions

      Options to pass DocumentClient createSet.

    Returns BinarySet

  • createNumberSet(list: number[], options?: CreateSetOptions): NumberSet
  • Create a number set from a number array.

    Parameters

    • list: number[]

      Number array to create set from.

    • Optional options: CreateSetOptions

      Options to pass DocumentClient createSet.

    Returns NumberSet

  • createSet(list: string[] | number[] | binaryType[], options?: CreateSetOptions): AttributeSetValues
  • Wrapper around the DocumentClient.createSet used by the below create*Set methods to create type safe sets. Choose to leverage DocumentClient implementation of set to allow DocumentClient to correctly auto convert to DynamoDB's native types.

    Parameters

    • list: string[] | number[] | binaryType[]

      Array of items to create the set from.

    • Optional options: CreateSetOptions

      Options to pass DocumentClient createSet.

    Returns AttributeSetValues

  • createStringSet(list: string[], options?: CreateSetOptions): StringSet
  • Create a string set from a string array.

    Parameters

    • list: string[]

      String array to create set from.

    • Optional options: CreateSetOptions

      Options to pass DocumentClient createSet.

    Returns StringSet

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

    Returns string

    The name of the primary (or HASH) key attribute.

  • Get the condition that is needed to support a specific PutWriteOptions.

    Parameters

    • options: undefined | PutWriteOptions

      Type of put to get the condition for.

    Returns void | Condition.Resolver

    Condition resolver that maps to the PutWriteOptions.

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

    Returns string

    The name of the sort (or RANGE) key attribute.

  • 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.

  • 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.

  • addBatchParams(options: { consumed?: string; metrics?: string; token?: string }, params: { ClientRequestToken?: string; ReturnConsumedCapacity?: string; ReturnItemCollectionMetrics?: string }): void
  • Translates options into batch or transact based input params.

    Parameters

    • options: { consumed?: string; metrics?: string; token?: string }

      Common batch or transact options to set on params.

      • Optional consumed?: string
      • Optional metrics?: string
      • Optional token?: string
    • params: { ClientRequestToken?: string; ReturnConsumedCapacity?: string; ReturnItemCollectionMetrics?: string }

      Input params to set based on options.

      • Optional ClientRequestToken?: string
      • Optional ReturnConsumedCapacity?: string
      • Optional ReturnItemCollectionMetrics?: string

    Returns void

  • Appends attributes names to the ProjectionExpression for input params.

    Type parameters

    Parameters

    • params: T

      Params to add ProjectionExpression on.

    • attributes: Table.ExpressionAttributes

      Definition of the attribute types required for table and index primary key and for index projected attributes.

    • Optional itemAttributes: string[]

      List of attribute names to return.

    Returns void

    Params that have ProjectionExpression added to.

  • addWriteParams<T>(params: T, item: { conditions?: Condition.Resolver[]; returnFailure?: string }, addParams?: AddExpressionParams): T & { ConditionExpression?: string; FilterExpression?: string; KeyConditionExpression?: string; ProjectionExpression?: string; UpdateExpression?: string } & ExpressionAttributeParams
  • Add expressions properties to the params object.

    Type parameters

    • T: { ConditionExpression?: string; FilterExpression?: string; KeyConditionExpression?: string; ProjectionExpression?: string; UpdateExpression?: string } & ExpressionAttributeParams & { ReturnValuesOnConditionCheckFailure?: string }

    Parameters

    • params: T

      The params object to add expression properties to.

    • item: { conditions?: Condition.Resolver[]; returnFailure?: string }

      Transact write item used to build params.

    • Optional addParams: AddExpressionParams

    Returns T & { ConditionExpression?: string; FilterExpression?: string; KeyConditionExpression?: string; ProjectionExpression?: string; UpdateExpression?: string } & ExpressionAttributeParams

    Params object that was passed in with expression added.

  • Finds the partition or sort key name for a table or index key schema.

    Parameters

    • keySchema: KeyTypesMap

      Table key schema to find the primary key in.

    • type: KeyTypes

      Primary key type to look for, either HASH or RANGE.

    Returns string

    Name of the key if it exists otherwise an empty string.

  • Maps PutWriteOptions to one of the put based ItemActions used by Fields to tell the type of put operation.

    Parameters

    • Optional options: PutWriteOptions

      Put write option to map into a put item action.

    Returns PutItemActions

  • Checks if item action is a put based action.

    Parameters

    Returns boolean

    True if item action is a put action.

Generated using TypeDoc