Suggestion : make record.getOne() able to filter over other fields than id
See original GitHub issueHi,
The current prototype of the record.getOne
methods only allows filtering over the ID only since it is a mandatory parameter of the function, however one might want to use this function with another unique & required field of the collection, for example a slug, but doing so is currently impossible with record.getOne
.
One workaround I found was to use record.getFullList()
with a filter on the desired field, but it seems kind of counter-intuitive and imperfect to use the method that queries the whole collection and is expected to return multiple results when you actually want a single record. This workaround also causes a problem when someone wants to set different permissions on the list
and view
actions on the API for their collection but is forced to use record.getMultiple
to compensate for the limitation of the current implementation of record.getOne
.
I would therefore suggest changing the signature + implementation of the record.getOne
function or adding a new one to give users an option to allow for searching over other unique+required fields than the id of the record.
Thanks in advance for your consideration ! 😃
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (3 by maintainers)
@goknsh Because that’s how devs most likely are going to use it, otherwise the returned record from the collection will be arbitrary and you’ll rely on whatever is the first row in the db query
select * from yourCollection limit 1;
.In the rare cases where you don’t need a filter and don’t care what record is returned, you can use just
getList(1, 1)
In the upcoming v0.8.0 there is a new helper
getFirstListItem(filter, queryParams = {})
that could be used for fetching the first found record matching a filter. As the name suggest, the above for now works only with the “List API rule”.Please note that v0.8.0 will have some breaking changes and will work only with PocketBase v0.8+. You can check the
rc
branch with the updated RecordService methods in https://github.com/pocketbase/js-sdk/tree/rc#recordservice.SDK changelog with the future changes could be found in https://github.com/pocketbase/js-sdk/blob/rc/CHANGELOG.md.