Skip to content

Commit

Permalink
indexer: remove generic type from KVStore class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Jun 25, 2024
1 parent 6ac28bd commit ca10222
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/indexer/src/hooks/useKVStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useIndexerContext } from "../context";
import type { KVStore } from "../plugins/kv";

export type UseKVStoreResult<T> = InstanceType<typeof KVStore<T>>;
export type UseKVStoreResult = InstanceType<typeof KVStore>;

export function useKVStore<T>(): UseKVStoreResult<T> {
export function useKVStore(): UseKVStoreResult {
const ctx = useIndexerContext();

if (!ctx?.kv) throw new Error("KV Plugin is not available in context!");
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export async function run<TFilter, TBlock, TRet>(
} catch (error) {
assert(error instanceof Error);
await indexer.hooks.callHook("handler:exception", { error });
throw new Error(error?.message || "Some Error Occurred!");
throw error;
}

await indexer.hooks.callHook("handler:after", { output });
Expand Down
16 changes: 9 additions & 7 deletions packages/indexer/src/plugins/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import sqlite3 from "sqlite3";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { KVStore } from "./kv";

type ValueType = { data: bigint };

describe("KVStore", () => {
let db: Database<sqlite3.Database, sqlite3.Statement>;
let store: KVStore<{ data: bigint }>;
let store: KVStore;
const key = "test_key";

beforeAll(async () => {
Expand All @@ -29,14 +31,14 @@ describe("KVStore", () => {
it("should put and get a value", async () => {
const value = { data: 0n };

await store.put(key, value);
const result = await store.get(key);
await store.put<ValueType>(key, value);
const result = await store.get<ValueType>(key);

expect(result).toEqual(value);
});

it("should return undefined for non-existing key", async () => {
const result = await store.get("non_existent_key");
const result = await store.get<ValueType>("non_existent_key");
expect(result).toBeUndefined();
});

Expand All @@ -45,15 +47,15 @@ describe("KVStore", () => {

const value = { data: 50n };

await store.put(key, value);
const result = await store.get(key);
await store.put<ValueType>(key, value);
const result = await store.get<ValueType>(key);

expect(result).toEqual(value);
});

it("should delete a value", async () => {
await store.del(key);
const result = await store.get(key);
const result = await store.get<ValueType>(key);

expect(result).toBeUndefined();

Expand Down
6 changes: 3 additions & 3 deletions packages/indexer/src/plugins/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export function kv<TFilter, TBlock, TRet>(args: SqliteArgs) {
});
}

export class KVStore<T> {
export class KVStore {
constructor(
private _db: Database,
private _finality: DataFinality,
private _endCursor: Cursor,
) {}

async get(key: string): Promise<T> {
async get<T>(key: string): Promise<T> {
const row = await this._db.get<{ v: string }>(
`
SELECT v
Expand All @@ -76,7 +76,7 @@ export class KVStore<T> {
return row ? deserialize(row.v) : undefined;
}

async put(key: string, value: T) {
async put<T>(key: string, value: T) {
await this._db.run(
`
UPDATE kvs
Expand Down

0 comments on commit ca10222

Please sign in to comment.