Skip to main content

veta_core/
db.rs

1use crate::{CreateNote, Error, Note, NoteQuery, TagCount, UpdateNote};
2
3/// Database abstraction that works for both SQLite and D1.
4///
5/// Uses `async_trait` with `?Send` bound for WASM compatibility.
6/// The `?Send` is critical - WASM is single-threaded and JS values aren't Send.
7#[async_trait::async_trait(?Send)]
8pub trait Database {
9    /// Add a new note and return its ID.
10    async fn add_note(&self, note: CreateNote) -> Result<i64, Error>;
11
12    /// Get a note by ID.
13    async fn get_note(&self, id: i64) -> Result<Option<Note>, Error>;
14
15    /// List notes matching the query.
16    async fn list_notes(&self, query: NoteQuery) -> Result<Vec<Note>, Error>;
17
18    /// Count notes matching the query (ignores limit).
19    async fn count_notes(&self, query: NoteQuery) -> Result<i64, Error>;
20
21    /// Update an existing note.
22    async fn update_note(&self, id: i64, update: UpdateNote) -> Result<bool, Error>;
23
24    /// Delete a note by ID. Returns true if deleted, false if not found.
25    async fn delete_note(&self, id: i64) -> Result<bool, Error>;
26
27    /// List all tags with their note counts.
28    async fn list_tags(&self) -> Result<Vec<TagCount>, Error>;
29
30    /// Search notes by pattern (regex) in title and body.
31    async fn grep(
32        &self,
33        pattern: &str,
34        tags: Option<&[String]>,
35        case_sensitive: bool,
36    ) -> Result<Vec<Note>, Error>;
37}