# 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.


---

# 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://docs.flexbase.in/data-and-providers/data-stores/document-db/raven-db.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.
