Skip to content

Commit 6ae15b5

Browse files
authored
Add listener support to readme (#96)
Co-authored-by: hfhbd <[email protected]>
1 parent d513708 commit 6ae15b5

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,37 @@ sqldelight {
2828
}
2929
````
3030

31+
## Usage
32+
33+
```kotlin
34+
val driver = PostgresNativeDriver(
35+
host = "localhost",
36+
port = 5432,
37+
user = "postgres",
38+
database = "postgres",
39+
password = "password",
40+
options = null,
41+
listenerSupport = ListenerSupport.Remote(coroutineScope)
42+
)
43+
```
44+
45+
This driver supports local and remote listeners.
46+
Local listeners only notify this client, ideally for testing or using the database with only one client at a time with
47+
SQLDelight only.
48+
Remote listener support uses `NOTIFY` and `LISTEN`, so you can use this with multiple clients or with existing database
49+
triggers.
50+
SQLDelight uses and expects the table name as payload, but you can provide a mapper function.
51+
3152
## License
3253

3354
Apache 2
3455

35-
This library uses some socket native code from [ktor](https://github.com/ktorio/ktor), licensed under Apache 2.
56+
This library uses some socket native code from [ktor](https://github.com/ktorio/ktor), licensed under Apache 2.
3657

3758
## Contributing
3859

3960
You need libpq installed: https://formulae.brew.sh/formula/libpq#default
61+
4062
You have to add the compiler flags to your path too.
4163
The exact commands depend on your config, but you will get them during installing libpq with homebrew.
4264

postgres-native-sqldelight-driver/src/commonMain/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriver.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public class PostgresNativeDriver(
3535

3636
private val listeners = mutableMapOf<Query.Listener, Job>()
3737

38-
private fun CoroutineScope.listen(queryKeysEscaped: List<String>, action: suspend (String) -> Unit) =
38+
private fun CoroutineScope.listen(queryKeys: List<String>, action: suspend (String) -> Unit) =
3939
launch {
4040
notifications.filter {
41-
it in queryKeysEscaped
41+
it in queryKeys
4242
}.collect {
4343
action(it)
4444
}
@@ -367,7 +367,12 @@ private fun CPointer<PGconn>.escaped(value: String): String {
367367
}
368368

369369
public fun PostgresNativeDriver(
370-
host: String, database: String, user: String, password: String, port: Int = 5432, options: String? = null,
370+
host: String,
371+
database: String,
372+
user: String,
373+
password: String,
374+
port: Int = 5432,
375+
options: String? = null,
371376
listenerSupport: ListenerSupport = ListenerSupport.None
372377
): PostgresNativeDriver {
373378
val conn = PQsetdbLogin(

0 commit comments

Comments
 (0)