abstract-cache-mongo
Advanced tools
Comparing version 1.0.0 to 1.0.1
29
index.js
@@ -5,7 +5,16 @@ 'use strict' | ||
function mapKey (inputKey, defaultSegment) { | ||
if (typeof inputKey === 'string') return {key: inputKey, segment: defaultSegment} | ||
return { | ||
key: inputKey.id, | ||
segment: inputKey.segment | ||
} | ||
} | ||
const proto = { | ||
delete: function (key) { | ||
const keyObj = mapKey(key, this._segment) | ||
return this.mongo.db | ||
.collection(this._segment) | ||
.deleteOne({key}) | ||
.collection(keyObj.segment) | ||
.deleteOne({key: keyObj.key}) | ||
.then((result) => { | ||
@@ -17,5 +26,6 @@ return result.deletedCount === 1 | ||
get: function (key) { | ||
const keyObj = mapKey(key, this._segment) | ||
return this.mongo.db | ||
.collection(this._segment) | ||
.findOne({key}) | ||
.collection(keyObj.segment) | ||
.findOne({key: keyObj.key}) | ||
.then((doc) => { | ||
@@ -26,3 +36,3 @@ if (!doc) return null | ||
const ttl = expires - now | ||
if (ttl < 1) { | ||
if (expires < now) { | ||
return this.delete(key) | ||
@@ -38,4 +48,4 @@ .then((result) => { | ||
return { | ||
item: doc.item, | ||
stored: doc.stored, | ||
item: doc.payload.item, | ||
stored: doc.payload.stored, | ||
ttl | ||
@@ -58,4 +68,5 @@ } | ||
set: function (key, value, ttl) { | ||
const keyObj = mapKey(key, this._segment) | ||
const doc = { | ||
key: key, | ||
key: keyObj.key, | ||
payload: { | ||
@@ -68,3 +79,3 @@ item: value, | ||
return this.mongo.db | ||
.collection(this._segment) | ||
.collection(keyObj.segment) | ||
.insertOne(doc) | ||
@@ -71,0 +82,0 @@ }, |
{ | ||
"name": "abstract-cache-mongo", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An abstract-cache compliant client with a MongoDB backing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -103,1 +103,20 @@ 'use strict' | ||
}) | ||
test('supports object keys', (t) => { | ||
t.plan(4) | ||
const key = {id: 'foo', segment: 'foobar'} | ||
const client = factory() | ||
client.start() | ||
.then(() => client.set(key, 'foo', 10000)) | ||
.then(() => client.has(key)) | ||
.then((result) => t.is(result, true)) | ||
.then(() => client.get(key)) | ||
.then((cached) => { | ||
t.type(cached, Object) | ||
t.ok(cached.item) | ||
t.is(cached.item, 'foo') | ||
}) | ||
.then(() => client.delete(key)) | ||
.then(() => client.stop()) | ||
.catch(t.threw) | ||
}) |
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
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
8931
228