index.queryLuaObjects and index.indexObjects documented in API/index produces the desired effect.
space-lua
-- Parses a task string to determine its priority based on emojis
local function parsePriority(taskText)
local priority = string.match(taskText, "❗") and 1
or string.match(taskText, "🔺") and 2
or string.match(taskText, "🔼") and 3
-- If none are found, 'priority' will be nil by default
return priority
end
-- Parse the name beside pause emoji
local function parseWait(taskText)
local name = string.match(taskText, "⏸️(.*) ")
return name
end
-- Get all task objects
local tasks = index.queryLuaObjects("task", {})
-- Walk through each taks and set attribute
for _, task in ipairs(tasks) do
if not task.done then
priority = parsePriority(task.name)
if priority then
-- Set priority attribute
task.priority = priority
end
waitingFor = parseWait(task.name)
if waitingFor then
-- Set wait attribute
task.wait = waitingFor
end
-- index the task with the updated attributes
index.indexObjects(task.page, {task})
end
end
But would this automatically index the additional attributes ? I mean if I add a new task with custom emoji attributes, it would not index it until this lua script runs - am I right ? Is there a hook we can attach to?
Yes and No. It works when I tried in a space with few tasks and pages. Query fails with Lua error: Maximum call stack size exceeded in a space with ~2k tasks, even if I filter with where or limit clause.
If I do not use the metatable trick all queries work, not sure if the metatable trick exposes a bug or that’s a limitation.
Now I started thinking are we using or mis-using? are we thinking or over-thinking?
I need custom attributes only for tasks. @deepkn I understood, you needed it for a different purpose.
What if we had a plugin which parses the object and sets attributes/tags based on user configuration? I am suspecting there will be a problem based on the order of plugins that run while indexing - e.g. upon page:index, the 3rd party plugin will add attributes and tags, will it be overwritten by the built-in task plugin?
Summary so far,
Set attribute in space lua using index.indexObjects works but needs to be hooked to page:index to properly work, I guess.
metatable trick works but not in large space with many objects
@LogeshG5 Seems to work just fine for me, just tested with around 2.5k tasks - similar to yours, parsing out multiple attributes out of the task text. All of them queried just fine, even without any limits. My suspicion would be this is something specific to your __index function - it is probably introducing a recursive call somewhere, by calling back into __index - from a quick look at the SB code itself, I do not see it ending up making a recursive call. If you still think there is a valid case - this might be worth filing a GitHub issue.
That said, I’m also curious to get @zef 's inputs here - probably also on the efficiency part of the metamethod trick. I’m guessing this is evaluating the function in __index every time the new field is accessed and everywhere it is done? I mean if I have a query like above in multiple pages, it is indeed invoking the __index method for each instantiation of the query and for each task in the query result?