Skip to content

Commit 8471b95

Browse files
authored
sqlite - chore: testing iterator with useKeyPrefix and documentation (#1466)
1 parent 3605ff8 commit 8471b95

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

packages/keyv/README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
5353
- [.deleteMany(keys)](#deletemanykeys)
5454
- [.clear()](#clear)
5555
- [.iterator()](#iterator)
56-
- [API - Properties](#api---properties)
57-
- [.namespace](#namespace-1)
58-
- [.ttl](#ttl-1)
59-
- [.store](#store-1)
60-
- [.serialize](#serialize-1)
61-
- [.deserialize](#deserialize-1)
62-
- [.compression](#compression-1)
63-
- [.useKeyPrefix](#usekeyprefix-1)
6456
- [How to Contribute](#how-to-contribute)
6557
- [License](#license)
6658

@@ -623,6 +615,22 @@ keyv.useKeyPrefix = true;
623615
console.log(keyv.useKeyPrefix); // true
624616
```
625617

618+
With many of the storage adapters you will also need to set the `namespace` option to `undefined` to have it work correctly. This is because in `v5` we started the transition to having the storage adapter handle the namespacing and `Keyv` will no longer handle it internally via KeyPrefixing. Here is an example of doing ith with `KeyvSqlite`:
619+
620+
```js
621+
import Keyv from 'keyv';
622+
import KeyvSqlite from '@keyv/sqlite';
623+
624+
const store = new KeyvSqlite('sqlite://path/to/database.sqlite');
625+
const keyv = new Keyv({ store });
626+
keyv.useKeyPrefix = false; // disable key prefixing
627+
store.namespace = undefined; // disable namespacing in the storage adapter
628+
629+
await keyv.set('foo', 'bar'); // true
630+
await keyv.get('foo'); // 'bar'
631+
await keyv.clear();
632+
```
633+
626634
# How to Contribute
627635

628636
We welcome contributions to Keyv! 🎉 Here are some guides to get you started with contributing:

packages/sqlite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"keyv": "workspace:^"
6767
},
6868
"devDependencies": {
69+
"@faker-js/faker": "^9.8.0",
6970
"@keyv/test-suite": "workspace:^",
7071
"@vitest/coverage-v8": "^3.2.4",
7172
"rimraf": "^6.0.1",

packages/sqlite/test/iterator.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as test from 'vitest';
2+
import {faker} from '@faker-js/faker';
3+
import Keyv from 'keyv';
4+
import KeyvSqlite from '../src/index.js';
5+
6+
test.it('Async Iterator with Keyv and useKeyPrefix true', async t => {
7+
const store = new KeyvSqlite({uri: 'sqlite://test/testdb2.sqlite', busyTimeout: 3000});
8+
const keyv = new Keyv({store, useKeyPrefix: true});
9+
await keyv.clear();
10+
// Test with Keyv instance
11+
const keyvData = {
12+
key: faker.string.alphanumeric(10),
13+
value: faker.string.alphanumeric(10),
14+
};
15+
await keyv.set(keyvData.key, keyvData.value);
16+
const keyvResult = await keyv.get(keyvData.key);
17+
t.expect(keyvResult).toBe(keyvData.value);
18+
// Ensure the Keyv instance can still use the iterator
19+
t.expect(keyv.iterator).toBeDefined();
20+
if (typeof keyv.iterator === 'function') {
21+
const keyvIterator = keyv.iterator({});
22+
let keyvDataFound = false;
23+
for await (const [key, raw] of keyvIterator) {
24+
t.expect(key).toBe(keyvData.key);
25+
t.expect(raw).toBe(keyvData.value);
26+
keyvDataFound = true;
27+
}
28+
29+
if (!keyvDataFound) {
30+
t.expect.fail('Keyv iterator did not find the expected data');
31+
}
32+
} else {
33+
t.expect.fail('Keyv iterator is not a function');
34+
}
35+
});
36+
37+
test.it('Async Iterator with Keyv and useKeyPrefix false', async t => {
38+
const store = new KeyvSqlite({uri: 'sqlite://test/testdb2.sqlite', busyTimeout: 3000});
39+
const keyv = new Keyv({store});
40+
keyv.namespace = undefined;
41+
keyv.useKeyPrefix = false;
42+
await keyv.clear();
43+
// Test with Keyv instance
44+
const keyvData = {
45+
key: faker.string.alphanumeric(10),
46+
value: faker.string.alphanumeric(10),
47+
};
48+
await keyv.set(keyvData.key, keyvData.value);
49+
const keyvResult = await keyv.get(keyvData.key);
50+
t.expect(keyvResult).toBe(keyvData.value);
51+
// Ensure the Keyv instance can still use the iterator
52+
t.expect(keyv.iterator).toBeDefined();
53+
if (typeof keyv.iterator === 'function') {
54+
const keyvIterator = keyv.iterator({});
55+
let keyvDataFound = false;
56+
for await (const [key, raw] of keyvIterator) {
57+
t.expect(key).toBe(keyvData.key);
58+
t.expect(raw).toBe(keyvData.value);
59+
keyvDataFound = true;
60+
}
61+
62+
if (!keyvDataFound) {
63+
t.expect.fail('Keyv iterator did not find the expected data');
64+
}
65+
} else {
66+
t.expect.fail('Keyv iterator is not a function');
67+
}
68+
});

packages/sqlite/vitest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export default defineConfig({
66
coverage: {
77
reporter: ['json', 'lcov', 'text'],
88
reportOnFailure: true,
9+
exclude: [
10+
'dist',
11+
'src/types.ts',
12+
'vitest.config.ts',
13+
],
914
},
1015
},
1116
});

0 commit comments

Comments
 (0)