Help with queries and accessing data

I am trying to manage some physical items which I keep notes in my SB instance and keep track of them when I lend them to other peoples.

The basic idea is working, but I need help with improving my output.

I have items people can borrow from me. These are tagged with #lendable.

A small slash command adds a small snippet to a page to keep track to whom and when I gave the item to the person.

```#lend
person: foobar
since: 2026-01-02
```

Lend items are displayed with

${query[[
  from p = index.tag "lend"
  order by lastModified desc
  select {
    Name = "[[" .. p.ref .. "]]",
    Person = p.person,
    Since = "for " .. dateDiff(p.when, date.today()) .. " day(s)",
  }
]]}

A other table shows all items at the moment.

${query[[
  from p = index.tag "lendable"
  order by p.lastModified desc
  select {
    Name = "[[" .. p.name .. "]]",
    Availability = (p.lend and p.person) or "available",
    Modified = p.lastModified
  }
]]}

The first item should not be available as it is lend to “foobar”.

I want to track the availability by setting the column to “not available” and “available” depending on the status if #lend data is present.

I already tried to different statements in my select, but I am not able to achieve my goal.

In the query I cannot query p.lend or p.person as these are null.

If it is of relevance both queries are on the same page.

function isItemLendable(pageName)
  local res = query[[
    from index.tag "lend"
    where _.page == pageName
    select _.page == pageName
  ]]

  borrowed = table.unpack(res)
  if table.unpack(res) == true then
    return "🟥 borrowed"
  else
    return "🟩 available"
  end
end