my_libs\db/
read.rs

1// src/db/read.rs
2
3use crate::api::model::Kot;
4use crate::db::connect::DatabaseConnection;
5use surrealdb::Result;
6
7// 📚 EDU (Lifetimes 'a):
8// Reader trzyma REFERENCJĘ (&) do połączenia, a nie samo połączenie.
9// <'a> mówi kompilatorowi: "Reader nie może żyć dłużej niż DatabaseConnection, do którego się odnosi".
10// To chroni nas przed używaniem zamkniętego połączenia (Use After Free).
11pub struct Reader<'a> {
12    db: &'a DatabaseConnection,
13}
14
15impl<'a> Reader<'a> {
16    pub fn new(db: &'a DatabaseConnection) -> Self {
17        Self { db }
18    }
19
20    pub async fn get_all_cats(&self) -> Result<Vec<Kot>> {
21        // Dostęp do .client jest teraz możliwy, bo daliśmy `pub` w connect.rs
22        let mut response = self.db.client.query("SELECT * FROM kot").await?;
23
24        // Jawnie mówimy Rustowi: "Oczekuję, że to będzie lista kotów"
25        let cats: Vec<Kot> = response.take(0)?;
26
27        Ok(cats)
28    }
29}