David Alonso
David Alonso9mo ago

Nested field indexes and query filters

Hello! I am trying to run this query:
const block = await context.db
.query("blocks")
.withIndex("by_parent_grid_idx", (q) =>
q.eq("parentBlockId", parentBlockId).eq("properties.gridLayout.i", gridLayout.i)
)
.unique();
const block = await context.db
.query("blocks")
.withIndex("by_parent_grid_idx", (q) =>
q.eq("parentBlockId", parentBlockId).eq("properties.gridLayout.i", gridLayout.i)
)
.unique();
But I'm not able to create an index for properties.gridLayout.i I'm surprised this query also doesn't return any results
const block = await context.db
.query("blocks")
.withIndex("by_parent", (q) => q.eq("parentBlockId", parentBlockId))
.filter((q) => q.eq("properties.gridLayout.i", gridLayout.i))
.unique();
const block = await context.db
.query("blocks")
.withIndex("by_parent", (q) => q.eq("parentBlockId", parentBlockId))
.filter((q) => q.eq("properties.gridLayout.i", gridLayout.i))
.unique();
the schema of blocks is a union and not all objects inside the union have a guaranteed gridLayout property. Just wondering if this is expected so that I know if I'm doing something wrong
3 Replies
lee
lee9mo ago
what prevents you from creating the index on "properties.gridLayout.i"? I think it should be supported, although typescript might not like it so you may need to cast to any or refactor the schema so the field always exists
David Alonso
David AlonsoOP9mo ago
Sorry I should have been clearer. It's possible to create the index but querying it isn't
No description
lee
lee9mo ago
oh gotcha. Yeah this is a typescript problem. You could do .eq("properties.gridLayout.i", i as any as undefined) to work around it. Thanks for reporting

Did you find this page helpful?