Skip to content

Commit

Permalink
Added useQuerySingleResult
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 29, 2022
1 parent caee5cb commit f51b123
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/IHP/DataSync/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ export function useQuery(queryBuilder) {
])

return records;
}

/**
* A version of `useQuery` when you only want to fetch a single record.
*
* Automatically adds a `.limit(1)` to the query and returns the single result instead of a list.
*
* @example
* const message = useQuerySingleresult(query('messages').filterWhere('id', '1f290b39-c6d1-4dff-8404-0581f470253c'));
*/
export function useQuerySingleResult(queryBuilder) {
const result = useQuery(queryBuilder.limit(1));
return result === null ? null : result[0];
}

0 comments on commit f51b123

Please sign in to comment.