Enmap API Reference / default
#
default (V, SV)
Defined in: index.ts:77
A simple, synchronous, fast key/value storage build around better-sqlite3. Contains extra utility methods for managing arrays and objects.
#
Type Parameters
#
Constructors
#
Constructor
new default<V, SV>(options): Enmap<V, SV>;
Defined in: index.ts:109
Initializes a new Enmap, with options.
#
Parameters
#
Returns
Enmap
<V
, SV
>
#
Example
import Enmap from 'enmap';
// Named, Persistent enmap
const myEnmap = new Enmap({ name: "testing" });
// Memory-only enmap
const memoryEnmap = new Enmap({ inMemory: true });
// Enmap that automatically assigns a default object when getting or setting anything.
const autoEnmap = new Enmap({name: "settings", autoEnsure: { setting1: false, message: "default message"}})
#
Accessors
#
size
#
Get Signature
get size(): number;
Defined in: index.ts:292
Get the number of key/value pairs saved in the enmap.
#
Returns
number
The number of elements in the enmap.
#
count
#
Get Signature
get count(): number;
Defined in: index.ts:300
#
Returns
number
#
length
#
Get Signature
get length(): number;
Defined in: index.ts:304
#
Returns
number
#
db
#
Get Signature
get db(): Database;
Defined in: index.ts:313
Get the better-sqlite3 database object. Useful if you want to directly query or interact with the underlying SQLite database. Use at your own risk, as errors here might cause loss of data or corruption!
#
Returns
Database
#
autonum
#
Get Signature
get autonum(): string;
Defined in: index.ts:327
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.
#
Example
enmap.set(enmap.autonum, "This is a new value");
#
Returns
string
The generated key number.
#
Methods
#
set()
set(
key,
value,
path?): this;
Defined in: index.ts:195
Sets a value in Enmap. If the key already has a value, overwrites the data (or the value in a path, if provided).
#
Parameters
#
Returns
this
#
Example
// 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', 'blue', 'color'); //modified previous object
enmap.set('ArraysToo', 'three', 2); // changes "tree" to "three" in array.
#
get()
get(key, path?): any;
Defined in: index.ts:221
Retrieves a value from the enmap, using its key.
#
Parameters
#
Returns
any
The parsed value for this key.
#
Example
const myKeyValue = enmap.get("myKey");
console.log(myKeyValue);
const someSubValue = enmap.get("anObjectKey", "someprop.someOtherSubProp");
#
has()
has(key): boolean;
Defined in: index.ts:250
Returns whether or not the key exists in the Enmap.
#
Parameters
#
Returns
boolean
#
Example
if(enmap.has("myKey")) {
// key is there
}
#
delete()
delete(key, path?): this;
Defined in: index.ts:263
Deletes a key in the Enmap.
#
Parameters
#
Returns
this
#
clear()
clear(): void;
Defined in: index.ts:282
Deletes everything from the enmap.
#
Returns
void
#
keys()
keys(): string[];
Defined in: index.ts:348
Get all the keys of the enmap as an array.
#
Returns
string
[]
An array of all the keys in the enmap.
#
indexes()
indexes(): string[];
Defined in: index.ts:357
#
Returns
string
[]
#
values()
values(): V[];
Defined in: index.ts:365
Get all the values of the enmap as an array.
#
Returns
V
[]
An array of all the values in the enmap.
#
entries()
entries(): [string, V][];
Defined in: index.ts:378
Get all entries of the enmap as an array, with each item containing the key and value.
#
Returns
[string
, V
][]
An array of arrays, with each sub-array containing two items, the key and the value.
#
update()
update(key, valueOrFunction): V;
Defined in: index.ts:413
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.
#
Parameters
#
Returns
V
The modified (merged) value.
#
Example
// Define an object we're going to update
enmap.set("obj", { a: 1, b: 2, c: 3 });
// Direct merge
enmap.update("obj", { d: 4, e: 5 });
// obj is now { a: 1, b: 2, c: 3, d: 4, e: 5 }
// Functional update
enmap.update("obj", (previous) => ({
...obj,
f: 6,
g: 7
}));
// this example takes heavy advantage of the spread operators.
// More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
#
observe()
observe(key, path?): any;
Defined in: index.ts:433
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.
#
Parameters
#
Returns
any
The value for this key.
#
push()
push(
key,
value,
path?,
allowDupes?): this;
Defined in: index.ts:457
Push to an array value in Enmap.
#
Parameters
#
Returns
this
#
Example
// 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
#
math()
math(
key,
operation,
operand,
path?): null | number;
Defined in: index.ts:487
Executes a mathematical operation on a value and saves it in the enmap.
#
Parameters
#
Returns
null
| number
The updated value after the operation
#
Example
// 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");
#
inc()
inc(key, path?): this;
Defined in: index.ts:512
Increments a key's value or property by 1. Value must be a number, or a path to a number.
#
Parameters
#
Returns
this
The udpated value after incrementing.
#
Example
// Assuming
points.set("number", 42);
points.set("numberInObject", {sub: { anInt: 5 }});
points.inc("number"); // 43
points.inc("numberInObject", "sub.anInt"); // {sub: { anInt: 6 }}
#
dec()
dec(key, path?): this;
Defined in: index.ts:533
Decrements a key's value or property by 1. Value must be a number, or a path to a number.
#
Parameters
#
Returns
this
The enmap.
#
Example
// Assuming
points.set("number", 42);
points.set("numberInObject", {sub: { anInt: 5 }});
points.dec("number"); // 41
points.dec("numberInObject", "sub.anInt"); // {sub: { anInt: 4 }}
#
ensure()
ensure(
key,
defaultValue,
path?): any;
Defined in: index.ts:559
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.
#
Parameters
#
Returns
any
The value from the database for the key, or the default value provided for a new key.
#
Example
// Simply ensure the data exists (for using property methods):
enmap.ensure("mykey", {some: "value", here: "as an example"});
enmap.has("mykey"); // always returns true
enmap.get("mykey", "here") // returns "as an example";
// Get the default value back in a variable:
const settings = mySettings.ensure("1234567890", defaultSettings);
console.log(settings) // enmap's value for "1234567890" if it exists, otherwise the defaultSettings value.
#
includes()
includes(
key,
value,
path?): boolean;
Defined in: index.ts:607
Performs Array.includes() on a certain enmap value. Works similar to Array.includes().
#
Parameters
#
Returns
boolean
Whether the array contains the value.
#
remove()
remove(
key,
val,
path?): this;
Defined in: index.ts:632
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.
#
Parameters
#
Returns
this
#
Example
// Assuming
enmap.set('array', [1, 2, 3])
enmap.set('objectarray', [{ a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 }])
enmap.remove('array', 1); // value is now [2, 3]
enmap.remove('objectarray', (value) => value.e === 5); // value is now [{ a: 1, b: 2, c: 3 }]
#
export()
export(): string;
Defined in: index.ts:650
Exports the enmap data to stringified JSON format. WARNING: Does not work on memory enmaps containing complex data!
#
Returns
string
The enmap data in a stringified JSON format.
#
import()
import(
data,
overwrite,
clear): this;
Defined in: index.ts:673
Import an existing json export from enmap. 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. (This means Enmap 5 data is compatible in Enmap 6).
#
Parameters
#
Returns
this
#
multi()
static multi<V, SV>(names, options?): Record<string, Enmap<V, SV>>;
Defined in: index.ts:715
Initialize multiple Enmaps easily.
#
Type Parameters
#
Parameters
#
Returns
Record
<string
, Enmap
<V
, SV
>>
An array of initialized Enmaps.
#
Example
// Using local variables.
const Enmap = require('enmap');
const { settings, tags, blacklist } = Enmap.multi(['settings', 'tags', 'blacklist']);
// Attaching to an existing object (for instance some API's client)
import Enmap from 'enmap';
Object.assign(client, Enmap.multi(["settings", "tags", "blacklist"]));
#
random()
random(count?): [string, V][];
Defined in: index.ts:738
Obtains random value(s) from this Enmap. This relies on Enmap#array.
#
Parameters
#
Returns
[string
, V
][]
The single value if count
is undefined,
or an array of values of count
length
#
randomKey()
randomKey(count?): string[];
Defined in: index.ts:755
Obtains random key(s) from this Enmap. This relies on Enmap#keyArray
#
Parameters
#
Returns
string
[]
The single key if count
is undefined,
or an array of keys of count
length
#
every()
every(valueOrFunction, path?): boolean;
Defined in: index.ts:775
Similar to Array.every(). Supports either a predicate function or a value to compare. Returns true only if the predicate function returns true for all elements in the array (or the value is strictly equal in all elements).
#
Parameters
#
Returns
boolean
#
some()
some(valueOrFunction, path?): boolean;
Defined in: index.ts:802
Similar to Array.some(). Supports either a predicate function or a value to compare. Returns true if the predicate function returns true for at least one element in the array (or the value is equal in at least one element).
#
Parameters
#
Returns
boolean
#
map()
map<R>(pathOrFn): R[];
Defined in: index.ts:827
Similar to Array.map(). Returns an array of the results of applying the callback to all elements.
#
Type Parameters
#
Parameters
#
Returns
R
[]
#
find()
find(pathOrFn, value?): null | V;
Defined in: index.ts:853
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 similar to
Array.find().
#
Parameters
#
Returns
null
| V
#
Examples
enmap.find('username', 'Bob');
enmap.find(val => val.username === 'Bob');
#
findIndex()
findIndex(pathOrFn, value?): null | string;
Defined in: index.ts:879
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 similar to
Array.findIndex().
#
Parameters
#
Returns
null
| string
#
Examples
enmap.findIndex('username', 'Bob');
enmap.findIndex(val => val.username === 'Bob');
#
reduce()
reduce<R>(predicate, initialValue?): R;
Defined in: index.ts:900
Similar to Array.reduce().
#
Type Parameters
#
Parameters
#
Returns
R
#
filter()
filter(pathOrFn, value?): V[];
Defined in: index.ts:920
Similar to Array.filter(). Returns an array of values where the given function returns true for that value. Alternatively you can provide a value and path to filter by using exact value matching.
#
Parameters
#
Returns
V
[]
#
sweep()
sweep(pathOrFn, value?): number;
Defined in: index.ts:950
Deletes entries that satisfy the provided filter function or value matching.
#
Parameters
#
Returns
number
The number of removed entries.
#
changed()
changed(cb): void;
Defined in: index.ts:988
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.
#
Parameters
#
Returns
void
#
Example
enmap.changed((keyName, oldValue, newValue) => {
console.log(`Value of ${keyName} has changed from: \n${oldValue}\nto\n${newValue}`);
});
#
partition()
partition(pathOrFn, value?): [V[], V[]];
Defined in: index.ts:998
Separates the Enmap into multiple arrays given a function that separates them.
#
Parameters
#
Returns
[V
[], V
[]]
An array of arrays with the partitioned data.