# Dynamo Db

## Description

DynamoDB is used as a **Document DB provider** behind the Flex abstraction (`IFlexDocumentStore`). Generated wiring maps your logical document models to Dynamo tables, and your business logic stays consistent across providers.

## Important Concepts

* **Table metadata is config-driven**: partition key / sort key and table names are configured.
* **Queries/handlers remain provider-agnostic**: you use `IFlexDocumentStore`.
* **AppContext is mandatory**: always call `dto.GetAppContext()`.

## Configuration in DI (where to add)

Provider wiring is generated in your Infrastructure project (under a `...DataStoreProviders/Document/DynamoDb` folder). You typically do **not** register individual queries/handlers here — Flex generates and wires those.

If you are wiring DynamoDB manually, add the **document-store provider** registration where you configure infrastructure services:

```csharp
// Infrastructure (example)
services.AddFlexDynamoDocumentStore<OrderDocument>(configuration);
```

## appsettings.json

```json
{
	"FlexBase": {
		"DataStores": {
			"Document": {
				"DynamoDb": {
					"Region": "us-east-1",
					"AccessKeyId": "<from-secrets>",
					"SecretAccessKey": "<from-secrets>",
					"TableName": "Orders",
					"PartitionKeyName": "id",
					"UseLocalStack": false,
					"ServiceUrl": null
				}
			}
		}
	}
}
```

## Examples (from the generated templates)

Dynamo uses the same generated shapes as other Document DB providers.

### Query example (Get-by-id)

```csharp
using Microsoft.Extensions.Logging;
using Sumeru.Flex;
using System;
using System.Threading.Tasks;

public class GetOrderById : FlexiQueryBridgeAsync<OrderDto>
{
		protected readonly ILogger<GetOrderById> _logger;
		protected readonly IFlexHost _flexHost;
		protected readonly IFlexDocumentStore _documentStore;
		protected GetOrderByIdParams _params;
		protected FlexAppContextBridge _flexAppContext;

		public GetOrderById(ILogger<GetOrderById> logger, IFlexHost flexHost, IFlexDocumentStore documentStore)
		{
				_logger = logger;
				_flexHost = flexHost;
				_documentStore = documentStore;
		}

		public virtual GetOrderById AssignParameters(GetOrderByIdParams @params)
		{
				_params = @params;
				return this;
		}

		public virtual async Task<OrderDto?> Fetch()
		{
				_flexAppContext = _params.GetAppContext();
				var doc = await _documentStore.GetByIdAsync<OrderDocument>(_params.Id);
				return doc == null ? null : new OrderDto { Id = doc.Id, Status = doc.Status };
		}
}

public class GetOrderByIdParams : DtoBridge
{
		public Guid Id { get; set; }
}
```

## Provider-specific considerations

* **Keys**: design your partition/sort keys around your read patterns.
* **Capacity mode**: provisioned vs on-demand is an environment choice; keep it in configuration.
* **TTL/Streams**: enable TTL for lifecycle cleanup and streams when you need change capture.
