DEV Community

Abhishek @ Ace The Cloud
Abhishek @ Ace The Cloud

Posted on

Exploring Supabase's Postgres-powered features for GoLang applications

Introduction
Supabase is a cloud-based platform that provides developers with various features that are essential in building and maintaining web and mobile applications. One of the most powerful features of Supabase is its Postgres-powered database. In this article, we will explore how to use Supabase's Postgres-powered features for GoLang applications.

Connecting to Supabase Postgres Database
To start using Supabase's Postgres-powered database, you first need to connect to it. Supabase provides a client library for GoLang that allows you to connect to the database and perform various operations. To get started, you need to install the client library.

go get github.com/supabase/supabase-go
Enter fullscreen mode Exit fullscreen mode

After installing the client library, you can create a Supabase client by passing in your Supabase credentials.

import (
    "context"
    "fmt"

    "github.com/supabase/supabase-go"
)

func main() {
    sb, err := supabase.NewClient(context.Background(), "<SUPABASE_URL>", "<SUPABASE_KEY>")
    if err != nil {
        fmt.Println("Failed to create Supabase client:", err)
        return
    }
}
Enter fullscreen mode Exit fullscreen mode

Once you have created the Supabase client, you can start using it to interact with the database.

Querying Data
The next step is to query data from the database. You can do this using the Supabase client's QueryBuilder. The QueryBuilder allows you to construct SQL queries and execute them against the database.

res, err := sb.From("todos").Select("*").Execute(context.Background())
if err != nil {
    fmt.Println("Failed to query todos:", err)
    return
}

fmt.Println("Todos:", res.Data)
Enter fullscreen mode Exit fullscreen mode

The above code queries all the rows from the "todos" table and prints them to the console. You can also filter the results by adding a "Where" clause to the query.

res, err := sb.From("todos").Select("*").Eq("completed", true).Execute(context.Background())
if err != nil {
    fmt.Println("Failed to query todos:", err)
    return
}

fmt.Println("Completed todos:", res.Data)
Enter fullscreen mode Exit fullscreen mode

The above code queries all the completed todos from the "todos" table and prints them to the console.

Inserting Data
To insert data into the database, you can use the Supabase client's InsertBuilder. The InsertBuilder allows you to construct SQL insert statements and execute them against the database.

res, err := sb.From("todos").Insert(map[string]interface{}{
    "title": "Buy milk",
    "completed": false,
}).Execute(context.Background())
if err != nil {
    fmt.Println("Failed to insert todo:", err)
    return
}

fmt.Println("Inserted todo:", res.Data)
Enter fullscreen mode Exit fullscreen mode

The above code inserts a new todo into the "todos" table and prints the inserted row to the console.

Updating Data
To update data in the database, you can use the Supabase client's UpdateBuilder. The UpdateBuilder allows you to construct SQL update statements and execute them against the database.

res, err := sb.From("todos").Update(map[string]interface{}{
    "completed": true,
}).Eq("title", "Buy milk").Execute(context.Background())
if err != nil {
    fmt.Println("Failed to update todo:", err)
    return
}

fmt.Println("Updated todo:", res.Data)
Enter fullscreen mode Exit fullscreen mode

The above code updates the "completed" field of the todo with title "Buy milk" to true and prints the updated row to the console.

Deleting Data
To delete data from the database, you can use the Supabase client's DeleteBuilder. The DeleteBuilder allows you to construct SQL delete statements and execute them against the database.

res, err := sb.From("todos").Delete().Eq("title", "Buy milk").Execute(context.Background())
if err != nil {
    fmt.Println("Failed to delete todo:", err)
    return
}

fmt.Println("Deleted todo:", res.Data)
Enter fullscreen mode Exit fullscreen mode

The above code deletes the todo with title "Buy milk" from the "todos" table and prints the deleted row to the console.

Conclusion
In this article, we explored how to use Supabase's Postgres-powered features for GoLang applications. We learned how to connect to the database, query data, insert data, update data, and delete data. Supabase's Postgres-powered database provides a robust and scalable solution for building and maintaining web and mobile applications. By leveraging Supabase's client library for GoLang, you can easily interact with the database and build powerful applications with ease.

Top comments (2)

Collapse
 
paihari profile image
Hari Bantwal

Where is

github.com/supabase/supabase-go Dont find the libraries

Collapse
 
rasyidmm profile image
Rasyid Maulid Majid

github.com/nedpals/supabase-go