Skip to content

Allow flatMap streams without index fields to merge #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:

- name: Run lint, test, and codegen
run: |
npm run lint
npm run test
npm run lint
npm run typecheck
npx convex codegen && git diff --exit-code
64 changes: 64 additions & 0 deletions packages/convex-helpers/server/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const schema = defineSchema({
d: v.number(),
e: v.number(),
}).index("cde", ["c", "d", "e"]),
channels: defineTable({
workspaceId: v.string(),
isPublic: v.boolean(),
ownerId: v.string(),
}).index("by_isPublicWorkspace", ["isPublic", "workspaceId", "ownerId"]),
channelMembers: defineTable({
channelId: v.id("channels"),
userId: v.string(),
}).index("by_user", ["userId"]),
});

function stripSystemFields(doc: GenericDocument) {
Expand Down Expand Up @@ -505,6 +514,61 @@ describe("stream", () => {
]);
});
});

test("merge with flatMap and default index fields", async () => {
const t = convexTest(schema, modules);
await t.run(async (ctx) => {
const workspaceId = "w";
const userId = "u";
const privateChannelId = await ctx.db.insert("channels", {
workspaceId,
isPublic: false,
ownerId: userId,
});
await ctx.db.insert("channelMembers", {
channelId: privateChannelId,
userId,
});
await ctx.db.insert("channels", {
workspaceId,
isPublic: true,
ownerId: userId,
});

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_isPublicWorkspace", (q) =>
q.eq("isPublic", false).eq("workspaceId", workspaceId),
)
.filterWith(
async (channel) => channel._id === membership.channelId,
),
[],
);

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

const merged = mergedStream(
[privateChannels, publicChannels],
["userId"],
);
const result = await merged.collect();
expect(result.map(stripSystemFields)).toEqual([
{ workspaceId, isPublic: false },
{ workspaceId, isPublic: true },
]);
});
});
test("streamIndexRange returns correct subset", async () => {
const t = convexTest(schema, modules);
await t.run(async (ctx) => {
Expand Down
9 changes: 7 additions & 2 deletions packages/convex-helpers/server/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@
mapper: (doc: T) => Promise<QueryStream<U>>,
mappedIndexFields: string[],
): QueryStream<U> {
normalizeIndexFields(mappedIndexFields);
if (mappedIndexFields.length > 0) {
normalizeIndexFields(mappedIndexFields);
}
return new FlatMapStream(this, mapper, mappedIndexFields);
}

Expand Down Expand Up @@ -1269,10 +1271,13 @@
} else {
innerStream = await this.#mapper(t);
if (
this.#mappedIndexFields.length > 0 &&
!equalIndexFields(innerStream.getIndexFields(), this.#mappedIndexFields)
) {
throw new Error(
`FlatMapStream: inner stream has different index fields than expected: ${JSON.stringify(innerStream.getIndexFields())} vs ${JSON.stringify(this.#mappedIndexFields)}`,
`FlatMapStream: inner stream has different index fields than expected: ${JSON.stringify(
innerStream.getIndexFields(),
)} vs ${JSON.stringify(this.#mappedIndexFields)}`,
);
}
if (innerStream.getOrder() !== this.#outerStream.getOrder()) {
Expand Down Expand Up @@ -1565,7 +1570,7 @@
equalIndexFields(orderingIndexFields, indexFields),
)
) {
throw new Error(

Check failure on line 1573 in packages/convex-helpers/server/stream.ts

View workflow job for this annotation

GitHub Actions / Test and lint

server/stream.test.ts > stream > merge with flatMap and default index fields

Error: indexFields must be some sequence of fields the stream is ordered by: ["userId","_creationTime","_id"], ["isPublic","workspaceId","ownerId","_creationTime","_id"] (2 equality fields) ❯ new OrderByStream server/stream.ts:1573:13 ❯ server/stream.ts:1011:19 ❯ new MergedStream server/stream.ts:1010:29 ❯ mergedStream server/stream.ts:993:10 ❯ server/stream.test.ts:561:22 ❯ invokeFunction ../../node_modules/convex/src/server/impl/registration_impl.ts:80:14 ❯ invokeMutation ../../node_modules/convex/src/server/impl/registration_impl.ts:60:18 ❯ runTransaction ../../node_modules/convex-test/dist/index.js:1177:31
`indexFields must be some sequence of fields the stream is ordered by: ${JSON.stringify(
indexFields,
)}, ${JSON.stringify(
Expand Down
Loading