From ca10222cccc31a82da540b30afffa2fde284db75 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 25 Jun 2024 22:33:43 +0530 Subject: [PATCH] indexer: remove generic type from KVStore class --- packages/indexer/src/hooks/useKVStore.ts | 4 ++-- packages/indexer/src/indexer.ts | 2 +- packages/indexer/src/plugins/kv.test.ts | 16 +++++++++------- packages/indexer/src/plugins/kv.ts | 6 +++--- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/indexer/src/hooks/useKVStore.ts b/packages/indexer/src/hooks/useKVStore.ts index 7642a6b..e8c473d 100644 --- a/packages/indexer/src/hooks/useKVStore.ts +++ b/packages/indexer/src/hooks/useKVStore.ts @@ -1,9 +1,9 @@ import { useIndexerContext } from "../context"; import type { KVStore } from "../plugins/kv"; -export type UseKVStoreResult = InstanceType>; +export type UseKVStoreResult = InstanceType; -export function useKVStore(): UseKVStoreResult { +export function useKVStore(): UseKVStoreResult { const ctx = useIndexerContext(); if (!ctx?.kv) throw new Error("KV Plugin is not available in context!"); diff --git a/packages/indexer/src/indexer.ts b/packages/indexer/src/indexer.ts index d252d8c..ccbb6f3 100644 --- a/packages/indexer/src/indexer.ts +++ b/packages/indexer/src/indexer.ts @@ -177,7 +177,7 @@ export async function run( } 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 }); diff --git a/packages/indexer/src/plugins/kv.test.ts b/packages/indexer/src/plugins/kv.test.ts index 8c35525..0745831 100644 --- a/packages/indexer/src/plugins/kv.test.ts +++ b/packages/indexer/src/plugins/kv.test.ts @@ -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; - let store: KVStore<{ data: bigint }>; + let store: KVStore; const key = "test_key"; beforeAll(async () => { @@ -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(key, value); + const result = await store.get(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("non_existent_key"); expect(result).toBeUndefined(); }); @@ -45,15 +47,15 @@ describe("KVStore", () => { const value = { data: 50n }; - await store.put(key, value); - const result = await store.get(key); + await store.put(key, value); + const result = await store.get(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(key); expect(result).toBeUndefined(); diff --git a/packages/indexer/src/plugins/kv.ts b/packages/indexer/src/plugins/kv.ts index 293d790..93139e8 100644 --- a/packages/indexer/src/plugins/kv.ts +++ b/packages/indexer/src/plugins/kv.ts @@ -56,14 +56,14 @@ export function kv(args: SqliteArgs) { }); } -export class KVStore { +export class KVStore { constructor( private _db: Database, private _finality: DataFinality, private _endCursor: Cursor, ) {} - async get(key: string): Promise { + async get(key: string): Promise { const row = await this._db.get<{ v: string }>( ` SELECT v @@ -76,7 +76,7 @@ export class KVStore { return row ? deserialize(row.v) : undefined; } - async put(key: string, value: T) { + async put(key: string, value: T) { await this._db.run( ` UPDATE kvs