Options
All
  • Public
  • Public/Protected
  • All
Menu

Collection of functions for constructing a Model schema with Field objects and the Field classes.

example

examples/Fields.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 so model.update will have some type safety.
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.updateParams({ id: 'P-1.S-1', age: Update.inc(1) });

// (jest) output of updateParams
expect(params).toEqual({
ExpressionAttributeNames: { '#n0': 'age' },
ExpressionAttributeValues: { ':v0': 1 },
Key: { P: 'P-1', S: 'S-1' },
TableName: 'ExampleTable',
ReturnValues: 'ALL_NEW',
UpdateExpression: 'SET #n0 = #n0 + :v0',
});

Hierarchy

  • Fields

Index

Constructors

Methods

  • Creates an indices based slots composite field object which can then return FieldCompositeSlot by index to use in a Model.schema.

    example

    examples/Fields.composite.ts, (imports: examples/Table.ts)

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

    // (TypeScript) Define model key and item interface.
    interface ModelKey {
    id: string;
    }
    // street, city, state and country only support simple set updates since they are part of a composite key
    interface ModelItem extends ModelKey {
    neighborhood: string;
    city: string;
    state: string;
    country: string;
    region: Update.String;
    }

    // Create composite slots to use in model schema below.
    const location = Fields.composite({ alias: gsi0.getSortKey(), count: 4, delimiter: ';' });
    const locSlots = location.createSlots();

    // Define the schema using Fields
    const model = Model.createModel<ModelKey, ModelItem>({
    schema: {
    id: Fields.split({ aliases: ['P', 'S'] }),
    neighborhood: locSlots[3],
    city: locSlots[2],
    state: locSlots[1],
    country: locSlots[0],
    region: Fields.string({ alias: gsi0.getPartitionKey() }),
    },
    table: table as Table,
    });

    const params = model.putParams({
    id: 'p1.s2',
    neighborhood: 'Birdland',
    city: 'Cupertino',
    state: 'CA',
    country: 'USA',
    region: 'NorthAmerica',
    });

    // test: value to expect
    expect(params).toEqual({
    Item: { G0P: 'NorthAmerica', G0S: 'USA;CA;Cupertino;Birdland', P: 'p1', S: 's2' },
    TableName: 'ExampleTable',
    });

    Parameters

    Returns FieldComposite

    New composite object with array field slots.

  • Creates an name based slots composite field object which can then return FieldCompositeSlot by name to use in a Model.schema.

    example

    examples/Fields.compositeNamed.ts, (imports: examples/Table.ts)

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

    // Create composite slots to use in model schema below.
    const locMap = { neighborhood: 3, city: 2, state: 1, country: 0 };
    const location = Fields.compositeNamed({
    alias: gsi0.getSortKey(),
    map: locMap,
    delimiter: ';',
    });

    interface ModelIdKey {
    id: string;
    }

    // neighborhood, city, state and country only support simple set updates since they are part of a composite key
    interface ModelItem extends ModelIdKey {
    neighborhood: string;
    city: string;
    state: string;
    country: string;
    region: Update.String;
    }

    const locSlots = location.createNamedSlots();

    // Define the schema using Fields
    const model = Model.createModel<ModelIdKey, ModelItem>({
    schema: {
    id: Fields.split({ aliases: [table.getPartitionKey(), table.getSortKey()] }),
    neighborhood: locSlots.neighborhood,
    city: locSlots.city,
    state: locSlots.state,
    country: locSlots.country,
    region: Fields.string({ alias: gsi0.getPartitionKey() }),
    },
    table: table as Table,
    });

    const params = model.putParams({
    id: 'P-1.S-2',
    neighborhood: 'Birdland',
    city: 'Cupertino',
    state: 'CA',
    country: 'USA',
    region: 'NorthAmerica',
    });

    // test: value to expect
    expect(params).toEqual({
    Item: { G0P: 'NorthAmerica', G0S: 'USA;CA;Cupertino;Birdland', P: 'P-1', S: 'S-2' },
    TableName: 'ExampleTable',
    });

    Type parameters

    • T: {}

    Parameters

    Returns FieldCompositeNamed<T>

    New composite object with named field slots.

  • Creates a field that add a created date as a number in seconds since UTC UNIX epoch time (January 1, 1970 00:00:00 UTC) to a table attribute.

    Parameters

    Returns FieldCreatedNumberDate

    New FieldCreatedNumberDate object.

  • Creates a field that will be incremented with each update. It also supports preventing an update if the table attribute doesn't match the model property.

    Parameters

    Returns FieldRevision

    New FieldRevision object.

  • Creates a split field object to use in a Model.schema. which can be used to split a model property into two or more table attributes. This is commonly used as an model id property which gets slit into the table's partition and sort keys. Example: Model schema contains 'id: Fields.split({ aliases: ['P','S'] })' and when id = 'guid.date' the field will split the id value in to the table primary key of { P: 'guid', S: 'date' }

    example

    examples/Fields.split.ts, (imports: examples/Table.ts)

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

    // (TypeScript) Define model key and item interface.
    interface ModelKey {
    id: string;
    }

    // Define the schema using Fields
    const model = Model.createModel<ModelKey>({
    schema: {
    id: Fields.split({ aliases: ['P', 'S'] }),
    },
    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' });

    Parameters

    Returns FieldSplit

    New FieldSplit object.

  • Creates a field that adds the Model name to a table attribute.

    example

    examples/Fields.type.ts, (imports: examples/Table.ts)

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

    // (TypeScript) Define model key and item interface.
    interface ModelKey {
    id: string;
    }
    interface ModelItem extends ModelKey {
    typename?: Update.String;
    }

    // Define the schema using Fields
    const model = Model.createModel<ModelKey, ModelItem>({
    name: 'ExampleModel',
    schema: {
    id: Fields.split({ aliases: ['P', 'S'] }),
    typename: Fields.type({ alias: 'T' }),
    },
    table: table as Table,
    });

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

    // (jest) output of putParams
    expect(params).toEqual({ Item: { P: 'P-1', S: 'S-1', T: 'ExampleModel' }, TableName: 'ExampleTable' });

    Parameters

    • Optional options: TypeOptions

      Options to initialize field with.

    Returns FieldType

    New FieldType object.

  • Creates a field that adds an updated date as a number in seconds since UTC UNIX epoch time (January 1, 1970 00:00:00 UTC) to a table attribute.

    Parameters

    Returns FieldUpdatedNumberDate

    New FieldUpdatedNumberDate object.

Generated using TypeDoc