supabase_js_rs/
lib.rs

1//! `supabase-js-rs` is a Rust bindings for Supabase JavaScript library via WebAssembly.
2//!
3
4use wasm_bindgen::prelude::*;
5
6/// Sign in with email and password credentials
7#[wasm_bindgen(getter_with_clone)]
8pub struct Credentials {
9    pub email: String,
10    pub password: String,
11}
12
13#[wasm_bindgen(getter_with_clone)]
14pub struct SignInWithOAuthCredentials {
15    pub provider: String,
16    pub options: JsValue,
17}
18
19#[wasm_bindgen(getter_with_clone)]
20pub struct CurrentSession {
21    pub access_token: String,
22    pub refresh_token: String,
23}
24
25/*
26#[wasm_bindgen(getter_with_clone)]
27pub struct MFAChallengeParams {
28    pub factor_id: String,
29}
30*/
31
32/*
33#[wasm_bindgen(getter_with_clone)]
34pub struct MFAVerifyParams {
35    pub factor_id: String,
36    pub challenge_id: String,
37    pub code: String,
38}
39*/
40
41#[wasm_bindgen]
42extern "C" {
43
44    #[derive(Debug, Clone, PartialEq)]
45    pub type SupabaseClient;
46
47    /// # Create client
48    ///
49    #[wasm_bindgen(js_namespace = ["supabase"], js_name = createClient)]
50    pub fn create_client(supabase_url: &str, supabase_key: &str) -> SupabaseClient;
51
52    #[wasm_bindgen(method, js_name = from)]
53    pub fn from(this: &SupabaseClient, table: &str) -> Database;
54
55    pub type Database;
56
57    #[wasm_bindgen(method, catch, js_name = select)]
58    pub async fn select(this: &Database, columns: Option<&str>) -> Result<JsValue, JsValue>;
59    #[wasm_bindgen(method, js_name = select)]
60    pub fn select_(this: &Database, columns: Option<&str>) -> Database;
61
62    /// # Order the query
63    ///
64    /// Order query result by column.
65    ///
66    /// ```ignore
67    /// #[derive(Serialize, Deserialize)]
68    /// #[serde(rename_all = "camelCase")]
69    /// struct OrderOptions {
70    ///     foreign_table: String,
71    ///     nulls_first: bool,
72    ///     ascending: bool,
73    /// }
74    /// let data: JsValue = client
75    /// .get()
76    /// .from("countries")
77    /// .select_(Some("name, cities ( name )"))
78    /// .order(
79    ///    "name",
80    ///    serde_wasm_bindgen::to_value(&OrderOptions {
81    ///     foreign_table: "cities".to_string(),
82    ///     nulls_first: false,
83    ///     ascending: true,
84    ///    }).unwrap(),
85    /// )
86    /// .await.unwrap();
87    /// ```
88    ///
89    #[wasm_bindgen(method, catch, js_name = order)]
90    pub async fn order(this: &Database, column: &str, options: JsValue)
91        -> Result<JsValue, JsValue>;
92    #[wasm_bindgen(method, js_name = order)]
93    pub fn order_(this: &Database, column: &str, options: JsValue) -> Database;
94
95    /// # Limit the query
96    ///
97    /// Limit the query result by count.
98    ///
99    #[wasm_bindgen(method, catch, js_name = limit)]
100    pub async fn limit(this: &Database, count: u32) -> Result<JsValue, JsValue>;
101    #[wasm_bindgen(method, js_name = limit)]
102    pub fn limit_(this: &Database, count: u32) -> Database;
103
104    /// # Limit the query to a range
105    ///
106    /// Limit the query result by from and to inclusively.
107    ///
108    #[wasm_bindgen(method, catch, js_name = range)]
109    pub async fn range(this: &Database, from: u32, to: u32) -> Result<JsValue, JsValue>;
110    #[wasm_bindgen(method, js_name = range)]
111    pub fn range_(this: &Database, from: u32, to: u32) -> Database;
112
113    /// # Retrieve the query as one row
114    ///
115    /// Return data as a single object instead of an array of objects.
116    ///
117    #[wasm_bindgen(method, catch, js_name = single)]
118    pub async fn single(this: &Database) -> Result<JsValue, JsValue>;
119
120    /// # Retrieve the query as 0-1 rows
121    ///
122    /// Return data as a single object instead of an array of objects.
123    ///
124    #[wasm_bindgen(method, catch, js_name = maybeSingle)]
125    pub async fn maybe_single(this: &Database) -> Result<JsValue, JsValue>;
126
127    /// # Retrieve the query as a CSV string
128    ///
129    /// Return data as a string in CSV format.
130    ///
131    /// ```ignore
132    /// let csv = client.get().from("countries").select_(Some("*")).csv().await.unwrap();
133    /// ```
134    ///
135    #[wasm_bindgen(method, catch, js_name = csv)]
136    pub async fn csv(this: &Database) -> Result<JsValue, JsValue>;
137
138    /// # Column is equal to a value
139    ///
140    /// Match only rows where column is equal to value.
141    ///
142    #[wasm_bindgen(method, catch, js_name = eq)]
143    pub async fn eq(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
144    #[wasm_bindgen(method, js_name = eq)]
145    pub fn eq_(this: &Database, column: &str, value: &JsValue) -> Database;
146
147    /// # Column is not equal to a value
148    ///
149    /// Match only rows where column is not equal to value.
150    ///
151    #[wasm_bindgen(method, catch, js_name = neq)]
152    pub async fn neq(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
153    #[wasm_bindgen(method, js_name = neq)]
154    pub fn neq_(this: &Database, column: &str, value: &JsValue) -> Database;
155
156    /// # Column is greater than a value
157    ///
158    /// Match only rows where column is greater than value.
159    ///
160    #[wasm_bindgen(method, catch, js_name = gt)]
161    pub async fn gt(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
162    #[wasm_bindgen(method, js_name = gt)]
163    pub fn gt_(this: &Database, column: &str, value: &JsValue) -> Database;
164
165    /// # Column is greater than or equal to a value
166    ///
167    /// Match only rows where column is greater than or equal to value.
168    ///
169    #[wasm_bindgen(method, catch, js_name = gte)]
170    pub async fn gte(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
171    #[wasm_bindgen(method, js_name = gte)]
172    pub fn gte_(this: &Database, column: &str, value: &JsValue) -> Database;
173
174    /// # Column is less than a value
175    ///
176    /// Match only rows where column is less than value.
177    ///
178    #[wasm_bindgen(method, catch, js_name = lt)]
179    pub async fn lt(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
180    #[wasm_bindgen(method, js_name = lt)]
181    pub fn lt_(this: &Database, column: &str, value: &JsValue) -> Database;
182
183    /// # Column is less than or equal to a value
184    ///
185    /// Match only rows where column is less than or equal to value.
186    ///
187    #[wasm_bindgen(method, catch, js_name = lte)]
188    pub async fn lte(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
189    #[wasm_bindgen(method, js_name = lte)]
190    pub fn lte_(this: &Database, column: &str, value: &JsValue) -> Database;
191
192    /// # Column matches a pattern
193    ///
194    /// Match only rows where column matches pattern case-sensitively.
195    ///
196    #[wasm_bindgen(method, catch, js_name = like)]
197    pub async fn like(this: &Database, column: &str, pattern: &str) -> Result<JsValue, JsValue>;
198    #[wasm_bindgen(method, js_name = like)]
199    pub fn like_(this: &Database, column: &str, pattern: &str) -> Database;
200
201    /// # Column matches a case-insensitive pattern
202    ///
203    /// Match only rows where column matches pattern case-insensitively.
204    ///
205    /// ```ignore
206    /// client.from("countries").select(None).ilike(&"name", &"%alba%").await;
207    /// ```
208    ///
209    #[wasm_bindgen(method, catch, js_name = ilike)]
210    pub async fn ilike(this: &Database, column: &str, pattern: &str) -> Result<JsValue, JsValue>;
211    #[wasm_bindgen(method, js_name = ilike)]
212    pub fn ilike_(this: &Database, column: &str, pattern: &str) -> Database;
213
214    /// # Column is a value
215    ///
216    /// Match only rows where column IS value.
217    ///
218    /// ```ignore
219    /// // check for nullness
220    /// client.from("countries").select(None).is("name", JsValue::NULL);
221    /// // or check for true of false
222    /// client.from("countries").select(None).is("name", JsValue::TRUE);
223    /// ```
224    ///
225    #[wasm_bindgen(method, catch, js_name = is)]
226    pub async fn is(this: &Database, column: &str, value: &JsValue) -> Result<JsValue, JsValue>;
227    #[wasm_bindgen(method, js_name = is)]
228    pub fn is_(this: &Database, column: &str, value: &JsValue) -> Database;
229
230    /// # Column is in an array
231    ///
232    /// Match only rows where column is included in the values array.
233    ///
234    #[wasm_bindgen(method, catch, js_name = in)]
235    pub async fn r#in(
236        this: &Database,
237        column: &str,
238        values: Vec<JsValue>,
239    ) -> Result<JsValue, JsValue>;
240    #[wasm_bindgen(method, js_name = in)]
241    pub fn r#in_(this: &Database, column: &str, values: Vec<JsValue>) -> Database;
242
243    /// # Column contains every element in a value
244    ///
245    /// Only relevant for jsonb, array, and range columns. Match only rows where column contains every element appearing in value.
246    ///
247    #[wasm_bindgen(method, catch, js_name = contains)]
248    pub async fn contains(
249        this: &Database,
250        column: &str,
251        value: JsValue,
252    ) -> Result<JsValue, JsValue>;
253    #[wasm_bindgen(method, js_name = contains)]
254    pub fn contains_(this: &Database, column: &str, value: JsValue) -> Database;
255
256    /// # Contained by value
257    ///
258    /// Only relevant for jsonb, array, and range columns. Match only rows where every element appearing in column is contained by value.
259    ///
260    #[wasm_bindgen(method, catch, js_name = containedBy)]
261    pub async fn contained_by(
262        this: &Database,
263        column: &str,
264        value: JsValue,
265    ) -> Result<JsValue, JsValue>;
266    #[wasm_bindgen(method, js_name = containedBy)]
267    pub fn contained_by_(this: &Database, column: &str, value: JsValue) -> Database;
268
269    /// # Greater than a range
270    ///
271    /// Only relevant for range columns. Match only rows where every element in column is greater than any element in range.
272    ///
273    #[wasm_bindgen(method, catch, js_name = rangeGt)]
274    pub async fn range_gt(this: &Database, column: &str, range: &str) -> Result<JsValue, JsValue>;
275    #[wasm_bindgen(method, js_name = rangeGt)]
276    pub fn range_gt_(this: &Database, column: &str, range: &str) -> Database;
277
278    /// # Greater than or equal to a range
279    ///
280    /// Only relevant for range columns. Match only rows where every element in column is either contained in range or greater than any element in range.
281    ///
282    #[wasm_bindgen(method, catch, js_name = rangeGte)]
283    pub async fn range_gte(this: &Database, column: &str, range: &str) -> Result<JsValue, JsValue>;
284    #[wasm_bindgen(method, js_name = rangeGte)]
285    pub fn range_gte_(this: &Database, column: &str, range: &str) -> Database;
286
287    /// # Less than a range
288    ///
289    /// Only relevant for range columns. Match only rows where every element in column is less than any element in range.
290    ///
291    #[wasm_bindgen(method, catch, js_name = rangeLt)]
292    pub async fn range_lt(this: &Database, column: &str, range: &str) -> Result<JsValue, JsValue>;
293    #[wasm_bindgen(method, js_name = rangeLt)]
294    pub fn range_lt_(this: &Database, column: &str, range: &str) -> Database;
295
296    /// # Less than or equal to a range
297    ///
298    /// Only relevant for range columns. Match only rows where every element in column is either contained in range or less than any element in range.
299    ///
300    #[wasm_bindgen(method, catch, js_name = rangeLte)]
301    pub async fn range_lte(this: &Database, column: &str, range: &str) -> Result<JsValue, JsValue>;
302    #[wasm_bindgen(method, js_name = rangeLte)]
303    pub fn range_lte_(this: &Database, column: &str, range: &str) -> Database;
304
305    /// # Mutually exclusive to a range
306    ///
307    /// Only relevant for range columns. Match only rows where column is mutually exclusive to range and there can be no element between the two ranges.
308    ///
309    #[wasm_bindgen(method, catch, js_name = rangeAdjacent)]
310    pub async fn range_adjacent(
311        this: &Database,
312        column: &str,
313        range: &str,
314    ) -> Result<JsValue, JsValue>;
315    #[wasm_bindgen(method, js_name = rangeAdjacent)]
316    pub fn range_adjacent_(this: &Database, column: &str, range: &str) -> Database;
317
318    /// # With a common element
319    ///
320    /// Only relevant for array and range columns. Match only rows where column and value have an element in common.
321    ///
322    #[wasm_bindgen(method, catch, js_name = overlaps)]
323    pub async fn overlaps(
324        this: &Database,
325        column: &str,
326        value: JsValue,
327    ) -> Result<JsValue, JsValue>;
328    #[wasm_bindgen(method, js_name = overlaps)]
329    pub fn overlaps_(this: &Database, column: &str, value: JsValue) -> Database;
330
331    /// # Match a string
332    ///
333    /// Only relevant for text and tsvector columns. Match only rows where column matches the query string in query.
334    ///
335    #[wasm_bindgen(method, catch, js_name = textSearch)]
336    pub async fn text_search(
337        this: &Database,
338        column: &str,
339        query: &str,
340        options: JsValue,
341    ) -> Result<JsValue, JsValue>;
342    #[wasm_bindgen(method, js_name = textSearch)]
343    pub fn text_search_(this: &Database, column: &str, query: &str, options: JsValue) -> Database;
344
345    /// # Update data
346    ///
347    /// Perform an UPDATE on the table or view.
348    ///
349    #[wasm_bindgen(method, catch, js_name = update)]
350    pub async fn update(this: &Database, values: &JsValue) -> Result<JsValue, JsValue>;
351    #[wasm_bindgen(method, js_name = update)]
352    pub fn update_(this: &Database, values: &JsValue) -> Database;
353
354    /// # Upsert data
355    ///
356    /// Perform an UPSERT on the table or view.
357    ///
358    #[wasm_bindgen(method, js_name = upsert)]
359    pub fn upsert(this: &Database, values: JsValue) -> Database;
360
361    /// # Delete data
362    ///
363    /// Should always be combined with filters
364    ///
365    /// ```ignore
366    /// let client = supabase_js_rs::create_client("https://xyzcompany.supabase.co", "public-anon-key");
367    /// let res: Result<JsValue, JsValue> = client.from("countries").delete().eq("id", 1.into_js_result().unwrap()).await;
368    /// ```
369    ///
370    #[wasm_bindgen(method, js_name = delete)]
371    pub fn delete(this: &Database) -> Database;
372
373    /// # Insert data
374    ///
375    /// Perform an INSERT into the table or view.
376    ///
377    #[wasm_bindgen(method, catch, js_name = insert)]
378    pub async fn insert(this: &Database, values: JsValue) -> Result<JsValue, JsValue>;
379    #[wasm_bindgen(method, js_name = insert)]
380    pub fn insert_(this: &Database, values: JsValue) -> Database;
381
382    /// Auth methods
383    #[wasm_bindgen(method, getter = auth)]
384    pub fn auth(this: &SupabaseClient) -> Auth;
385
386    pub type Auth;
387
388    /// # Sign in anonymously
389    ///
390    #[wasm_bindgen(method, catch, js_name = signInAnonymously)]
391    pub async fn sign_in_anonymously(this: &Auth) -> Result<JsValue, JsValue>;
392
393    /// # Create a new user
394    ///
395    #[wasm_bindgen(method, catch, js_name = signUp)]
396    pub async fn sign_up(this: &Auth, credentials: Credentials) -> Result<JsValue, JsValue>;
397
398    /// # Sign in a user
399    ///
400    #[wasm_bindgen(method, catch, js_name = signInWithPassword)]
401    pub async fn sign_in_with_password(
402        this: &Auth,
403        credentials: Credentials,
404    ) -> Result<JsValue, JsValue>;
405
406    /// # Sign in a user through OTP
407    ///
408    /// Log in a user using magiclink or a one-time password (OTP).
409    ///
410    #[wasm_bindgen(method, catch, js_name = signInWithOtp)]
411    pub async fn sign_in_with_otp(this: &Auth, credentials: JsValue) -> Result<JsValue, JsValue>;
412
413    /// # Sign in a user through OAuth
414    ///
415    /// Log in an existing user via a third-party provider.
416    ///
417    #[wasm_bindgen(method, catch, js_name = signInWithOAuth)]
418    pub async fn sign_in_with_oauth(
419        this: &Auth,
420        credentials: SignInWithOAuthCredentials,
421    ) -> Result<JsValue, JsValue>;
422
423    /// # Sign out a user
424    ///
425    #[wasm_bindgen(method, catch, js_name = signOut)]
426    pub async fn sign_out(this: &Auth) -> Result<JsValue, JsValue>;
427
428    /// # Retrieve a session
429    ///
430    /// Returns the session, refreshing it if necessary.
431    #[wasm_bindgen(method, catch, js_name = getSession)]
432    pub async fn get_session(this: &Auth) -> Result<JsValue, JsValue>;
433
434    /// # Retrieve a new session
435    ///
436    /// Returns a new session, regardless of expiry status.
437    #[wasm_bindgen(method, catch, js_name = refreshSession)]
438    pub async fn refresh_session(this: &Auth) -> Result<JsValue, JsValue>;
439
440    /// # Retrieve a user
441    ///
442    /// Takes in an optional access token jwt or get the jwt from the current session.
443    #[wasm_bindgen(method, catch, js_name = getUser)]
444    pub async fn get_user(this: &Auth, jwt: Option<&str>) -> Result<JsValue, JsValue>;
445
446    /// # Update user
447    ///
448    /// Updates user data, if there is a logged in user.
449    ///
450    #[wasm_bindgen(method, catch, js_name = updateUser)]
451    pub async fn update_user(this: &Auth, attributes: JsValue) -> Result<JsValue, JsValue>;
452
453    #[wasm_bindgen(method, catch, js_name = setSession)]
454    pub async fn set_session(
455        this: &Auth,
456        current_session: CurrentSession,
457    ) -> Result<JsValue, JsValue>;
458
459    /// Listen to auth events
460    ///
461    /// # Example
462    ///
463    /// ```ignore
464    /// let client = supabase_js_rs::create_client("SUPABASE_URL", "SUPABASE_ANON_KEY");
465    /// let auth_event_callback: Closure<dyn FnMut(JsValue, JsValue)> = Closure::new(move |event: JsValue, session: JsValue| {
466    ///
467    /// });
468    /// client.auth().on_auth_state_change(&auth_event_callback);
469    /// auth_event_callback.forget();
470    /// ```
471    #[wasm_bindgen(method, js_name = onAuthStateChange)]
472    pub fn on_auth_state_change(this: &Auth, callback: &Closure<dyn FnMut(JsValue, JsValue)>);
473
474    /// # Send a password reset request
475    ///
476    /// Sends a password reset request to an email address.
477    ///
478    #[wasm_bindgen(method, catch, js_name = resetPasswordForEmail)]
479    pub async fn reset_password_for_email(
480        this: &Auth,
481        email: &str,
482        options: JsValue,
483    ) -> Result<JsValue, JsValue>;
484
485    /*
486    pub type Mfa;
487
488    #[wasm_bindgen(method, getter = mfa)]
489    pub fn mfa(this: &Auth) -> Mfa;
490
491    /// Create a challenge
492    #[wasm_bindgen(method, catch, js_name = challenge)]
493    pub fn challenge(this: &Mfa, params: MFAChallengeParams) -> Result<JsValue, JsValue>;
494
495    /// Verify a challenge
496    #[wasm_bindgen(method, catch, js_name = verify)]
497    pub fn verify(this: &Mfa, params: MFAVerifyParams) -> Result<JsValue, JsValue>;
498    */
499
500    #[wasm_bindgen(method, js_name = channel)]
501    pub fn channel(this: &SupabaseClient, name: &str) -> RealtimeChannel;
502
503    /// # Unsubscribe from all channels
504    ///
505    #[wasm_bindgen(method, js_name = removeAllChannels)]
506    pub fn remove_all_channels(this: &SupabaseClient);
507
508    /// # Retrieve all channels
509    ///
510    #[wasm_bindgen(method, js_name = getChannels)]
511    pub fn get_channels(this: &SupabaseClient) -> JsValue;
512
513    pub type RealtimeChannel;
514
515    /// # Subscribe to database changes
516    ///
517    #[wasm_bindgen(method, js_name = on)]
518    pub fn on(
519        this: &RealtimeChannel,
520        r#type: &str,
521        filter: &JsValue,
522        callback: &Closure<dyn Fn(JsValue)>,
523    ) -> RealtimeChannel;
524
525    #[wasm_bindgen(method, js_name = subscribe)]
526    pub fn subscribe(
527        this: &RealtimeChannel,
528        callback: Option<&Closure<dyn FnMut(JsValue, JsValue)>>,
529    ) -> RealtimeChannel;
530
531    #[wasm_bindgen(method, js_name = storage)]
532    pub fn storage(this: &SupabaseClient) -> Storage;
533
534    pub type Storage;
535
536    /// # Create a bucket
537    ///
538    /// Creates a new Storage bucket
539    ///
540    #[wasm_bindgen(method, catch, js_name = createBucket)]
541    pub async fn create_bucket(this: &Storage, id: &str) -> Result<JsValue, JsValue>;
542
543    /// # Retrieve a bucket
544    ///
545    /// Retrieves the details of an existing Storage bucket.
546    ///
547    #[wasm_bindgen(method, catch, js_name = getBucket)]
548    pub async fn get_bucket(this: &Storage, id: &str) -> Result<JsValue, JsValue>;
549
550    /// # List all buckets
551    ///
552    /// Retrieves the details of all Storage buckets within an existing project.
553    ///
554    #[wasm_bindgen(method, catch, js_name = listBuckets)]
555    pub async fn list_buckets(this: &Storage) -> Result<JsValue, JsValue>;
556
557    /// # Update a bucket
558    ///
559    /// Updates a Storage bucket
560    ///
561    #[wasm_bindgen(method, catch, js_name = updateBucket)]
562    pub async fn update_bucket(this: &Storage, options: JsValue) -> Result<JsValue, JsValue>;
563
564    /// # Empty a bucket
565    ///
566    /// Removes all objects inside a single bucket.
567    ///
568    #[wasm_bindgen(method, catch, js_name = emptyBucket)]
569    pub async fn empty_bucket(this: &Storage, id: &str) -> Result<JsValue, JsValue>;
570
571    /// # Delete a bucket
572    ///
573    /// Deletes an existing bucket. A bucket can't be deleted with existing objects inside it.
574    ///
575    #[wasm_bindgen(method, catch, js_name = deleteBucket)]
576    pub async fn delete_bucket(this: &Storage, id: &str) -> Result<JsValue, JsValue>;
577
578}