enmap-mongo
Advanced tools
Comparing version 1.0.3 to 2.0.0
{ | ||
"name": "enmap-mongo", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A MongoDB provider for Enmap", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,9 +29,11 @@ const { MongoClient } = require('mongodb'); | ||
async init(enmap) { | ||
this.enmap = enmap; | ||
this.client = await MongoClient.connect(this.url); | ||
this.db = this.client.db(this.dbName).collection(this.name); | ||
const rows = await this.db.find({}).toArray(); | ||
for (const row of rows) { | ||
enmap.set(row._id, row.value); | ||
if (this.fetchAll) { | ||
await this.fetchEverything(); | ||
this.ready(); | ||
} else { | ||
this.ready(); | ||
} | ||
this.ready(); | ||
return this.defer; | ||
@@ -47,2 +49,13 @@ } | ||
fetch(key) { | ||
return this.db.get(key); | ||
} | ||
async fetchEverything() { | ||
const rows = await this.db.find({}).toArray(); | ||
for (const row of rows) { | ||
this.enmap.set(row._id, row.value); | ||
} | ||
} | ||
/** | ||
@@ -63,16 +76,2 @@ * Set a value to the Enmap. | ||
/** | ||
* Asynchronously ensure a write to the Enmap. | ||
* @param {(string|number)} 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. | ||
* @param {*} 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. | ||
*/ | ||
async setAsync(key, val) { | ||
if (!key || !['String', 'Number'].includes(key.constructor.name)) { | ||
throw new Error('Keys should be strings or numbers.'); | ||
} | ||
await this.db.update({ _id: key }, { _id: key, value: val }, { upsert: true }); | ||
} | ||
/** | ||
* Delete an entry from the Enmap. | ||
@@ -86,9 +85,12 @@ * @param {(string|number)} key Required. The key of the element to delete from the EnMap object. | ||
hasAsync(key) { | ||
return this.db.find({ _id: key }).limit(1); | ||
} | ||
/** | ||
* Asynchronously ensure an entry deletion from the Enmap. | ||
* @param {(string|number)} key Required. The key of the element to delete from the EnMap object. | ||
* @param {boolean} bulk Internal property used by the purge method. | ||
* Deletes all entries in the database. | ||
* @return {Promise<*>} Promise returned by the database after deletion | ||
*/ | ||
async deleteAsync(key) { | ||
await this.db.remove({ _id: key }, { single: true }); | ||
bulkDelete() { | ||
return this.db.drop(); | ||
} | ||
@@ -95,0 +97,0 @@ |
21975
6