Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement delete multiple keys #74

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Keyv extends EventEmitter {
}

_getKeyPrefix(key) {
if (Array.isArray(key)) {
return key.map(key => `${this.opts.namespace}:${key}`);
}
return `${this.opts.namespace}:${key}`;
}

Expand Down Expand Up @@ -90,7 +93,12 @@ class Keyv extends EventEmitter {
key = this._getKeyPrefix(key);
const store = this.opts.store;
return Promise.resolve()
.then(() => store.delete(key));
.then(() => {
if (store instanceof Map && Array.isArray(key)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't only be implemented for Maps, the main benefit of using Keyv over alternatives is that it provides a consistent interface across multiple backends.

If we implement a feature only for a single backend it would break that.

return Promise.all(key.map(key => store.delete(key)));
}
return store.delete(key);
});
}

clear() {
Expand Down
15 changes: 15 additions & 0 deletions test/keyv.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ test.serial('.set(key, val, ttl) where ttl is "0" overwrites default tll option
tk.reset();
});

test.serial('.delete(key) where key is a single key', async t => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should be in @keyv/test-suite so they cover all storage adapters, not just Keyv + Map.

const store = new Map();
const keyv = new Keyv({ store });
await keyv.set('foo', 'bar');
t.is(await keyv.delete('foo'), true);
});

test.serial('.delete(key) where key is array of keys', async t => {
const store = new Map();
const keyv = new Keyv({ store });
await keyv.set('foo', 'bar');
await keyv.set('fizz', 'buzz');
t.deepEqual(await keyv.delete(['foo', 'fizz']), [true, true]);
});

test.serial('Keyv uses custom serializer when provided instead of json-buffer', async t => {
t.plan(3);
const store = new Map();
Expand Down