Skip to content

Saved Scripts

Users can save scripts to their account for later reuse. Scripts are stored in MongoDB and associated with the user who created them. Each script includes the code, language, version, and optional description.

Data Model

Each saved script contains:

class SavedScriptResponse(BaseModel):
    script_id: str
    name: str
    script: str
    lang: str
    lang_version: str
    description: str | None = None
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)

Scripts are scoped to individual users—a user can only access their own saved scripts.

API Endpoints

Service Layer

The SavedScriptService handles business logic with comprehensive logging:

    async def create_saved_script(
        self, saved_script_create: DomainSavedScriptCreate, user_id: str
    ) -> DomainSavedScript:
        self.logger.info(
            "Creating new saved script",
            extra={
                "user_id": user_id,
                "script_name": saved_script_create.name,
                "script_length": len(saved_script_create.script),
            },
        )

        created_script = await self.saved_script_repo.create_saved_script(saved_script_create, user_id)

        self.logger.info(
            "Successfully created saved script",
            extra={
                "script_id": str(created_script.script_id),
                "user_id": user_id,
                "script_name": created_script.name,
            },
        )
        return created_script

All operations log the user ID, script ID, and relevant metadata for auditing.

Storage

Scripts are stored in the saved_scripts MongoDB collection with a compound index on (user_id, script_id) for efficient per-user queries.

The repository enforces user isolation—queries always filter by user_id to prevent cross-user access.

Frontend Integration

The frontend displays saved scripts in a dropdown, allowing users to:

  1. Select a saved script to load into the editor
  2. Save the current editor content as a new script
  3. Update an existing script with new content
  4. Delete scripts they no longer need

When loading a script, the frontend sets both the code content and the language/version selectors to match the saved values.

Key Files

File Purpose
services/saved_script_service.py Business logic
api/routes/saved_scripts.py API endpoints
schemas_pydantic/saved_script.py Request/response models
db/repositories/saved_script_repository.py MongoDB operations