B4J Tutorial [BANano] SupaBase (FireBase Alternative) crud example with SSE

Hi

Supabase is an open source Firebase alternative. It provides all the backend services you need to build a product. Supabase uses Postgres database with real-time capabilities. Basically, supabase provides an interface to manage postgres database that you can use to create table and insert, edit and delete data in the table.

We can use REST API or client libraries from supabase to access the data in the postgres database. Supabase is not just about accessing the database. it also provides some solutions out of the box such as Authentication, File Storage and Real-time capabilities.

For this example we will use the javascript sdk. I am using this for the SithasoDaisy framework.

First things first

1. Sign Up


2. Get the JavaScript SDK and include in your project.

3. Create a project.

4. Create a table with columns.

5. Get your keys...

supabaseconf.jpg


We have a table called books with a few fields. You can create any schema you want.

1671095380516.png



We initialize the class:

B4X:
'SELECT_ALL
    Dim supabase As SDUISupaBase
    supabase.Initialize(Me, "supabase", my_url, my_key)
    supabase.TableName = "books"
 

Attachments

  • SDUISupaBase.zip
    3.5 KB · Views: 85
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Subscriptions (insert, update, delete)

One can also subscribe to events that happen within a table.

B4X:
Dim supabase As SDUISupaBase
    supabase.Initialize(Me, "supabase", my_url, my_key)
    supabase.TableName = "books"
    supabase.Subscribe

B4X:
Private Sub supabase_Changes (payload As Map)
    Log(payload)
End Sub

To activate subscriptions, one executes .Subscribe. So each time we add, update and delete a record on the table, the _changes callback is fired.

1671272050746.png
 

Mashiane

Expert
Licensed User
Longtime User
Complete Code

B4X:
Dim supabase As SDUISupaBase
    supabase.Initialize(Me, "supabase", my_url, my_key)
    supabase.TableName = "books"
    supabase.Subscribe
    
    'SELECT ALL
    supabase.CLEAR_WHERE
    supabase.ADD_ORDER_BY("title", True)
    BANano.Await(supabase.SELECT_ALL)
    Do While supabase.NextRow
        'Log(supabase.record)
    Loop
    supabase.MoveLast
    Dim sid As String = supabase.GetString("id")
    Log(sid)
    '
    'CREATE
    supabase.PrepareRecord
    supabase.SetField("title", "Xolani Mbanga " & DateTime.Now)
    supabase.SetField("author", "Mashy")
    supabase.SetField("finished", False)
    Dim bAdded As Boolean = BANano.Await(supabase.CREATE)
    'Log(bAdded)
    '
    'UPDATE
    supabase.PrepareRecord
    supabase.setfield("id", sid)
    supabase.SetField("title", "Anele Mbanga " & SDUIShared.DateTimeNow)
    supabase.SetField("author", "Anie")
    supabase.SetField("finished", True)
    Dim bUpdated As Boolean = BANano.Await(supabase.UPDATE)
    'Log(bUpdated)
    '
    'DELETE
    Dim bDeleted As Boolean = BANano.Await(supabase.DELETE(sid))
    'Log(bDeleted)
    '
    'SELECT_WHERE...
    supabase.CLEAR_WHERE
    supabase.ADD_FIELD("id")
    supabase.ADD_FIELD("title")
    supabase.ADD_WHERE("finished", "=", False)
    supabase.ADD_ORDER_BY("title", False)
    Dim result As List = BANano.Await(supabase.SELECT_WHERE)
    'Log(result)
    '
    'READ...
    Dim rec As Map = BANano.Await(supabase.READ(1))
    'Log(rec)


Catching Subscription

B4X:
Private Sub supabase_Changes (eventType As String, New As Map, Old As Map, TableName As String)
    Select Case TableName
    Case "books"    
        Select Case eventType
        Case "INSERT"
        Case "UPDATE"
            'get id that was updated
            Dim sid As String = Old.Get("id")
        Case "DELETE"
            'get id that was deleted
            Dim sid As String = Old.Get("id")
        End Select
    End Select
End Sub
 
Top