Plugin API - Fetch a single row from a table

Hi,

I kind of already asked this here RECORD() effciency - #3 by Mark_Daniel but hiding it in a reply to an existing topic probably wasn’t the best.

Using the plugin API in full document access mode is there an easy way to just fetch one row, ideally by table id and row id? I have found fetchSelectedRecord but that only operates on the selected table and fetchTable but the array of values format isn’t very convenient if you want to find a particular row, it looks like you would have to linear search the id array to get the array index and then use that to get the values from the other arrays. I guess if it’s guaranteed that the id array is in order I could binary search which would be at least be quicker. Is there a nice javascript way of slicing all the value arrays into a new object given the index?

Thanks,
Mark

I’d say it’s a legitimate feature request, because there indeed isn’t a way at the moment. We are working on improvements to this API, so something better may become available before long.

Meanwhile, what I can suggest is this bit of code to convert the result of fetchTable() (which is in a column-oriented format) to something more convenient:

function toRecordMap(columnData) {
  const fieldNames = Object.keys(columnData);
  return new Map(columnData.id.map((id, index) => {
    const values = fieldNames.map(col => [col, columnData[col][index]]);
    return [id, Object.fromEntries(values)];
  }));
}

This returns a JS Map. E.g. if

const m = toRecordMap(await grist.docApi.fetchTable(tableId));

then m.get(rowId) will return an object representing that one row.

Thanks, with a combination of that and a few library methods I’ve got something working.

Mark