arrow-left

Only this pageAll pages
gitbookPowered by GitBook
1 of 11

3.x

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Using Enmap.multi

To account for people that might use a large number of enmaps in the same project, I've created a new `multi()` method that can be used to instanciate multiple peristent enmaps together.

The method takes 3 arguments:

  • An array of names for the enmaps to be created.

  • A Provider (not instanciated), from any of the available ones.

  • An options object containing any of the options needed to instanciate the provider. Do not add name to this, as it will use the names in the array instead.

Below, an example that uses destructuring:

const Enmap = require('enmap');
const Provider = require('enmap-mongo');
const { settings, tags, blacklist, langs } = 
    Enmap.multi(['settings', 'tags', 'blacklist', 'langs'],
    Provider, { url: "mongodb://localhost:27017/enmap" });

API Documentation

hashtag
Enmap ⇐ Map

A enhanced Map structure with additional utility methods. Can be made persistent

Kind: global class Extends: Map

  • ⇐ Map

    • instance

      • ⇒ Promise.

hashtag
enmap.fetchEverything() ⇒ Promise.

Fetches every key from the persistent enmap and loads them into the current enmap value.

Kind: instance method of Returns: Promise. - The enmap containing all values, as a promise..

hashtag
enmap.fetch(keyOrKeys) ⇒ Promise.

Force fetch one or more key values from the enmap. If the database has changed, that new value is used.

Kind: instance method of Returns: Promise. - A single value if requested, or a non-persistent enmap of keys if an array is requested.

hashtag
enmap.evict(keyOrArrayOfKeys)

Removes a key from the cache - useful when using the fetchAll feature.

Kind: instance method of

hashtag
enmap.autonum() ⇒ number

Generates an automatic numerical key for inserting a new value.

Kind: instance method of Returns: number - The generated key number. Example

hashtag
enmap.changed(cb)

Function called whenever data changes within Enmap after the initial load. Can be used to detect if another part of your code changed a value in enmap and react on it.

Kind: instance method of

Example

hashtag
enmap.set(key, val, path) ⇒ Map

Set the value in Enmap.

Kind: instance method of Returns: Map - The Enmap.

Example

hashtag
enmap.setProp(key, path, val) ⇒ Map

Modify the property of a value inside the enmap, if the value is an object or array. This is a shortcut to loading the key, changing the value, and setting it back.

Kind: instance method of Returns: Map - The EnMap.

hashtag
enmap.push(key, val, path, allowDupes) ⇒ Map

Push to an array value in Enmap.

Kind: instance method of Returns: Map - The EnMap.

Example

hashtag
enmap.pushIn(key, path, val, allowDupes) ⇒ Map

Push to an array element inside an Object or Array element in Enmap.

Kind: instance method of Returns: Map - The EnMap.

hashtag
enmap.math(key, operation, operand, path) ⇒ Map

Executes a mathematical operation on a value and saves it in the enmap.

Kind: instance method of Returns: Map - The EnMap.

Example

hashtag
enmap.inc(key, path) ⇒ Map

Increments a key's value or property by 1. Value must be a number, or a path to a number.

Kind: instance method of Returns: Map - The EnMap.

Example

hashtag
enmap.dec(key, path) ⇒ Map

Decrements a key's value or property by 1. Value must be a number, or a path to a number.

Kind: instance method of Returns: Map - The EnMap.

Example

hashtag
enmap.get(key, path) ⇒ *

Retrieves a key from the enmap. If fetchAll is false, returns a promise.

Kind: instance method of Returns: * - The value for this key.

Example

hashtag
enmap.getProp(key, path) ⇒ *

Returns the specific property within a stored value. If the key does not exist or the value is not an object, throws an error.

Kind: instance method of Returns: * - The value of the property obtained.

hashtag
enmap.has(key, path) ⇒ boolean

Returns whether or not the key exists in the Enmap.

Kind: instance method of

Example

hashtag
enmap.hasProp(key, path) ⇒ boolean

Returns whether or not the property exists within an object or array value in enmap.

Kind: instance method of Returns: boolean - Whether the property exists.

hashtag
enmap.delete(key, path)

Deletes a key in the Enmap.

Kind: instance method of

hashtag
enmap.deleteProp(key, path)

Delete a property from an object or array value in Enmap.

Kind: instance method of

hashtag
enmap.deleteAll(bulk)

Calls the delete() method on all items that have it.

Kind: instance method of

hashtag
enmap.remove(key, val, path) ⇒ Map

Remove a value in an Array or Object element in Enmap. Note that this only works for values, not keys. Complex values such as objects and arrays will not be removed this way.

Kind: instance method of Returns: Map - The EnMap.

hashtag
enmap.removeFrom(key, path, val) ⇒ Map

Remove a value from an Array or Object property inside an Array or Object element in Enmap. Confusing? Sure is.

Kind: instance method of Returns: Map - The EnMap.

hashtag
enmap.array() ⇒ Array

Creates an ordered array of the values of this Enmap. The array will only be reconstructed if an item is added to or removed from the Enmap, or if you change the length of the array itself. If you don't want this caching behaviour, use Array.from(enmap.values()) instead.

Kind: instance method of

hashtag
enmap.keyArray() ⇒ Array

Creates an ordered array of the keys of this Enmap The array will only be reconstructed if an item is added to or removed from the Enmap, or if you change the length of the array itself. If you don't want this caching behaviour, use Array.from(enmap.keys()) instead.

Kind: instance method of

hashtag
enmap.random([count]) ⇒ * | Array.

Obtains random value(s) from this Enmap. This relies on .

Kind: instance method of Returns: * | Array. - The single value if count is undefined, or an array of values of count length

hashtag
enmap.randomKey([count]) ⇒ * | Array.

Obtains random key(s) from this Enmap. This relies on

Kind: instance method of Returns: * | Array. - The single key if count is undefined, or an array of keys of count length

hashtag
enmap.findAll(prop, value) ⇒ Array

Searches for all items where their specified property's value is identical to the given value (item[prop] === value).

Kind: instance method of

Example

hashtag
enmap.find(propOrFn, [value]) ⇒ *

Searches for a single item where its specified property's value is identical to the given value (item[prop] === value), or the given function returns a truthy value. In the latter case, this is identical to .

All Enmap used in Discord.js are mapped using their `id` property, and if you want to find by id you should use the `get` method. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.

Kind: instance method of

Example

Example

hashtag
enmap.exists(prop, value) ⇒ boolean

Searches for the existence of a single item where its specified property's value is identical to the given value (item[prop] === value).

Do not use this to check for an item by its ID. Instead, use `enmap.has(id)`. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) for details.

Kind: instance method of

Example

hashtag
enmap.filter(fn, [thisArg]) ⇒

Identical to , but returns a Enmap instead of an Array.

Kind: instance method of

hashtag
enmap.filterArray(fn, [thisArg]) ⇒ Array

Identical to .

Kind: instance method of

hashtag
enmap.map(fn, [thisArg]) ⇒ Array

Identical to .

Kind: instance method of

hashtag
enmap.some(fn, [thisArg]) ⇒ boolean

Identical to .

Kind: instance method of

hashtag
enmap.every(fn, [thisArg]) ⇒ boolean

Identical to .

Kind: instance method of

hashtag
enmap.reduce(fn, [initialValue]) ⇒ *

Identical to .

Kind: instance method of

hashtag
enmap.clone() ⇒

Creates an identical shallow copy of this Enmap.

Kind: instance method of Example

hashtag
enmap.concat(...enmaps) ⇒

Combines this Enmap with others into a new Enmap. None of the source Enmaps are modified.

Kind: instance method of

Example

hashtag
enmap.equals(enmap) ⇒ boolean

Checks if this Enmap shares identical key-value pairings with another. This is different to checking for equality using equal-signs, because the Enmaps may be different objects, but contain the same data.

Kind: instance method of Returns: boolean - Whether the Enmaps have identical contents

hashtag
Enmap.multi(names, Provider, options) ⇒ Array.

Initialize multiple Enmaps easily.

Kind: static method of Returns: Array. - An array of initialized Enmaps.

Example

hashtag
Enmap.migrate(source, target)

Migrates an Enmap from version 3 or lower to a Version 4 enmap, which is locked to sqlite backend only. Version 4 uses a different way of storing data, so is not directly compatible with version 3 data. Note that this migration also makes the data unuseable with version 3, so it should only be used to migrate once.

Kind: static method of

Example

.fetch(keyOrKeys) ⇒ Promise.

  • .evict(keyOrArrayOfKeys)

  • .autonum() ⇒ number

  • .changed(cb)

  • .set(key, val, path) ⇒ Map

  • .setProp(key, path, val) ⇒ Map

  • .push(key, val, path, allowDupes) ⇒ Map

  • .pushIn(key, path, val, allowDupes) ⇒ Map

  • .math(key, operation, operand, path) ⇒ Map

  • .inc(key, path) ⇒ Map

  • .dec(key, path) ⇒ Map

  • .get(key, path) ⇒ *

  • .getProp(key, path) ⇒ *

  • .has(key, path) ⇒ boolean

  • .hasProp(key, path) ⇒ boolean

  • .delete(key, path)

  • .deleteProp(key, path)

  • .deleteAll(bulk)

  • .remove(key, val, path) ⇒ Map

  • .removeFrom(key, path, val) ⇒ Map

  • .array() ⇒ Array

  • .keyArray() ⇒ Array

  • .random([count]) ⇒ * | Array.

  • .randomKey([count]) ⇒ * | Array.

  • .findAll(prop, value) ⇒ Array

  • .find(propOrFn, [value]) ⇒ *

  • .exists(prop, value) ⇒ boolean

  • .filter(fn, [thisArg]) ⇒ Enmap

  • .filterArray(fn, [thisArg]) ⇒ Array

  • .map(fn, [thisArg]) ⇒ Array

  • .some(fn, [thisArg]) ⇒ boolean

  • .every(fn, [thisArg]) ⇒ boolean

  • .reduce(fn, [initialValue]) ⇒ *

  • .clone() ⇒ Enmap

  • .concat(...enmaps) ⇒ Enmap

  • .equals(enmap) ⇒ boolean

  • static

    • .multi(names, Provider, options) ⇒ Array.

    • .migrate(source, target)

  • null

    Optional. The path to the property to modify inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    null

    Optional. The path to the property to modify inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    allowDupes

    boolean

    false

    Optional. Allow duplicate values in the array (default: false).

    Required. The value push to the array property.

    allowDupes

    boolean

    false

    Allow duplicate values in the array (default: false).

    The right operand of the operation.

    path

    string

    null

    Optional. The property path to execute the operation on, if the value is an object or array.

    null

    Optional. The name of the array property to remove from. Can be a path with dot notation, such as "prop1.subprop2.subprop3". If not presents, removes directly from the value.

    Param

    Type

    Description

    keyOrKeys

    string | number

    A single key or array of keys to force fetch from the enmap database.

    Param

    Type

    Description

    keyOrArrayOfKeys

    *

    A single key or array of keys to remove from the cache.

    Param

    Type

    Description

    cb

    function

    A callback function that will be called whenever data changes in the enmap.

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the element to add to The Enmap. If the Enmap is persistent this value MUST be a string or number.

    val

    *

    Required. The value of the element to add to The Enmap. If the Enmap is persistent this value MUST be stringifiable as JSON.

    path

    Param

    Type

    Description

    key

    string | number

    Required. The key of the element to add to The Enmap or array. This value MUST be a string or number.

    path

    *

    Required. The property to modify inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    val

    *

    Required. The value to apply to the specified property.

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the array element to push to in Enmap. This value MUST be a string or number.

    val

    *

    Required. The value to push to the array.

    path

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the element. This value MUST be a string or number.

    path

    *

    Required. The name of the array property to push to. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    val

    Param

    Type

    Default

    Description

    key

    string | number

    The enmap key on which to execute the math operation.

    operation

    string

    Which mathematical operation to execute. Supports most math ops: =, -, *, /, %, ^, and english spelling of those operations.

    operand

    Param

    Type

    Default

    Description

    key

    string | number

    The enmap key where the value to increment is stored.

    path

    string

    null

    Optional. The property path to increment, if the value is an object or array.

    Param

    Type

    Default

    Description

    key

    string | number

    The enmap key where the value to decrement is stored.

    path

    string

    null

    Optional. The property path to decrement, if the value is an object or array.

    Param

    Type

    Default

    Description

    key

    string | number

    The key to retrieve from the enmap.

    path

    string

    null

    Optional. The property to retrieve from the object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Description

    key

    string | number

    Required. The key of the element to get from The Enmap.

    path

    *

    Required. The property to retrieve from the object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the element to add to The Enmap or array. This value MUST be a string or number.

    path

    string

    null

    Optional. The property to verify inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Description

    key

    string | number

    Required. The key of the element to check in the Enmap or array.

    path

    *

    Required. The property to verify inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the element to delete from The Enmap.

    path

    string

    null

    Optional. The name of the property to remove from the object. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Description

    key

    string | number

    Required. The key of the element to delete the property from in Enmap.

    path

    *

    Required. The name of the property to remove from the object. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    Param

    Type

    Default

    Description

    bulk

    boolean

    true

    Optional. Defaults to True. whether to use the provider's "bulk" delete feature if it has one.

    Param

    Type

    Default

    Description

    key

    string | number

    Required. The key of the element to remove from in Enmap. This value MUST be a string or number.

    val

    *

    Required. The value to remove from the array or object.

    path

    Param

    Type

    Description

    key

    string | number

    Required. The key of the element. This value MUST be a string or number.

    path

    *

    Required. The name of the array property to remove from. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

    val

    *

    Required. The value to remove from the array property.

    Param

    Type

    Description

    [count]

    number

    Number of values to obtain randomly

    Param

    Type

    Description

    [count]

    number

    Number of keys to obtain randomly

    Param

    Type

    Description

    prop

    string

    The property to test against

    value

    *

    The expected value

    Param

    Type

    Description

    propOrFn

    string | function

    The property to test against, or the function to test with

    [value]

    *

    The expected value - only applicable and required if using a property for the first argument

    Param

    Type

    Description

    prop

    string

    The property to test against

    value

    *

    The expected value

    Param

    Type

    Description

    fn

    function

    Function used to test (should return a boolean)

    [thisArg]

    Object

    Value to use as this when executing function

    Param

    Type

    Description

    fn

    function

    Function used to test (should return a boolean)

    [thisArg]

    Object

    Value to use as this when executing function

    Param

    Type

    Description

    fn

    function

    Function that produces an element of the new array, taking three arguments

    [thisArg]

    *

    Value to use as this when executing function

    Param

    Type

    Description

    fn

    function

    Function used to test (should return a boolean)

    [thisArg]

    Object

    Value to use as this when executing function

    Param

    Type

    Description

    fn

    function

    Function used to test (should return a boolean)

    [thisArg]

    Object

    Value to use as this when executing function

    Param

    Type

    Description

    fn

    function

    Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and enmap

    [initialValue]

    *

    Starting value for the accumulator

    Param

    Type

    Description

    ...enmaps

    Enmap

    Enmaps to merge

    Param

    Type

    Description

    enmap

    Enmap

    Enmap to compare with

    Param

    Type

    Description

    names

    Array.

    Array of strings. Each array entry will create a separate enmap with that name.

    Provider

    EnmapProvider

    Valid EnmapProvider object.

    options

    Object

    Options object to pass to the provider. See provider documentation for its options.

    Param

    Type

    Description

    source

    Provider

    A valid Enmap provider. Can be any existing provider.

    target

    Provider

    An SQLite Enmap Provider. Cannot work without enmap-sqlite as the target.

    Enmap
    .fetchEverything()
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    array
    Enmap
    keyArray
    Enmap
    Enmap
    Array.find()arrow-up-right
    Enmap
    Enmap
    Enmap
    Array.filter()arrow-up-right
    Enmap
    Array.filter()arrow-up-right
    Enmap
    Array.map()arrow-up-right
    Enmap
    Array.some()arrow-up-right
    Enmap
    Array.every()arrow-up-right
    Enmap
    Array.reduce()arrow-up-right
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap
    Enmap

    string

    string

    *

    number

    string

    enmap.set(enmap.autonum(), "This is a new value");
    enmap.changed((keyName, oldValue, newValue) => {
      console.log(`Value of ${key} has changed from: \n${oldValue}\nto\n${newValue});
    });
    // Direct Value Examples
    enmap.set('simplevalue', 'this is a string');
    enmap.set('isEnmapGreat', true);
    enmap.set('TheAnswer', 42);
    enmap.set('IhazObjects', { color: 'black', action: 'paint', desire: true });
    enmap.set('ArraysToo', [1, "two", "tree", "foor"])
    
    // Settings Properties
    enmap.set('IhazObjects', 'color', 'blue'); //modified previous object
    enmap.set('ArraysToo', 2, 'three'); // changes "tree" to "three" in array.
    // Assuming
    enmap.set("simpleArray", [1, 2, 3, 4]);
    enmap.set("arrayInObject", {sub: [1, 2, 3, 4]});
    
    enmap.push("simpleArray", 5); // adds 5 at the end of the array
    enmap.push("arrayInObject", "five", "sub"); adds "five" at the end of the sub array
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
    
    points.math("number", "/", 2); // 21
    points.math("number", "add", 5); // 26
    points.math("number", "modulo", 3); // 2
    points.math("numberInObject", "+", 10, "sub.anInt");
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
    
    points.inc("number"); // 43
    points.inc("numberInObject", "sub.anInt"); // {sub: { anInt: 6 }}
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
    
    points.dec("number"); // 41
    points.dec("numberInObject", "sub.anInt"); // {sub: { anInt: 4 }}
    const myKeyValue = enmap.get("myKey");
    console.log(myKeyValue);
    
    const someSubValue = enmap.get("anObjectKey", "someprop.someOtherSubProp");
    if(enmap.has("myKey")) {
      // key is there
    }
    
    if(!enmap.has("myOtherKey", "oneProp.otherProp.SubProp")) return false;
    enmap.findAll('username', 'Bob');
    enmap.find('username', 'Bob');
    enmap.find(val => val.username === 'Bob');
    if (enmap.exists('username', 'Bob')) {
     console.log('user here!');
    }
    const newColl = someColl.clone();
    const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
    // Using local variables and the mongodb provider.
    const Enmap = require('enmap');
    const Provider = require('enmap-mongo');
    const { settings, tags, blacklist } = Enmap.multi(['settings', 'tags', 'blacklist'], Provider, { url: "some connection URL here" });
    
    // Attaching to an existing object (for instance some API's client)
    const Enmap = require("enmap");
    const Provider = require("enmap-mongo");
    Object.assign(client, Enmap.multi(["settings", "tags", "blacklist"], Provider, { url: "some connection URL here" }));
    // This example migrates from enmap-mongo to the new format.
    // Assumes: npm install enmap@3.1.4 enmap-sqlite@latest enmap-mongo@latest
    const Enmap = require("enmap");
    const Provider = require("enmap-mongo");
    const SQLite = require("enmap-sqlite");
    
    let options = {
     name: 'test',
     dbName: 'enmap',
     url: 'mongodb://username:password@localhost:27017/enmap'
    };
    
    const source = new Provider(options);
    const target = new SQLite({"name": "points"});
    
    Enmap.migrate(source, target);

    Enmap Installation

    hashtag
    Pre-Requisites

    In order to install Enmap, you'll need a few things installed on your machine. First off, you need NodeJS (version 8 or higher required). For Windows and MacOS, simply download and install from the websitearrow-up-right. For Linux, see this page for installationarrow-up-right.

    Enmap v3 is no longer maintained on NPM and is only available on Github. You will need to install a git tool on your computer to install it.

    Next, enmap has a specific pre-requisite which is needed for the sqlite dependency. How to install these depends on your operating system, so see below for instructions:

    On Windows, two things are required to install enmap-sqlite. Python 2.7 and the Visual Studio C++ Build Tools. They are required for any module that is built on the system, which includes sqlite.

    The Windows Built Tools require over 3GB of space to install and use. Make sure you have enough space before starting this download and install!

    To install the necessary pre-requisites on Windows, the easiest is to simply run the following command, under an administrative command prompt or powershell:

    It's very important that this be run in the administrative prompt, and not a regular one.

    hashtag
    Installing enmap

    To install Enmap in your project, all you need to to is run the following command in your project folder:

    This may take a few minutes, then you're ready to use it.

    Once the windows-build-tools are installed (this might take quite some time, depending on your internet connection), close all open command prompts, powershell windows, and editors with a built-in console/prompt. Otherwise, the next command will not work.

    On Linux, the pre-requisites are much simpler in a way. A lot of modern systems (such as Ubuntu, since 16.04) already come with python 2.7 pre-installed. For some other systems, you might have to fiddle with it to either get python 2.7 installed, or to install both 2.7 and 3.x simultaneously. Google will be your friend.

    As for the C++ build tools, that's installed using the simple command: sudo apt-get install build-essential for most debian-based systems. For others, look towards your package manager and specificall "GCC build tools". Your mileage may vary but hey, you're using Linux, you should know this stuff.

    As of writing this page, MacOS versions seem to all come pre-built with Python 2.7 on the system. You will, however, need the C++ build tools.

    • Install XCodearrow-up-right

    • Once XCode is installed, go to Preferences, Downloads, and install the Command Line Tools.

    Once installed, you're ready to continue.

    npm -g --add-python-to-path install windows-build-tools node-gyp
    npm i eslachance/enmap#v3

    Usage Documentation

    Inside your script, initialize a new Enmap:

    const Enmap = require("enmap");
    
    // Initialize an instance of Enmap
    const myCollection = new Enmap();
    
    // Adding data is simply a `set` command: 
    myCollection.set("myKey", "a value");
    
    // Getting a value is done by key 
    let result = myCollection.get("myKey");

    Mathematical Methods

    circle-exclamation

    This page is a work in progress and may not have the polish of a usual Evie-Written document!

    Some quick docs:

    hashtag

    Persistent Enmaps

    Persistence requires an additional Provider module.

    Official Enmap Providers:

    • : Recommended, supports sharding locally.

    • : Best for sharding if you can install a rethinkdb server. Supports updating from the database on all shards (enmap-rethink 2.1.0 and higher)

    enmap.math(key, operation, operator, [objectPath])

    Possible Operators (accepts all variations listed below, as strings):

    • +, add, addition: Increments the value in the enmap by the provided value.

    • -, sub, subtract: Decrements the value in the enmap by the provided value.

    • *, mult, multiply: Multiply the value in the enmap by the provided value.

    • /, div, divide: Divide the value in the enmap by the provided value.

    • %, mod, modulo: Gets the modulo of the value in the enmap by the provided value.

    • ^, exp, exponential: Raises the value in the enmap by the power of the provided value.

    hashtag
    enmap.inc(key, [objectPath])

    hashtag
    enmap.dec(key. [objectPath])

    Enmap-PGSQLarrow-up-right: Postgresql database provider. Supports Sharding.

  • Enmap-Mongoarrow-up-right: Mongodb database provider. Supports Sharding.

  • Enmap-Level:arrow-up-right Sort of not recommended at this point, doesn't support sharding, no longer the most efficient/faster provider.

  • When using any provider, it's very important to understand that it takes time to load the data from the database. Certain providers also take some time to even open the database connection, and that's also something to consider.

    hashtag
    Using defer

    To make sure that all your data is loaded before you start working, Enmap provides a handy property called defer , which is a promise that is resolved once the provider is ready and all the data has been loaded into memory. There are a few ways to use defer , since it's a promise.

    Enmap-SQLitearrow-up-right
    Enmap-Rethinkarrow-up-right
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
     
    points.math("number", "/", 2); // 21
    points.math("number", "add", 5); // 26
    points.math("number", "modulo", 3); // 2
    points.math("numberInObject", "+", 10, "sub.anInt");
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
     
    points.inc("number"); // 43
    points.inc("numberInObject", "sub.anInt"); // {sub: { anInt: 6 }}
    // Assuming
    points.set("number", 42);
    points.set("numberInObject", {sub: { anInt: 5 }});
     
    points.dec("number"); // 41
    points.dec("numberInObject", "sub.anInt"); // {sub: { anInt: 4 }}
    // Load Enmap
    const Enmap = require('enmap');
    
    // Load EnmapSQLite
    const EnmapSQLite = require('enmap-sqlite');
    
    // Initialize the sqlite database with a table named "test"
    const provider = new EnmapSQLite({ name: 'test' });
    
    // Initialize the Enmap with the provider instance.
    const myColl = new Enmap({ provider: provider });
    
    // Persistent providers load in an **async** fashion
    // and provide a handy defer property:
    
    myColl.defer.then(() => {
        // all data is loaded now.
        console.log(myColl.size + "keys loaded");
    });
    
    // You can also await it if your function is async: 
    (async function() {
        await myColl.defer;
        console.log(myColl.size + "keys loaded");
        // Do stuff here!
    }());
    
    // Persistent collections should be **closed** before shutdown: 
    await myColl.db.close();

    Using the fetchAll option

    As described in the home page, one disadvantage of Enmap is that it loads all your data in memory, so you're sacrificing RAM in order to gain speed. In larger projects, this might become a concern fairly quickly - or when using larger data sets that take more memory.

    For this purpose, I surrepticiously added a new option somewhere around Enmap 2.0 called "fetchAll", which defaults to "true" if you don't specify it - which is the old behaviour, and current default at the same time. But, if you set fetchAll to false in the Enmap options, data is not fetched on load.

    hashtag
    What does it mean if the data isn't loaded?

    If fetchAll is set to false, no data (by default) will be loaded from the database - the Enmap will be completely empty. This means that doing a .size check returns 0, looping and filtering doesn't return anything, and get() requests all return null.

    Ok but... how's that useful? It's useful because if you don't need the data, it's not loaded. To load data, there are 2 different methods available.

    hashtag
    Fetching Data

    • enmap.fetchEverything() will, of course, fetch all the data from the database - this is the method that's called on init() if fetchAll is true. This means you can stagger your loading time, or that you can decide, later, under certain conditions, you do want to load everything.

    • enmap.fetch(key) can fetch a single key from the database, saves it to enmap, and returns a promise with the fetched value.

    hashtag
    That's it!

    Yup. Those are the only things you really need to know for the current version of Enmap's fetchAll feature.

    hashtag
    Upcoming Features:

    I'm working on the following features in future versions of enmap, related to fetch methods:

    • Add the ability to check if the DB has a key without fetching

    • Add a method to uncache a key from enmap

    • Add a method to count keys in the DB (like enmap.keyCount() maybe?)

    Working with Objects

    Enmap is a great way to store structured data, and offers a few helper features that directly affect both objects and arrays.

    Let's assume for a moment that we want to store the following data structure in Enmap:

    This structure has 5 "properties": first, second, changeme, isCool, sub. The sub property has 2 properties of its own, yay and thing.

    To store this structure in Enmap, you can use a variable, or just straight-up write the object:

    Note: All further methods require the value to be an object. If you attempt to get, set, modify or remove using the below methods and your value isn't an object, Enmap will throw an error.

    hashtag
    Getting properties

    Retrieving a specific property from an object is done through the get() method, by specifying both the key and the "path" to the property you want.

    The exact method is <Enmap>.get(key, path).

    hashtag
    Checking if a property exists

    You can also check if a specific property exists or not. This is done through the has method, with a key, and path to the property:

    hashtag
    Modifying Properties

    There are a few various ways to modify properties of both Objects and Arrays. The very basic way to set a property on an object or array is through .set(key, value, path) like the following examples:

    As you can see, setProp() and getProp() work on the same concept that the path can be as complex as you want.

    Arrays have additional helper methods, .

    Basic Data Use

    Now that we have a functional Enmap structure (which we'll always refer to as myEnmap), we're ready to start writing data to it, and getting data from it.

    The code samples on this page assume that you have correctly initialized myEnmap, and awaited its initialization if it's persistent.

    hashtag
    Writing Data

    In terms of Enmap, "writing", "adding" and "editing" data is essentially the same thing. When using the basic set() method, if the key does not exist it's created, and if it does, it's modified.

    Enmap supports most native JavaScript data types, with a few small exceptions.

    • Complex objects like Set(), Map(), etc, are not supported.

    • Class instances and Functions are not supported.

    As a general rule, except for null and undefined, anything that can be handled by JSON.stringify() will work as expected in Enmap. This includes:

    • String

    • Number

    • Integer

    Objects and Arrays are a little more complex to deal with, so they have their own page. See for more information.

    The usage for the set() method is simple:

    • key must be a string or integer. A key should be unique, otherwise it will be overwritten by new values using the same key.

    • value must be a supported native data type as mentioned above.

    Here are a few examples of writing simple data values:

    hashtag
    Retrieving Data

    Getting data back from an Enmap is just as simple as writing to it. All you need is the key of what you want to retrieve, and you get its value back!

    That's pretty much it for only retrieiving a single data value. There are more complex operations that are available, take a look at for the more advanced things you can do on Enmap's data!

    What is Enmap?

    Enmap stands for "Enhanced Map", and is a data structure based on the native JavaScript Map() structure with additional helper methods from the native Array() structure.

    Enmap also offers persistence, which means it will automatically save everything to save to it in a database, in the background, without any additional code or delays.

    hashtag
    Why Enmap?

    While there are other better-known systems that offer some features of Enmap, especially caching in memory, Enmap is targetted specifically to newer users of JavaScript that might not want to deal with complicated systems like Redis for caching, or database queries.

    hashtag
    Advantage/Disadvantage

    Here are some advantages of using Enmap:

    • Simple to Install: Enmap itself only requires a simple npm install command to install and use, and a single line to initialize. When using persistent providers, some additional pre-requisites are necessary. .

    • Simple to Use: Basic enmap usage can be completely done with 1-2 lines of initalization, and 3 commands, set(), get() and delete().

    Some disadvantages, compared to using a database connection directly:

    • More memory use: Since Enmap resides in memory and (by default) all its data is loaded when it starts, your entire data resides in RAM. When using a large amount of data on a low-end computer or VPS, this might be an issue for some users.

    • Limited power: You can have multiple Enmap "tables" loaded in your app, but they do not and cannot have relationships between them. Basically, one enmap value can't refer to another value in another enmap. This is something databases can be very good at, but due to the simplistic nature of Enmap, it's not possible here.

    Array Methods

    While Enmap is a Map enhanced with Array methods, Enmap also offers some enhanced array methods for the data stored inside of it. Talk about ArrayCeption!

    So what do I mean by methods for your stored data? I mean that you can store arrays inside Enmap, and directly push, pull, add and remove from those arrays. There are methods to work both on direct arrays, as well as arrays stored inside of an object.

    Let's take a look at two example entries in enmap that we can use. The first is a direct array, the second is an array inside an object.

    myEnmap.set("simpleArray", [1,2,3,4,5]);
    
    myEnmap.set("arrInObj", {
      name: "Bob",
      aliases: ["Bobby", "Robert"]
    });

    hashtag
    Adding to the array

    There are two methods to push to an array, one for simple arrays and one for arrays inside objects. Pushing in an enmap array is the same as a regular array push: it adds the element to the end of the array.

    The second parameter in pushIn is the "path" to the array in an object. It works the same as the properties path used in .

    hashtag
    Removing from the array

    Similarly, you can remove from an array. Note, however, that this will only work if you're removing a string or number. Removing an object from an array is not supported.

    const myStructure = {
      first: "blah",
      second: "foo",
      changeme: "initial",
      isCool: false
      sub: {
        yay: true,
        thing: "amagig"
      }
    }
    enmap.fetch([array, of, keys]) will fetch each key in the requested array, and return an array of [key, value] pairs for each fetched value.
    Figure out how to "fetch on query" without breaking the nonblocking nature of enmap.
    Very Fast: Since Enmap resides in memory, accessing its data is blazing fast (as fast as Map() is). Even with persistence, Enmap still only accesses data from memory so you get it almost instantly.
    Lack of scalability
    : Enmap is great for small apps that require a simple key/value storage. However, a scalable app spread over multiple processes, shards, or clusters, will be severely limited by Enmap as it cannot update itself from the database on change - one process would not be aware of another process' changes.
    See Installation for details
    you can see them here
    Boolean
  • Object

  • Array

  • Working with Objects
    Array Methods
    myEnmap.push("simpleArray", 6);
    // now [1,2,3,4,5,6]
    
    myEnmap.push("arrInObj", "Robby", "aliases");
    // now ["Bobby", "Robert", "Robby"]
    myEnmap.remove("simpleArray", 2);
    // now [1,3,4,5,6]
    
    myEnmap.remove("arrInObject", "Bobby", "aliases");
    // now ["Robert", "Robby"]
    Working With Objects
    myEnmap.set("someObject", myStructure);
    
    // Or directly the object
    myEnmap.set("someObject", {first: "blah", ...});
    
    // Works with arrays, too!
    myEnmap.set("someArray", ["one", "two", "three"]);
    const second = myEnmap.get("someObject", "second");
    // returns "foo"
    
    const thing = myEnmap.get("someObject", "sub.thing");
    // returns true
    
    // The path can be dynamic, too: 
    const propToGet = "thing";
    const blah = myEnmap.get("someObject", `sub.${propToGet}`);
    myEnmap.has("someObject", "sub.thing"); // returns true
    
    myEnmap.has("someObject", "heck"); // returns false.
    // Set an object property
    myEnmap.set("someObject", "sub.blah", "newThing");
    
    // Set an array property
    myEnmap.set("someArray", "four", 3);
    <Enmap>.set(key, value);
    myEnmap.set('boolean', true);
    myEnmap.set('integer', 42);
    myEnmap.set('someFloat', 73.2345871);
    myEnmap.set("Test2", "test2");
    const floatValue = myEnmap.get('someFloat');
    const test = myEnmap.get('Test2');
    
    // you can even use booleans in conditions: 
    if(myEnmap.get('boolean')) {
      // boolean is true!
    }