deno icon indicating copy to clipboard operation
deno copied to clipboard

Deno kv should have built in sorting

Open quick007 opened this issue 1 year ago • 2 comments

It would be great if KV had a built-in way to sort, like a way to create sort (and maybe search?) indexes. This is possible to do manually, as demonstrated in Craig's Deno Diary (which is an amazing resource if you've never used it). Even so, building it into the core of KV would make it far more user-friendly to boot and make code much cleaner.

Some ideas for things that could be sorted (both ascending and descending):

  • timestamp added
  • based on part of a key
  • based on part of a value

I'm trying to think of syntax for this, and I have some ideas but please feel free to chip in in the comments. Ideally, it's all code-based and doesn't require messing around on the website. The two main issues with implementing this is: (a) no solidified data structure and (b) solutions need to be backward compatible. For a, we can assume that a value missing from an object is null. (Meaning it's not part of the index). B is slightly harder though- it would be great if it was clean and easy to add indexing to an existing project without having to create/run migration scripts.

With that said, there are a few different ways to add indexing. The main distinction is KV level indexing vs insert level indexing. With insert-level indexing, users would specify what they want to index whenever they insert an object into KV. KV-level indexing applies indexing to anything that matches a key when being added. Personally, I'm in favor of the ladder because it means everything is applied globally and standardized across data, while making it easy to migrate. While KV theoretically allows for any data structure as the value, in practice more or less everyone has a standardized data set anyway.

quick007 avatar Jan 23 '24 02:01 quick007

If I understand correctly, the proposed solution is basically an autoindexing mechanism, not targeted specifically at sorting.

I'm pretty sure such a feature has crossed core team's minds. That's how DBs work since forever. I guess the team either doesn't want that built-in or is prioritizing other stuff first.

This lib implements lots of stuff on top of KV: https://github.com/oliver-oloughlin/kvdex

Leokuma avatar Jan 26 '24 16:01 Leokuma

Yes, you're right. That's effectively what I'm asking for (hence why I add that searching could be added with this as well). I did assume it had occurred to them, but I also didn't see any issues or roadmaps so I decided to create one.

That's a cool wrapper, I'll check it out!

quick007 avatar Jan 26 '24 21:01 quick007