From 32b879cf730da9b1722784597b3a02e03978d211 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Mon, 29 Aug 2016 14:41:02 -0700 Subject: [PATCH 1/2] Add lint NPM script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 88eeb28e..5809cd23 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "mBaaS" ], "scripts": { + "lint": "grunt eslint", "test": "grunt mocha-and-karma" }, "dependencies": { From 9db0682b0758818f1c2eec348b697ddd218229c9 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Mon, 29 Aug 2016 14:41:21 -0700 Subject: [PATCH 2/2] Add remoting for KeyValue model TTL feature --- common/models/key-value-model.js | 14 +++++++++++ test/key-value-model.test.js | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/common/models/key-value-model.js b/common/models/key-value-model.js index c7fbbbe3..9fffc5e5 100644 --- a/common/models/key-value-model.js +++ b/common/models/key-value-model.js @@ -16,6 +16,11 @@ module.exports = function(KeyValueModel) { throwNotAttached(this.modelName, 'expire'); }; + // TODO add api docs + KeyValueModel.ttl = function(key, options, callback) { + throwNotAttached(this.modelName, 'ttl'); + }; + // TODO add api docs KeyValueModel.keys = function(filter, options, callback) { throwNotAttached(this.modelName, 'keys'); @@ -62,6 +67,15 @@ module.exports = function(KeyValueModel) { http: { path: '/:key/expire', verb: 'put' }, }); + this.remoteMethod('ttl', { + accepts: { + arg: 'key', type: 'string', required: true, + http: { source: 'path' }, + }, + returns: { arg: 'value', type: 'any', root: true }, + http: { path: '/:key/ttl', verb: 'get' }, + }); + this.remoteMethod('keys', { accepts: { arg: 'filter', type: 'object', required: false, diff --git a/test/key-value-model.test.js b/test/key-value-model.test.js index 6ca2e1e2..26e0aa1e 100644 --- a/test/key-value-model.test.js +++ b/test/key-value-model.test.js @@ -81,6 +81,47 @@ describe('KeyValueModel', function() { .expect(404, done); }); + it('provides "ttl(key)" at "GET /key/ttl"', function(done) { + request.put('/CacheItems/ttl-key?ttl=2000') + .end(function(err, res) { + if (err) return done(err); + request.get('/CacheItems/ttl-key/ttl') + .end(function(err, res) { + if (err) return done(err); + expect(res.body).to.be.number; + done(); + }); + }); + }); + + it('returns 204 when getting TTL for a key that does not have TTL set', + function(done) { + request.put('/CacheItems/ttl-key') + .end(function(err, res) { + if (err) return done(err); + request.get('/CacheItems/ttl-key/ttl') + .expect(204, done); + }); + }); + + it('returns 404 when getting TTL for a key when TTL has expired', + function(done) { + request.put('/CacheItems/ttl-key?ttl=10') + .end(function(err, res) { + setTimeout(function() { + if (err) return done(err); + request.get('/CacheItems/ttl-key/ttl') + .expect(404, done); + }, 20); + }); + }); + + it('returns 404 when getting TTL for a key that does not exist', + function(done) { + request.get('/CacheItems/key-does-not-exist/ttl') + .expect(404, done); + }); + it('provides "keys(filter)" at "GET /keys"', function(done) { CacheItem.set('list-key', AN_OBJECT_VALUE, function(err) { if (err) return done(err);