Query collection items based on fields value #357
Replies: 2 comments 2 replies
-
Hey! If you mean just querying an entire collection, you can already do this with
Do you mean being able to pass a dynamic collection name (based on category)?If so, I've handled a simple case like this in one of my projects with something like this (haven't tested the code I'm writing here): const sessionsLookup = {
'sydney': reader.collections.sydneySessions,
'woopi': reader.collections.woopiSessions,
'coffs': reader.collections.coffsSessions
}
const collection = sessionsLookup[region] // region is received a prop
const sessions = await collection.all() I agree it would be nice to be able to pass the collection name dynamically. |
Beta Was this translation helpful? Give feedback.
-
At this stage, filtering content in the Reader API would involve loading all the files in the collection anyway, since we're not using a database. That's fine for smaller collections, but we're a bit wary of adding something that looks like it should "just work" and then someone uses it on a collection with a few thousand items in it and ... it's not going to scale well. Also, designing a filter syntax is a fair bit of surface area to document and maintain (I mean there are several we could copy, like Prisma's or most other ORMs, but it would still be custom) and we're trying to keep things lighter right now so we can focus on things that are hard to do in user land. If you've got a collection where the scale / performance isn't going to be a problem, then it's pretty easy to do yourself:
There may even be data filtering libraries on npm that give you a more expressive syntax for this, or optimise the process in various ways? If you wanted something like this in your site to be queried from the codebase at runtime you could (for example) throw it in redis or sqlite, or even an optimised in-memory data structure. So I'm not sure this is something we should actually try and solve in Keystatic itself, it feels like more of a separate / generic problem space. (that's not a hard line, just sharing current thinking) Also, while we could probably cover a range of common requirements ( Honestly, I actually think the flexibility of just writing whatever comparison logic you want in a Definitely, we should at least add some examples to the Reader docs for anyone else looking for this functionality. @emilienbidet if you're interested in contributing, that would be an awesome start! |
Beta Was this translation helpful? Give feedback.
-
Hello KeyStatic core team !
I was wondering if the Keystatic reader API plan to integrate a way to query over the collection items.
See an example below :
My use case is the following :
I want to create a blog with categories pages that contain posts filtered by categories.
If you think it could be integrated in the Keystatic vision. I would be glad to help develop this feature.
Thank you very much
Emilien
Beta Was this translation helpful? Give feedback.
All reactions