Skip to content

Instantly share code, notes, and snippets.

@pavelpoley
Created September 6, 2025 17:37
Show Gist options
  • Select an option

  • Save pavelpoley/32fddae4274bffdd3ba60558f864f66f to your computer and use it in GitHub Desktop.

Select an option

Save pavelpoley/32fddae4274bffdd3ba60558f864f66f to your computer and use it in GitHub Desktop.
PowerSync
import android.content.Context
import co.touchlab.kermit.Logger
import co.touchlab.kermit.Logger.Companion.setMinSeverity
import co.touchlab.kermit.Severity
import com.powersync.DatabaseDriverFactory
import com.powersync.ExperimentalPowerSyncAPI
import com.powersync.PowerSyncDatabase
import com.powersync.connector.supabase.SupabaseConnector
import com.powersync.db.schema.PendingStatement
import com.powersync.db.schema.PendingStatementParameter
import com.powersync.db.schema.RawTable
import com.powersync.db.schema.Schema
import com.powersync.sync.SyncOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
object PowerSyncClient {
fun createConnector(): SupabaseConnector {
return SupabaseConnector(
supabaseClient = SupabaseClient.client,
powerSyncEndpoint = "*****"
)
}
@OptIn(ExperimentalPowerSyncAPI::class)
val schema = Schema(
listOf(
RawTable(
name = Tables.Collections.TABLE_NAME,
put = PendingStatement(
"INSERT OR REPLACE INTO ${Tables.Collections.TABLE_NAME} " +
"(${Tables.Collections.ID}, ${Tables.Collections.USER_ID}, " +
"${Tables.Collections.NAME_COL}, ${Tables.Collections.CREATED_AT}, ${Tables.Collections.UPDATED_AT}) " +
"VALUES (?, ?, ?, ?, ?)",
listOf(
PendingStatementParameter.Id,
PendingStatementParameter.Column(Tables.Collections.USER_ID),
PendingStatementParameter.Column(Tables.Collections.NAME_COL),
PendingStatementParameter.Column(Tables.Collections.CREATED_AT),
PendingStatementParameter.Column(Tables.Collections.UPDATED_AT)
)
),
delete = PendingStatement(
"DELETE FROM ${Tables.Collections.TABLE_NAME} WHERE ${Tables.Collections.ID} = ?",
listOf(PendingStatementParameter.Id)
)
)
)
)
private var _database: PowerSyncDatabase? = null
val database: PowerSyncDatabase
get() = _database ?: error("Not initialized. Call initialize(context) first.")
fun initialize(context: Context) {
val driverFactory = DatabaseDriverFactory(context.applicationContext)
val logger = Logger.withTag("PowerSync").apply {
setMinSeverity(Severity.Verbose) // hide in production
}
_database = PowerSyncDatabase(
factory = driverFactory,
schema = schema,
logger = logger,
dbFilename = "collections.db"
)
}
private suspend fun createLocalTables() {
withContext(Dispatchers.IO) {
database.execute(
"""
CREATE TABLE IF NOT EXISTS ${Tables.Collections.TABLE_NAME} (
${Tables.Collections.ID} TEXT PRIMARY KEY,
${Tables.Collections.USER_ID} TEXT,
${Tables.Collections.NAME_COL} TEXT,
${Tables.Collections.CREATED_AT} TEXT,
${Tables.Collections.UPDATED_AT} TEXT
)
"""
)
}
}
@OptIn(ExperimentalPowerSyncAPI::class)
suspend fun connect() {
withContext(Dispatchers.IO) {
database.connect(
connector = createConnector(),
options = SyncOptions(newClientImplementation = true)
)
createLocalTables()
database.waitForFirstSync()
}
}
suspend fun disconnect() {
withContext(Dispatchers.IO) {
database.disconnect()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment