From b221af7cf66a0b3110d61c0b6052b1ab6bc5ad99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 16 Aug 2016 14:56:15 +0200 Subject: [PATCH] KeyValueModel: add API for listing keys - Expose "keys()" at "GET /keys" - Add a dummy implementation for "iterateKeys" to serve a useful error message when the model is not attached correctly. --- common/models/key-value-model.js | 19 +++++++++++++++++++ test/key-value-model.test.js | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/common/models/key-value-model.js b/common/models/key-value-model.js index 200465ca..c7fbbbe3 100644 --- a/common/models/key-value-model.js +++ b/common/models/key-value-model.js @@ -16,6 +16,16 @@ module.exports = function(KeyValueModel) { throwNotAttached(this.modelName, 'expire'); }; + // TODO add api docs + KeyValueModel.keys = function(filter, options, callback) { + throwNotAttached(this.modelName, 'keys'); + }; + + // TODO add api docs + KeyValueModel.iterateKeys = function(filter, options) { + throwNotAttached(this.modelName, 'iterateKeys'); + }; + KeyValueModel.setup = function() { KeyValueModel.base.setup.apply(this, arguments); @@ -51,6 +61,15 @@ module.exports = function(KeyValueModel) { ], http: { path: '/:key/expire', verb: 'put' }, }); + + this.remoteMethod('keys', { + accepts: { + arg: 'filter', type: 'object', required: false, + http: { source: 'query' }, + }, + returns: { arg: 'keys', type: ['string'], root: true }, + http: { path: '/keys', verb: 'get' }, + }); }; }; diff --git a/test/key-value-model.test.js b/test/key-value-model.test.js index e81ed63c..6ca2e1e2 100644 --- a/test/key-value-model.test.js +++ b/test/key-value-model.test.js @@ -80,6 +80,20 @@ describe('KeyValueModel', function() { .send({ ttl: 10 }) .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); + request.get('/CacheItems/keys') + .send(AN_OBJECT_VALUE) + .end(function(err, res) { + if (err) return done(err); + if (res.body.error) return done(res.body.error); + expect(res.body).to.eql(['list-key']); + done(); + }); + }); + }); }); function setupAppAndCacheItem() {