dev

DataStore is an easy-to-use and fault-proof Web Storage API abstraction interface meant for Fandom developers. It provides a friendly interface layer for the management, leasing, and interaction of both sessionStorage and localStorage stores.

Installation

Documentation

Constructor

window.dev.dataStore.DataStore(name: string, options: { type: "local" | "session" })
See #Instance methods.
Creates a new DataStore object. Attach this object to a constant, as you wouldn't be able to use it without referencing it. DataStore accepts two types of stores, local and session. Duplicate store names are disallowed to prevent confusion. DataStore instances encapsulate store logic, methods, properties and getters don't change their behaviour regardless of the store type, further reducing possible confusion.

Static properties

window.dev.dataStore.hasRan: boolean Readonly
Indicates if the script has ran before or not. Do not change this outside of normal operation.
window.dev.dataStore.disabled: boolean
Indicates if the script was manually disabled by another script. If you are trying to debug, develop another storage script, or have a script that's incompatible with DataStore, set this to true at the earliest possible moment, this might come in handy if DataStore is causing you trouble. It is unwise to override this if it's set to true in any way before you.
window.dev.dataStore.exitReason: number | null Readonly
The reason DataStore exited early. This property follows the following enum:
window.dev.dataStore.util Readonly
Holds utility methods. See #Static methods.
window.dev.dataStore.stores: { local: DataStore[] | [], session: DataStore[] | [] }
An object containing arrays of local and session DataStores in their own properties respectively.
window.dev.dataStore.storeTypes: { LOCAL: "local", SESSION: "session" }
An enum containing LOCAL and SESSION. Mostly used as a helper to prevent ambiguity.
get window.dev.dataStore.length(): number
Getter overriding length, providing the total sum of all registered DataStores in the current environment.

Static methods

window.dev.dataStore.util.msgFmt(): (...msg: string[]): string
A glorified concatenator that prefixes "[DataStore]" to anything that's in the input.
window.dev.dataStore.isStoreNameDuplicate(name: string): boolean
Checks if a given name already exists in both local and session storage. Returns a true indicating if it is indeed a duplicate, false otherwise.
window.dev.dataStore.index(): void
(Re-)Indexes and finds DataStore storage spaces in the browser storage. Useful if there are DataStore-compatible storage keys not made by DataStore, or when you're inserting them using the browser's developer tools manually.

Instance properties

DataStore.prototype.storeName: string
The name of the store.
DataStore.prototype.storeType: "local" | "session" Readonly
The type of the store.
DataStore.prototype.storePrefix: string Readonly
The prefix used by the store in browser storage.

Instance methods

DataStore.prototype.list(): [key: string]: string
Returns an array of keys corresponding to this store in the key-value format
DataStore.prototype.get(key: string): string | null
Fetches a key in the current store. Returns null when getting an empty/nonexistent key, inheriting Storage.getItem() behaviour.
DataStore.prototype.store(k: { key: string, value: string | object } | [string, string | object] | string, v?: string | object): void
Stores items in the store. Incredibly versatile. Can accept:
  1. (k: string, v: string | object)
  2. (k: [string, string | object])
  3. ({key: string, value: string | object})
More overloads are planned.
DataStore.prototype.remove(key: string): void
Removes a key from the store.
DataStore.prototype.clear(): void
Clears (remove) all of the keys in the store, but not deleting the store itself, making it usable again. To remove an entire store, use DataStore.prototype.delete().
DataStore.prototype.delete(): void
Removes the store completely from DataStore, along with deleting its storage keys. To remove just the keys and not the store, use DataStore.prototype.clear().

Examples

The examples in this section compound from one another, so the next example likely requires the previous example's code to have been ran/executed.

Alias and instantiate local DataStore

This example shows how to alias DataStore to a more convenient name, then instantiating a new local DataStore instance with the name "scriptName".

var DataStore = window.dev.dataStore;
var lStore = new DataStore.DataStore( "scriptName", { type: "local" } );

Get the global store list

This example shows how to reference then log the global store list from DataStore.stores. You can see the full structure of a DataStore instance here.

var allStores = DataStore.stores;
console.log( allStores );
/** output:
 *  {
 *    "local": [
 *      {
 *        "storeName": "scriptName",
 *        "storeType": "local",
 *        "storePrefix": "dev-scriptName",
 *        "_fullStorePrefix": "dev-scriptName-"
 *      }
 *    ],
 *    "session": []
 *  }
 */

Storing, getting, and deleting keys

This example shows how to store multiple key entries in the store "scriptName" with different implementations, then removing one of them, then logs all of them.

lStore.store("isCool", "true");

lStore.store([
  "inFandom",
  "true"
]);

lStore.store({
  key: "randomString",
  value: "hello world"
});

lStore.remove( "isCool" );

console.log( lstore.get("isCool") )
// output: null

console.log( lstore.get("inFandom") )
// output: true

console.log( lstore.get("randomString") )
// output: hello world

Deleting a store instance

This example shows how to delete a store instance, clearing all the associated keys in it, then removing itself from the global store list.

lStore.delete();

console.log( allStores );
// output: { local: [], session: [] }
Text above can be found here (edit)