Add remoting for KeyValue model TTL feature

This commit is contained in:
Simon Ho 2016-08-29 14:41:21 -07:00
parent 32b879cf73
commit 9db0682b07
2 changed files with 55 additions and 0 deletions

View File

@ -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,

View File

@ -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);