I donāt think thereās any server-side technology that specifically targets talking to a ReScript client (I could be wrong). PocketBase is pretty simple and easy to set up, though (single binary, serving single SQLite DB file). And it serves a JSON API derived from your database schema in a predictable way, so would be easy to just write down the expected types and āmagicā convert them from parsed JSON. E.g. I just created a collection posts
with two fields title
and content
, and the API response preview looks like:
{
"page": 1,
"perPage": 30,
"totalPages": 1,
"totalItems": 2,
"items": [
{
"id": "RECORD_ID",
"collectionId": "fzd019dixstilec",
"collectionName": "posts",
"created": "2022-01-01 01:00:00.123Z",
"updated": "2022-01-01 23:59:59.456Z",
"title": "test",
"content": "test"
},
{
"id": "RECORD_ID",
"collectionId": "fzd019dixstilec",
"collectionName": "posts",
"created": "2022-01-01 01:00:00.123Z",
"updated": "2022-01-01 23:59:59.456Z",
"title": "test",
"content": "test"
}
]
}
As you can imagine, itās easy to model the types in ReScript:
type page<'item> = {
page: int,
perPage: int,
totalPages: int,
totalItems: int,
items: array<'item>,
}
external decodePage: Js.Json.t => page<_> = "%identity"
type post = {
id: string,
created: string,
updated: string,
title: string,
content: string,
}
Now you can do something like:
// : unit => promise<page<_>> (inferred from usage)
let getPage = async () => {
let resp = await Fetch.fetch("/api/collections/posts/records")
let json = await Fetch.Response.json(resp)
decodePage(json)
}
let getFirstPostTitle = async () => {
let page = await getPage()
page.items[0].title
}