Foundation.Database

A very simplified weak abstraction around the window.localStorage API. Databases values will persist between sessions.

Each instance can be given a prefix value which will be prefixed to each key used through the instance. For example, with a prefix of levelOne_ a set('score', value) call would actually store the value in localStorage as levelOne_score. From an instance with the same prefix, however, retrieval would be through get('score'). A prefix value of '' (an empty string) can be used to nullify this behavior.

All values will be stringifyied / parsed automatically when setting / getting.


Example

// Create instance with default prefix (foundation_)
var dbDefault = new Foundation.Database();

// Create instance with custom prefix (custom_)
var dbCustom = new Foundation.Database('custom_');

// Set a number
dbDefault.set('foo', 5);

// Get a number (already parsed)
console.log(dbDefault.get('foo') === 5); // true

// Check if a key exists in default
console.log(dbDefault.has('foo')); // true

// Check if a key exists in custom
console.log(dbCustom.has('foo')); // false


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/actors/_digest.ts'
  • '/foundation/core/actors/Database.ts'

Extends

This type extends:

equals check is based on the prefix property and active state.

Implements

This type does not implement any interfaces (except those in the base).


Constructors

()

  • Default constructor.
  • Sets the storage prefix to foundation_.

(prefix: string)

  • Explicitly set prefix.

Public Properties

has(key: string): boolean

  • Returns true if key is a valid entry in the current database (identified by prefix).

set(key: string, value: any): Database

  • Sets key in the current database (identified by prefix) to value. Value will be automatically stringified for API insertion.
  • Returns the current instance for chaining.

get(key: string): any

  • Get the value store at key the current database (identified by prefix). The value will be automatically parsed from API retrieval.
  • If key does not exist, null will be returned.

remove(key: string): Database

  • Removes the value of key from the current database (identified by prefix).
  • Returns the current instance for chaining.

clear(): Database

  • Removes all entries from the current database (identified by prefix).
  • Returns the current instance for chaining.