rust_supabase_sdk 0.2.17

An SDK kit for SupaBase so that Rust lovers can use SupaBase with the low level abstracted away. If you want new features tell me and I'll add them.
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#[cfg(test)]
mod tests {
    use dotenv::dotenv;
    use rust_supabase_sdk::{
        select::{Filter, FilterGroup, LogicalOperator, Operator, SelectQuery, Sort, SortDirection},
        SupabaseClient
    };
    use serde_json::{json, Value};
    use uuid::Uuid;
    use std::env;
    use tokio::time::{sleep, Duration};
    use rust_supabase_sdk::query;

    /// Utility cleanup function to delete records from the given table.
    async fn cleanup_records(client: &SupabaseClient, table_name: &str, records: &[Value]) {
        for record in records {
            if let Some(id) = record["id"].as_str() {
                let _ = client.delete(table_name, id).await;
                sleep(Duration::from_millis(30)).await;
            }
        }
    }

    async fn clean_all() {
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";
        let records = supabase_client.select(table_name, SelectQuery::new()).await.unwrap();
        cleanup_records(&supabase_client, table_name, &records).await;
    }

    #[tokio::test]
    async fn clean_up() {
        dotenv().ok();
        clean_all().await;
    }

    #[tokio::test]
    async fn test_eq_query() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        // Insert 30 records with name "Test Organisation"
        for _ in 0..30 {
            supabase_client.insert(table_name, json!({ "name": "Test Organisation" })).await.unwrap();
            sleep(Duration::from_millis(30)).await;
        }
        // Insert one record with a different name.
        let diff_id = supabase_client.insert(table_name, json!({ "name": "Different Organisation" })).await.unwrap();

        let records = supabase_client.select(
            table_name,
            query!("name" == "Test Organisation").to_query(),
        ).await.unwrap();
        assert_eq!(records.len(), 30);

        clean_all().await;
        let _ = supabase_client.delete(table_name, &diff_id).await;
    }

    #[tokio::test]
    async fn test_and_query() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        let _ = supabase_client.insert(table_name, json!({ "name": "Org A", "category": "Finance" })).await.unwrap();
        let _ = supabase_client.insert(table_name, json!({ "name": "Org A", "category": "Tech" })).await.unwrap();

        let query = (query!("name" == "Org A") & query!("category" == "Tech")).to_query();
        assert_eq!(query, SelectQuery {
            filter: Some(FilterGroup {
                operator: LogicalOperator::And,
                filters: vec![
                    Filter {
                        column: "name".to_string(),
                        operator: Operator::Eq,
                        value: "Org A".to_string(),
                    },
                    Filter {
                        column: "category".to_string(),
                        operator: Operator::Eq,
                        value: "Tech".to_string(),
                    },
                ],
            }),
            sorts: vec![],
        });

        let records = supabase_client.select(
            table_name,
            query,
        ).await.unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0]["category"], "Tech");

        clean_all().await;
    }

    #[tokio::test]
    async fn test_and_query_with_uuids() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        let id1 = Uuid::new_v4().to_string();
        let id2 = Uuid::new_v4().to_string();

        let _ = supabase_client.insert(table_name, json!({ "id1": id1.clone(), "id2": id2.clone() })).await.unwrap();
        let _ = supabase_client.insert(table_name, json!({ "id1": id1.clone(), "id2": id1.clone() })).await.unwrap();

        let id1_clone = id1.clone();
        let id2_clone = id2.clone();
        let query = (query!("id1" == id1) & query!("id2" == id2)).to_query();
        assert_eq!(query, SelectQuery {
            filter: Some(FilterGroup {
                operator: LogicalOperator::And,
                filters: vec![
                    Filter {
                        column: "id1".to_string(),
                        operator: Operator::Eq,
                        value: id1_clone.to_string(),
                    },
                    Filter {
                        column: "id2".to_string(),
                        operator: Operator::Eq,
                        value: id2_clone.to_string(),
                    },
                ],
            }),
            sorts: vec![],
        });

        let records = supabase_client.select(
            table_name,
            query,
        ).await.unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0]["id1"], id1_clone);
        assert_eq!(records[0]["id2"], id2_clone);

        clean_all().await;
    }

    #[tokio::test]
    async fn test_or_query() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        let id_x = supabase_client.insert(table_name, json!({ "name": "Org X" })).await.unwrap();
        let id_z = supabase_client.insert(table_name, json!({ "name": "Org Z" })).await.unwrap();

        let query = (query!("name" == "Org X") | query!("name" == "Org Z")).to_query();
        assert_eq!(query, SelectQuery {
            filter: Some(FilterGroup {
                operator: LogicalOperator::Or,
                filters: vec![
                    Filter {
                        column: "name".to_string(),
                        operator: Operator::Eq,
                        value: "Org X".to_string(),
                    },
                    Filter {
                        column: "name".to_string(),
                        operator: Operator::Eq,
                        value: "Org Z".to_string(),
                    },
                ],
            }),
            sorts: vec![],
        });

        let records = supabase_client.select(
            table_name,
            query,
        ).await.unwrap();
        assert_eq!(records.len(), 2);

        // check IDs
        let ids: Vec<&str> = records.iter().map(|r| r["id"].as_str().unwrap()).collect();
        assert!(ids.contains(&id_x.as_str()));
        assert!(ids.contains(&id_z.as_str()));

        clean_all().await;
    }

    #[tokio::test]
    async fn test_sorting_created_at() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        for i in 1..=5 {
            supabase_client.insert(table_name, json!({ "name": format!("Org {}", i) })).await.unwrap();
            sleep(Duration::from_millis(30)).await;
        }

        let asc_sort = Sort::new("created_at", SortDirection::Asc);
        let desc_sort = Sort::new("created_at", SortDirection::Desc);

        let asc_query = SelectQuery { filter: None, sorts: vec![asc_sort] };
        let desc_query = SelectQuery { filter: None, sorts: vec![desc_sort] };

        let asc_records = supabase_client.select(table_name, asc_query).await.unwrap();
        let desc_records = supabase_client.select(table_name, desc_query).await.unwrap();

        let asc_dates: Vec<&str> = asc_records.iter().map(|r| r["created_at"].as_str().unwrap()).collect();
        let desc_dates: Vec<&str> = desc_records.iter().map(|r| r["created_at"].as_str().unwrap()).collect();

        assert_eq!(asc_dates.iter().rev().cloned().collect::<Vec<&str>>(), desc_dates);

        clean_all().await;
    }

    /// Test the not equal operator (using !=).
    #[tokio::test]
    async fn test_neq_operator() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert three records with different names.
        let _ = client.insert(table, json!({ "name": "Alice" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Bob" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Charlie" })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        // Build a DSL query: select records where name != "Alice"
        let filter = query!("name" != "Alice").to_filter_group();
        let select_query = SelectQuery { filter: Some(filter), sorts: Vec::new() };
        let records = client.select(table, select_query).await.unwrap();

        // Expect Bob and Charlie (2 records).
        assert_eq!(records.len(), 2);
        for rec in &records {
            assert_ne!(rec["name"].as_str().unwrap(), "Alice");
        }
        clean_all().await;
    }

    /// Test the greater than operator (using >).
    #[tokio::test]
    async fn test_gt_operator() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert records with a numeric "score" field.
        let _ = client.insert(table, json!({ "name": "Item1", "score": 50 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item2", "score": 75 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item3", "score": 100 })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        // Build a DSL query: select records where score > 60.
        let filter = query!("score" > 60).to_filter_group();
        let select_query = SelectQuery { filter: Some(filter), sorts: Vec::new() };
        let records = client.select(table, select_query).await.unwrap();

        // Expect records with scores 75 and 100.
        assert_eq!(records.len(), 2);
        for rec in &records {
            let score = rec["score"].as_i64().unwrap();
            assert!(score > 60);
        }
        clean_all().await;
    }

    /// Test the less than operator (using <).
    #[tokio::test]
    async fn test_lt_operator() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert records with a numeric "score" field.
        let _ = client.insert(table, json!({ "name": "Item1", "score": 10 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item2", "score": 20 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item3", "score": 30 })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        // Build a DSL query: select records where score < 25.
        let filter = query!("score" < 25).to_filter_group();
        let select_query = SelectQuery { filter: Some(filter), sorts: Vec::new() };
        let records = client.select(table, select_query).await.unwrap();

        // Expect items with score 10 and 20.
        assert_eq!(records.len(), 2);
        for rec in &records {
            let score = rec["score"].as_i64().unwrap();
            assert!(score < 25);
        }
        clean_all().await;
    }

    /// Test the like operator.
    ///
    /// The LIKE operator in PostgREST supports SQL-like pattern matching.
    /// For example, the pattern "Alph%" will match any string starting with "Alph" (the "%" is a wildcard).
    #[tokio::test]
    async fn test_like_operator() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert records with names that follow a pattern.
        let _ = client.insert(table, json!({ "name": "Alpha" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Beta" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Alphabet" })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        // Build a DSL query: select records where name is like "Alph%"
        // The pattern "Alph%" will match "Alpha" and "Alphabet" (since "%" is a wildcard).
        let filter = FilterGroup::new(
            LogicalOperator::Or,
            vec![
                Filter {
                    column: "name".to_string(),
                    operator: Operator::Like,
                    value: "Alph%".to_string(),
                }
            ],
        );
        let select_query = SelectQuery { filter: Some(filter), sorts: Vec::new() };
        let records = client.select(table, select_query).await.unwrap();

        // Expect to match 2 records: "Alpha" and "Alphabet".
        assert_eq!(records.len(), 2);
        for rec in &records {
            let name = rec["name"].as_str().unwrap();
            assert!(name.starts_with("Alph"));
        }
        clean_all().await;
    }

    // Test combining filter and sort
    #[tokio::test]
    async fn test_filter_and_sort() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert records with a numeric "score" field.
        let _ = client.insert(table, json!({ "name": "Item1", "score": 10 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item2", "score": 20 })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Item3", "score": 30 })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        let records = client.select(
            table,
            query!("score" < 25)
                .to_query()
                .sort("score", SortDirection::Desc)
        ).await.unwrap();

        // Expect items with score 20 and 10, in descending order.
        assert_eq!(records.len(), 2);
        assert_eq!(records[0]["score"].as_i64().unwrap(), 20);
        assert_eq!(records[1]["score"].as_i64().unwrap(), 10);
        clean_all().await;
    }

    #[tokio::test]
    async fn can_create_simple_filter_query() {
        let lecture_id = "8e662d9e-c920-4d2f-bda7-09e5173cc494";
        let user_id = lecture_id;
        let filter = (query!("lecture_id" == lecture_id) & query!("user_id" == user_id)).to_filter_group();
        assert_eq!(filter, FilterGroup {
            operator: LogicalOperator::And,
            filters: vec![Filter {
                column: "lecture_id".to_string(),
                operator: Operator::Eq,
                value: lecture_id.to_string(),
            }, Filter {
                column: "user_id".to_string(),
                operator: Operator::Eq,
                value: user_id.to_string(),
            }],
        });

        assert_eq!(filter.to_query_string(), "lecture_id=eq.8e662d9e-c920-4d2f-bda7-09e5173cc494&user_id=eq.8e662d9e-c920-4d2f-bda7-09e5173cc494");
    }

    #[tokio::test]
    async fn can_use_expression_in_query_eq_macro() {
        dotenv().ok();
        let supabase_client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table_name = "test_data";

        // Insert 30 records with name "Test Organisation"
        for _ in 0..5 {
            supabase_client.insert(table_name, json!({ "name": "Test Organisation" })).await.unwrap();
            sleep(Duration::from_millis(30)).await;
        }
        let query_name_varible = "Test Organisation".to_string();
        let first_half = "Test".to_string();
        let second_half = "Organisation".to_string();
        // Insert one record with a different name.
        let diff_id = supabase_client.insert(table_name, json!({ "name": "Different Organisation" })).await.unwrap();
        let records = supabase_client.select(
            table_name,
            query!("name" == query_name_varible.clone()).to_query(),
        ).await.unwrap();

        let same_records = supabase_client.select(
            table_name,
            query!("name" == format!("{} {}", first_half, second_half)).to_query(),
        ).await.unwrap();

        let same_records_2 = supabase_client.select(
            table_name,
            query!("name" == &query_name_varible).to_query(),
        ).await.unwrap();

        assert_eq!(records, same_records);
        assert_eq!(records, same_records_2);
        assert_eq!(records.len(), 5);

        clean_all().await;
        let _ = supabase_client.delete(table_name, &diff_id).await;
    }

    #[tokio::test]
    async fn can_select_with_empty_query() {
        dotenv().ok();
        let client = SupabaseClient::new(
            env::var("SUPABASE_URL").unwrap(),
            env::var("SUPABASE_API_KEY").unwrap(),
            None,
        );
        let table = "test_data";

        // Insert records with names that follow a pattern.
        let _ = client.insert(table, json!({ "name": "Alpha" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Beta" })).await.unwrap();
        let _ = client.insert(table, json!({ "name": "Alphabet" })).await.unwrap();
        sleep(Duration::from_millis(30)).await;

        // Build a DSL query: select records where name is like "Alph%"
        // The pattern "Alph%" will match "Alpha" and "Alphabet" (since "%" is a wildcard).

        let select_query = SelectQuery::new();
        let records = client.select(table, select_query).await.unwrap();

        // Expect to match 2 records: "Alpha" and "Alphabet".
        assert_eq!(records.len(), 3);
        clean_all().await;
    }
}