Full Documentation
The complete and unadultered API documentation for every single method and property accessible in Enmap.
The following is the complete list of methods available in Enmap. As it is auto-generated from the source code and its comments, it's a little more "raw" than the Usage docs. However, it has the benefit of being more complete and usually more up to date than the manually written docs.
If you're doing a PR on the docs github, please do not manually edit the below contents, as it will be overwritten. Check the src/index.js source code and change the comments there instead!

new Enmap(iterable, [options])

Initializes a new Enmap, with options.
Param
Type
Default
Description
iterable
iterable | string
If iterable data, only valid in non-persistent enmaps. If this parameter is a string, it is assumed to be the enmap's name, which is a shorthand for adding a name in the options and making the enmap persistent.
[options]
Object
Additional options for the enmap. See https://enmap.evie.codes/usage#enmap-options for details.
[options.name]
string
The name of the enmap. Represents its table name in sqlite. If present, the enmap is persistent. If no name is given, the enmap is memory-only and is not saved in the database. As a shorthand, you may use a string for the name instead of the options (see example).
[options.fetchAll]
boolean
Defaults to true. When enabled, will automatically fetch any key that's requested using get, or other retrieval methods. This is a "syncroneous" operation, which means it doesn't need any of this promise or callback use.
[options.dataDir]
string
Defaults to ./data. Determines where the sqlite files will be stored. Can be relative (to your project root) or absolute on the disk. Windows users , remember to escape your backslashes!
[options.cloneLevel]
string
Defaults to deep. Determines how objects and arrays are treated when inserting and retrieving from the database. See https://enmap.evie.codes/usage#enmap-options for more details on this option.
[options.ensureProps]
boolean
defaults to true. If enabled and the value in the enmap is an object, using ensure() will also ensure that every property present in the default object will be added to the value, if it's absent. See ensure API reference for more information.
[options.autoEnsure]
*
default is disabled. When provided a value, essentially runs ensure(key, autoEnsure) automatically so you don't have to. This is especially useful on get(), but will also apply on set(), and any array and object methods that interact with the database.
[options.autoFetch]
boolean
defaults to true. When enabled, attempting to get() a key or do any operation on existing keys (such as array push, etc) will automatically fetch the current key value from the database. Keys that are automatically fetched remain in memory and are not cleared.
[options.serializer]
function
Optional. If a function is provided, it will execute on the data when it is written to the database. This is generally used to convert the value into a format that can be saved in the database, such as converting a complete class instance to just its ID. This function may return the value to be saved, or a promise that resolves to that value (in other words, can be an async function).
[options.deserializer]
function
Optional. If a function is provided, it will execute on the data when it is read from the database. This is generally used to convert the value from a stored ID into a more complex object. This function may return a value, or a promise that resolves to that value (in other words, can be an async function).
[options.wal]
boolean
false
Check out Write-Ahead Logging: https://www.sqlite.org/wal.html
Example
1
const Enmap = require("enmap");
2
// Non-persistent enmap:
3
const inMemory = new Enmap();
4
5
// Named, Persistent enmap with string option
6
const myEnmap = new Enmap("testing");
7
8
// Enmap that does not fetch everything, but does so on per-query basis:
9
const myEnmap = new Enmap({name: "testing", fetchAll: false});
10
11
// Enmap that automatically assigns a default object when getting or setting anything.
12
const autoEnmap = new Enmap({name: "settings", autoEnsure: { setting1: false, message: "default message"}})
Copied!

enmap.count ⇒ integer

Retrieves the number of rows in the database for this enmap, even if they aren't fetched.
Kind: instance property of Enmap Returns: integer - The number of rows in the database.

enmap.indexes ⇒ array.<string>

Retrieves all the indexes (keys) in the database for this enmap, even if they aren't fetched.
Kind: instance property of Enmap Returns: array.<string> - Array of all indexes (keys) in the enmap, cached or not.

enmap.autonum ⇒ number

Generates an automatic numerical key for inserting a new value. This is a "weak" method, it ensures the value isn't duplicated, but does not guarantee it's sequential (if a value is deleted, another can take its place). Useful for logging, actions, items, etc - anything that doesn't already have a unique ID.
Kind: instance property of Enmap Returns: number - The generated key number. Example
1
enmap.set(enmap.autonum, "This is a new value");
Copied!

enmap.set(key, val, path) ⇒ Enmap

Sets a value in Enmap.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
Required. The key of the element to add to The Enmap.
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
string
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"
Example
1
// Direct Value Examples
2
enmap.set('simplevalue', 'this is a string');
3
enmap.set('isEnmapGreat', true);
4
enmap.set('TheAnswer', 42);
5
enmap.set('IhazObjects', { color: 'black', action: 'paint', desire: true });
6
enmap.set('ArraysToo', [1, "two", "tree", "foor"])
7
8
// Settings Properties
9
enmap.set('IhazObjects', 'blue', 'color'); //modified previous object
10
enmap.set('ArraysToo', 'three', 2); // changes "tree" to "three" in array.
Copied!

enmap.update(key, valueOrFunction) ⇒ *

Update an existing object value in Enmap by merging new keys. This only works on objects, any other value will throw an error. Heavily inspired by setState from React's class components. This is very useful if you have many different values to update and don't want to have more than one .set(key, value, prop) lines.
Kind: instance method of Enmap Returns: * - The updated, merged value.
Param
Type
Description
key
string
The key of the object to update.
valueOrFunction
*
Either an object to merge with the existing value, or a function that provides the existing object and expects a new object as a return value. In the case of a straight value, the merge is recursive and will add any missing level. If using a function, it is your responsibility to merge the objects together correctly.
Example
1
// Define an object we're going to update
2
enmap.set("obj", { a: 1, b: 2, c: 3 });
3
4
// Direct merge
5
enmap.update("obj", { d: 4, e: 5 });
6
// obj is now { a: 1, b: 2, c: 3, d: 4, e: 5 }
7
8
// Functional update
9
enmap.update("obj", (previous) => ({
10
...obj,
11
f: 6,
12
g: 7
13
}));
14
// this example takes heavy advantage of the spread operators.
15
// More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
Copied!

enmap.get(key, path) ⇒ *

Retrieves a key from the enmap. If fetchAll is false, returns a promise.
Kind: instance method of Enmap Returns: * - The value for this key.
Param
Type
Default
Description
key
string
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"
Example
1
const myKeyValue = enmap.get("myKey");
2
console.log(myKeyValue);
3
4
const someSubValue = enmap.get("anObjectKey", "someprop.someOtherSubProp");
Copied!

enmap.observe(key, path) ⇒ *

Returns an observable object. Modifying this object or any of its properties/indexes/children will automatically save those changes into enmap. This only works on objects and arrays, not "basic" values like strings or integers.
Kind: instance method of Enmap Returns: * - The value for this key.
Param
Type
Default
Description
key
*
The key to retrieve from the enmap.
path
string
null
Optional. The property to retrieve from the object or array.

enmap.fetchEverything() ⇒ Enmap

Fetches every key from the persistent enmap and loads them into the current enmap value.
Kind: instance method of Enmap Returns: Enmap - The enmap containing all values.

enmap.fetch(keyOrKeys) ⇒ Enmap | *

Force fetch one or more key values from the enmap. If the database has changed, that new value is used.
Kind: instance method of Enmap Returns: Enmap | * - The Enmap, including the new fetched values, or the value in case the function argument is a single key.
Param
Type
Description
keyOrKeys
string | number | Array.<(string|number)>
A single key or array of keys to force fetch from the enmap database.

enmap.evict(keyOrArrayOfKeys) ⇒ Enmap

Removes a key or keys from the cache - useful when disabling autoFetch.
Kind: instance method of Enmap Returns: Enmap - The enmap minus the evicted keys.
Param
Type
Description
keyOrArrayOfKeys
string | number | Array.<(string|number)>
A single key or array of keys to remove from the cache.

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 Enmap
Param
Type
Description
cb
function
A callback function that will be called whenever data changes in the enmap.
Example
1
enmap.changed((keyName, oldValue, newValue) => {
2
console.log(`Value of ${keyName} has changed from: \n${oldValue}\nto\n${newValue}`);
3
});
Copied!

enmap.close() ⇒ Promise.<*>

Shuts down the database. WARNING: USING THIS MAKES THE ENMAP UNUSEABLE. You should only use this method if you are closing your entire application. This is useful if you need to copy the database somewhere else, or if you're somehow losing data on shutdown.
Kind: instance method of Enmap Returns: Promise.<*> - The promise of the database closing operation.

enmap.push(key, val, path, allowDupes) ⇒ Enmap

Push to an array value in Enmap.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
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
string
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).
Example
1
// Assuming
2
enmap.set("simpleArray", [1, 2, 3, 4]);
3
enmap.set("arrayInObject", {sub: [1, 2, 3, 4]});
4
5
enmap.push("simpleArray", 5); // adds 5 at the end of the array
6
enmap.push("arrayInObject", "five", "sub"); // adds "five" at the end of the sub array
Copied!

enmap.math(key, operation, operand, path) ⇒ Enmap

Executes a mathematical operation on a value and saves it in the enmap.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
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
number
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.
Example
1
// Assuming
2
points.set("number", 42);
3
points.set("numberInObject", {sub: { anInt: 5 }});
4
5
points.math("number", "/", 2); // 21
6
points.math("number", "add", 5); // 26
7
points.math("number", "modulo", 3); // 2
8
points.math("numberInObject", "+", 10, "sub.anInt");
Copied!

enmap.inc(key, path) ⇒ Enmap

Increments a key's value or property by 1. Value must be a number, or a path to a number.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
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.
Example
1
// Assuming
2
points.set("number", 42);
3
points.set("numberInObject", {sub: { anInt: 5 }});
4
5
points.inc("number"); // 43
6
points.inc("numberInObject", "sub.anInt"); // {sub: { anInt: 6 }}
Copied!

enmap.dec(key, path) ⇒ Enmap

Decrements a key's value or property by 1. Value must be a number, or a path to a number.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
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.
Example
1
// Assuming
2
points.set("number", 42);
3
points.set("numberInObject", {sub: { anInt: 5 }});
4
5
points.dec("number"); // 41
6
points.dec("numberInObject", "sub.anInt"); // {sub: { anInt: 4 }}
Copied!

enmap.ensure(key, defaultValue, path) ⇒ *

Returns the key's value, or the default given, ensuring that the data is there. This is a shortcut to "if enmap doesn't have key, set it, then get it" which is a very common pattern.
Kind: instance method of Enmap Returns: * - The value from the database for the key, or the default value provided for a new key.
Param
Type
Default
Description
key
string
Required. The key you want to make sure exists.
defaultValue
*
Required. The value you want to save in the database and return as default.
path
string
null
Optional. If presents, ensures both the key exists as an object, and the full path exists. Can be a path with dot notation, such as "prop1.subprop2.subprop3"
Example
1
// Simply ensure the data exists (for using property methods):
2
enmap.ensure("mykey", {some: "value", here: "as an example"});
3
enmap.has("mykey"); // always returns true
4
enmap.get("mykey", "here") // returns "as an example";
5
6
// Get the default value back in a variable:
7
const settings = mySettings.ensure("1234567890", defaultSettings);
8
console.log(settings) // enmap's value for "1234567890" if it exists, otherwise the defaultSettings value.
Copied!

enmap.has(key, path) ⇒ boolean

Returns whether or not the key exists in the Enmap.
Kind: instance method of Enmap
Param
Type
Default
Description
key
string
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"
Example
1
if(enmap.has("myKey")) {
2
// key is there
3
}
4
5
if(!enmap.has("myOtherKey", "oneProp.otherProp.SubProp")) return false;
Copied!

enmap.includes(key, val, path) ⇒ boolean

Performs Array.includes() on a certain enmap value. Works similar to Array.includes().
Kind: instance method of Enmap Returns: boolean - Whether the array contains the value.
Param
Type
Default
Description
key
string
Required. The key of the array to check the value of.
val
string | number
Required. The value to check whether it's in the array.
path
*
Required. The property to access the array inside the value object or array. Can be a path with dot notation, such as "prop1.subprop2.subprop3"

enmap.delete(key, path) ⇒ Enmap

Deletes a key in the Enmap.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
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"

enmap.clear()

Deletes everything from the enmap. If persistent, clears the database of all its data for this table.
Kind: instance method of Enmap

enmap.destroy() ⇒ null

Completely destroys the entire enmap. This deletes the database tables entirely. It will not affect other enmap data in the same database, however. THIS ACTION WILL DESTROY YOUR DATA AND CANNOT BE UNDONE.
Kind: instance method of Enmap

enmap.remove(key, val, path) ⇒ Enmap

Remove a value in an Array or Object element in Enmap. Note that this only works for values, not keys. Note that only one value is removed, no more. Arrays of objects must use a function to remove, as full object matching is not supported.
Kind: instance method of Enmap Returns: Enmap - The enmap.
Param
Type
Default
Description
key
string
Required. The key of the element to remove from in Enmap. This value MUST be a string or number.
val
* | function
Required. The value to remove from the array or object. OR a function to match an object. If using a function, the function provides the object value and must return a boolean that's true for the object you want to remove.
path
string
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.
Example
1
// Assuming
2
enmap.set('array', [1, 2, 3])
3
enmap.set('objectarray', [{ a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 }])
4
5
enmap.remove('array', 1); // value is now [2, 3]
6
enmap.remove('objectarray', (value) => value.e === 5); // value is now [{ a: 1, b: 2, c: 3 }]
Copied!

enmap.export() ⇒ string

Exports the enmap data to a JSON file. WARNING: Does not work on memory enmaps containing complex data!
Kind: instance method of Enmap Returns: string - The enmap data in a stringified JSON format.

enmap.import(data, overwrite, clear) ⇒ Enmap

Import an existing json export from enmap from a string. This data must have been exported from enmap, and must be from a version that's equivalent or lower than where you're importing it.
Kind: instance method of Enmap Returns: Enmap - The enmap with the new data.
Param
Type
Default
Description
data
string
The data to import to Enmap. Must contain all the required fields provided by export()
overwrite
boolean
true
Defaults to true. Whether to overwrite existing key/value data with incoming imported data
clear
boolean
false
Defaults to false. Whether to clear the enmap of all data before importing (WARNING: Any exiting data will be lost! This cannot be undone.)

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 Enmap

enmap.keyArray() ⇒ Array.<(string|number)>

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 Enmap

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

Obtains random value(s) from this Enmap. This relies on array.
Kind: instance method of Enmap Returns: * | Array.<*> - The single value if count is undefined, or an array of values of count length
Param
Type
Description
[count]
number
Number of values to obtain randomly

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

Obtains random key(s) from this Enmap. This relies on keyArray
Kind: instance method of Enmap Returns: * | Array.<*> - The single key if count is undefined, or an array of keys of count length
Param
Type
Description
[count]
number
Number of keys to obtain randomly

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 Enmap
Param
Type
Description
prop
string
The property to test against
value
*
The expected value
Example
1
enmap.findAll('username', 'Bob');
Copied!

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 Array.find().
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 Enmap
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
Example
1
enmap.find('username', 'Bob');
Copied!
Example
1
enmap.find(val => val.username === 'Bob');
Copied!

enmap.findKey(propOrFn, [value]) ⇒ string | number

Searches for the key of 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 Array.findIndex().
Kind: instance method of Enmap
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
Example
1
enmap.findKey('username', 'Bob');
Copied!
Example
1
enmap.findKey(val => val.username === 'Bob');
Copied!

enmap.sweep(fn, [thisArg]) ⇒ number

Removes entries that satisfy the provided filter function.
Kind: instance method of Enmap Returns: number - The number of removed entries
Param
Type
Description
fn
function
Function used to test (should return a boolean)
[thisArg]
Object
Value to use as this when executing function

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

Identical to Array.filter(), but returns a Enmap instead of an Array.
Kind: instance method of Enmap
Param
Type
Description
fn
function
Function used to test (should return a boolean)
[thisArg]
Object
Value to use as this when executing function

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

Identical to Array.filter().
Kind: instance method of Enmap
Param
Type
Description
fn
function
Function used to test (should return a boolean)
[thisArg]
Object
Value to use as this when executing function

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

Identical to Array.map().
Kind: instance method of Enmap
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

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

Identical to Array.some().
Kind: instance method of Enmap
Param
Type
Description
fn
function
Function used to test (should return a boolean)
[thisArg]
Object
Value to use as this when executing function

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

Identical to Array.every().
Kind: instance method of Enmap
Param
Type
Description
fn
function
Function used to test (should return a boolean)
[thisArg]
Object
Value to use as this when executing function

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

Identical to Array.reduce().
Kind: instance method of Enmap
Param
Type
Description
fn
function
Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and enmap
[initialValue]
*
Starting value for the accumulator

enmap.clone() ⇒ Enmap

Creates an identical shallow copy of this Enmap.
Kind: instance method of Enmap Example
1
const newColl = someColl.clone();
Copied!

enmap.concat(...enmaps) ⇒ Enmap

Combines this Enmap with others into a new Enmap. None of the source Enmaps are modified.
Kind: instance method of Enmap
Param
Type
Description
...enmaps
Enmap
Enmaps to merge
Example
1
const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
Copied!

Enmap.multi(names, options) ⇒ Array.<Map>

Initialize multiple Enmaps easily.
Kind: static method of Enmap Returns: Array.<Map> - An array of initialized Enmaps.
Param
Type
Description
names
Array.<string>
Array of strings. Each array entry will create a separate enmap with that name.
options
Object
Options object to pass to each enmap, excluding the name..
Example
1
// Using local variables.
2
const Enmap = require('enmap');
3
const { settings, tags, blacklist } = Enmap.multi(['settings', 'tags', 'blacklist']);
4
5
// Attaching to an existing object (for instance some API's client)
6
const Enmap = require("enmap");
7
Object.assign(client, Enmap.multi(["settings", "tags", "blacklist"]));
Copied!
Last modified 2mo ago
Export as PDF
Copy link