Skip to content

Commit

Permalink
Unify kv:key list output in local vs remote case (#3720)
Browse files Browse the repository at this point in the history
* Unify `kv:key list` output in local vs remote case
Fix #3715, Fix #3716

* Add tests
  • Loading branch information
JacksonKearl authored and lrapoport-cf committed Aug 11, 2023
1 parent c6564b7 commit 9deb085
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 88 deletions.
203 changes: 118 additions & 85 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1731,33 +1731,95 @@ describe("wrangler", () => {
});

it("should list local kv storage", async () => {
await runWrangler(`kv:key list --namespace-id some-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`"[]"`);
const keyValues = [
{
key: "a",
value: "value",
},
{
key: "a/b",
value: "value",
},
{
key: "a/c",
value: "value",
},
{
key: "b",
value: "value",
},
];
writeFileSync("./keys.json", JSON.stringify(keyValues));
await runWrangler(
`kv:bulk put keys.json --namespace-id some-namespace-id --local`
);

await runWrangler(`kv:key list --namespace-id some-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}"
"[]
Success!
[
{
\\"name\\": \\"a\\"
},
{
\\"name\\": \\"a/b\\"
},
{
\\"name\\": \\"a/c\\"
},
{
\\"name\\": \\"b\\"
}
]"
`);

await runWrangler(
`kv:key put val value --namespace-id some-namespace-id --local`
`kv:key list --namespace-id some-namespace-id --local --prefix a`
);
await runWrangler(
`kv:key list --namespace-id some-namespace-id --local --prefix a/b`
);
await runWrangler(
`kv:key list --namespace-id some-namespace-id --local --prefix abc`
);

await runWrangler(`kv:key list --namespace-id some-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}
Writing the value \\"value\\" to key \\"val\\" on namespace some-namespace-id.
{
\\"keys\\": [
{
\\"name\\": \\"val\\"
}
],
\\"list_complete\\": true
}"
"[]
Success!
[
{
\\"name\\": \\"a\\"
},
{
\\"name\\": \\"a/b\\"
},
{
\\"name\\": \\"a/c\\"
},
{
\\"name\\": \\"b\\"
}
]
[
{
\\"name\\": \\"a\\"
},
{
\\"name\\": \\"a/b\\"
},
{
\\"name\\": \\"a/c\\"
}
]
[
{
\\"name\\": \\"a/b\\"
}
]
[]"
`);
});

Expand Down Expand Up @@ -1794,12 +1856,7 @@ describe("wrangler", () => {

it("should put local bulk kv storage", async () => {
await runWrangler(`kv:key list --namespace-id bulk-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}"
`);
expect(std.out).toMatchInlineSnapshot(`"[]"`);

const keyValues = [
{
Expand All @@ -1816,44 +1873,32 @@ describe("wrangler", () => {
`kv:bulk put keys.json --namespace-id bulk-namespace-id --local`
);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}
"[]
Success!"
`);

await runWrangler(
`kv:key get test --namespace-id bulk-namespace-id --local --text`
);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}
"[]
Success!
value"
`);

await runWrangler(`kv:key list --namespace-id bulk-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"keys\\": [],
\\"list_complete\\": true
}
"[]
Success!
value
{
\\"keys\\": [
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
],
\\"list_complete\\": true
}"
[
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
]"
`);
});

Expand All @@ -1875,17 +1920,14 @@ describe("wrangler", () => {
await runWrangler(`kv:key list --namespace-id bulk-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"Success!
{
\\"keys\\": [
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
],
\\"list_complete\\": true
}"
[
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
]"
`);
const keys = ["hello", "test"];
writeFileSync("./keys.json", JSON.stringify(keys));
Expand All @@ -1894,39 +1936,30 @@ describe("wrangler", () => {
);
expect(std.out).toMatchInlineSnapshot(`
"Success!
{
\\"keys\\": [
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
],
\\"list_complete\\": true
}
[
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
]
Success!"
`);

await runWrangler(`kv:key list --namespace-id bulk-namespace-id --local`);
expect(std.out).toMatchInlineSnapshot(`
"Success!
{
\\"keys\\": [
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
],
\\"list_complete\\": true
}
[
{
\\"name\\": \\"hello\\"
},
{
\\"name\\": \\"test\\"
}
]
Success!
{
\\"keys\\": [],
\\"list_complete\\": true
}"
[]"
`);
});

Expand Down
11 changes: 8 additions & 3 deletions packages/wrangler/src/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from "./helpers";
import type { EventNames } from "../metrics";
import type { CommonYargsArgv } from "../yargs-types";
import type { KeyValue } from "./helpers";
import type { KeyValue, NamespaceKeyInfo } from "./helpers";

export function kvNamespace(kvYargs: CommonYargsArgv) {
return kvYargs
Expand Down Expand Up @@ -340,15 +340,20 @@ export const kvKey = (kvYargs: CommonYargsArgv) => {
const config = readConfig(args.config, args);
const namespaceId = getKVNamespaceId(args, config);

let result;
let result: NamespaceKeyInfo[];
let metricEvent: EventNames;
if (args.local) {
const kvGateway = localGateway(
args.persistTo,
config.configPath,
namespaceId
);
result = await kvGateway.list();
result = (await kvGateway.list({ prefix })).keys.map((key) => ({
name: key.name,
expiration: key.expiration,
metadata:
key.metadata === undefined ? undefined : JSON.parse(key.metadata),
}));
metricEvent = "list kv keys (local)";
} else {
const accountId = await requireAuth(config);
Expand Down

0 comments on commit 9deb085

Please sign in to comment.