Skip to content

Commit

Permalink
Merge pull request #8 from powersync-ja/fix-watched-queries
Browse files Browse the repository at this point in the history
[Fix] Watched queries + Schema indexes
  • Loading branch information
stevensJourney authored Mar 20, 2024
2 parents 95ddf99 + 8f9289d commit 7a0abe0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ captures
!*.xcodeproj/project.xcworkspace/
!*.xcworkspace/contents.xcworkspacedata
**/xcshareddata/WorkspaceSettings.xcsettings
Pods/
Pods/
plugins/sonatype
dialect/bin
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString

Expand All @@ -38,10 +39,17 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

init {
scope.launch {
tableUpdates().debounce(DEFAULT_WATCH_THROTTLE_MS).collect { tables ->
val dataTables = tables.map { toFriendlyTableName(it) }.filter { it.isNotBlank() }
driver.notifyListeners(queryKeys = dataTables.toTypedArray())
}
val accumulatedUpdates = mutableSetOf<String>();
tableUpdates()
// Debounce will discard any events which occur inside the debounce window
// This will accumulate those table updates
.onEach { tables -> accumulatedUpdates.addAll(tables) }
.debounce(DEFAULT_WATCH_THROTTLE_MS)
.collect {
val dataTables = accumulatedUpdates.map { toFriendlyTableName(it) }.filter { it.isNotBlank() }
driver.notifyListeners(queryKeys = dataTables.toTypedArray());
accumulatedUpdates.clear();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package com.powersync.db.schema

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* Describes an indexed column.
*/
@Serializable
data class IndexedColumn (
data class IndexedColumn(
/**
* Name of the column to index.
*/
@SerialName("name")
val column: String,

/**
* Whether this column is stored in ascending order in the index.
*/
private val ascending: Boolean = true
private val ascending: Boolean = true,

private var columnDefinition: Column? = null,

/**
* The column definition type
*/
var type: ColumnType? = null
) {
companion object {
fun ascending(column: String) = IndexedColumn(column, true)
fun descending(column: String) = IndexedColumn(column, false)
}

/**
* Sets the parent column definition. The column definition's type
* is required for the serialized JSON payload of powersync_replace_schema
*/
fun setColumnDefinition(column: Column) {
this.type = column.type;
this.columnDefinition = column;
}

fun toSql(table: Table): String {
val fullColumn = table[column] // errors if not found
return fullColumn.let {
Expand Down
18 changes: 17 additions & 1 deletion core/src/commonMain/kotlin/com/powersync/db/schema/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.powersync.db.schema

import com.powersync.invalidSqliteCharacters
import kotlinx.serialization.Serializable
import kotlin.math.log

/**
* A single table in the schema.
*/
@Serializable

data class Table constructor(
data class Table constructor(
/**
* The synced table name, matching sync rules.
*/
Expand All @@ -34,6 +35,21 @@ data class Table constructor(
*/
private val _viewNameOverride: String? = null
) {

init {
/**
* Need to set the column definition for each index column.
* This is required for serialization
*/
indexes.forEach { index ->
index.columns.forEach {
val matchingColumn = columns.find { c -> c.name == it.column }
?: throw AssertionError("Could not find column definition for index ${index.name}:${it.column}")
it.setColumnDefinition(column = matchingColumn)
}
}
}

companion object {
/**
* Create a table that only exists locally.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.powersync.demos

import com.powersync.db.schema.Column
import com.powersync.db.schema.Index
import com.powersync.db.schema.IndexedColumn
import com.powersync.db.schema.Schema
import com.powersync.db.schema.Table

val AppSchema: Schema = Schema(
listOf(
Table(
"customers",
listOf(
name = "customers",
columns = listOf(
Column.text("name"),
Column.text("email")
),
indexes = listOf(
Index("name", listOf(IndexedColumn.descending("name")))
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ development=true
RELEASE_SIGNING_ENABLED=true
# Library config
GROUP=com.powersync
LIBRARY_VERSION=0.0.1-ALPHA4
LIBRARY_VERSION=0.0.1-ALPHA5
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
# POM
POM_URL=https://github.com/powersync-ja/powersync-kotlin/
Expand Down

0 comments on commit 7a0abe0

Please sign in to comment.