reactive-pg 0.2.0

Reactive Postgres layer for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/// Adapted from https://github.com/nothingisdead/pg-live-query/blob/master/watcher.js
use async_trait::async_trait;
use derive_new::new;
use futures::StreamExt;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Debug, sync::Arc};
use tokio::{sync::RwLock, task::JoinHandle};

use tokio_postgres::{
    tls::NoTlsStream, types::Json, AsyncMessage, Client, Connection, NoTls, Socket,
};

#[derive(Default)]
pub struct TmpTable {
    pub name: String,
    pub fields: Vec<String>,
}

#[derive(new)]
pub struct Watcher<T> {
    ctx: Arc<RwLock<WatcherCtx<T>>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct JsonEvent {
    pub id: i32,
    pub op: i32,
    pub data: serde_json::Value,
}

impl<T> Watcher<T>
where
    T: Sync + Send + 'static + DeserializeOwned,
{
    pub async fn start(
        &mut self,
        mut connection: Connection<Socket, NoTlsStream>,
    ) -> JoinHandle<()> {
        let (tx, mut rx) = tokio::sync::mpsc::channel(100);

        let handle = tokio::spawn(async move {
            let mut stream = futures::stream::poll_fn(move |cx| connection.poll_message(cx));

            while let Some(message) = stream.next().await {
                let message = message
                    .map_err(|e| {
                        eprintln!("failed to get message from db: {}", e);
                        e
                    })
                    .unwrap();

                if let AsyncMessage::Notification(not) = &message {
                    let updated_table_nb: u32 = serde_json::from_str(not.payload()).unwrap();

                    tx.send(updated_table_nb)
                        .await
                        .map_err(|e| {
                            eprintln!("failed to send message on channel: {:?}", e);
                            e
                        })
                        .unwrap();
                }
            }
        });

        let ctx = Arc::clone(&self.ctx);

        tokio::spawn(async move {
            while rx.recv().await.is_some() {
                ctx.write().await.handle_event().await;
            }
        });

        self.ctx.write().await.start().await;

        handle
    }
}

#[derive(new)]
pub struct WatcherCtx<T> {
    cb: Box<dyn Fn(Vec<Event<T>>) + Sync + Send + 'static>,
    query: String,
    pub client: Client,

    #[new(default)]
    triggers: Vec<String>,

    #[new(default)]
    result_table: TmpTable,

    #[new(default)]
    source_tables: Vec<String>,

    #[new(value = "true")]
    first_run: bool,

    phantom: std::marker::PhantomData<T>,
}

impl<T> WatcherCtx<T>
where
    T: Sync + Send + 'static + DeserializeOwned,
{
    pub async fn start(&mut self) {
        self.client
            .query("LISTEN __live_update;", &[])
            .await
            .unwrap();

        self.init().await
    }

    pub async fn init(&mut self) {
        self.collect_source_tables();
        self.create_triggers().await;

        // If no initial record, we can't infer the table schema. Skipping until the first event
        if !self.setup_query_result_table().await {
            return;
        }

        self.update_result_table().await;
    }

    pub async fn setup_query_result_table(&mut self) -> bool {
        let tmp_table_name = "query_result";

        let columns = self.get_query_result_columns().await;

        if columns.is_empty() {
            // Delay the setup until there is at least one record
            return false;
        }

        let fields = columns.iter().map(|(name, _)| name.clone()).collect();

        let columns_def = columns
            .iter()
            .map(|(name, t)| format!("{} {} NOT NULL", name, t))
            .collect::<Vec<_>>()
            .join(",\n");

        let query = format!(
            r#"
            CREATE TEMP TABLE {} (
                {}
            )
        "#,
            tmp_table_name, columns_def,
        );

        self.client.execute(&query, &[]).await.unwrap();

        self.result_table = TmpTable {
            name: tmp_table_name.to_string(),
            fields,
        };

        true
    }

    pub async fn get_query_result_columns(&self) -> Vec<(String, String)> {
        let query = format!("SELECT * FROM ({}) q LIMIT 1", self.query);

        let columns = if let Some(first) = self.client.query(&query, &[]).await.unwrap().get(0) {
            first
                .columns()
                .iter()
                .map(|c| (c.name().to_string(), c.type_().name().to_string()))
                .collect()
        } else {
            vec![]
        };

        columns
    }

    pub fn collect_source_tables(&mut self) {
        use sqlparser::dialect::PostgreSqlDialect;
        let sql_ast =
            sqlparser::parser::Parser::parse_sql(&PostgreSqlDialect {}, &self.query).unwrap();
        use sqlparser::ast::{SetExpr, Statement, TableFactor};
        let mut names = Vec::new();

        for stmt in sql_ast {
            match stmt {
                Statement::Query(query) => match &*query.body {
                    SetExpr::Select(select) => {
                        for table in &select.from {
                            match &table.relation {
                                TableFactor::Table {
                                    name,
                                    alias: _,
                                    args: _,
                                    with_hints: _,
                                } => names.push(name.to_string()),
                                _ => {}
                            }
                        }
                    }
                    _ => {}
                },
                _ => {}
            }
        }

        self.source_tables = names;
    }

    pub async fn create_triggers(&mut self) {
        for (i, table_name) in self.source_tables.iter().enumerate() {
            if !self.triggers.contains(&table_name) {
                let trigger_name = &format!(r#""__live_update{}""#, table_name);
                let l_key = i.to_string();

                let drop_sql = format!(
                    r#"
                    DROP TRIGGER IF EXISTS
                        {}
                    ON
                        {}
                "#,
                    trigger_name, table_name
                );

                self.client.execute(&drop_sql, &[]).await.unwrap();

                let func_sql = format!(
                    r#"
                    CREATE OR REPLACE FUNCTION pg_temp.{}()
                    RETURNS TRIGGER AS $$
                        BEGIN
                            EXECUTE pg_notify('__live_update', '{}');
                        RETURN NULL;
                        END;
                    $$ LANGUAGE plpgsql
                "#,
                    trigger_name, l_key
                );

                self.client.execute(&func_sql, &[]).await.unwrap();

                let create_sql = format!(
                    "
                    CREATE TRIGGER
                        {}
                    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON
                        {}
                    EXECUTE PROCEDURE pg_temp.{}()
                ",
                    trigger_name, table_name, trigger_name
                );

                self.client.execute(&create_sql, &[]).await.unwrap();

                self.triggers.push(table_name.clone());
            }
        }
    }

    pub async fn update_result_table(&mut self) {
        let i_table = "query_result";

        let q_obj = self
            .result_table
            .fields
            .iter()
            .map(|name| format!("'{name}', i.{name}"))
            .collect::<Vec<_>>()
            .join(",");

        let u_obj = self
            .result_table
            .fields
            .iter()
            .map(|name| format!("'{name}', u.{name}"))
            .collect::<Vec<_>>()
            .join(",");
        let cols = self.result_table.fields.join(", ");
        let set_cols = self
            .result_table
            .fields
            .iter()
            .map(|name| format!("{} = q.{}", name, name))
            .collect::<Vec<_>>()
            .join(", ");
        let update_sql = format!(
            "WITH
                q AS (
                    SELECT
                        *,
                        ROW_NUMBER() OVER() AS lol
                    FROM
                        ({}) t
                ),
                i AS (
                    INSERT INTO {i_table} (
                        {cols}
                    )
                    SELECT
                        {cols}
                    FROM
                        q
                    WHERE q.id NOT IN (
                        SELECT id FROM {i_table}
                    )
                    RETURNING
                        {i_table}.*
                ),
				d AS (
					DELETE FROM
						{i_table}
					WHERE
						NOT EXISTS(
							SELECT
								1
							FROM
								q
							WHERE
								q.id = {i_table}.id
						)
					RETURNING
						{i_table}.id
				),
                u AS (
					UPDATE {i_table} SET
                        {set_cols}
					FROM
						q
					WHERE
						{i_table}.id = q.id
					RETURNING
						{i_table}.*
				)
            SELECT
                jsonb_build_object(
                    'id', i.id,
                    'op', 1,
                    'data', jsonb_build_object(
                        {q_obj}
                    )
                ) AS c
            FROM
                i JOIN
                q ON
                    i.id = q.id
            UNION ALL
			SELECT
				jsonb_build_object(
					'id', u.id,
					'op', 2,
					'data', jsonb_build_object({u_obj})
				) AS c
			FROM
				u JOIN
				q ON
					u.id = q.id
			UNION ALL
			SELECT
				jsonb_build_object(
					'id', d.id,
					'op', 3,
                    'data', jsonb_build_object('id', d.id)
				) AS c
			FROM
				d
        ",
            self.query
        );

        let res: Vec<_> = self
            .client
            .query(&update_sql, &[])
            .await
            .unwrap_or_else(|err| {
                panic!("error {}", err);
            });

        // Don't send the first result, as it will contains the whole query.
        if self.first_run {
            self.first_run = false;

            return;
        }

        let res = res
            .into_iter()
            .map(|row| row.get("c"))
            .collect::<Vec<Json<serde_json::Value>>>();

        let json_value = res.iter().map(|json| json.0.clone()).collect::<Vec<_>>();
        let json_events = json_value
            .into_iter()
            .map(|json| serde_json::from_value(json).unwrap())
            .collect::<Vec<JsonEvent>>();

        let events = json_events
            .into_iter()
            .map(|event| match event.op {
                1 => Event::Insert(serde_json::from_value(event.data).unwrap()),
                2 => Event::Update(serde_json::from_value(event.data).unwrap()),
                3 => Event::Delete(event.id),
                _ => unimplemented!(),
            })
            .collect::<Vec<Event<T>>>();

        (self.cb)(events);
    }

    pub async fn handle_event(&mut self) {
        // The table was previously empty, sending it all after setting up the triggers.
        if self.result_table.fields.is_empty() {
            self.first_run = false;

            if !self.setup_query_result_table().await {
                return;
            }
        }

        self.update_result_table().await;
    }
}

pub async fn watch<T>(
    query: &str,
    handler: Box<dyn Fn(Vec<Event<T>>) + Sync + Send + 'static>,
) -> JoinHandle<()>
where
    T: Debug + Send + Sync + 'static + DeserializeOwned,
{
    let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");

    let (client, connection) = tokio_postgres::connect(&db_url, NoTls).await.unwrap();

    let mut watcher = Watcher::new(Arc::new(RwLock::new(WatcherCtx::new(
        handler,
        query.to_string(),
        client,
    ))));

    watcher.start(connection).await
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Event<T> {
    Insert(T),
    Update(T),
    Delete(i32),
}

#[async_trait]
pub trait WatchableSql<F> {
    async fn watch<T>(&self, handler: F) -> JoinHandle<()>
    where
        F: Fn(Vec<Event<T>>) + Sync + Send + 'static,
        T: Debug + Send + Sync + 'static + DeserializeOwned;
}

#[async_trait]
impl<F> WatchableSql<F> for str {
    async fn watch<T>(&self, handler: F) -> JoinHandle<()>
    where
        F: Fn(Vec<Event<T>>) + Sync + Send + 'static,
        T: Debug + Send + Sync + 'static + DeserializeOwned,
    {
        watch(self, Box::new(handler)).await
    }
}