Scalable and distributed custom feed generator, ott - on that topic
1use anyhow::Result;
2use ott_types::Embedding;
3use pgvector::Vector;
4use sqlx::PgPool;
5
6pub struct PgClient {
7 pool: PgPool,
8}
9
10impl PgClient {
11 pub async fn new() -> Result<Self> {
12 let database_url = std::env::var("DATABASE_URL")?;
13 let pool = PgPool::connect(&database_url).await?;
14 Ok(Self { pool: pool })
15 }
16
17 pub async fn insert_embeddings(
18 self: &Self,
19 vectors: &Vec<Embedding>,
20 ) -> Result<(), sqlx::Error> {
21 let mut tx = self.pool.begin().await?;
22
23 for embedding in vectors {
24 let vector = Vector::from(embedding.vector.clone());
25
26 sqlx::query("INSERT INTO vectors (uri, vector) VALUES ($1, $2)")
27 .bind(&embedding.uri)
28 .bind(vector)
29 .execute(&mut *tx)
30 .await?;
31 }
32
33 tx.commit().await?;
34 Ok(())
35 }
36}