Skip to content

Database Configuration

Moka.Auth provides built-in support for multiple database providers and offers flexible configuration options for data storage and management.

Supported Providers

Moka.Auth supports the following database providers:

  • SQLite - Lightweight file-based database, perfect for development and small applications
  • PostgreSQL - Recommended for production applications with advanced features
  • SQL Server - Enterprise-grade relational database for Microsoft-centric environments
  • MySQL/MariaDB - Open-source relational database management system

Configuration Methods

Method 1: Using MokaAuthDbContext

The simplest approach, as shown in the Samples.JwtSqlite project:

// Configure Moka.Auth with SQLite
builder.Services.AddMokaAuthDbContext(dbOptions =>
{
dbOptions.ConnectionString = "Data Source=moka.db";
dbOptions.DbType = DbType.SQLite;
dbOptions.AutoMigrate = true;
});

Method 2: Via appsettings.json

Define your database settings in appsettings.json:

{
"MokaAuth": {
"Database": {
"Provider": "SQLite",
"ConnectionString": "Data Source=moka_auth.db",
"AutoMigrate": true,
"EnableSensitiveDataLogging": false
}
}
}

Then in your Program.cs, bind these settings:

// Option 1: Use binding from configuration
builder.Services.AddMokaAuthDbContext(dbOptions =>
{
builder.Configuration.GetSection("MokaAuth:Database").Bind(dbOptions);
});

Available Database Options

The DatabaseOptions class provides the following configuration:

OptionDescriptionDefault
ConnectionStringDatabase connection string""
DbTypeDatabase provider type (SQLite, PostgreSQL, MySQL, SqlServer)DbType.SQLite
AutoMigrateAutomatically apply migrations on startuptrue
SchemaNameSchema name (for PostgreSQL)null
MaxRetryCountMaximum number of retries for database operations5
MaxRetryDelaySecondsMaximum retry delay in seconds30
EnableSensitiveDataLoggingEnable sensitive data logging (not recommended for production)false
EnableDetailedErrorsEnable detailed error messagesfalse
CommandTimeoutSecondsCommand timeout in secondsnull

Provider-Specific Examples

SQLite Example (from Samples.JwtSqlite)

builder.Services.AddMokaAuthDbContext(dbOptions =>
{
dbOptions.ConnectionString = "Data Source=moka.db";
dbOptions.DbType = DbType.SQLite;
dbOptions.AutoMigrate = true;
});

PostgreSQL Example

builder.Services.AddMokaAuthDbContext(dbOptions =>
{
dbOptions.ConnectionString = "Host=localhost;Database=moka_auth;Username=user;Password=pass;";
dbOptions.DbType = DbType.PostgreSQL;
dbOptions.SchemaName = "auth"; // Optional schema
dbOptions.AutoMigrate = true;
});

SQL Server Example

builder.Services.AddMokaAuthDbContext(dbOptions =>
{
dbOptions.ConnectionString = "Server=(localdb)\\mssqllocaldb;Database=MokaAuth;Trusted_Connection=True;MultipleActiveResultSets=true";
dbOptions.DbType = DbType.SqlServer;
dbOptions.AutoMigrate = true;
});

MySQL Example

builder.Services.AddMokaAuthDbContext(dbOptions =>
{
dbOptions.ConnectionString = "Server=localhost;Database=moka_auth;User=root;Password=password;";
dbOptions.DbType = DbType.MySQL;
dbOptions.AutoMigrate = true;
});

Custom Database Context

If you need more control, you can use a custom database context:

// Using a custom database context
builder.Services.AddMokaAuthDbContext<CustomAuthDbContext>(dbOptions =>
{
dbOptions.ConnectionString = "Data Source=moka.db";
dbOptions.DbType = DbType.SQLite;
dbOptions.AutoMigrate = true;
});

Database Middleware

Don’t forget to add the database middleware to your application pipeline:

var app = builder.Build();
// ... other middleware
// Apply database configuration
app.UseMokaAuthDatabase();
// ... other middleware

Data Seeding

Moka.Auth provides several methods to seed your database with initial data. See the Quick Start Guide for detailed examples.