After wrestling with Dexie and JS promises for a few days, I decided to bag IndexedDB/Dexie.
They will not be how I am going to implement persistence in XVG.
The reasons are as follows:
- IndexedDB baroquely complex asynchronous I/O scheme
- Dexie’s non-intuitive API, with elaborate versions of simple DB calls such as Select ALL
- Dexie’s tediously nested implementation of standard db concepts, such as rows
- IndexedDB overly elaborate support of concepts such as composite keys are ill-suited to cleanly support the task at hand — namely, saving arrays that contain positional coordinate info and other graphics related data.
I was, finally, able to get it to work: as you can see here.
But, frankly, the result was not worth the hassle. It requires too much custom programming, in order to implement functionality that the relational model and SQL (a declarative language) offer natively.
(IndexedDB basically is a key-value pair DB. If you want to get an idea of what a sluggish, contorted nightmare that can become, check out WP’s very own DB schema!).
So I turned to HTML5 Web SQL — a technology that was trashed and abandoned by FF and the W3C ten years ago for stupid political reasons– but is still supported by Chrome, Edge, and Opera. Too tied to SQLite its detractors said. No webby enough, they moaned. No demand by the Web dev community for a built-in relational db in the browser.
Well, Web SQL has not gone away, and still works just fine.
I was able in an hour to put together a toy script (see the example code below) that works perfectly
With a relational db, I can mostly avoid losing my mind dealing with JS’s horrible idea of what an array is. (Here is another article worth reading that proves what a dumpster fire JS is when it comes to mathematical operations)
I do realize some of the potential security issues that are at play with this approach. I am going to do some things to address that down the road, when I am no longer the only user of XVG.
Also, it is possible that Chrome et al might drop Web SQL one day.
In recognition of that fact, I might — down the road — migrate XVG to Typescript and Deno, once the latter offers a solid plugin extension that connects to SQLite and works on Windows.
Below are some additional screenshots that show what can be accomplished with Web SQL with only a few lines of declarative code.