# 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"
        ]
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aerocode.gitbook.io/tynamo/define-entity/define-property/datatype.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
