tigawana
tigawana7mo ago

List kv items using a partial key

Hey guys am trying to figure out kv and how to get stuffout of it how do i get iteme that have the key ["repo_pkgjson",reponame] example
const repos = await kv.get(["repo_pkgjson","tigawanna/all-emps-web"]);
const repos = await kv.get(["repo_pkgjson","tigawanna/all-emps-web"]);
returns
{
"key": [
"repo_pkgjson",
"tigawanna/all-emps-web"
],
"value": {
"name": "all-emps-vite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 3000",
"build": "tsc && vite build ",
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "^4.16.1",
"@tanstack/react-query-devtools": "^4.16.1",
"dayjs": "^1.11.6",
"pocketbase": "^0.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.6.0",
"react-intersection-observer": "^9.4.1",
"react-router-dom": "^6.4.3",
"tailwind-scrollbar": "^2.0.1"
},
"devDependencies": {

},
"pkg_type": "React+Vite",
"favdeps": [
"tailwindcss",
"typescript",
"react-router-dom",
"react-icons",
"dayjs",
"pocketbase"
]
},
"versionstamp": "000000000000046d0000"
}
{
"key": [
"repo_pkgjson",
"tigawanna/all-emps-web"
],
"value": {
"name": "all-emps-vite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 3000",
"build": "tsc && vite build ",
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "^4.16.1",
"@tanstack/react-query-devtools": "^4.16.1",
"dayjs": "^1.11.6",
"pocketbase": "^0.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.6.0",
"react-intersection-observer": "^9.4.1",
"react-router-dom": "^6.4.3",
"tailwind-scrollbar": "^2.0.1"
},
"devDependencies": {

},
"pkg_type": "React+Vite",
"favdeps": [
"tailwindcss",
"typescript",
"react-router-dom",
"react-icons",
"dayjs",
"pocketbase"
]
},
"versionstamp": "000000000000046d0000"
}
but this
const repos = await kv.list({prefix: ["repo_pkgjson"]});
const repos = await kv.list({prefix: ["repo_pkgjson"]});
returns an empty array , waht am i doing wrong?
5 Replies
cknight
cknight7mo ago
Can you show the full code? kv.list returns an async iterator, not an array of entries. You want to do for await (const repo of repos) { ... } with the result of the list operation.
NDH
NDH7mo ago
list returns a KvListIterator. You must await each entry.
const entries = db.list({ prefix: ["repo_pkgjson"] })
for await (const entry of entries) {
console.info('key: ', entry.key)
console.info('value ', entry.value)
}
const entries = db.list({ prefix: ["repo_pkgjson"] })
for await (const entry of entries) {
console.info('key: ', entry.key)
console.info('value ', entry.value)
}
https://deno.land/api@v1.35.3?unstable=&s=Deno.Kv&p=prototype.list
tigawana
tigawana7mo ago
Thanks guys it worked
Kofi GOLO
Kofi GOLO7mo ago
Better yet since Deno 1.38: - in one step
const repos = await Array.fromAsync(kv.list({ prefix: ["repo_pkgjson"] }));
const repos = await Array.fromAsync(kv.list({ prefix: ["repo_pkgjson"] }));
- in two steps
const iter = kv.list({ prefix: ["repo_pkgjson"] });
const repos = await Array.fromAsync(iter);
const iter = kv.list({ prefix: ["repo_pkgjson"] });
const repos = await Array.fromAsync(iter);
And you can still access each of them before storing them. For instance if you just want the Value and not the KV Part Key nor the Versionstamp:
const iter = kv.list({ prefix: ["repo_pkgjson"] });
const repos = await Array.fromAsync(iter, repo => repo.value);
const iter = kv.list({ prefix: ["repo_pkgjson"] });
const repos = await Array.fromAsync(iter, repo => repo.value);
tigawana
tigawana7mo ago
Thank you even more