Skip to content

RavenDb: Ensure Database created #1024

@JohnGalt1717

Description

@JohnGalt1717

Related to an existing integration?

Yes

Existing integration

CommunityToolkit.Aspire.Hosting.RavenDB

Overview

Right now the database is ensured created based on the parameter in the settings that you can override.

This means that every project that you load is going to call this, and if it isn't there is going to cause multiple requests for the same database on each of those.

It would be preferable to have a parameter on the AddDatabase call that would ensure created and this implementation hooked OnInitializeResource and did the ensure created using the connection string that was created.

This would default to false for backwards compatibility.

Usage example

.AddDatabase(name, databaseName, true)

Breaking change?

No

Alternatives

Hack it and add the event manually and do it like this:

    var databaseBuilder = builder.AddDatabase(name, databaseName);

    databaseBuilder.OnInitializeResource(
        async (resource, evt, ct) =>
        {
            if (!createIfNotExists)
            {
                return;
            }

            var connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct);

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException(
                    "RavenDB connection string is not available."
                );
            }

            //Create the database
            var settings = RavenDBClientSettings.Parse(connectionString);

            var store = settings.GetStore();
            store.EnsureDatabaseExists();
        }
    );

    return databaseBuilder;

Additional context

No response

Help us help you

No, just wanted to propose this

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions