# putItem

### Spec

```typescript
async putItem<TSource>(
    tnmInput: TynamoPutItemInput<TSource>
): Promise<TynamoPutItemOutput<TSource>>;
```

### Input&#x20;

```typescript
export interface TynamoPutItemInput<TSource> {
    Item: TSource;
    ExpressionAttributeValues?: any;
    
    // Dervied from DynamoDB
    ReturnValues?: ReturnValue;
    ReturnConsumedCapacity?: ReturnConsumedCapacity;
    ReturnItemCollectionMetrics?: ReturnItemCollectionMetrics;
    ExpressionAttributeNames?: ExpressionAttributeNameMap;
    ConditionExpression?: ConditionExpression;
}
```

| Name                      | Type            | Info                           |
| ------------------------- | --------------- | ------------------------------ |
| Item                      | `@DynamoEntity` | Item to insert into the table. |
| ExpressionAttributeValues | `@DynamoEntity` | Value used for expression.     |

{% hint style="info" %}
Unlisted param is derived from `DynamoDB`.

Check [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html) for more information.
{% endhint %}

### Output

```typescript
export interface TynamoPutItemOutput<TSource> {
    Attributes?: TSource;
    
    // Derived from DynamoDB.
    $response: AWS.Response<PutItemOutput, AWS.AWSError>;
    ConsumedCapacity?: ConsumedCapacity;
    ItemCollectionMetrics?: ItemCollectionMetrics;
}
```

| Name       | Type            | Info                                                                                                                                     |
| ---------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| Attributes | `@DynamoEntity` | The attribute values as they appeared before the `PutItem` operation, but only if ReturnValues is specified as `ALL_OLD` in the request. |

{% hint style="info" %}
Unlisted param is derived from `DynamoDB`.

Check [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html) for more information.
{% endhint %}

### Example

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

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

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

const tynamo: Tynamo = new Tynamo({
    region: "ap-northeast-2",
    endpoint: "http://localhost:8000"
});
const tynamoTable = tynamo.getTableOf(Cat);


// Insert if, garfield is not exist.
await tynamoTable.putItem({
    Item: new Cat(666, "garfield"),
    ConditionExpression: "attribute_not_exists(#id)"
});
```


---

# 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/tynamo-1/create-tynamo/tynamotable/putitem.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.
