Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🆙 Supabase のライブラリを v2 にバージョンアップ #88

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package club.nito.core.network

import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.gotrue.GoTrue
import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.postgrest.Postgrest
import io.github.jan.supabase.realtime.Realtime
import io.github.jan.supabase.serializer.KotlinXSerializer
Expand All @@ -19,7 +19,7 @@ internal fun createNitoSupabaseClient(
supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd0Zmp1a3JhdXlocmJnbHJ6bHZhIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTkwMDQ3NTgsImV4cCI6MjAxNDU4MDc1OH0.zRjlzXVyz4vBM8Tb8GcpyPyTkCmOwdV-Xs18Agw2w-E",
) {
install(Postgrest)
install(GoTrue)
install(Auth)
install(Realtime)

defaultSerializer = KotlinXSerializer(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import club.nito.core.model.AuthStatus
import club.nito.core.model.FetchSingleResult
import club.nito.core.model.UserInfo
import club.nito.core.model.UserSession
import io.github.jan.supabase.gotrue.GoTrue
import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.gotrue.SessionStatus
import io.github.jan.supabase.gotrue.providers.builtin.Email
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

public class SupabaseAuthRemoteDataSource(
private val goTrue: GoTrue,
private val goTrue: Auth,
) : AuthRemoteDataSource {
override val authStatus: Flow<FetchSingleResult<AuthStatus>> = goTrue.sessionStatus.map {
when (it) {
Expand All @@ -37,12 +37,12 @@ public class SupabaseAuthRemoteDataSource(
}
}

override suspend fun login(email: String, password: String): Unit = goTrue.loginWith(Email) {
override suspend fun login(email: String, password: String): Unit = goTrue.signInWith(Email) {
this.email = email
this.password = password
}

override suspend fun logout(): Unit = goTrue.logout()
override suspend fun logout(): Unit = goTrue.signOut()

override suspend fun modifyAuthUser(email: String?, password: String?): UserInfo = goTrue.modifyUser {
this.email = email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package club.nito.core.network.di
import club.nito.core.network.createNitoKtorJsonSettings
import club.nito.core.network.createNitoSupabaseClient
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.gotrue.GoTrue
import io.github.jan.supabase.gotrue.gotrue
import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.gotrue.auth
import kotlinx.serialization.json.Json
import org.koin.core.module.Module
import org.koin.dsl.module
Expand All @@ -18,7 +18,7 @@ public val supabaseClientModule: Module = module {
single<Json> {
createNitoKtorJsonSettings()
}
single<GoTrue> {
get<SupabaseClient>().gotrue
single<Auth> {
get<SupabaseClient>().auth
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,43 @@ import club.nito.core.network.participation.model.NetworkParticipant
import club.nito.core.network.participation.model.toNetworkModel
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.postgrest.postgrest
import io.github.jan.supabase.postgrest.query.Returning
import io.github.jan.supabase.postgrest.query.Count

public class SupabaseParticipantRemoteDataSource(
private val client: SupabaseClient,
) : ParticipantRemoteDataSource {
private val postgrest = client.postgrest["participants"]

override suspend fun getParticipants(scheduleId: String): List<Participant> = postgrest
.select(
filter = {
.select {
filter {
and {
eq("schedule_id", scheduleId)
exact("deleted_at", null)
}
},
)
}
}
.decodeList<NetworkParticipant>()
.map(NetworkParticipant::toParticipant)

override suspend fun getParticipants(scheduleIds: List<String>): List<Participant> = postgrest
.select(
filter = {
.select {
filter {
and {
isIn("schedule_id", scheduleIds)
exact("deleted_at", null)
}
},
)
}
}
.decodeList<NetworkParticipant>()
.map(NetworkParticipant::toParticipant)

override suspend fun participate(declaration: ParticipantDeclaration): Long {
val result = postgrest.insert(
value = declaration.toNetworkModel(),
upsert = true,
returning = Returning.MINIMAL,
filter = {},
)
return result.count() ?: 0
) {
count(Count.EXACT)
}
return result.countOrNull() ?: 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ public class SupabaseScheduleRemoteDataSource(
order: Order,
after: Instant?,
): List<Schedule> = postgrest
.select(
filter = {
.select {
filter {
exact(Column.DELETED_AT.columnName, null)
after?.let { gte(Column.SCHEDULED_AT.columnName, it) }
order(Column.SCHEDULED_AT.columnName, order = order.toSupabaseOrder())
limit(count = limit.toLong())
},
)
}
order(Column.SCHEDULED_AT.columnName, order = order.toSupabaseOrder())
limit(count = limit.toLong())
}
.decodeList<NetworkSchedule>()
.map(NetworkSchedule::toSchedule)
.also { log.d { "getScheduleList: $it" } }

override suspend fun getSchedule(id: String): Schedule {
return postgrest
.select(
single = true,
filter = { eq("id", id) },
)
.select {
single()
filter {
exact(Column.DELETED_AT.columnName, null)
}
}
.decodeSingle<NetworkSchedule>()
.toSchedule()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ public class SupabaseUserRemoteDataSource(

override suspend fun getProfile(userId: String): UserProfile? {
return postgrest
.select(
filter = {
.select {
single()
filter {
eq("id", userId)
},
)
}
}
.decodeSingleOrNull<NetworkUserProfile>()
?.let(NetworkUserProfile::toUserProfile)
}

override suspend fun getProfiles(userIds: List<String>): List<UserProfile> {
return postgrest
.select(
filter = {
.select {
filter {
isIn("id", userIds)
},
)
}
}
.decodeList<NetworkUserProfile>()
.map(NetworkUserProfile::toUserProfile)
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ossLicenses = "17.0.1"
detekt = "1.23.4"
twitterComposeRule = "0.0.26"
kover = "0.7.4"
supabase = "1.4.5"
supabase = "2.0.0-alpha-2"
logback = "1.4.11"
graphqlKotlin = "7.0.2"
kotest = "5.8.0"
Expand Down