Is it possible to return specific fields via lookupOne() / lookupRecords()?

I’m aware of the ability of both TableName.lookupOne() and TableName.lookupRecords() to return a specific field only, e.g., by doing something like User.lookupOne().favoriteMovie.

…but is there a way to retrieve N number of specific fields?

For example, instead of only getting the favoriteMovie field, if I wanted to get the favoriteMovie, favoriteSong, and age fields from a User table. I know I can just omit the specific field and then get to those values in code, but if my table is large, it would faster and more efficient to just get the fields I need…

Hey there, if I’m not wrong about Grist’s inner workings in this case, then

  1. there isn’t a way but
  2. it shouldn’t matter performance-wise. Fields are implemented as attribute getters on Record/RecordSet objects, so what you should do is get the record set once:
    recset = User.all (where ‘all’ is equivalent to ‘lookupRecords()’ without arguments, by the way)
    and then query the needed attributes:
    movie, song, age = recset.favoriteMovie, recset.favoriteSong, recset.age
    or even something like:
    return [ getattr(recset, attr_name) for attr_name in ("favoriteMovie", "favoriteSong", "age") ]

Hope this helps!

2 Likes