cognee_database/traits/notebook_db.rs
1//! `NotebookDb` trait — CRUD operations for the `notebooks` table.
2
3use async_trait::async_trait;
4use chrono::{DateTime, Utc};
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8use crate::types::DatabaseError;
9
10// ─── Notebook model ───────────────────────────────────────────────────────────
11
12/// A single row from the `notebooks` table.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct Notebook {
15 pub id: Uuid,
16 pub owner_id: Uuid,
17 pub name: String,
18 /// JSON array of notebook cells. Opaque `Value` — serialized/deserialized
19 /// as-is so the router DTOs can parse the typed structure.
20 pub cells: serde_json::Value,
21 pub deletable: bool,
22 pub created_at: DateTime<Utc>,
23}
24
25// ─── Update patch ─────────────────────────────────────────────────────────────
26
27/// Fields that can be updated in a `PUT /{notebook_id}` call.
28///
29/// `None` means "leave the existing value unchanged" (mirrors Python's
30/// truthiness-gated assignment).
31#[derive(Debug, Clone, Default)]
32pub struct NotebookUpdatePatch {
33 pub name: Option<String>,
34 pub cells: Option<serde_json::Value>,
35}
36
37// ─── NotebookDb trait ─────────────────────────────────────────────────────────
38
39/// CRUD operations for the `notebooks` table.
40#[async_trait]
41pub trait NotebookDb: Send + Sync + 'static {
42 /// Return all notebooks owned by `owner_id`, ordered by `created_at` asc.
43 async fn list_by_owner(&self, owner_id: Uuid) -> Result<Vec<Notebook>, DatabaseError>;
44
45 /// Insert a new notebook row and return it.
46 ///
47 /// The implementation generates a fresh `uuid4` id for the new row.
48 async fn create(
49 &self,
50 owner_id: Uuid,
51 name: String,
52 cells: serde_json::Value,
53 deletable: bool,
54 ) -> Result<Notebook, DatabaseError>;
55
56 /// Insert a notebook with a caller-supplied id (used by the tutorial seeder
57 /// to guarantee deterministic UUID5 ids across SDK restarts).
58 async fn create_seeded(
59 &self,
60 id: Uuid,
61 owner_id: Uuid,
62 name: String,
63 cells: serde_json::Value,
64 deletable: bool,
65 ) -> Result<Notebook, DatabaseError>;
66
67 /// Fetch a notebook by id, scoped to the owner.
68 async fn get_by_id_and_owner(
69 &self,
70 id: Uuid,
71 owner_id: Uuid,
72 ) -> Result<Option<Notebook>, DatabaseError>;
73
74 /// Apply a partial update. Returns the updated row or `None` when not
75 /// found (ownership check included).
76 async fn update(
77 &self,
78 id: Uuid,
79 owner_id: Uuid,
80 patch: NotebookUpdatePatch,
81 ) -> Result<Option<Notebook>, DatabaseError>;
82
83 /// Delete a notebook. Returns `true` if a row was actually removed.
84 async fn delete(&self, id: Uuid, owner_id: Uuid) -> Result<bool, DatabaseError>;
85}