diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts index 69eb8bed96..60cf11b8ef 100644 --- a/cli/tests/unit/kv_test.ts +++ b/cli/tests/unit/kv_test.ts @@ -1138,6 +1138,51 @@ dbTest("operation size limit", async (db) => { ); }); +dbTest("keys must be arrays", async (db) => { + await assertRejects( + // @ts-expect-error invalid type + async () => await db.get("a"), + TypeError, + ); + + await assertRejects( + // @ts-expect-error invalid type + async () => await db.getMany(["a"]), + TypeError, + ); + + await assertRejects( + // @ts-expect-error invalid type + async () => await db.set("a", 1), + TypeError, + ); + + await assertRejects( + // @ts-expect-error invalid type + async () => await db.delete("a"), + TypeError, + ); + + await assertRejects( + async () => + await db.atomic() + // @ts-expect-error invalid type + .mutate({ key: "a", type: "set", value: 1 } satisfies Deno.KvMutation) + .commit(), + TypeError, + ); + + await assertRejects( + async () => + await db.atomic() + // @ts-expect-error invalid type + .check({ key: "a", versionstamp: null }) + .set(["a"], 1) + .commit(), + TypeError, + ); +}); + // This function is never called, it is just used to check that all the types // are behaving as expected. async function _typeCheckingTests() { diff --git a/ext/kv/01_db.ts b/ext/kv/01_db.ts index 05e9a66d8f..38dcb4ae09 100644 --- a/ext/kv/01_db.ts +++ b/ext/kv/01_db.ts @@ -48,7 +48,6 @@ class Kv { } async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) { - key = convertKey(key); const [entries]: [RawKvEntry[]] = await core.opAsync( "op_kv_snapshot_read", this.#rid, @@ -76,7 +75,6 @@ class Kv { keys: Deno.KvKey[], opts?: { consistency?: Deno.KvConsistencyLevel }, ): Promise[]> { - keys = keys.map(convertKey); const ranges: RawKvEntry[][] = await core.opAsync( "op_kv_snapshot_read", this.#rid, @@ -103,7 +101,6 @@ class Kv { } async set(key: Deno.KvKey, value: unknown) { - key = convertKey(key); value = serializeValue(value); const checks: Deno.AtomicCheck[] = []; @@ -123,8 +120,6 @@ class Kv { } async delete(key: Deno.KvKey) { - key = convertKey(key); - const checks: Deno.AtomicCheck[] = []; const mutations = [ [key, "delete", null], @@ -211,14 +206,14 @@ class AtomicOperation { check(...checks: Deno.AtomicCheck[]): this { for (const check of checks) { - this.#checks.push([convertKey(check.key), check.versionstamp]); + this.#checks.push([check.key, check.versionstamp]); } return this; } mutate(...mutations: Deno.KvMutation[]): this { for (const mutation of mutations) { - const key = convertKey(mutation.key); + const key = mutation.key; let type: string; let value: RawValue | null; switch (mutation.type) { @@ -247,12 +242,12 @@ class AtomicOperation { } set(key: Deno.KvKey, value: unknown): this { - this.#mutations.push([convertKey(key), "set", serializeValue(value)]); + this.#mutations.push([key, "set", serializeValue(value)]); return this; } delete(key: Deno.KvKey): this { - this.#mutations.push([convertKey(key), "delete", null]); + this.#mutations.push([key, "delete", null]); return this; } @@ -296,14 +291,6 @@ class KvU64 { } } -function convertKey(key: Deno.KvKey | Deno.KvKeyPart): Deno.KvKey { - if (Array.isArray(key)) { - return key; - } else { - return [key as Deno.KvKeyPart]; - } -} - function deserializeValue(entry: RawKvEntry): Deno.KvEntry { const { kind, value } = entry.value; switch (kind) {