nostr_database/
lib.rs

1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Copyright (c) 2023-2025 Rust Nostr Developers
3// Distributed under the MIT software license
4
5//! Nostr Database
6
7#![warn(missing_docs)]
8#![warn(rustdoc::bare_urls)]
9#![warn(clippy::large_futures)]
10#![allow(clippy::mutable_key_type)] // TODO: remove when possible. Needed to suppress false positive for `BTreeSet<Event>`
11
12use std::sync::Arc;
13
14pub use nostr;
15
16mod collections;
17mod error;
18mod events;
19#[cfg(feature = "flatbuf")]
20pub mod flatbuffers;
21pub mod memory;
22pub mod prelude;
23pub mod profile;
24mod wipe;
25
26pub use self::collections::events::Events;
27pub use self::error::DatabaseError;
28pub use self::events::helper::{DatabaseEventResult, DatabaseHelper};
29pub use self::events::{
30    DatabaseEventStatus, IntoNostrEventsDatabase, NostrEventsDatabase, NostrEventsDatabaseExt,
31    RejectedReason, SaveEventStatus,
32};
33#[cfg(feature = "flatbuf")]
34pub use self::flatbuffers::{FlatBufferBuilder, FlatBufferDecode, FlatBufferEncode};
35pub use self::memory::{MemoryDatabase, MemoryDatabaseOptions};
36pub use self::profile::Profile;
37pub use self::wipe::NostrDatabaseWipe;
38
39/// Backend
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum Backend {
42    /// Memory
43    Memory,
44    /// RocksDB
45    RocksDB,
46    /// Lightning Memory-Mapped Database
47    LMDB,
48    /// SQLite
49    SQLite,
50    /// IndexedDB
51    IndexedDB,
52    /// Custom
53    Custom(String),
54}
55
56impl Backend {
57    /// Check if it's a persistent backend
58    ///
59    /// All values different from [`Backend::Memory`] are considered persistent
60    pub fn is_persistent(&self) -> bool {
61        !matches!(self, Self::Memory)
62    }
63}
64
65#[doc(hidden)]
66pub trait IntoNostrDatabase {
67    fn into_nostr_database(self) -> Arc<dyn NostrDatabase>;
68}
69
70impl IntoNostrDatabase for Arc<dyn NostrDatabase> {
71    fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
72        self
73    }
74}
75
76impl<T> IntoNostrDatabase for T
77where
78    T: NostrDatabase + Sized + 'static,
79{
80    fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
81        Arc::new(self)
82    }
83}
84
85impl<T> IntoNostrDatabase for Arc<T>
86where
87    T: NostrDatabase + 'static,
88{
89    fn into_nostr_database(self) -> Arc<dyn NostrDatabase> {
90        self
91    }
92}
93
94/// Nostr Database
95pub trait NostrDatabase: NostrEventsDatabase + NostrDatabaseWipe {
96    /// Name of the backend database used
97    fn backend(&self) -> Backend;
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn test_backend_is_persistent() {
106        assert!(!Backend::Memory.is_persistent());
107        assert!(Backend::RocksDB.is_persistent());
108        assert!(Backend::LMDB.is_persistent());
109        assert!(Backend::SQLite.is_persistent());
110        assert!(Backend::IndexedDB.is_persistent());
111        assert!(Backend::Custom("custom".to_string()).is_persistent());
112    }
113}