SurrealDB, the perfect backend for Elm apps?

Anyone tried SurrealDB? I just tried it and find a lot of things reminding me of elm.
New and radical ideas. Simple and super performant with a small footprint.
It is made by two dedicated brilliant brothers, not a company.
The main goal is to be able to develop applications with no API or backends, you just need the database and a frontend.

It is like Firebase, but with a better database, better access control and can run on an embedded device or scale up to large clusters. I think it is just a few MB self contained thing. Written in Rust
And can also run inside the browser itself if you want offline support (wasm)
It takes the best from SQL, Document and graph databases. Has native json support so you can easily work with arrays and structured objects. You can choose to use schema or not. Or only schema for some tables. As it is a database made for webapps its primary interface is HTTP and websockets, both a perfect fit for elm. The database can even hold javascript functions to have calculated fields and rules. (maybe we someday could compile those from elm functions?) :slight_smile:

They say they have been using it in production themself for several years, but it is released to the public only the last couple of months.
It’s geting raving reviews by anyone that has looked at it.

Here is the Fireship “SurrealDB in 100 seconds”:

The website also looks amazing:

Just wanted to share this with others looking for a nice elm backend :slight_smile:


Thanks for pointing this out. I’ve not looked at it, but certainly intend to.

I’m curious.

This comment about nested documents has me hesitating a little -

@Atlewee From your description I first though it was yet another Firebase alternative, but SurrealDB, as it name says, is simply a database without authentication or hosting or serverless functions. So it could merely replace Firebase Firestore or Firebase Realtime DB, except that it is a SQL database. And without Firebase offline capabilities.

Yes you are correct, I was thinking of the Firebase realtime database. Not the whole firebase solution :slight_smile:
But it also seems to have built in user account management / signup and stuff + functions that can run on triggers inside the database. So sort of cloud-functions capabilities as well.
And you can write your own js/wasm traversing functions so it happens inside the database instead of sending data to the client before the next step in treaversal.

I would not call surreal an SQL database… It seems to be mostly a json document database but with additional graph and SQL features + schema possibilities.

As for offline, they are definetly aiming for offline first webapps. That’s the whole reason why SurrealDB is designed to run embedded in the browser, as well as in a server environment.

I’m currently checking it out but the documentation has a lot of Lorem Ipsums… @Atlewee Did you find good docs to get you started on auth and Websockets connections? Or a gist of using it with Elm? Thanks!

Yes docs is not complete.

Websocket looks simple. Connect unauthenticated and run the signin command passing username,password or a Token.
All websocket commands is a json object like this:

  "id": <an id so you can identify responses later on>,
  "method": <one of the available commands>,
  "params": <an array of parameters>

So the websocket message to select a namespace and database would look like this in elm:

  [ ( "id", E.string "1" )
  , ( "method", E.string "use" )
  , ( "params", E.list identity [ E.string "testNamespace", E.string "testDatbase" ] )                                    

As the websocket docs is not created yet I looked at the source of the javascript client to see how it uses the websocket:

The source on the server will also tell you all the available commands:

Best description I found about auth (external JWT and how access-control works) was this answer:

Once you get that running the rest of the documentation you need seems nice and complete:

Wow thanks! This is very thorough. I’ll use it for my next project :slight_smile:

Am I missing something here, or are there no features related to migrating/changing your schema?

I can’t seem to find any mention of schema migrations in the docs nor github :thinking:

Seeing your name, I now see that my title is incorrect. Should be something like. “If for any reason you cant use lamdera yet, would SurrealDB be the next best thing for elm-apps?” :slight_smile:
I guess nothing beats the lamdera experience when it fits your requirements.

Great question, I have not looked into migrations yet. ( I just discovered surrealDB 6days ago )
Found a discussion on discord about migrations:

Where the answer from one of the makers is this:

So no tools for migrations yet. But I guess one could turn of schema for the effected tables, change data and turn on new schema? But not sure if that is possible in a single transaction, have not tried.
Or map the complete db or tables over to a new db/table with transform functions, immutable style.

Talking about immutability:
I forgot to mention one of the biggest difference to other databases: SurrealDB is immutable. It is meant to tell you how a single field in a table has changed over time in a performant way and with compact storage of changes.
For those interested I found this really interesting whitepaper written by one of the authors comparing performant immutable database structures: (it is more or less describing design decisions and how the previous version worked under the hood, it had a different name then and was written in go. They have later found even more performant structures and changed to rust + renamed to surrealDB after that)

Very interesting, thanks for sharing!

I’m blown away how convincingly it’s presented as a production ready system with no data migration concepts :sweat_smile: at first I thought maybe you were expected to make new tables but their foreign key system is effectively hardcoded to a table name if I’m understanding correctly.

That’s my understanding as well. Especially knowing how well you handle Evergreen migrations in Lamdera, it’s a bit harrowing to build a production app with no migration tooling at all…

In their defence, they do use SurrealDB in their production apps, and they are technically in beta. This feature request seems to have been picked up, though: Feature: `RENAME` statement for fields, databases and events · Issue #1615 · surrealdb/surrealdb · GitHub

I’ve been using it for a few days now and I’m thoroughly enjoying the parts that just work. I think I’ll stick with it for this project, have a few makeshift scripts to test database health, and hope they come up with the rename field before I need it :stuck_out_tongue:

1 Like

Cool, interested to hear your experience report once you feel you’ve got a handle on it.

Also if you come across documentation on the supported types (I.e byte sizes, lengths, encodings, limitations, etc) that would be cool to see.

1 Like

Sounds good! In the meantime, I found this project with a lot of cool patterns. I’m learning a lot from them!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.