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:
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:
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
ok <:hooray_deno:1035517542200004688> after further reading the KV documentation I found what I was looking for
https://docs.deno.com/kv/manual/operations
Operations | Deno Docs
The Deno KV API provides a set of operations that can be performed on the key
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:
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. 🦕