a trivial storage adapter for GunDB using localForage
localForage is a common interface for various browser (and Node.js) storage mechanisms - it wraps IndexedDB, WebSQL or localStorage out-of-the-box and may easily be extended to support additional storage solutions (including remote ones)
GunDB's built-in storage adapters based on RAD are completely broken (RAD will soon be replaced by something else). Until then, this storage adapter may be used instead.
In contrast to the author's in-memory storage and direct localStorage adapters, this one can be used to persist large volumes of shared data (up to the quota defined by a client's browser).
"direct" means that a node's complete id (its "soul") is used as a key for localForage to store that node's contents. While this may work well for short souls, it will consume a lot of memory as soon as the node ids get longer...
Important: after two weeks of intensive work and no substantial outcome, I have decided to give up on GunDB - it is full of design flaws, bugs and - even worse - race conditions and the implementation looks like being hacked in a style used 40 years ago (when source code had to be compact and variable names short and objects to be returned by reference because of performance constraints)
I wish everbody working with and on GunDB good luck - but will no longer participate myself
Nevertheless, you may still use my contributions in any way you like - they are MIT licensed
Load localForage into your web page and copy the contents of file directLocalForageAdapter.js into a <script>
element and add it to the <head>
section of your HTML document right after the one for GunDB itself.
<script src="https://cdn.jsdelivr.net/npm/localforage/dist/localforage.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script>
... insert source code here
</script>
Then, instantiate a localForage instance as described in their docs and create your GunDB instance with the following options (among others, if need be):
const Store = localforage.createInstance({
name:'GunDB-Test', // name used for error messages
driver:localforage.INDEXEDDB, // or any other driver(s)
storeName:'GunDB-Test', // name of actual database
})
const Gun = GUN({
localStorage:false, directLocalForage:Store
})
i.e., pass your localForage instance as the value of a GunDB option called directLocalForage
From now on, work with GunDB as usual - your nodes will be persisted in the configured database.