ImmortalDB is the best way to store persistent key-value data in the browser. Data saved to ImmortalDB is redundantly stored in Cookies, IndexedDB, LocalStorage, and SessionStorage, and relentlessly self heals if any data therein is deleted or corrupted.
For example, clearing cookies is a common user action, even for relatively non-technical users. And browsers will unceremoniously delete IndexedDB, LocalStorage, and/or SessionStorage without warning under storage pressure.
ImmortalDB is resilient in the face of such events.
In this way, ImmortalDB is like Evercookie, but
-
Is actively maintained and well documented.
-
Provides a simple, modern, Promise-based API.
-
Strikes an equitable balance between reliability and respect for the user. Data is stored reliably but can also be voluntarily purged if the user designedly clears cookies and application storage.
-
Doesn't use nefarious exploits nor deprecated third party plugins like Flash, Silverlight, or Java. Only standard, ratified HTML5 APIs are used.
-
Doesn't vandalize performance or the user experience. For example, Evercookie's CSS History Knocking can beget a deluge of background HTTP requests, and loading Silverlight or Flash can raise unsought permission modals or thrash the user's disk.
When you store a key-value pair in ImmortalDB, that key and value are saved redundantly in the browser's cookies, IndexedDB, LocalStorage, and SessionStorage data stores.
When a value is retrieved via its key, ImmortalDB
- Looks up that key in every data store.
- Counts each unique returned value.
- Determines the most commonly returned unique value as the 'correct' value.
- Returns this correct value.
Then ImmortalDB self-heals: if any data store(s) returned a value different than the determined correct value, or no value at all, the correct value is rewritten to that store. In this way, consensus, reliability, and redundancy is maintained.
ImmortalDB's API is simple. To store a value, use set(key, value)
:
import { ImmortalDB } from 'ImmortalDB'
await ImmortalDB.set('key', 'value')
key
and value
must be
DOMStrings.
ImmortalDB.set(key, value)
also always returns value
, so it can be chained or
embedded, like
const countPlusOne = (await ImmortalDB.set('count', countNumberOfClowns())) + 1
To retrieve a value, use get(key, default=null)
:
const value = await ImmortalDB.get('key', default=null)
get()
returns the value associated with key
, if key
exists. If key
doesn't exist, default
is returned. key
must be a
DOMString.
Finally, to remove a key, use remove(key)
:
ImmortalDB.set('hi', 'bonjour')
console.log(await ImmortalDB.get('hi')) // Prints 'bonjour'.
await ImmortalDB.remove('hi')
console.log(await ImmortalDB.get('hi')) // Prints 'null'.
key
must be a
DOMString.
The data stores that ImmortalDB stores data in can also be configured. For example, to only store data reliably in cookies and LocalStorage:
import { ImmortalStorage, CookieStore, LocalStorageStore } from 'ImmortalDB'
const stores = [new CookieStore(), new LocalStorageStore()]
const db = ImmortalStorage(stores)
await db.set('key', JSON.Stringify({1:1}))
Available stores are
CookieStore
-> Keys and values are stored indocument.cookie
.IndexedDbStore
-> Keys and values are stored inwindow.indexedDB
.LocalStorageStore
-> Keys and values are stored inwindow.localStorage
.SessionStorageStore
-> Keys and values are stored inwindow.sessionStorage
.
New storage implementations can easily be added, too; they need only implement
the methods get(key, default)
, set(key, value)
, and remove(key)
.
Installing ImmortalDB with npm is easy.
$ npm install ImmortalDB
Or include dist/immortal-db[.min].js
and use window.ImmortalDB
directly.
<html>
<head>
<script src="immortal-db.min.js"></script>
<script>
(async () => {
const db = ImmortalDB.ImmortalDB;
await db.set('hi', 'lolsup');
})();
</script>
</head>
...
</html>