Stream: beginners

Topic: How to handle state?


view this post on Zulip Hannes Nevalainen (Dec 10 2023 at 01:49):

How do you handle state in Roc? For instance with the basic-web server platform, let's say that I have an in memory counter that I wanna increase on every request? Is there a way to do that from Roc?

view this post on Zulip Richard Feldman (Dec 10 2023 at 03:47):

basic-webserver doesn't currently support that feature, but in general it's possible for a platform to offer that!

view this post on Zulip Luke Boswell (Dec 10 2023 at 03:58):

I am using sqlite3 for that. Works great with basic-webserver

view this post on Zulip Luke Boswell (Dec 10 2023 at 03:58):

The todos app shows a basic use case. But yeah definitely not as nice as having something all in memory

view this post on Zulip Luke Boswell (Dec 10 2023 at 04:02):

@Richard Feldman is there an interim design I could implement which works like Stored ability and keeps things in memory for webserver? I would love to have a cache or something similar.

view this post on Zulip Richard Feldman (Dec 10 2023 at 04:10):

it wouldn't quite be like Stored, but you could use the equivalent of Model here to let the application specify a particular type that the platform knows about

view this post on Zulip Richard Feldman (Dec 10 2023 at 04:11):

and then the platform can have the Response include that as a type parameter

view this post on Zulip Richard Feldman (Dec 10 2023 at 04:11):

this wouldn't be nearly as ergonomic as Stored, which is one reason basic-webserver doesn't do it :big_smile:

view this post on Zulip Hannes Nevalainen (Dec 10 2023 at 11:20):

Would be sweet to have sqlite builtin to the platform instead of shelling out to a child process like that. Not sure that version is resistant to todos named ');DROP TABLE todos; -- either, but I can be missing something :)

view this post on Zulip Luke Boswell (Dec 10 2023 at 19:57):

Your not missing anything, that's a simple example to show something using webserver. I would love to add sqlite effects for that platform, but not sure if we should be coupled so closely to a single db? I think sqlite3 is probable an exception given it is so common. Other db's will probably interface using tcp/ip while sqlite3 is a library.

view this post on Zulip Brendan Hansknecht (Dec 10 2023 at 19:58):

Yeah, sqlite is one of the example pain points of a popular c library that has no good way to interact with roc through a generic interface like tcp

view this post on Zulip Brendan Hansknecht (Dec 10 2023 at 19:59):

Instead needs custom platform support

view this post on Zulip Brendan Hansknecht (Dec 10 2023 at 19:59):

Could maybe load it with libffi, but would be a pain and probably not worth doing


Last updated: Jul 06 2025 at 12:14 UTC