Skip to content

sqlite - chore: testing iterator with useKeyPrefix and documentation #1466

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

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/keyv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
- [.deleteMany(keys)](#deletemanykeys)
- [.clear()](#clear)
- [.iterator()](#iterator)
- [API - Properties](#api---properties)
- [.namespace](#namespace-1)
- [.ttl](#ttl-1)
- [.store](#store-1)
- [.serialize](#serialize-1)
- [.deserialize](#deserialize-1)
- [.compression](#compression-1)
- [.useKeyPrefix](#usekeyprefix-1)
- [How to Contribute](#how-to-contribute)
- [License](#license)

Expand Down Expand Up @@ -623,6 +615,22 @@ keyv.useKeyPrefix = true;
console.log(keyv.useKeyPrefix); // true
```

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`:

```js
import Keyv from 'keyv';
import KeyvSqlite from '@keyv/sqlite';

const store = new KeyvSqlite('sqlite://path/to/database.sqlite');
const keyv = new Keyv({ store });
keyv.useKeyPrefix = false; // disable key prefixing
store.namespace = undefined; // disable namespacing in the storage adapter

await keyv.set('foo', 'bar'); // true
await keyv.get('foo'); // 'bar'
await keyv.clear();
```

# How to Contribute

We welcome contributions to Keyv! 🎉 Here are some guides to get you started with contributing:
Expand Down
1 change: 1 addition & 0 deletions packages/sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"keyv": "workspace:^"
},
"devDependencies": {
"@faker-js/faker": "^9.8.0",
"@keyv/test-suite": "workspace:^",
"@vitest/coverage-v8": "^3.2.4",
"rimraf": "^6.0.1",
Expand Down
68 changes: 68 additions & 0 deletions packages/sqlite/test/iterator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as test from 'vitest';
import {faker} from '@faker-js/faker';
import Keyv from 'keyv';
import KeyvSqlite from '../src/index.js';

test.it('Async Iterator with Keyv and useKeyPrefix true', async t => {
const store = new KeyvSqlite({uri: 'sqlite://test/testdb2.sqlite', busyTimeout: 3000});
const keyv = new Keyv({store, useKeyPrefix: true});
await keyv.clear();
// Test with Keyv instance
const keyvData = {
key: faker.string.alphanumeric(10),
value: faker.string.alphanumeric(10),
};
await keyv.set(keyvData.key, keyvData.value);
const keyvResult = await keyv.get(keyvData.key);
t.expect(keyvResult).toBe(keyvData.value);
// Ensure the Keyv instance can still use the iterator
t.expect(keyv.iterator).toBeDefined();
if (typeof keyv.iterator === 'function') {
const keyvIterator = keyv.iterator({});
let keyvDataFound = false;
for await (const [key, raw] of keyvIterator) {
t.expect(key).toBe(keyvData.key);
t.expect(raw).toBe(keyvData.value);
keyvDataFound = true;
}

if (!keyvDataFound) {
t.expect.fail('Keyv iterator did not find the expected data');
}
} else {
t.expect.fail('Keyv iterator is not a function');
}
});

test.it('Async Iterator with Keyv and useKeyPrefix false', async t => {
const store = new KeyvSqlite({uri: 'sqlite://test/testdb2.sqlite', busyTimeout: 3000});
const keyv = new Keyv({store});
keyv.namespace = undefined;
keyv.useKeyPrefix = false;
await keyv.clear();
// Test with Keyv instance
const keyvData = {
key: faker.string.alphanumeric(10),
value: faker.string.alphanumeric(10),
};
await keyv.set(keyvData.key, keyvData.value);
const keyvResult = await keyv.get(keyvData.key);
t.expect(keyvResult).toBe(keyvData.value);
// Ensure the Keyv instance can still use the iterator
t.expect(keyv.iterator).toBeDefined();
if (typeof keyv.iterator === 'function') {
const keyvIterator = keyv.iterator({});
let keyvDataFound = false;
for await (const [key, raw] of keyvIterator) {
t.expect(key).toBe(keyvData.key);
t.expect(raw).toBe(keyvData.value);
keyvDataFound = true;
}

if (!keyvDataFound) {
t.expect.fail('Keyv iterator did not find the expected data');
}
} else {
t.expect.fail('Keyv iterator is not a function');
}
});
5 changes: 5 additions & 0 deletions packages/sqlite/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export default defineConfig({
coverage: {
reporter: ['json', 'lcov', 'text'],
reportOnFailure: true,
exclude: [
'dist',
'src/types.ts',
'vitest.config.ts',
],
},
},
});
Loading