question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement fetchFirst, fetchSingle and fetchAll on QueryBuilder

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

2reactions
jasontheilercommented, Jan 9, 2021

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() and findOne() methods, like this:

  • fetch(): IContentDocument[]
  • fetchOne(): IContentDocument | null
1reaction
Tahulcommented, May 9, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python cursor's fetchall, fetchmany(), fetchone() to ... - PYnative
This lesson will show how to use fetchall() , fetchmany() , and fetchone() to retrieve data from MySQL, PostgreSQL, SQLite database. First ...
Read more >
Data Retrieval And Manipulation - Doctrine
It is the primary use-case of a database. ... fetchOne() - Retrieves the value of the first column of the next row from...
Read more >
Result — TYPO3 Explained main documentation
The object represents a query result set and has methods to fetch single rows with ->fetchAssociative() or to fetch all rows as an...
Read more >
How to fetch results into a custom object now that fetchAll ...
Use this trait in all classes that you want to build from an associative array. I assume you're looping through your array of...
Read more >
Querying Data from a Database using fetchone() and fetchall()
This connector helps in enabling the Python programs to use MySQL databases. In the below code, we have used MySQL using Python for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found