Expand description
§Monarch-DB
Monarch-DB is a lightweight SQLite database migration tool designed to run whenever the first connection in an app opens. It provides a simple, reliable way to manage SQLite database schema evolution in Rust applications.
§Quick Start
use monarch_db::{StaticMonarchConfiguration, MonarchDB, ConnectionConfiguration};
// Define your migrations at compile time
let config = StaticMonarchConfiguration {
name: "my_app",
enable_foreign_keys: true,
migrations: [
// Migration 1: Create users table
r#"
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"#,
// Migration 2: Create posts table
r#"
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
content TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
"#,
],
};
// Convert to MonarchDB instance
let monarch_db: MonarchDB = config.into();
// Create connection configuration
let connection_config = ConnectionConfiguration {
database: None, // Use in-memory database for this example
};
// Create database connection with migrations applied
let connection = monarch_db.create_connection(&connection_config)?;
// Use your database normally
connection.execute(
"INSERT INTO users (username, email) VALUES (?, ?)",
["alice2", "alice2@example.com"],
)?;§Directory-Based Configuration
Use directory-based configuration when you want to manage migrations as separate files:
use monarch_db::{MonarchConfiguration, MonarchDB, ConnectionConfiguration};
let config = MonarchConfiguration {
name: "my_app".to_string(),
enable_foreign_keys: true,
migration_directory: "./migrations".into(),
};
let monarch_db = MonarchDB::from_configuration(config)?;
let connection_config = ConnectionConfiguration {
database: Some("./my_app.db".into()),
};
let connection = monarch_db.create_connection(&connection_config)?;
// Database is ready with all migrations applied§Configuration Types
StaticMonarchConfiguration- For compile-time embedded migrationsMonarchConfiguration- For runtime directory-based migrationsConnectionConfiguration- For specifying database file paths
§Core Types
MonarchDB- Main migration manager that applies schema changesMigrations- Helper for applying migrations to database connections
Structs§
- Connection
Configuration - Configuration for opening a new SQLite database connection.
- Migrations
- Helper struct for applying migrations to a database connection.
- Monarch
Configuration - Configuration for MonarchDB that loads migrations from a directory at runtime.
- MonarchDB
- MonarchDB manages schema migrations and new connections for a database.
- Static
Monarch Configuration - Configuration for MonarchDB with compile-time known migrations.