Skip to content

Commit

Permalink
Merge pull request #11 from mnbjhu/add_lq_to_readme
Browse files Browse the repository at this point in the history
Adding live queries section
  • Loading branch information
mnbjhu committed Sep 5, 2023
2 parents 3493250 + 41b7843 commit 60c6c14
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,41 @@ assert(author.username == "John")
assert(author.password == "1234")
```

<b>Live Queries</b>

Basic Usage:
```kotlin
// You can listen to changes in a table using the observeLiveQuery function
val incoming: LiveQueryFlow<User> = db.observeLiveQuery("user")
// Each frame contains the type of change and the updated record
incoming.collect { frame ->
when (frame) {
is LiveQueryAction.Create -> {
println("New record created")
}
is LiveQueryAction.Update -> {
println("Record updated")
}
is LiveQueryAction.Delete -> {
println("Record deleted")
}
}
}
// LiveQueryFlow implements to Closeable interface
incoming.close()
```

For more control you can use the regular query method and subscribe to updates manually:
```kotlin
val result = db.query(
"LIVE SELECT * FROM user WHERE username = \$username",
bind("username", "John")
)
val liveQueryId = result.first().data<String>()
val incoming = db.subscribe<TestClass>(liveQueryId)
db.kill(liveQueryId)
db.unsubscribe(liveQueryId)
```
## Links
- [SurrealDB](https://surrealdb.com/)
- [Api Documentation](https://mnbjhu.github.io/surrealdb-kotlin-driver/api/)
Expand Down

0 comments on commit 60c6c14

Please sign in to comment.