| Crates.io | nexus-common |
| lib.rs | nexus-common |
| version | 0.4.1 |
| created_at | 2025-07-01 08:06:01.744813+00 |
| updated_at | 2025-07-01 11:38:57.8216+00 |
| description | Nexus common utils |
| homepage | https://github.com/pubky/pubky-nexus |
| repository | https://github.com/pubky/pubky-nexus |
| max_upload_size | |
| id | 1732885 |
| size | 485,162 |
Nexus Common is a foundational crate that provides shared configuration, database connectors, data models and media processing utilities used throughout the Nexus indexer stack.
The nexus-common crate offers:
Configuration Management: TOML-based loader with default file generation, homeādir expansion, and asynchronous loading
Database Connectivity:
Connect to Neo4j and Redis using dedicated connectors. Use functions such as get_neo4j_graph() and get_redis_conn() to obtain connections for executing queries and handling errors.
Data Models:
Define core entities such as Files, Users, Posts, Tags, Notifications, and Follows. Each model provides functionality for caching, indexing, and graph operations.
Indexing and Caching:
Utilities to index and retrieve records from Redis. Includes support for pagination, lexicographical range queries, and sorted sets.
Shared Types and Traits:
Common types such as Pagination, Timeframe, and StreamSorting along with a collection of traits are provided to ensure a consistent interface across the Nexus backend.
Media Processing:
Process media files with support for image and video variant creation. The crate includes processors (like ImageProcessor and a prototype VideoProcessor) that generate different file variants (e.g. main, feed, small) and manage content types.
This crate is designed as a backbone for other services (e.g, homeserver watcher and API) crates in the Nexus stack, enabling consistent access to core functionalities and shared data structures.
config/ConfigLoader, default templates, home-dir expansion, async loadingdb/Neo4jConnector, RedisConnector, get_neo4j_graph(), get_redis_conn(), error handlingThe data models cover the core domain entities and their graph/cache operations:
The crate provides common types and utilities (types/) that are used across different modules, such as:
Pagination for paginated queriesTimeframe for filtering data based on time rangesStreamSorting for ordering streamsmedia/ImageProcessor, VideoProcessor, FileVariant, and VariantController for automated processing pipelinesMIME-type management and storage directory configuration
To add nexus-common to your project, include it in your Cargo.toml dependencies:
cargo add nexus-common
Below is an example demonstrating how to load a configuration using the provided loader trait:
use nexus_common::config::{ConfigLoader, DaemonConfig};
use std::path::Path;
use nexus_common::types::DynError;
#[tokio::main]
async fn main() -> Result<(), DynError> {
let cfg: DaemonConfig = DaemonConfig::read_config_file(
config::expand_home_dir("~/.pubky-nexus".into())
).await?;
println!("Loaded config: {:#?}", cfg);
Ok(())
}
Below is an example demonstrating how to get the connectors of the data bases:
use nexus_common::db::{Neo4jConnector, RedisConnector, get_neo4j_graph, get_redis_conn};
use nexus_common::db::Neo4JConfig;
use nexus_common::types::DynError;
#[tokio::main]
async fn main() -> Result<(), DynError> {
// Initialize connectors (once per app)
Neo4jConnector::init(Neo4JConfig::default()).await?;
RedisConnector::init("redis://127.0.0.1:6379").await?;
// Use helper functions
let graph = get_neo4j_graph()?;
let mut redis_conn = get_redis_conn().await?;
Ok(())
}
Demonstrates cache-first retrieval of domain entities, attempting to load from Redis and falling back to Neo4j if not found
use nexus_common::models::user::UserDetails;
use nexus_common::types::DynError;
use nexus_common::{StackManager, StackConfig};
#[tokio::main]
async fn main() -> Result<(), DynError> {
StackManager::setup("common-example", StackConfig::default()).await?
// Cache-first: Redis -> Neo4j fallback
if let Some(user) = UserDetails::get_by_id("some_user_id").await? {
println!("User: {}", user.name);
}
Ok(())
}
Contributions to nexus-common are welcome! Please open issues or submit pull requests on the project's repository. Follow the established coding conventions and include tests for new features.
This project is licensed under the MIT License.