Skip to content

Commit 88e4de5

Browse files
committed
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.
1 parent 3dfd86f commit 88e4de5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

common/models/key-value-model.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ module.exports = function(KeyValueModel) {
1616
throwNotAttached(this.modelName, 'expire');
1717
};
1818

19+
// TODO add api docs
20+
KeyValueModel.keys = function(filter, options, callback) {
21+
throwNotAttached(this.modelName, 'keys');
22+
};
23+
24+
// TODO add api docs
25+
KeyValueModel.iterateKeys = function(filter, options) {
26+
throwNotAttached(this.modelName, 'iterateKeys');
27+
};
28+
1929
KeyValueModel.setup = function() {
2030
KeyValueModel.base.setup.apply(this, arguments);
2131

@@ -51,6 +61,15 @@ module.exports = function(KeyValueModel) {
5161
],
5262
http: { path: '/:key/expire', verb: 'put' },
5363
});
64+
65+
this.remoteMethod('keys', {
66+
accepts: {
67+
arg: 'filter', type: 'object', required: false,
68+
http: { source: 'query' },
69+
},
70+
returns: { arg: 'keys', type: ['string'], root: true },
71+
http: { path: '/keys', verb: 'get' },
72+
});
5473
};
5574
};
5675

test/key-value-model.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ describe('KeyValueModel', function() {
8080
.send({ ttl: 10 })
8181
.expect(404, done);
8282
});
83+
84+
it('provides "keys(filter)" at "GET /keys"', function(done) {
85+
CacheItem.set('list-key', AN_OBJECT_VALUE, function(err) {
86+
if (err) return done(err);
87+
request.get('/CacheItems/keys')
88+
.send(AN_OBJECT_VALUE)
89+
.end(function(err, res) {
90+
if (err) return done(err);
91+
if (res.body.error) return done(res.body.error);
92+
expect(res.body).to.eql(['list-key']);
93+
done();
94+
});
95+
});
96+
});
8397
});
8498

8599
function setupAppAndCacheItem() {

0 commit comments

Comments
 (0)