UNPKG

heapstash

Version:

HeapStash is a library that allows for easy caching in Node.js, with many advanced features such as TTL, maximum items in memory cache, external cache support, and more.

38 lines 1.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const index_1 = __importDefault(require("./index")); module.exports = async (settings) => { const mongo = new index_1.default(); const collection = settings.client.db(settings.db).collection(settings.collection); await collection.createIndex({ "expireAt": 1 }, { "expireAfterSeconds": 0 }); mongo.tasks.get = async (id) => { const result = await collection.findOne({ id }); if (!result) { throw new Error(""); } else { return result; } }; mongo.tasks.put = async (id, data) => { const updateObject = { "$set": data }; if (data.ttl) { updateObject["$set"].expireAt = new Date(data.ttl); delete data.ttl; } function set(id) { return collection.updateOne({ id }, updateObject, { "upsert": true }); } await Promise.all((Array.isArray(id) ? id : [id]).map(set)); }; mongo.tasks.remove = async (id) => { await collection.deleteOne({ id }); }; mongo.tasks.clear = async () => { await collection.deleteMany({}); }; return mongo; }; //# sourceMappingURL=MongoDB.js.map