Implement fetchFirst, fetchSingle and fetchAll on QueryBuilder
See original GitHub issueIs your feature request related to a problem? Please describe.
I’m using TypeScript and every time I would like to use the result of the QueryBuilder.fetch()
method I have to cast the IContentDocument | IContentDocument[]
to an IContentDocument
, which is very annoying, especially since I only need a single item.
If I don’t do the cast then I get an error e.g.
Property 'slug' does not exist on type 'IContentDocument | IContentDocument[]'.
Property 'slug' does not exist on type 'IContentDocument[]'.
Describe the solution you’d like
It would be nice to have a (couple of) method(s) that only returns an IContentDocument
or only returns an IContentDocument[]
so the casting is no longer needed.
fetchSingle() IContentDocument
: returns the item when exactly one is found. throws an error when zero or more then one are found.
fetchFirst(): IContentDocument | undefined
: returns the first item when one or more are found. returns undefined
when no item is found.
fetchAll(): IContentDocument[]
: returns an array containing all items. the array is empty when no items are found.
Describe alternatives you’ve considered
Additional context
The names come from Entity Framework (.NET)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:6
Top GitHub Comments
I face the same problem with the
QueryBuilder.fetch()
method and would also really like to see more options here!But since the Nuxt Content module describes its API as “MongoDB like”, the methods should maybe be based on MongoDB’s
find()
andfindOne()
methods, like this:fetch(): IContentDocument[]
fetchOne(): IContentDocument | null
Hello there;
@nuxt/content v2 implements a Mongo-like query builder which is coming with the functions you’re asking for: https://content-v2.nuxtjs.org/api/query-content
I am closing this as there is no plan to fix this in content v1.
Feel free to re-open with a PR targeted at v1 branch!