Bluestep JS Documentation
    Preparing search index...

    Class UserData

    Store and retrieve unstructured data in a user's account or the current session.

    Historical Context: Used to maintain state between pages, e.g. style preferences or date searching.

    There are many methods to store data relevant to a user. localStorage, sessionStorage, cookies, IndexedDB, cacheStorage, and more. bluestep.js offers its own 'session storage' in the form of UserData. UserData is insular to the user/session, i.e. data will not leak in nor out, and can only be accessed via code.

    Like many forms of storage (localStorage, sessionStorage, cacheStorage) and NoSQL databases, UserData is structed in key/value pairs. The underlying BlueStep database, on the other hand, is a structured database with predefined fields (the only browser analogue is IndexedDB). Values must be strings or an array of strings. On creation, a key/value pair is given an expiration timestamp (like a cookie). Special functions are provided to allow each list of strings to be used as a stack, queue or standard list.

    Akin to localStorage except saved to database; persists after session. This is not accessible to super users.

    Akin to sessionStorage except saved to server's RAM; dumped after session.

    Index

    Constructors

    Methods

    • Adds a value or array of values to the list stored for the given key.

      Parameters

      • key: string
      • value: string | string[]
      • Optionalexpire: number | Instant

        Determines when the list of values stored under the given name will expire. Defaults to one week from the current [[Instant]]. If passing a number, must be in millisSinceEpoch.

      Returns void

      userData.add('names', 'Oswald', B.time.Instant.now().plus(1, 'HOURS'));
      userData.add('names', ['Sebastian', 'Torben', 'Rory']);
      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben', 'Rory']
      explore example
      [[Time]], [[Java.Time.InstantStatic.now]], [[Java.Time.Instant.plus]]

      Using the JavaScript Date constructor

      userData.add('names', 'Vincent', new Date().getTime() + 60*60*1000);
      
    • Removes all values stored for the given name. Returns true if this caused a change to the values stored.

      Parameters

      • key: string

      Returns boolean

      console.log(`${key} was ${userData.clear(key) ? '' : 'un'}successfully cleared from UserData`);
      
    • Sets the expiration of the value(s) connected to key to the time when the user next logs out or is automatically logged out by session timeout.

      Parameters

      • key: string

      Returns boolean

      const willExpire = userData.expireOnLogout('myKey');
      
    • Gets the first value stored for the given name

      Parameters

      • key: string

      Returns string

      const firstListVal = userData.getFirst('names'); // 'Oswald'
      
    • Gets the list of values stored for a key. Returns an empty list if the key doesn't exist or contains an empty list.

      Parameters

      • key: string

      Returns EList<string>

      const listVal = userData.get('names'); // ['Oswald', Sebastian', 'Torben', 'Rory']
      
    • Returns true if there are no values stored for the given name

      Parameters

      • key: string

      Returns boolean

      let keyIsEmpty = userData.clear('names');
      
    • Gets the last value stored for the given name.

      Parameters

      • key: string

      Returns string

      const lastListVal = userData.getLast('names'); // 'Rory'
      
    • Gets the last value stored for the given name and removes it from the list.

      Parameters

      • key: string

      Returns string

      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben', 'Rory']
      userData.pop('names');
      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben']
    • Removes the value at the given index from the list of values stored for the name given. Returns the value removed.

      Parameters

      • key: string
      • index: number

      Returns string

      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben', 'Rory']
      userData.removeByIndex('names', 1);
      console.log(userData.get('names')); // ['Oswald', 'Torben', 'Rory']
    • Removes the value given from the list of values stored for the name given. Returns true if the value was found and removed successfully.

      Parameters

      • key: string
      • value: string

      Returns boolean

      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben', 'Rory']
      const didRemove = userData.removeByValue('myKey', 'Torben');
      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Rory']
    • Clears any currently stored values and replaces them with a new list of values. The string values will be added to the list in the order defined by the index values, but the index values themselves will be discarded.

      Parameters

      • key: string
      • value: string | string[]
      • Optionalexpire: number | Instant

        Determines when the list of values stored under the given name will expire. Defaults to one week from the current [[Instant]]. If passing a number, must be in millisSinceEpoch.

      Returns void

      console.log(userData.get('dates')); // ['2005-05-05','2015-05-05','2025-05-05']
      userData.set('dates', ['2000-01-01','2010-01-01','2020-01-01']);
      console.log(userData.get('dates')); // ['2000-01-01','2010-01-01','2020-01-01']
    • Gets the first value stored for the given name and removes it from the list.

      Parameters

      • key: string

      Returns string

      console.log(userData.get('names')); // ['Oswald', 'Sebastian', 'Torben', 'Rory']
      userData.shift('names');
      console.log(userData.get('names')); // ['Sebastian', 'Torben', 'Rory']
    • Returns a JSON of all of the active userData keys and their cooresponding values.

      Returns void