Supabase
Introducció
Section titled “Introducció”Supabase …
You can use supabase-kt to interact with your Postgres database, listen to database changes, invoke Deno Edge Functions, build login and user management functionality, and manage large files.
Base de dades
Section titled “Base de dades”How you connect to your database depends on where you’re connecting from:
- For frontend applications, use the Data API
- For Postgres clients, use a connection string
The direct connection string connects directly to your Postgres instance.
The connection string looks like this:
postgresql://postgres:[YOUR-PASSWORD]@db.apbkobhfnmcqqzqeeqss.supabase.co:5432/postgres
Get your project’s direct string from the Database Settings page:
- Go to the
Settings
section. - Click
Database
. - Under
Connection string
, make sureDisplay connection pooler
is unchecked. Copy the URI.
Entorn de treball
Section titled “Entorn de treball”> gradle init --package dev.xtec --project-name supabase --java-version 21 --type kotlin-application --dsl kotlin --test-framework kotlintest --no-split-project --no-incubating --overwrite
Configura el fitxer build.gradle.kts
:
plugins { kotlin("plugin.serialization") version "2.0.0" // ...}
dependencies { implementation("io.github.cdimascio:dotenv-kotlin:6.5.0") implementation(platform("io.github.jan-tennert.supabase:bom:3.0.3")) implementation("io.github.jan-tennert.supabase:postgrest-kt") implementation("io.github.jan-tennert.supabase:auth-kt") implementation("io.github.jan-tennert.supabase:realtime-kt") implementation("io.ktor:ktor-client-cio:3.0.3") // ...
Crea un fitxer app/src/main/resources/.env/
per guardar …
URL = ...API = ...
Modifica el fitxer App.kt
:
import io.github.cdimascio.dotenv.dotenvimport io.github.jan.supabase.auth.Authimport io.github.jan.supabase.createSupabaseClientimport io.github.jan.supabase.postgrest.Postgrest
fun main() {
val dotenv = dotenv()
val supabase = createSupabaseClient( supabaseUrl = dotenv["URL"], supabaseKey = dotenv["KEY"] ) { install(Auth) install(Postgrest) //install(Storage) }}
Verifica que funciona
Base de dades
Section titled “Base de dades”Crea una taula nova amb el SQL Editor:
{% image “sql-editor.png” %}
create table if not exists dog ( id serial primary key, name text not null)
Tens més informació a Managing tables views and data
Crear les classes Dog
i DogInsert
:
@Serializabledata class Dog( val id: Long, val name: String)
@Serializabledata class DogInsert( val name: String)
Necessites la classe DogInsert
perquè l’atribut id
és una clau subrogada.
Ja pots inserir el primer gos:
fun main() { // ...
runBlocking {
val pg = supabase.postgrest
pg.from("dog").insert(DogInsert("Trufa")) }}
Executa el codi i verifica al “Table Editor” que s’ha inserit el nou registre:
{% image “table-editor-dog.png” %}
Si vols retornar les dades inserides, pots utilitzar el mètode select()
dins de la sol·licitud:
fun main() { // ...
runBlocking {
val pg = supabase.postgrest
pg.from("dog").insert(DogInsert("Ketsu")) { select() }.decodeSingle<Dog>().also { println(it) }
// Dog(id=2, name=Ketsu) }}
També pots inserir diversos gossos a la vegada:
fun main() { // ...
runBlocking {
val pg = supabase.postgrest
pg.from("dog").insert( listOf(DogInsert("Lassie"), DogInsert("Maverick")) ) { select() }.decodeList<Dog>().onEach { println(it) }
// Dog(id=3, name=Lassie) // Dog(id=4, name=Maverick) }}
Direct connection string
Section titled “Direct connection string”https://supabase.com/docs/guides/database/connecting-to-postgres#direct-connections
-
https://supabase.com/docs/guides/database/tables?queryGroups=language&language=kotlin Exemple: https://github.com/supabase/supabase/tree/master/examples/product-sample-supabase-kt
-
https://supabase.com/docs/guides/getting-started/tutorials/with-kotlin
-
https://www.restack.io/docs/supabase-knowledge-supabase-kotlin-integration