Comparing version 4.7.1 to 4.8.0
{ | ||
"name": "enmap", | ||
"version": "4.7.1", | ||
"version": "4.8.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", |
@@ -11,2 +11,5 @@ // Lodash should probably be a core lib but hey, it's useful! | ||
// Package.json | ||
const pkgdata = require('../package.json'); | ||
// Symbols are used to create "private" methods. | ||
@@ -100,3 +103,3 @@ // https://medium.com/front-end-hacking/private-methods-in-es6-and-writing-your-own-db-b2e30866521f | ||
// This is completely ignored in all situations except destroying the enmap. | ||
this[_defineSetting]('isDestroyed', 'Boolean', false, false); | ||
this[_defineSetting]('isDestroyed', 'Boolean', true, false); | ||
@@ -791,2 +794,43 @@ // Define the data directory where the enmap is stored. | ||
/** | ||
* Exports the enmap data to a JSON file. | ||
* **__WARNING__**: Does not work on memory enmaps containing complex data! | ||
* @returns {String} The enmap data in a stringified JSON format. | ||
*/ | ||
export() { | ||
this[_readyCheck](); | ||
if (this.persistent) this.fetchEverything(); | ||
return JSON.stringify({ | ||
name: this.name, | ||
version: pkgdata.version, | ||
exportDate: Date.now(), | ||
keys: this.map((value, key) => ({ key, value })) | ||
}, null, 2); | ||
} | ||
/** | ||
* | ||
* @param {string} data The data to import to Enmap. Must contain all the required fields provided by export() | ||
* @param {boolean} overwrite Defaults to `true`. Whether to overwrite existing key/value data with incoming imported data | ||
* @param {boolean} clear Defaults to `false`. Whether to clear the enmap of all data before importing | ||
* (**__WARNING__**: Any exiting data will be lost! This cannot be undone.) | ||
* @returns {Enmap} The enmap with the new data. | ||
*/ | ||
import(data, overwrite = true, clear = false) { | ||
this[_readyCheck](); | ||
if (clear) this.deleteAll(); | ||
if (_.isNil(data)) throw new Err(`No data provided for import() in "${this.name}"`, 'EnmapImportError'); | ||
try { | ||
const parsed = JSON.parse(data); | ||
for (const thisEntry of parsed) { | ||
const { key, value } = thisEntry; | ||
if (!overwrite && this.has(key)) continue; | ||
this.set(key, value); | ||
} | ||
} catch (err) { | ||
throw new Err(`Data provided for import() in "${this.name}" is invalid JSON. Stacktrace:\n${err}`, 'EnmapImportError'); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Initialize multiple Enmaps easily. | ||
@@ -793,0 +837,0 @@ * @param {Array<string>} names Array of strings. Each array entry will create a separate enmap with that name. |
158281
2368