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

feat: implement KeyValueStore.recordExists #60

Merged
merged 1 commit into from
Feb 16, 2024
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
16 changes: 16 additions & 0 deletions src/resource_clients/key_value_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ export class KeyValueStoreClient {
};
}

/**
* Tests whether a record with the given key exists in the key-value store without retrieving its value.
* @param key The queried record key.
* @returns `true` if the record exists, `false` otherwise.
*/
async recordExists(key: string): Promise<boolean> {
ow(key, ow.string);
try {
const result = await this._handleFile(key, stat);
return !!result;
} catch (err) {
if (err.code === 'ENOENT') return false;
throw new Error(`Error checking file '${key}' in directory '${this.storeDir}'.\nCause: ${err.message}`);
}
}

async getRecord(key: string, options: KeyValueStoreClientGetRecordOptions = {}): Promise<KeyValueStoreRecord | undefined> {
ow(key, ow.string);
ow(options, ow.object.exactShape({
Expand Down
21 changes: 21 additions & 0 deletions test/key_value_stores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,27 @@ describe('setRecord', () => {
});
});

describe('recordExists', () => {
const storeName = 'first';
const startCount = TEST_STORES[1].recordCount;

test('retruns true for existing records', async () => {
let savedRecord = numToRecord(3);
let record = await storageLocal.keyValueStore(storeName).recordExists(savedRecord.key);
expect(record).toBeTruthy();

savedRecord = numToRecord(30);
record = await storageLocal.keyValueStore('second').recordExists(savedRecord.key);
expect(record).toBeTruthy();
});

test('returns false for non-existent records', async () => {
const savedRecord = numToRecord(startCount + 1);
const record = await storageLocal.keyValueStore('first').recordExists(savedRecord.key);
expect(record).toBeFalsy();
});
});

describe('getRecord', () => {
const storeName = 'first';
const startCount = TEST_STORES[1].recordCount;
Expand Down
Loading