Skip to content

Commit 210e77a

Browse files
committed
initial
0 parents  commit 210e77a

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.PHONY: test
2+
3+
default: test
4+
5+
test:
6+
./node_modules/.bin/mocha

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "cache-manager-mongoose",
3+
"version": "0.0.1",
4+
"description": "mongoose store for node-cache-manager",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"cache-manager": "^1.4.0",
13+
"chai": "^3.5.0",
14+
"mocha": "^2.4.5",
15+
"mongoose-mock": "^0.4.0",
16+
"proxyquire": "^1.7.4",
17+
"sinon": "^1.17.3"
18+
},
19+
"dependencies": {
20+
"mongoose": "^4.4.4"
21+
}
22+
}

src/index.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"use strict";
2+
3+
const mongoose = require("mongoose");
4+
5+
6+
const schemaTemplate = {
7+
obj: {
8+
_id: String,
9+
val: mongoose.Schema.Types.Mixed,
10+
exp: Number
11+
},
12+
options: {
13+
collection: "cacheman",
14+
versionKey: false
15+
}
16+
};
17+
18+
const EMPTY = {};
19+
20+
class MongooseStoreError extends Error {};
21+
22+
class MongooseStore {
23+
constructor(args) {
24+
if (args.model) {
25+
switch (typeof args.model) {
26+
case "object":
27+
this.model = args.model;
28+
break;
29+
case "string":
30+
this.model = mongoose.model(args.model);
31+
break;
32+
default:
33+
throw new MongooseStoreError("unexpected type of args.model in constructor");
34+
}
35+
} else {
36+
this.model = this.makeModel(args);
37+
}
38+
this.ttl = args.ttl || 60;
39+
}
40+
41+
makeModel(args) {
42+
const options = Object.assign({}, schemaTemplate.options, args.modelOptions);
43+
const schema = new mongoose.Schema(
44+
schemaTemplate.obj,
45+
options
46+
);
47+
return mongoose.model(args.model, schema);
48+
}
49+
50+
result(fn, error, result) {
51+
if (fn) {
52+
fn(error, result);
53+
}
54+
}
55+
56+
get(key, options, fn) {
57+
return this.model.findOne({_id: key})
58+
}
59+
60+
set(key, val, options, fn) {
61+
try {
62+
options = options || {};
63+
let ttl = options.ttl || this.ttl;
64+
65+
return this.model.update(
66+
{_id: key},
67+
{
68+
val: val,
69+
exp: Date.now() + ttl * 1000
70+
},
71+
{upsert: true}
72+
).then(() => {
73+
this.result(fn);
74+
}).catch(e => {
75+
this.result(fn, e)
76+
});
77+
} catch (e) {
78+
this.result(fn, e)
79+
}
80+
}
81+
82+
del(key, options, fn) {
83+
84+
}
85+
86+
reset(key, fn) {
87+
}
88+
}
89+
90+
module.exports = {
91+
create : function (args) {
92+
return new MongooseStore(args);
93+
}
94+
};

test/indexSpec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
let mongoose = require("mongoose-mock"),
4+
proxyquire = require("proxyquire"),
5+
cmm = proxyquire("../src/index", {mongoose: mongoose}),
6+
cm = require("cache-manager"),
7+
sinon = require("sinon"),
8+
expect = require("chai").expect;
9+
10+
describe("cache-manager-mongoose", function() {
11+
let modelStub = {
12+
testProp: true,
13+
update: () => Promise.resolve(),
14+
findOne: () => Promise.resolve(),
15+
remove: () => Promise.resolve()
16+
};
17+
18+
it("accepts model passed directly", function() {
19+
let cache = cm.caching({
20+
store: cmm,
21+
model: modelStub
22+
});
23+
expect(cache.store.model.testProp).to.be.true;
24+
});
25+
26+
it("finds model if passed as string", sinon.test(function() {
27+
this.stub(mongoose, "model").returns({dummyField: true});
28+
let cache = cm.caching({
29+
store: cmm,
30+
model: "dummyModel"
31+
});
32+
sinon.assert.calledOnce(mongoose.model);
33+
expect(cache.store.model.dummyField).to.be.true;
34+
}));
35+
36+
it("creates new model if model not provided", function() {
37+
let cache = cm.caching({
38+
store: cmm
39+
});
40+
expect(cache.store.model).not.to.be.undefined;
41+
});
42+
43+
it("set saves key, value, expires record", sinon.test(function(done) {
44+
let cache = cm.caching({
45+
store: cmm,
46+
model: modelStub
47+
});
48+
cache.set("someKey", "someValue", null, function (err, result) {
49+
done();
50+
});
51+
let updateSpy = sinon.stub(modelStub, "update");
52+
sinon.assert.calledOnce(updateSpy);
53+
}));
54+
});

0 commit comments

Comments
 (0)