Skip to content

impossible to mergeStream between flatmapped and not-flatmapped #690

@marnec

Description

@marnec

I was trying to perform the equivalent operation of a UNION between between rows coming from a JOIN and rows coming from a simple query. The two sets of rows had identical set of columns but the operation fails.

const userMemberships = stream(ctx.db, schema).query("channelMembers").withIndex("by_user", (q) => q.eq("userId", userId))


const privateChannels = userMemberships
      .flatMap(async (membership) => stream(ctx.db, schema)
        .query("channels")
        .withIndex("by_isPublicInWorkspace", q => q.eq("isPublic", false).eq("workspaceId", workspaceId))
        .filterWith(async (q) => q._id === membership.channelId)
        ,
        ['isPublic', "workspaceId"]
      )

const publicChannels = stream(ctx.db, schema).query("channels").withIndex("by_isPublicInWorkspace", (q) => q.eq("isPublic", true).eq("workspaceId", workspaceId))

return mergedStream([privateChannels, publicChannels], [INDEX_FIELDS])

Wathever I tried to put in INDEX_FIELDS I had this error:

7/17/2025, 10:35:27 PM [CONVEX Q(channels:listByUserMembership)] Uncaught Error: indexFields must be some sequence of fields the stream is ordered by: ["_creationTime","_id"], ["workspaceId","userId","_creationTime","_id","_id"] (2 equality fields)

I believe that flatmaps brings along the index_fields of the parent table, making it impossible to reconcile with the index_fields of the child table INDEX_FIELDS(parent+child) != INDEX_FIELDS(child),

Honestly I would be lying if I'd say that I understand what is happening here but I strongly feel that this should be possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions