> For the complete documentation index, see [llms.txt](https://docs.adiom.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.adiom.io/reference/connectors/dynamodb.md).

# DynamoDB

The DynamoDB connector is supported both as a source and a destination. It is built into dsync/dsynct. This page describes it as a source. As a destination, the connector writes the initial sync only: change events are accepted but not applied.

## Configuration

Specify `dynamodb` as the source. Credentials and region are taken from the standard AWS environment (for example `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION`, or an AWS CLI profile).

```
dynamodb
```

| Option             | Default  | Description                                                                                                     |
| ------------------ | -------- | --------------------------------------------------------------------------------------------------------------- |
| `--number-type`    | `string` | How DynamoDB numbers (`N`) are emitted in BSON: `string`, `int64`, `int32`, `float64` or `number` (Decimal128). |
| `--bson-id-format` | `binary` | How the BSON `_id` is built: `binary` or `composite`. See [ID Format](#id-format).                              |

CDC requires [DynamoDB Streams](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html) to be enabled on each table with the "New image" or "New and old images" view type.

## Data Type

The DynamoDB connector can emit **BSON** or **JSON**. Dsync picks BSON when the destination accepts it (for example MongoDB) and JSON otherwise (for example Cosmos DB NoSQL).

## ID Format

The ID is built from the table's key attributes: the partition key, then the sort key if the table has one. The connector also writes the ID into the document, replacing any existing attribute of the same name: `_id` for BSON, `id` for JSON.

| Data type         | Table keys             | ID field written to the document                       | ID parts on CDC events                               |
| ----------------- | ---------------------- | ------------------------------------------------------ | ---------------------------------------------------- |
| JSON              | Partition key only     | `id`: the key value as a string                        | One part, named after the key attribute              |
| JSON              | Partition and sort key | `id`: both values joined with `-`, for example `"a-7"` | Three parts: partition key, sort key, combined `id`  |
| BSON, `binary`    | Partition key only     | `_id`: the key value                                   | One part                                             |
| BSON, `binary`    | Partition and sort key | `_id`: a binary value encoding both keys               | One part                                             |
| BSON, `composite` | Partition key only     | `_id`: the key value as a string                       | One part                                             |
| BSON, `composite` | Partition and sort key | `_id`: both values joined with `-`                     | Three parts: partition key, sort key, combined `_id` |

In JSON, all ID parts are strings, even for numeric keys. With `composite`, the partition and sort key parts keep the type chosen by `--number-type`.

If a key attribute is itself named `id`, the joined value is not used: the document `id` (and the combined ID part) is the value of that attribute alone, and the other key only appears as its own part.

## Emitted Format

This is what the DynamoDB source hands to Dsync, and therefore what a [transformation](https://docs.adiom.io/enterprise/running-dsynct/data-transformations.md) receives.

**Namespace:** the table name.

**Initial sync:** each item is emitted with all of its attributes, including the key attributes, plus the ID field described above.

| DynamoDB type    | BSON                                        | JSON          |
| ---------------- | ------------------------------------------- | ------------- |
| `S`              | String                                      | String        |
| `N`              | Set by `--number-type`; a string by default | Number        |
| `B`              | Binary                                      | Base64 string |
| `BOOL`           | Boolean                                     | Boolean       |
| `NULL`           | Null                                        | `null`        |
| `L`              | Array                                       | Array         |
| `M`              | Document                                    | Object        |
| `SS`, `NS`, `BS` | Array                                       | Array         |

**CDC:** changes are read from DynamoDB Streams.

| Stream event | Event type | Payload                               |
| ------------ | ---------- | ------------------------------------- |
| `INSERT`     | `INSERT`   | The full new item, with the ID field. |
| `MODIFY`     | `UPDATE`   | The full new item, with the ID field. |
| `REMOVE`     | `DELETE`   | None.                                 |

### Example

Item in table `orders` with partition key `pk` (string) and sort key `sk` (number):

```json
{"pk": {"S": "a"}, "sk": {"N": "7"}, "count": {"N": "42"}, "tags": {"SS": ["x", "y"]}}
```

Emitted as JSON (for example to Cosmos DB NoSQL):

| Stage          | ID                     | Payload                                                              |
| -------------- | ---------------------- | -------------------------------------------------------------------- |
| Initial sync   | (part of the document) | `{"pk": "a", "sk": 7, "count": 42, "tags": ["x", "y"], "id": "a-7"}` |
| Insert, update | `["a", "7", "a-7"]`    | `{"pk": "a", "sk": 7, "count": 42, "tags": ["x", "y"], "id": "a-7"}` |
| Delete         | `["a", "7", "a-7"]`    | None                                                                 |

Emitted as BSON with the default options (for example to MongoDB):

| Stage          | ID                     | Payload                                                                      |
| -------------- | ---------------------- | ---------------------------------------------------------------------------- |
| Initial sync   | (part of the document) | `{"pk": "a", "sk": "7", "count": "42", "tags": ["x", "y"], "_id": <binary>}` |
| Insert, update | `<binary>`             | `{"pk": "a", "sk": "7", "count": "42", "tags": ["x", "y"], "_id": <binary>}` |
| Delete         | `<binary>`             | None                                                                         |

### Things to Know

* In BSON, numbers are strings unless you set `--number-type`. Choose the number type first, then write type conversions in the transform only for the fields that need something different.
* In JSON, numbers are converted to floating point, which loses precision above 2^53. Use `--number-type number` to keep the exact value.
* In JSON, an item attribute named `id` is overwritten by the generated ID unless it is a key attribute, in which case the generated ID has the same value. In BSON, an attribute named `_id` is always overwritten.
* With a sort key, the joined ID is ambiguous if key values can contain `-`. Build your own ID in a transform from the separate parts if that matters.
* DynamoDB sets lose their set semantics and become arrays.
* The connector does not support fetching items by ID, so `testsync`, Verify IDs and the Sync Tester's Test Sync cannot use it as a source. Test transforms in Transform Studio with sample items, then with a sync into a test destination.
