Backend
Overview
Section titled “Overview”Privatefolio Backend is a Node.js/Bun service that aggregates multi-chain cryptocurrency data, provides a REST and WebSocket API, and persists data in a local SQLite database. It powers the desktop app and frontend with real-time account balances, transactions, and price history.
Architecture
Section titled “Architecture”Frontend <── HTTP/WebSocket ──> Backend Server <── API SDK ──> Data Aggregator (Relayer) │ └── SQLite (persistent storage)- API Server handles HTTP routes and WebSocket RPC for frontend communication.
- Backend Relayer schedules tasks for data fetching, processing, and side-effects.
- Database uses SQLite for fast, local persistence.
- Utilities provide config management, process control, and inter-service communications.
Package structure
Section titled “Package structure”packages/backend/├── src/│ ├── api/ # HTTP & WebSocket API definitions│ ├── config/ # Environment and settings loaders│ ├── sqlite/ # Schema and migration logic│ ├── utils/ # Helper functions│ ├── backend-server.ts # HTTP/WebSocket server implementation│ ├── backend-relayer.ts # Task scheduler and data aggregator│ └── start.ts # Entry point wiring server + relayer├── data/ # Runtime database and log files├── build/ # Compiled outputs for production├── test/ # Vitest test suites organized by feature└── package.json # Scripts, dependencies, and build setupCore Components
Section titled “Core Components”- API Server (
src/backend-server.ts): serves REST (/ping,/info,/download,/upload) and WebSocket RPC. Serves static frontend build. - Backend Relayer (
src/backend-relayer.ts,src/start.ts): uses Cron viacronerand audit-log subscriptions to enqueue data tasks (balances, prices, net worth). - Database (
src/sqlite/): initializes and migrates SQLite database; defines interfaces insrc/interfaces.ts. - Utilities (
src/utils/,src/backend-comms.ts): config parsing, throttling, file upload/download handlers, and inter-process messaging. - Configuration (
src/server-env.ts,src/settings.ts): loads environment variables (PORT, APP_VERSION, GIT_HASH, GIT_DATE) and default settings (throttle durations, data dirs).
API Endpoints
Section titled “API Endpoints”- HTTP
GET /ping→pongGET /info→ build metadata (version, commit, build date)GET /download&POST /upload→ file operations for account backupsOPTIONS /*→ CORS preflight- Static file serving from frontend build
- WebSocket RPC
- Dynamic method invocation: send
{ id, method, params }, receives{ id, result }or error. - Supports callback functions via
FunctionReferencemessages.
- Dynamic method invocation: send
Database
Section titled “Database”- Uses SQLite via
sqlite3(Node.js) andsqlite(Bun) packages. - Database files located under
packages/backend/data/databases/. - Schema and migrations defined in
src/sqlite/; initialized on startup. - Data access via typed interfaces (
src/interfaces.ts).
Configuration
Section titled “Configuration”- Environment variables:
PORT(default 4001 dev, 5555 prod)APP_VERSION,GIT_HASH,GIT_DATE(populated from git/npm)DATA_LOCATION(overrides default data directory)BUN_SQL,GITHUB_CI(test flags)
- Settings file:
src/settings.tsdefines throttle durations and cron schedule.
Development
Section titled “Development”Prerequisites: Node.js v20+, Bun, Yarn.
yarn # install dependenciesyarn build # compile TypeScript & build frontendyarn dev # runs backend (watch) + relayerTesting
Section titled “Testing”yarn test # run Vitest (Node SQLite)yarn test:bun # run Bun-specific SQLite testsyarn test:ci # CI mode (Node + Bun)Tests located in packages/backend/test/ organized by feature (e.g., balances, tags, bun).
Deployment
Section titled “Deployment”# build production bundleyarn build# Docker image build & run (from packages/backend)yarn docker:buildyarn docker:runyarn docker:removeIn Electron, backend is started automatically on app launch.