Caramastifer
Caramastifer•8mo ago

Get only the first records with deno kv but reversed ordered

I'm using Deno KV to store articles. Generating the ID for the articles:
export function generateUniqueChronologicallyOrderedId(): string {
return ulid();
}
export function generateUniqueChronologicallyOrderedId(): string {
return ulid();
}
this will order the articles in the DB chronologically in an ascendent way. If I want to get all the articles reversed ordered I'm doing:
export async function getAllArticles() {
const articles = [];

for await (const res of kv.list({ prefix: ["article"] })) {
articles.push(res.value);
}

return articles.reverse();
}
export async function getAllArticles() {
const articles = [];

for await (const res of kv.list({ prefix: ["article"] })) {
articles.push(res.value);
}

return articles.reverse();
}
But I want to get only the 4 most recent articles. How can I achieve that without waiting for the full database to load and then reverse it? Thank you very much for your help community!
2 Replies
Caramastifer
Caramastifer•8mo ago
ok <:hooray_deno:1035517542200004688> after further reading the KV documentation I found what I was looking for https://docs.deno.com/kv/manual/operations
// Return all users in reverse order, ending with "sam"
const iter = kv.list<string>({ prefix: ["users"], start: ["users", "sam"] }, {
reverse: true,
});
const users = [];
for await (const res of iter) users.push(res);
console.log(users[0]); // { key: ["users", "taylor"], value: "taylor", versionstamp: "0059e9035e5e7c5e0000" }
console.log(users[1]); // { key: ["users", "sam"], value: "sam", versionstamp: "00e0a2a0f0178b270000" }
// Return all users in reverse order, ending with "sam"
const iter = kv.list<string>({ prefix: ["users"], start: ["users", "sam"] }, {
reverse: true,
});
const users = [];
for await (const res of iter) users.push(res);
console.log(users[0]); // { key: ["users", "taylor"], value: "taylor", versionstamp: "0059e9035e5e7c5e0000" }
console.log(users[1]); // { key: ["users", "sam"], value: "sam", versionstamp: "00e0a2a0f0178b270000" }
Operations | Deno Docs
The Deno KV API provides a set of operations that can be performed on the key
Deno AI Helper
Deno AI Helper•8mo ago
To retrieve only the 4 most recent articles in reverse order without loading the entire database, you can utilize the list operation with the reverse option set to true. Additionally, you can use the limit option to specify that you only want to retrieve 4 records. Here's how you can modify your getAllArticles function to achieve this:
export async function getAllArticles() {
const articles = [];
const iter = kv.list({ prefix: ["article"] }, { reverse: true, limit: 4 });

for await (const res of iter) {
articles.push(res.value);
}

return articles; // They are already in reverse order due to the `reverse: true` option.
}
export async function getAllArticles() {
const articles = [];
const iter = kv.list({ prefix: ["article"] }, { reverse: true, limit: 4 });

for await (const res of iter) {
articles.push(res.value);
}

return articles; // They are already in reverse order due to the `reverse: true` option.
}
In this code snippet, the reverse: true option ensures that the articles are returned in descending order, and the limit: 4 option ensures that only the 4 most recent articles are fetched. This way, you don't need to load the entire database or manually reverse the order of the results. Remember to replace kv with your actual Deno KV instance variable, and ensure that you have the necessary permissions and that you're using the --unstable flag, as Deno KV is currently in beta and requires this flag for execution. 🦕