DataType

Supported data type

Type

Full name

Require

N

Number

number

S

String

string

B

Binary

Buffer

BOOL

Boolean

boolean

NS

Number Set

number[ ]

SS

String Set

string[ ]

BS

Binary Set

Buffer[ ]

L

List

@DynamoEntity[ ] | Number[ ] | String[ ] | Boolean[ ]

M

Map

@DynamoEntity[ ]

Specify data type

@DynamoEntity()
class Cat {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    @DynamoProperty({
        keyType: KeyType.attr,
        dataType: DataType.S   // <<<
    })
    name!: string;

    constructor(id: number, name: string) {
        this.id = id;
        this.name = name;
    }
}

Scalar-data-type does not need to be specified.

[number, string, Boolean, Buffer] are inferred to [N, S, BOOL, B] respectively.

Nested entity

Only classes with @DynamoEntity be nested with DataType.M.

@DynamoEntity()
class Catnip {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    constructor(id: number) {
        this.id = id;
    }
}

@DynamoEntity()
class Cat {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    @DynamoProperty({ keyType: KeyType.attr })
    name!: string;

    @DynamoProperty({ keyType: KeyType.attr, dataType: DataType.M })  // <<<
    catnip!: Catnip;

    constructor(id: number, name: string, catnip: Catnip) {
        this.id = id;
        this.name = name;
        this.catnip = catnip;
    }
}

For example,

const badCat: Cat = new Cat(666, "garfield", new Catnip(777));
const dynamo: AttributeMap = Mapper.formation(badCat, Cat);

It will formationed as,

{
    "id": {
        "N": "666"
    },
    "name": {
        "S": "garfield"
    },
    "catnip": {
        "M": {  // <<<
            "id": {
                "N": "777"
            }
        }
    }
}

Array

Only classes with @DynamoEntity be list-element with DataType.L, sourceDataType.

@DynamoEntity()
class Catnip {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    constructor(id: number) {
        this.id = id;
    }
}

@DynamoEntity()
class Cat {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    @DynamoProperty({ keyType: KeyType.attr })
    name!: string;

    @DynamoProperty({ 
        keyType: KeyType.attr, 
        dataType: DataType.L,     // <<<
        sourceDataType: Catnip    // <<<
    })
    catnips!: Catnip[];

    constructor(id: number, name: string, catnips: Catnip[]) {
        this.id = id;
        this.name = name;
        this.catnips = catnips;
    }
}

For example,

const badCat: Cat = new Cat(666, "garfield", [new Catnip(777), new Catnip(888)]);
const dynamo: AttributeMap = Mapper.formation(badCat, Cat);

It will formationed as,

{
    "id": {
        "N": "666"
    },
    "name": {
        "S": "garfield"
    },
    "catnips": {
        "L": [
            {
                "id": {
                    "N": "777"
                }
            },
            {
                "id": {
                    "N": "888"
                }
            }
        ]
    }
}

Set

@DynamoEntity()
class Cat {
    @DynamoProperty({ keyType: KeyType.hash })
    id!: number;

    @DynamoProperty({ keyType: KeyType.attr })
    name!: string;

    @DynamoProperty({ 
        keyType: KeyType.attr, 
        dataType: DataType.NS   // <<<
    })
    favoriteNumbers!: number[];

    constructor(id: number, name: string, favoriteNumbers: number[]) {
        this.id = id;
        this.name = name;
        this.favoriteNumbers = favoriteNumbers;
    }
}

sourceDataType should not specified when using ScalarArray.

For example,

const badCat: Cat = new Cat(666, "garfield", [666, 777]);
const dynamo: AttributeMap = Mapper.formation(badCat, Cat);

It will formationed as,

{
    "id": {
        "N": "666"
    },
    "name": {
        "S": "garfield"
    },
    "favoriteNumbers": {
        "NS": [  // <<<
            "666",
            "777"
        ]
    }
}

Last updated

Was this helpful?