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

Unify kv:key list output in local vs remote case #3720

Merged
merged 2 commits into from
Aug 9, 2023
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
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
Loading