pub struct BearDb { /* private fields */ }Expand description
Handle to Bear’s database. All operations use short-lived connections internally.
Implementations§
Source§impl BearDb
impl BearDb
Sourcepub fn new() -> Result<Self, BearError>
pub fn new() -> Result<Self, BearError>
Create a new BearDb handle. Opens a temporary connection to discover schema metadata, generates normalizing CTEs, then closes the connection.
Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Retrieves all tags from Bear
Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Sourcepub fn notes(&self, query: NotesQuery) -> Result<Vec<BearNote>, BearError>
pub fn notes(&self, query: NotesQuery) -> Result<Vec<BearNote>, BearError>
Retrieves notes from Bear, ordered by most recently modified.
§Examples
let db = BearDb::new()?;
// Get 10 most recent notes (default)
let notes = db.notes(NotesQuery::default())?;
// Get 20 most recent notes
let notes = db.notes(NotesQuery::new().limit(20))?;
// Get all notes including trashed and archived
let notes = db.notes(NotesQuery::new().no_limit().include_all())?;Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Sourcepub fn note_links(&self, from: BearNoteId) -> Result<Vec<BearNote>, BearError>
pub fn note_links(&self, from: BearNoteId) -> Result<Vec<BearNote>, BearError>
Retrieves all notes linked from the specified note
Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Retrieves all tag IDs associated with the specified note
Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Sourcepub fn query(&self, sql: &str) -> Result<DataFrame, BearError>
pub fn query(&self, sql: &str) -> Result<DataFrame, BearError>
Execute a generic SQL SELECT query and return results as a Polars DataFrame.
The query automatically has the normalizing CTEs prepended, so you can query
against clean table names: notes, tags, note_tags, note_links.
§Safety
This method trusts the read-only connection flags to prevent writes. Only SELECT queries should be used, though this is not enforced by the library.
§Examples
let db = BearDb::new()?;
// Query normalized tables
let df = db.query("SELECT title, modified FROM notes LIMIT 5")?;
// Join tables
let df = db.query(r"
SELECT n.title, t.name as tag_name
FROM notes n
JOIN note_tags nt ON n.id = nt.note_id
JOIN tags t ON nt.tag_id = t.id
WHERE n.is_trashed = 0
LIMIT 10
")?;
println!("{}", df);Examples found in repository?
3fn main() -> Result<(), BearError> {
4 let db = BearDb::new()?;
5
6 // Example 1: Using typed API
7 println!("=== Typed API Examples ===\n");
8
9 let tags = db.tags()?;
10 println!("Total tags: {}\n", tags.count());
11
12 // Using the new NotesQuery API - much more readable!
13 db.notes(NotesQuery::default())?
14 .into_iter()
15 .for_each(|note| {
16 println!("Note: {:?}", note.title());
17 db.note_links(note.id())
18 .unwrap()
19 .into_iter()
20 .for_each(|link| println!(" Linked: {:?}", link.title()));
21
22 let note_tags = db.note_tags(note.id()).unwrap();
23 println!(" Tags: {:?}\n", tags.names(¬e_tags));
24 });
25
26 // Example 2: Using generic SQL query API with DataFrames
27 println!("\n=== Generic SQL Query API Examples ===\n");
28
29 // Simple query
30 println!("Top 5 most recent notes:");
31 let df = db
32 .query("SELECT title, created FROM notes WHERE is_trashed = 0 ORDER BY created DESC LIMIT 5")?;
33 println!("{}\n", df);
34
35 // Join query
36 println!("Notes with their tags:");
37 let df = db.query(
38 r"
39 SELECT n.title, t.name as tag_name
40 FROM notes n
41 JOIN note_tags nt ON n.id = nt.note_id
42 JOIN tags t ON nt.tag_id = t.id
43 WHERE n.is_trashed = 0
44 ORDER BY n.modified DESC
45 LIMIT 10
46 ",
47 )?;
48 println!("{}\n", df);
49
50 // Aggregation query
51 println!("Tag usage statistics:");
52 let df = db.query(
53 r"
54 SELECT t.name, COUNT(*) as note_count
55 FROM tags t
56 JOIN note_tags nt ON t.id = nt.tag_id
57 GROUP BY t.id, t.name
58 ORDER BY note_count DESC
59 LIMIT 10
60 ",
61 )?;
62 println!("{}\n", df);
63
64 Ok(())
65}Auto Trait Implementations§
impl Freeze for BearDb
impl RefUnwindSafe for BearDb
impl Send for BearDb
impl Sync for BearDb
impl Unpin for BearDb
impl UnwindSafe for BearDb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more