IzanagiDB Svelte Themes

Izanagidb

Hybrid Database for Document Versioning (SvelteKit + FastAPI + PostgreSQL + MongoDB + Docker)

šŸ‘ļø IzanagiDB

IzanagiDB Logo

IzanagiDB is a tool that lets you save different versions of your document. Instead of just overwriting a file, it saves every change you make.

Think of it like "Undo/Redo" for your database. You can look back at what a document looked like yesterday, see exactly what words were changed, and "rewind" back to a previous version if you make a mistake.

Why two databases?

We use a Hybrid Setup because different databases are good at different things:

  1. PostgreSQL (The Librarian): It keeps track of the "Who, When, and Where." It knows which user made a change and when they did it.
  2. MongoDB (The Warehouse): It stores the actual content. Since your data might change shape (adding new fields), MongoDB is flexible enough to store it without breaking.

šŸ› ļø The Stack

  • Backend: FastAPI (Python 3.14) - Handles the logic and talks to the databases.
  • Frontend: Svelte 5 - The user interface (Port 7999).
  • Database A: PostgreSQL - Stores user accounts and the history list.
  • Database B: MongoDB - Stores the actual document data and the changes (deltas).
  • Tools: Docker - Connects everything through a private virtual network.

šŸ—ļø How it works (Docker Networking)

Inside the Docker network, the apps talk to each other using internal names. Your Python code connects to db for Postgres and nosql for Mongo. This keeps the databases private and secure from the outside world.


šŸ“‚ Project Structure

IzanagiDB/
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ README.md
ā”œā”€ā”€ Design_Roadmap.md            # How I reasoned on system design
ā”œā”€ā”€ docker-compose.yml           # Orchestrates all services
ā”œā”€ā”€ generate_keys.py             # Generates RSA keys for JWT
ā”œā”€ā”€ python_requirements.txt      # Python dependencies
│
ā”œā”€ā”€ backend/
│   ā”œā”€ā”€ Dockerfile               # Backend container definition
│   └── app/
│       ā”œā”€ā”€ __init__.py
│       ā”œā”€ā”€ main.py              # FastAPI app entry point + CORS
│       ā”œā”€ā”€ config.py            # Environment variables & settings
│       ā”œā”€ā”€ database.py          # PostgreSQL & MongoDB connections
│       ā”œā”€ā”€ tables.py            # SQLAlchemy ORM models
│       ā”œā”€ā”€ schemas.py           # Pydantic validation schemas
│       ā”œā”€ā”€ auth.py              # JWT & password hashing logic
│       ā”œā”€ā”€ dependencies.py      # JWT authentication dependency
│       ā”œā”€ā”€ create_databases.sql # Database schema (for reference, not used)
│       └── routes/
│           ā”œā”€ā”€ __init__.py
│           ā”œā”€ā”€ auth.py          # /auth endpoints (login, register, etc.)
│           └── documents.py     # /documents endpoints (CRUD, versions)
│
└── frontend/
    ā”œā”€ā”€ Dockerfile               # Frontend container definition
    ā”œā”€ā”€ package.json             # Node dependencies
    ā”œā”€ā”€ package-lock.json
    ā”œā”€ā”€ vite.config.ts           # Vite config (port 7999)
    ā”œā”€ā”€ svelte.config.js         # SvelteKit config
    ā”œā”€ā”€ tsconfig.json            # TypeScript config
    ā”œā”€ā”€ .prettierrc              # Code formatting
    ā”œā”€ā”€ .prettierignore
    ā”œā”€ā”€ .npmrc
    ā”œā”€ā”€ .gitignore
    ā”œā”€ā”€ README.md
    │
    ā”œā”€ā”€ static/
    │   └── robots.txt
    │
    └── src/
        ā”œā”€ā”€ app.html              # HTML template
        ā”œā”€ā”€ app.d.ts              # TypeScript declarations
        ā”œā”€ā”€ lib/
        │   ā”œā”€ā”€ index.ts
        │   ā”œā”€ā”€ styles.css        # Global CSS variables & fonts
        │   ā”œā”€ā”€ assets/
        │   │   └── favicon.svg
        │   └── components/
        │       └── Nav.svelte    # Navigation component
        │
        └── routes/
            ā”œā”€ā”€ +page.svelte      # Home page (/)
            ā”œā”€ā”€ +layout.svelte    # Global layout with Nav
            │
            ā”œā”€ā”€ auth/
            │   └── +page.svelte  # Login/Signup page (/auth)
            │
            └── documents/
                ā”œā”€ā”€ +page.svelte  # Document list (/documents)
                └── [id]/
                    └── +page.svelte  # Document viewer/editor (/documents/[id])

šŸ› ļø Environment Setup & Installation

This project is built using the latest features of Python 3.14 and SvelteKit. It is assumed that Python 3.14 is already installed on your system.


Environment Configuration

The backend requires environment variables for database connections and JWT authentication. The .env file is gitignored, so add one of your own, whose content looks like this:

POSTGRES_HOST=postgre
POSTGRES_PORT=5432
POSTGRES_USER=izanagi_user
POSTGRES_PASSWORD=izanagi_pass
POSTGRES_DB=izanagi_db

MONGO_HOST=mongo
MONGO_PORT=27017

# JWT Configuration
JWT_ALGORITHM=RS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=30

Backend Setup (FastAPI)

We use a dedicated virtual environment to manage dependencies and ensure version consistency.

  1. Create the Virtual Environment:

Navigate to the backend/ directory and create an environment named izanagi_venv:

python3.14 -m venv izanagi_venv
  1. Activate the Environment:

    source izanagi_venv/bin/activate  # Linux/macOS
    izanagi_venv\Scripts\activate     # Windows 
    
  2. Install Core Libraries:

pip install -r python_requirements.txt

Generate JWT RSA Keys

IzanagiDB uses RS256 (RSA asymmetric encryption) for JWT tokens. You need to generate a private/public key pair before starting the backend. These keys will be saved in backend/ directory, but their paths are added in .gitignore. Simply run:

cd backend/app
python3.14 ../../generate_keys.py

Frontend Setup (SvelteKit)

SvelteKit acts as the modern framework for our Svelte 5 components. It manages routing and communicates with the FastAPI backend via API calls.

  1. Initialize SvelteKit:

If you are starting the frontend/ folder from scratch, use the following command to bootstrap a SvelteKit Minimal project with TypeScript and Prettier:

npx sv create --template minimal --types ts --add prettier --install npm frontend
  1. Install Dependencies:

After the project is created, navigate to the frontend/ directory and install the text-diffing library required for the document version viewer:

cd frontend
npm install diff
  1. Configure API Proxying:

To avoid CORS issues during development, ensure your SvelteKit fetch calls point to the FastAPI default port (http://localhost:8000).

Database & Orchestration

Since IzanagiDB relies on a hybrid database approach, the easiest way to get the environment ready is through Docker, as defined in the docker-compose.yml.

  1. Verify Docker Installation: Ensure Docker and Docker Compose are running.

  2. Launch the Stack:

docker-compose up --build

This command pulls the official images for PostgreSQL and MongoDB, sets up the internal network, and starts your Python and SvelteKit services simultaneously.


šŸš€ How to Start

To launch the entire system (Databases, Backend, and Frontend), run the following command in your terminal:

docker-compose down && docker-compose up --build
  • Frontend: Access the UI at http://localhost:7999
  • Backend: Access the API docs at http://localhost:8000/docs

šŸ›”ļø License

This project is licensed under the GNU General Public License version 3 (GPLv3).

Top categories

Loading Svelte Themes