feat(snippets): initialize snippet db

This commit creates the initial backend infrastructure for the snippets feature. It sets up the core data model, a dedicated `SnippetManager` for database interactions using `rusqlite`, and integrates it into the Tauri application state.
This commit is contained in:
ByteAtATime 2025-06-24 18:46:47 -07:00
parent 21cd217a26
commit 1a2fa36a79
No known key found for this signature in database
4 changed files with 60 additions and 4 deletions

View file

@ -0,0 +1,36 @@
use crate::error::AppError;
use rusqlite::{Connection, Result as RusqliteResult};
use std::sync::Mutex;
use tauri::{AppHandle, Manager};
pub struct SnippetManager {
db: Mutex<Connection>,
}
impl SnippetManager {
pub fn new(app_handle: AppHandle) -> Result<Self, AppError> {
let data_dir = app_handle
.path()
.app_local_data_dir()
.map_err(|_| AppError::DirectoryNotFound)?;
let db_path = data_dir.join("snippets.sqlite");
let db = Connection::open(db_path)?;
Ok(Self { db: Mutex::new(db) })
}
pub fn init_db(&self) -> RusqliteResult<()> {
let db = self.db.lock().unwrap();
db.execute(
"CREATE TABLE IF NOT EXISTS snippets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
keyword TEXT NOT NULL UNIQUE,
content TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)",
[],
)?;
Ok(())
}
}

View file

@ -1 +1,3 @@
pub mod input_manager;
pub mod input_manager;
pub mod manager;
pub mod types;

View file

@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Snippet {
pub id: i64,
pub name: String,
pub keyword: String,
pub content: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}