Options
All
  • Public
  • Public/Protected
  • All
Menu

The Model object that wraps access to the DynamoDB table and makes it easy to map table data to and from model data.

example

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

import { Fields, Model, Table, Update } from 'dynamodb-datamodel';
import { table } from './Table';

// (TypeScript) Define model key and item interface.
export interface ModelKey {
id: string;
}
// Use Update.* types to support type checking when using Model.update.
export interface ModelItem extends ModelKey {
name: Update.String;
age?: Update.Number;
children?: Update.List<{ name: string; age: number }>;
sports?: Update.StringSet;
}

// Define the schema using Fields
export const model = Model.createModel<ModelKey, ModelItem>({
schema: {
id: Fields.split({ aliases: ['P', 'S'] }),
name: Fields.string(),
age: Fields.number(),
children: Fields.list(),
sports: Fields.stringSet(),
},
table: table as Table,
});

// Generate params to pass to DocumentClient or call the action method
const params = model.getParams({ id: 'P-1.S-1' });

// (jest) output of getParams
expect(params).toEqual({ Key: { P: 'P-1', S: 'S-1' }, TableName: 'ExampleTable' });

Hierarchy

Implements

Index

Constructors

  • Initialize this class with params.

    Parameters

    • params: ModelParams

      Params to initialize this class with.

    Returns Model

Properties

name?: string

The type name of the model. Used by {@link Fields."type"} to set a type attribute.

schema: ModelSchema

Schema to use for mapping data between the model and table data.

table: Table

Table to used for reading and writing model data to dynamodb.

Methods

  • Adds a delete item by key to a batch write operation.

    Parameters

    • batchWrite: BatchWrite

      BatchWrite operation to added this key to.

    • key: ModelCore

      Key of item to delete in BatchWrite.

    Returns ModelResult

    Object to get the resulting item from the BatchWrite result.

  • Adds a get item by key to a batch get operation.

    Parameters

    • batchGet: BatchGet

      BatchGet operation to added this key to.

    • key: ModelCore

      Key of item to fetch in BatchGet.

    Returns ModelResult

    Object to get the resulting item from the BatchGet result.

  • Adds a put item to a batch write operation.

    Parameters

    • batchWrite: BatchWrite

      BatchWrite operation to added this key to.

    • item: ModelData

      Item added to the batch write operation.

    Returns ModelResult

    Object to get the resulting item from the BatchWrite result.

  • Adds an item condition check to a transact write operation.

    Parameters

    • transactWrite: TransactWrite

      TransactWrite operation to added this item condition check to.

    • key: ModelCore

      Key of item to used in condition check.

    • conditions: Condition.Resolver[]

      List of conditions to validate when executing the transact write.

    • Optional returnFailure: string

      Determines what to return on transaction failure.

    Returns ModelResult

    Object to get the resulting item from the TransactWrite result.

  • Adds an item delete to a transact write operation.

    Parameters

    • transactWrite: TransactWrite

      TransactWrite operation to added this delete item to.

    • key: ModelCore

      Key of item to delete in transact write.

    • Optional conditions: Condition.Resolver[]

      List of conditions to validate when executing the transact write.

    • Optional returnFailure: string

      Determines what to return on transaction failure.

    Returns ModelResult

    Object to get the resulting item from the TransactWrite result.

  • Adds a get item to a transact get operation.

    Parameters

    • transactGet: TransactGet

      TransactGet operation to added this key to.

    • key: ModelCore

      Key of item to fetch in BatchGet.

    • Optional itemAttributes: string[]

      List of attributes to fetch in TransactGet, if this is empty then all item attributes are return.

    Returns ModelResult

    Object to get the resulting item from the TransactGet result.

  • Adds an item put to a transact write operation.

    Parameters

    • transactWrite: TransactWrite

      TransactWrite operation to added this put item to.

    • item: ModelData

      Item to put in the transact write.

    • Optional conditions: Condition.Resolver[]

      List of conditions to validate when executing the transact write.

    • Optional returnFailure: string

      Determines what to return on transaction failure.

    Returns ModelResult

    Object to get the resulting item from the TransactWrite result.

  • Adds an item update to a transact write operation.

    Parameters

    • transactWrite: TransactWrite

      TransactWrite operation to added this update item to.

    • item: ModelUpdate

      Item to update in transact write.

    • Optional conditions: Condition.Resolver[]

      List of conditions to validate when executing the transact write.

    • Optional returnFailure: string

      Determines what to return on transaction failure.

    Returns ModelResult

    Object to get the resulting item from the TransactWrite result.

  • Wrapper method for DocumentClient.get method. That uses the model as input and output.

    Parameters

    • key: ModelCore

      Model key of item to get.

    • options: GetOptions = {}

      Additional optional options to use for get.

    Returns Promise<GetOutput<ModelOut>>

    An async promise that contains the model data and the table get result object.

  • Converts table item data to model data. Method called from the in the model methods after reading or writing data to the table to convert the item and attribute output to model properties.

    Parameters

    • data: undefined | Table.AttributeValuesMap

      Table item data to convert to model data.

    • context: ModelContext

      Context used for converting table to model data, passed to each field object.

    Returns ModelOut

    Model data converted from the table data.

  • Converts model data to table data. Methods called called from the model methods before reading or writing data to the table, to convert the model data to the table data.

    Parameters

    • data: ModelData

      Model data to convert to table data.

    • context: TableContext

      Context used for converting model to table data, passed to each field object.

    Returns TableData

    Table data converted from the model data.

  • Converts model update data to table update data, similar to toTable but since table updates supports attribute based update expressions updates are handled differently then other actions.

    Parameters

    • data: ModelUpdate

      Model update data to convert to table update data.

    • context: TableContext

      Context used for converting model to table data, passed to each field object.

    Returns TableUpdateData

    Table data converted from the model data.

  • Initializes each field in the schema with the model and associated property name.

    Parameters

    • schema: ModelSchema

      Model schema to initialize.

    • model: Model

      Model to use when initialize each field in the schema.

    Returns void

  • Helper method that splits the table data into a key and item.

    Parameters

    Returns TableData

    The key, item and raw converted model data.

Generated using TypeDoc