Saving data from a web page
There are many ways to save data from a web page, depending on your use-case, technologies, and other constraints. First, you’ll want to pick a storage mechanism. Val Town has two built-in:
Blob storage is a very simple key-value store for storing files, JSON, images, large blobs of data, while SQLite is a SQL database with ACID transactions, indexes, and more. You can also use external services like Firebase, Supabase, Neon, Upstash, etc.
Once you’ve picked a storage mechanism, you’ll need to decide how to interact with it. You can either use form submissions or client-side JavaScript to make API calls to Val Town.
Blob storage
For example, this val is an app that displays comments, accepts new comments, and allows you to delete comments.
There are three parts relating to blob storage:
GET /
- Getting the comments from blob storage to render them on the pagePOST /comment
- Handling the form submission to add a new comment to blob storagePOST /comment/:id/delete
– Handling the delete button to remove a comment from blob storage
The way this val is rendered (Hono JSX) and the the HTML page talks to the server (form submissions) is arbitrary and can be replaced with any other front-end framework or server-side rendering. For example, you could have a NextJS or Remix app that makes a call to Val Town to get the data. Or you a client side VueJS app that makes a call to Val Town to get the data from the browser.
This is a fork of the above app that uses client-side React to make the API calls:
You could easily host the React part anywhere (Vercel, Netlify, etc) and have it make calls to Val Town (via your val’s HTTP URL) to get and modify the data.
You can learn more about blob storage here: Val Town Blob.
SQLite example
The above demo is simple because it stores the data in the same simple JSON format that you want on the frontend. However, there are many limitations to blob storage. The most egregious is that it’s not concurrency safe. If two people try to add a comment at the same time, one of the comments will be lost. For that – and many other reasons, such as indexing, querying, etc – you might want to use SQLite.
This is the same Hono demo but using SQLite on the backend. We will leave it as an exercise to an ambitious and helpful reader to convert the React demo to use SQLite and submit a PR to this docs page.
Note: we left the CREATE TABLE
statement in the code, so that this val will immediately work if you fork it. However, you may want to comment out that line after the first run to speed up this val.
You can learn more about SQLite here: Val Town SQLite.