> For the complete documentation index, see [llms.txt](https://aerocode.gitbook.io/tynamo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aerocode.gitbook.io/tynamo/define-entity/define-property/datatype.md).

# 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

```typescript
@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;
    }
}
```

{% hint style="info" %}
Scalar-data-type does not need to be specified.

\[`number`, `string`, `Boolean`, `Buffer`] are inferred to \[`N`, `S`, `BOOL`, `B`] respectively.
{% endhint %}

### Nested entity

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

```typescript
@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,

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

It will formationed as,

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

### Array

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

```typescript
@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,

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

It will formationed as,

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

### Set

```typescript
@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;
    }
}
```

{% hint style="warning" %}
`sourceDataType` should not specified when using ScalarArray.
{% endhint %}

For example,

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

It will formationed as,

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