Comparing version 5.1.2 to 5.2.0
{ | ||
"name": "enmap", | ||
"version": "5.1.2", | ||
"version": "5.2.0", | ||
"description": "A simple database wrapper to make sqlite database interactions much easier for beginners, with additional array helper methods.", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
@@ -23,3 +23,3 @@ # Enmap - Enhanced Maps | ||
> slowly reverting some of the features that I attempted to add to enmap to make it support multiple connections, such as polling. Enmap 5.0 starts this process by removing | ||
> the dependency on better-sqlite-pool. So, if you're using enmap in a sharded/multithreaded/multiprocess setup, don't update, and hold off on (JOSH)[https://www.npmjs.com/package/josh]. | ||
> the dependency on better-sqlite-pool. So, if you're using enmap in a sharded/multithreaded/multiprocess setup, don't update, and hold off on [JOSH](https://www.npmjs.com/package/josh). | ||
> When JOSH is released, it'll be able to scale up with your application. | ||
@@ -71,10 +71,6 @@ | ||
``` | ||
I have legit tried several databases, from popular complicated ones to pretty basic ones. The only database I had absolutely no issue with was and still is enmap. | ||
``` | ||
> I have legit tried several databases, from popular complicated ones to pretty basic ones. The only database I had absolutely no issue with was and still is enmap. | ||
- [LostSorrow#1237 on Discord](https://discordapp.com/channels/298508738623438848/481857430427533312/586619143361396740) | ||
``` | ||
I know how to use a real db, but enmap is so sweet and easy to use | ||
``` | ||
- [CHY4E#0505 on Discord](https://discordapp.com/channels/260202843686830080/267727088465739778/588901731979624448) | ||
> I know how to use a real db, but enmap is so sweet and easy to use | ||
- [CHY4E#0505 on Discord](https://discordapp.com/channels/260202843686830080/267727088465739778/588901731979624448) |
149
src/index.js
// Lodash should probably be a core lib but hey, it's useful! | ||
const _ = require('lodash'); | ||
const { | ||
get: _get, | ||
set: _set, | ||
has: _has, | ||
delete: _delete, | ||
isNil, | ||
isFunction, | ||
isArray, | ||
isObject, | ||
toPath, | ||
merge, | ||
clone, | ||
cloneDeep, | ||
} = require('lodash'); | ||
@@ -175,3 +188,3 @@ // Custom error codes with stack support. | ||
this[_readyCheck](); | ||
if (_.isNil(key) || !['String', 'Number'].includes(key.constructor.name)) { | ||
if (isNil(key) || !['String', 'Number'].includes(key.constructor.name)) { | ||
throw new Err('Enmap require keys to be strings or numbers.', 'EnmapKeyTypeError'); | ||
@@ -183,5 +196,5 @@ } | ||
const oldValue = super.has(key) ? this[_clone](data) : null; | ||
if (!_.isNil(path)) { | ||
if (_.isNil(data)) data = {}; | ||
_.set(data, path, val); | ||
if (!isNil(path)) { | ||
if (isNil(data)) data = {}; | ||
_set(data, path, val); | ||
} else { | ||
@@ -201,3 +214,3 @@ // New 4.6.0: typecheck | ||
} | ||
if (_.isFunction(this.changedCB)) { | ||
if (isFunction(this.changedCB)) { | ||
this.changedCB(key, oldValue, data); | ||
@@ -228,9 +241,9 @@ } | ||
this[_readyCheck](); | ||
if (_.isNil(key)) return null; | ||
if (isNil(key)) return null; | ||
this[_fetchCheck](key); | ||
key = key.toString(); | ||
if (!_.isNil(path)) { | ||
if (!isNil(path)) { | ||
this[_check](key, ['Object', 'Array']); | ||
const data = super.get(key); | ||
return _.get(data, path); | ||
return _get(data, path); | ||
} | ||
@@ -294,3 +307,3 @@ const data = super.get(key); | ||
this[_readyCheck](); | ||
if (_.isArray(keyOrKeys)) { | ||
if (isArray(keyOrKeys)) { | ||
const data = this.db.prepare(`SELECT * FROM ${this.name} WHERE key IN (${'?, '.repeat(keyOrKeys.length).slice(0, -2)})`).all(keyOrKeys); | ||
@@ -315,3 +328,3 @@ for (const row of data) { | ||
evict(keyOrArrayOfKeys) { | ||
if (_.isArray(keyOrArrayOfKeys)) { | ||
if (isArray(keyOrArrayOfKeys)) { | ||
keyOrArrayOfKeys.forEach(key => super.delete(key)); | ||
@@ -377,3 +390,3 @@ } else { | ||
this[_readyCheck](); | ||
if (_.isNil(path)) throw new Err(`No path provided to set a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to set a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
return this.set(key, val, path); | ||
@@ -404,7 +417,7 @@ } | ||
const data = super.get(key); | ||
if (!_.isNil(path)) { | ||
const propValue = _.get(data, path); | ||
if (!isNil(path)) { | ||
const propValue = _get(data, path); | ||
if (!allowDupes && propValue.indexOf(val) > -1) return this; | ||
propValue.push(val); | ||
_.set(data, path, propValue); | ||
_set(data, path, propValue); | ||
} else { | ||
@@ -430,3 +443,3 @@ if (!allowDupes && data.indexOf(val) > -1) return this; | ||
this[_fetchCheck](key); | ||
if (_.isNil(path)) throw new Err(`No path provided to push a value in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to push a value in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
return this.push(key, val, path, allowDupes); | ||
@@ -460,3 +473,3 @@ } | ||
this[_check](key, 'Number', path); | ||
if (_.isNil(path)) { | ||
if (isNil(path)) { | ||
if (operation === 'random' || operation === 'rand') { | ||
@@ -468,3 +481,3 @@ return this.set(key, Math.round(Math.random() * operand)); | ||
const data = this.get(key); | ||
const propValue = _.get(data, path); | ||
const propValue = _get(data, path); | ||
if (operation === 'random' || operation === 'rand') { | ||
@@ -493,3 +506,3 @@ return this.set(key, Math.round(Math.random() * propValue), path); | ||
this[_check](key, 'Number', path); | ||
if (_.isNil(path)) { | ||
if (isNil(path)) { | ||
let val = this.get(key); | ||
@@ -499,4 +512,4 @@ return this.set(key, ++val); | ||
const data = this.get(key); | ||
let propValue = _.get(data, path); | ||
_.set(data, path, ++propValue); | ||
let propValue = _get(data, path); | ||
_set(data, path, ++propValue); | ||
return this.set(key, data); | ||
@@ -522,3 +535,3 @@ } | ||
this[_check](key, 'Number', path); | ||
if (_.isNil(path)) { | ||
if (isNil(path)) { | ||
let val = this.get(key); | ||
@@ -528,4 +541,4 @@ return this.set(key, --val); | ||
const data = this.get(key); | ||
let propValue = _.get(data, path); | ||
_.set(data, path, --propValue); | ||
let propValue = _get(data, path); | ||
_set(data, path, --propValue); | ||
return this.set(key, data); | ||
@@ -545,3 +558,3 @@ } | ||
this[_fetchCheck](key); | ||
if (_.isNil(path)) throw new Err(`No path provided to get a property from "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to get a property from "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
return this.get(key, path); | ||
@@ -571,5 +584,5 @@ } | ||
this[_fetchCheck](key); | ||
if (_.isNil(defaultValue)) throw new Err(`No default value provided on ensure method for "${key}" in "${this.name}"`, 'EnmapArgumentError'); | ||
if (isNil(defaultValue)) throw new Err(`No default value provided on ensure method for "${key}" in "${this.name}"`, 'EnmapArgumentError'); | ||
const clonedValue = this[_clone](defaultValue); | ||
if (!_.isNil(path)) { | ||
if (!isNil(path)) { | ||
if (this.ensureProps) this.ensure(key, {}); | ||
@@ -581,6 +594,6 @@ if (!super.has(key)) throw new Err(`Key "${key}" does not exist in "${this.name}" to ensure a property`, 'EnmapKeyError'); | ||
} | ||
if (this.ensureProps && _.isObject(super.get(key))) { | ||
if (!_.isObject(clonedValue)) throw new Err(`Default value for "${key}" in enmap "${this.name}" must be an object when merging with an object value.`, 'EnmapArgumentError'); | ||
if (this.ensureProps && isObject(super.get(key))) { | ||
if (!isObject(clonedValue)) throw new Err(`Default value for "${key}" in enmap "${this.name}" must be an object when merging with an object value.`, 'EnmapArgumentError'); | ||
// const merged = Object.assign(clonedValue, super.get(key)); | ||
const merged = _.merge(clonedValue, super.get(key)); | ||
const merged = merge(clonedValue, super.get(key)); | ||
super.set(key, merged); | ||
@@ -613,6 +626,6 @@ return merged; | ||
this[_fetchCheck](key); | ||
if (!_.isNil(path)) { | ||
if (!isNil(path)) { | ||
this[_check](key, 'Object'); | ||
const data = super.get(key); | ||
return _.has(data, path); | ||
return _has(data, path); | ||
} | ||
@@ -632,3 +645,3 @@ return super.has(key); | ||
this[_fetchCheck](key); | ||
if (_.isNil(path)) throw new Err(`No path provided to check for a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to check for a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
return this.has(key, path); | ||
@@ -651,9 +664,9 @@ } | ||
const data = super.get(key); | ||
if (!_.isNil(path)) { | ||
const propValue = _.get(data, path); | ||
if (_.isArray(propValue)) { | ||
if (!isNil(path)) { | ||
const propValue = _get(data, path); | ||
if (isArray(propValue)) { | ||
return propValue.includes(val); | ||
} | ||
throw new Err(`The property "${path}" in key "${key}" is not an Array in the enmap "${this.name}" (property was of type "${propValue && propValue.constructor.name}")`, 'EnmapTypeError'); | ||
} else if (_.isArray(data)) { | ||
} else if (isArray(data)) { | ||
return data.includes(val); | ||
@@ -675,8 +688,8 @@ } | ||
const oldValue = this.get(key); | ||
if (!_.isNil(path)) { | ||
if (!isNil(path)) { | ||
let data = this.get(key); | ||
path = _.toPath(path); | ||
path = toPath(path); | ||
const last = path.pop(); | ||
const propValue = path.length ? _.get(data, path) : data; | ||
if (_.isArray(propValue)) { | ||
const propValue = path.length ? _get(data, path) : data; | ||
if (isArray(propValue)) { | ||
propValue.splice(last, 1); | ||
@@ -687,3 +700,3 @@ } else { | ||
if (path.length) { | ||
_.set(data, path, propValue); | ||
_set(data, path, propValue); | ||
} else { | ||
@@ -718,3 +731,3 @@ data = propValue; | ||
this[_fetchCheck](key); | ||
if (_.isNil(path)) throw new Err(`No path provided to delete a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to delete a property in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
this.delete(key, path); | ||
@@ -784,14 +797,14 @@ } | ||
const data = super.get(key); | ||
if (!_.isNil(path)) { | ||
const propValue = _.get(data, path); | ||
if (_.isArray(propValue)) { | ||
if (!isNil(path)) { | ||
const propValue = _get(data, path); | ||
if (isArray(propValue)) { | ||
propValue.splice(propValue.indexOf(val), 1); | ||
_.set(data, path, propValue); | ||
} else if (_.isObject(propValue)) { | ||
_.delete(data, `${path}.${val}`); | ||
_set(data, path, propValue); | ||
} else if (isObject(propValue)) { | ||
_delete(data, `${path}.${val}`); | ||
} | ||
} else if (_.isArray(data)) { | ||
} else if (isArray(data)) { | ||
const index = data.indexOf(val); | ||
data.splice(index, 1); | ||
} else if (_.isObject(data)) { | ||
} else if (isObject(data)) { | ||
delete data[val]; | ||
@@ -815,3 +828,3 @@ } | ||
this[_fetchCheck](key); | ||
if (_.isNil(path)) throw new Err(`No path provided to remove an array element in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
if (isNil(path)) throw new Err(`No path provided to remove an array element in "${key}" of enmap "${this.name}"`, 'EnmapPathError'); | ||
return this.remove(key, val, path); | ||
@@ -847,3 +860,3 @@ } | ||
if (clear) this.deleteAll(); | ||
if (_.isNil(data)) throw new Err(`No data provided for import() in "${this.name}"`, 'EnmapImportError'); | ||
if (isNil(data)) throw new Err(`No data provided for import() in "${this.name}"`, 'EnmapImportError'); | ||
try { | ||
@@ -977,12 +990,12 @@ const parsed = JSON.parse(data); | ||
if (!type) return; | ||
if (!_.isArray(type)) type = [type]; | ||
if (!_.isNil(path)) { | ||
if (!isArray(type)) type = [type]; | ||
if (!isNil(path)) { | ||
this[_check](key, 'Object'); | ||
const data = super.get(key); | ||
if (_.isNil(_.get(data, path))) { | ||
if (isNil(_get(data, path))) { | ||
throw new Err(`The property "${path}" in key "${key}" does not exist. Please set() it or ensure() it."`, 'EnmapPathError'); | ||
} | ||
if (!type.includes(_.get(data, path).constructor.name)) { | ||
if (!type.includes(_get(data, path).constructor.name)) { | ||
throw new Err(`The property "${path}" in key "${key}" is not of type "${type.join('" or "')}" in the enmap "${this.name}" | ||
(key was of type "${_.get(data, path).constructor.name}")`, 'EnmapTypeError'); | ||
(key was of type "${_get(data, path).constructor.name}")`, 'EnmapTypeError'); | ||
} | ||
@@ -1074,4 +1087,4 @@ } else if (!type.includes(this.get(key).constructor.name)) { | ||
if (this.cloneLevel === 'none') return data; | ||
if (this.cloneLevel === 'shallow') return _.clone(data); | ||
if (this.cloneLevel === 'deep') return _.cloneDeep(data); | ||
if (this.cloneLevel === 'shallow') return clone(data); | ||
if (this.cloneLevel === 'deep') return cloneDeep(data); | ||
throw new Err('Invalid cloneLevel. What did you *do*, this shouldn\'t happen!', 'EnmapOptionsError'); | ||
@@ -1092,3 +1105,3 @@ } | ||
[_defineSetting](name, type, writable, defaultValue, value) { | ||
if (_.isNil(value)) value = defaultValue; | ||
if (isNil(value)) value = defaultValue; | ||
if (value.constructor.name !== type) { | ||
@@ -1098,3 +1111,3 @@ throw new Err(`Wrong value type provided for options.${name}: Provided "${defaultValue.constructor.name}", expecting "${type}", in enmap "${this.name}".`); | ||
Object.defineProperty(this, name, { | ||
value: !_.isNil(value) ? value : defaultValue, | ||
value: !isNil(value) ? value : defaultValue, | ||
writable, | ||
@@ -1187,6 +1200,6 @@ enumerable: false, | ||
if (typeof prop !== 'string') throw new TypeError('Key must be a string.'); | ||
if (typeof value === 'undefined') throw new Error('Value must be specified.'); | ||
if (isNil(value)) throw new Error('Value must be specified.'); | ||
const results = []; | ||
for (const item of this.values()) { | ||
if (item[prop] === value) results.push(item); | ||
if (item[prop] === value || (isObject(item) && _get(item, prop) === value)) results.push(item); | ||
} | ||
@@ -1213,5 +1226,5 @@ return results; | ||
if (typeof propOrFn === 'string') { | ||
if (typeof value === 'undefined') throw new Error('Value must be specified.'); | ||
if (isNil(value)) throw new Error('Value must be specified.'); | ||
for (const item of this.values()) { | ||
if (item[propOrFn] === value) return item; | ||
if (item[propOrFn] === value || (isObject(item) && _get(item, propOrFn) === value)) return item; | ||
} | ||
@@ -1242,5 +1255,5 @@ return null; | ||
if (typeof propOrFn === 'string') { | ||
if (typeof value === 'undefined') throw new Error('Value must be specified.'); | ||
if (isNil(value)) throw new Error('Value must be specified.'); | ||
for (const [key, val] of this) { | ||
if (val[propOrFn] === value) return key; | ||
if (val[propOrFn] === value || (isObject(val) && _get(val, propOrFn) === value)) return key; | ||
} | ||
@@ -1247,0 +1260,0 @@ return null; |
@@ -38,2 +38,3 @@ /* global describe, test, beforeEach, afterEach, expect */ | ||
}); | ||
describe('Advanced Data Types', () => { | ||
@@ -75,2 +76,46 @@ enmap = new Enmap(); | ||
describe('Advanced Data Type Methods', () => { | ||
let enmap; | ||
beforeEach(() => { | ||
enmap = new Enmap(); | ||
enmap.set('obj1', { | ||
prop: "prop", | ||
foo: "bar", | ||
sub: { | ||
value: "subvalue" | ||
} | ||
}); | ||
enmap.set('obj2', { | ||
prop: "prop", | ||
foo: "phar", | ||
sub: { | ||
value: "subvalue" | ||
} | ||
}); | ||
enmap.set('arr1', ['one', 'two', 3, 4]); | ||
}); | ||
test('can findAll using both properties and path', () => { | ||
expect(enmap.findAll('prop', 'prop').length).toBe(2); | ||
expect(enmap.findAll('sub.value', 'subvalue').length).toBe(2); | ||
}); | ||
test('can find using both properties and path', () => { | ||
expect(enmap.find('prop', 'prop')).toEqual({ | ||
prop: "prop", | ||
foo: "bar", | ||
sub: { | ||
value: "subvalue" | ||
} | ||
}); | ||
expect(enmap.find('sub.value', 'subvalue')).toEqual({ | ||
prop: "prop", | ||
foo: "bar", | ||
sub: { | ||
value: "subvalue" | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('Basic Enmap Options', () => { | ||
@@ -77,0 +122,0 @@ let enmap; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
161166
12
2441
75