Packages
blessing_surreal
0.1.0
A SurrealDB client for Gleam, over the WebSocket RPC protocol. Not affiliated with SurrealDB Ltd.
Current section
Files
Jump to
Current section
Files
blessing_surreal
README.md
README.md
# blessing_surreal
A [SurrealDB](https://surrealdb.com) client for Gleam. It talks to the SurrealDB RPC protocol over WebSocket.
This package is not from SurrealDB Ltd, and they do not support it. "SurrealDB" is their name for their database.
Status: early (`0.1.0`). It works with SurrealDB v4.0.1. The JSON format is the only format now.
<a id="surreal-install"></a>
## Install
```sh
gleam add blessing_surreal
```
<a id="surreal-use"></a>
## Use
```gleam
import blessing_surreal as surreal
import gleam/dynamic/decode
import gleam/json
pub fn main() {
let assert Ok(db) = surreal.connect("ws://localhost:8000/rpc", 5000)
let assert Ok(_) = surreal.signin(db, surreal.Root("root", "root"))
let assert Ok(_) = surreal.select_database(db, "main", "main")
let assert Ok(amounts) =
surreal.query_one(
db,
"SELECT VALUE amount FROM txn WHERE category = $c",
[#("c", json.string("food"))],
decode.list(decode.int),
)
}
```
<a id="surreal-live-queries"></a>
### Live queries
`live` sends each change to a Gleam `Subject`. One process can forward the changes to a browser.
```gleam
let changes = process.new_subject()
let assert Ok(live_id) = surreal.live(db, "LIVE SELECT * FROM txn", [], changes)
case process.receive(changes, 5000) {
Ok(surreal.Change(action:, record:, result:, ..)) -> todo as "use the change"
Ok(surreal.Resumed(..)) -> todo as "read the data again"
Error(Nil) -> todo as "no change in 5 seconds"
}
let assert Ok(_) = surreal.kill(db, live_id)
```
SurrealDB v4 refuses `LIVE SELECT` on a table that does not exist. Define the table first.
<a id="surreal-reconnect"></a>
### Reconnect
If the WebSocket closes, the connection opens it again. It waits 100 ms, then twice as long each time, up to 5 seconds. Then it signs in again, selects the database again, and starts each live query again.
- A live query keeps the id that `live` returned. Its subscriber gets `Resumed(live_id)`. Changes while the WebSocket was closed are lost, so read the data again.
- While the WebSocket is closed, a call fails at once with `Closed`. It does not wait for the timeout.
- A call that was waiting when the WebSocket closed gets `Closed`. It is not sent again, because it can have changed data already.
- After a reconnect, `authenticate` uses the same token again. If the token has expired, the next calls fail. Call `authenticate` with a new token.
<a id="surreal-access-for-each-user"></a>
### Access for each user
Your server can sign a JWT for a user with the key of a `DEFINE ACCESS ... TYPE RECORD WITH JWT`. Call `authenticate` with it. Then SurrealDB applies the table permissions, for example `WHERE owner = $auth.id`, to that connection.
<a id="surreal-limits-of-the-json-format"></a>
## Limits of the JSON format
- A JSON string never becomes a record id. When a variable holds one, write `type::record($id)` in the query.
- Decimals come back as text, for example `"-25000"`. Decode them with `decode.string`.
- A write that a permission blocks returns an empty result, not an error. Treat an empty result as "not allowed".
A later version adds CBOR, which keeps these types.
<a id="surreal-tests"></a>
## Tests
The tests need SurrealDB on port 8200. You need mise and Docker. The reconnect tests stop and restart the container with `docker compose`, so run them from the project root.
```sh
mise install
docker compose up -d
gleam test
```
<a id="surreal-credits"></a>
## Credits
- [SurrealDB](https://surrealdb.com) by SurrealDB Ltd: the database and its RPC protocol.
- [gramps](https://github.com/rawhat/gramps) by Alex Manning: WebSocket frame and HTTP response codecs.
- [gleam_json](https://github.com/gleam-lang/json), [gleam_http](https://github.com/gleam-lang/http), [gleam_erlang](https://github.com/gleam-lang/erlang), [gleam_otp](https://github.com/gleam-lang/otp), and [gleam_time](https://github.com/gleam-lang/time): JSON, requests, processes, and time.
- [JSON Web Token (RFC 7519)](https://www.rfc-editor.org/rfc/rfc7519): the token format that the tests sign.
<a id="surreal-license"></a>
## License
MIT. See `LICENSE`.