> For the complete documentation index, see [llms.txt](https://docs.flexbase.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flexbase.in/data-and-providers/data-stores/document-db/raven-db.md).

# Raven Db

## Description

RavenDB is used as a **Document DB provider** behind `IFlexDocumentStore`. Generated infrastructure manages cluster URLs, certificates, and conventions while your handlers/queries stay consistent across providers.

## Important Concepts

* **Cluster/conventions are centralized**: provider wiring handles the Raven document store configuration.
* **AppContext is mandatory**: always call `dto.GetAppContext()`.
* **Provider-agnostic business code**: you depend on `IFlexDocumentStore`.

## Configuration in DI (where to add)

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

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

```csharp
// Infrastructure (example)
services.AddFlexRavenDocumentStore<TicketDocument>(configuration);
```

## appsettings.json

```json
{
	"FlexBase": {
		"DataStores": {
			"Document": {
				"RavenDb": {
					"Urls": ["https://raven1.company.local", "https://raven2.company.local"],
					"DatabaseName": "{YourApplication}",
					"CertificatePath": "<optional path>",
					"CertificatePassword": "<optional password>",
					"UseOptimisticConcurrency": true
				}
			}
		}
  }
}
```

## Examples (from the generated templates)

Raven uses the standard DocumentStore templates.

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

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

public class GetTicketById : FlexiQueryBridgeAsync<TicketDto>
{
	protected readonly ILogger<GetTicketById> _logger;
	protected readonly IFlexHost _flexHost;
	protected readonly IFlexDocumentStore _documentStore;
	protected GetTicketByIdParams _params;
	protected FlexAppContextBridge _flexAppContext;

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

	public virtual GetTicketById AssignParameters(GetTicketByIdParams @params)
	{
		_params = @params;
		return this;
	}

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

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

## Provider-specific considerations

* **Certificates**: keep cert material in your secrets provider; only reference names/paths in config.
* **Cluster URLs**: prefer multiple URLs for failover.
* **Indexes**: predefine key indexes so queries don’t rely on ad-hoc index creation.
