Skip to content

Set up database connection with libsql #101

Closed

Description

@malibio

Overview

Set up database connection management using libsql (Turso embedded) with user-selectable database location via Tauri.

Context: Database Path Strategy

Path is provided by Tauri client (not hardcoded in core):

  • User-selectable: Folder picker dialog (can choose Dropbox/iCloud for auto-sync)
  • Default fallback: Platform-specific app data directory
  • Core library agnostic: packages/core just receives PathBuf

Implementation Tasks

  • Accept PathBuf parameter in DatabaseService::new()
  • Configure libsql database connection for local file
  • Implement connection initialization and error handling
  • Configure SQLite settings (WAL mode, foreign keys, JSON support)
  • Set up database initialization (CREATE TABLE IF NOT EXISTS)
  • Add connection error handling and recovery

Acceptance Criteria

  • DatabaseService accepts PathBuf from Tauri
  • libsql connection works with any valid file path
  • Connection initialization verifies database availability
  • SQLite features enabled (foreign keys, WAL mode, JSON operators)
  • Database initialization creates nodes + node_mentions tables
  • Pure JSON schema (no migrations, no ALTER TABLE)
  • Error handling for invalid paths or permission issues
  • Works with user-selected folders (Dropbox, iCloud, etc.)

Technical Specifications

Database Service:

use libsql::Database;
use std::path::PathBuf;

pub struct DatabaseService {
    db: Arc<Database>,
    db_path: PathBuf,
}

impl DatabaseService {
    pub async fn new(db_path: PathBuf) -> Result<Self> {
        // Ensure parent directory exists
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        
        let db = Database::open(&db_path).await?;
        
        Ok(Self { db: Arc::new(db), db_path })
    }
}

Tauri Integration (separate issue #110):

// User folder picker
#[tauri::command]
async fn select_db_location(app: AppHandle) -> Result<String> {
    let folder = app.dialog().file().pick_folder().await?;
    let db_path = folder.join("nodespace.db");
    
    // Save preference for next launch
    save_db_path_preference(&app, &db_path)?;
    
    // Initialize database at chosen location
    DatabaseService::new(db_path.clone()).await?;
    
    Ok(db_path.to_string_lossy().to_string())
}

Benefits:

  • Users choose where database lives (local-first flexibility)
  • Can select cloud-synced folders for backup
  • Core library stays platform-agnostic

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions