Enmap - Enhanced Maps
Enhanced Maps are a data structure that can be used to store data in memory that can also be saved in a database behind the scenes. The data is synchronized to the database automatically, seamlessly, and asynchronously so it should not adversely affect your performance compared to using Maps for storage.
FAQs
Q: So what's Enmap
A: Enmaps are the Javascript Map() data structure with additional utility methods.
Q: What is "Persistent"?
A: With the use of the optional providers modules, any data added to the Enmap
is stored not only in temporary memory but also backed up in a local file
database. This does not require a server. Saving things in memory enables
faster code, but it may take more memory.
Q: How big can the Enmap be?
A: In its initial implementation, upon loading Enmap, all
key/value pairs are loaded in memory. The size of the memory used is directly
proportional to the size of your actual database.
Future versions will have ways to load partial or temporary values, etc.
Installation
To use Enmap, install it via NPM:
npm i enmap
Basic Usage
Inside your script, initialize a new Enmap:
const Enmap = require("enmap");
const myCollection = new Enmap();
myCollection.set("myKey", "a value");
let result = myCollection.get("myKey");
Adding Persistence
Persistence requires an additional Provider module. Currently only one is available:
npm i enmap-level
It must be initialized with the appropriate values
const Enmap = require('enmap');
const EnmapLevel = require('enmap-level');
const level = new EnmapLevel({ name: 'test' });
const myColl = new Enmap({ provider: level });
myColl.defer.then(() => {
console.log(myColl.size + "keys loaded");
});
(async function() {
await myColl.defer;
console.log(myColl.size + "keys loaded");
}());
await myColl.db.close();
Enmap ⇐ Map
Enhanced Map structure with additional utility methods.
Can be made persistent with optional provider modules.
Kind: global class
Extends: Map
- Enmap ⇐
Map
- .init() ⇒
Void
- .validateName() ⇒
boolean
- .close()
- .set(key, val) ⇒
Map
- .setAsync(key, val) ⇒
Map
- .delete(key, bulk)
- .deleteAsync(key, bulk)
- .purge() ⇒
Promise
- .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
- .deleteAll() ⇒
Array.<Promise>
- .equals(enmap) ⇒
boolean
enmap.init() ⇒ Void
Internal method called on persistent Enmaps to load data from the underlying database.
Kind: instance method of Enmap
enmap.validateName() ⇒ boolean
Internal method used to validate persistent enmap names (valid Windows filenames);
Kind: instance method of Enmap
Returns: boolean
- Indicates whether the name is valid.
enmap.close()
Shuts down the underlying persistent enmap database.
Kind: instance method of Enmap
enmap.set(key, val) ⇒ Map
Kind: instance method of Enmap
Returns: Map
- The EnMap object.
Param | Type | Description |
---|
key | * | Required. The key of the element to add to the EnMap object. 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 object. If the EnMap is persistent this value MUST be stringifiable as JSON. |
enmap.setAsync(key, val) ⇒ Map
Kind: instance method of Enmap
Returns: Map
- The EnMap object.
Param | Type | Description |
---|
key | * | Required. The key of the element to add to the EnMap object. 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 object. If the EnMap is persistent this value MUST be stringifiable as JSON. |
enmap.delete(key, bulk)
Kind: instance method of Enmap
Param | Type | Default | Description |
---|
key | * | | Required. The key of the element to delete from the EnMap object. |
bulk | boolean | false | Internal property used by the purge method. |
enmap.deleteAsync(key, bulk)
Kind: instance method of Enmap
Param | Type | Default | Description |
---|
key | * | | Required. The key of the element to delete from the EnMap object. |
bulk | boolean | false | Internal property used by the purge method. |
enmap.purge() ⇒ Promise
Completely deletes all keys from an EnMap, including persistent data.
Kind: instance method of Enmap
enmap.array() ⇒ Array
Creates an ordered array of the values of this Enmap, and caches it internally.
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
Creates an ordered array of the keys of this Enmap, and caches it internally.
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,
and thus the caching mechanism applies here as well.
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,
and thus the caching mechanism applies here as well.
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
enmap.findAll('username', 'Bob');
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 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
enmap.find('username', 'Bob');
Example
enmap.find(val => val.username === 'Bob');
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 for details.
Kind: instance method of Enmap
Param | Type | Description |
---|
prop | string | The property to test against |
value | * | The expected value |
Example
if (enmap.exists('username', 'Bob')) {
console.log('user here!');
}
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
const newColl = someColl.clone();
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
const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
enmap.deleteAll() ⇒ Array.<Promise>
Calls the delete()
method on all items that have it.
Kind: instance method of Enmap
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 Enmap
Returns: boolean
- Whether the Enmaps have identical contents
Param | Type | Description |
---|
enmap | Enmap | Enmap to compare with |