perf: Scope the collection owner filter subquery to the team

This commit is contained in:
Tom Moor committed 2026-11-25 09:19:07 -05:00
1 parent bbc3e5724c
commit 3816ca2f52
4 files changed
+40 -16

No files matched your search

@@ -828,7 +828,7 @@ export default class PostgresSearchProvider extends BaseSearchProvider {
}
if (filter) {
where[Op.and].push(buildSearchWhere<Document>(filter));
where[Op.and].push(buildSearchWhere<Document>(filter, teamId));
}
return where;
+10
View File
@@ -257,6 +257,16 @@ describe("Filters", () => {
buildWhere({ field: "userId", operator: "neq", value: "u" })
).toThrow();
});
it("rejects ownerIds without a teamId", () => {
expect(() =>
buildWhere({
field: "ownerIds",
operator: "eq",
value: "6f2e8f0c-4a8b-4d3e-9c1a-2b7d5e9f1a3c",
})
).toThrow("`ownerIds` filter requires a teamId");
});
});
describe("dateFromDuration", () => {
+27 -13
View File
@@ -78,7 +78,13 @@ function isGroup<F extends string>(
return "filters" in filter;
}
function ownedByWhere(userIds: string[]): Record<string, unknown> {
function ownedByWhere(
userIds: string[],
teamId: string | undefined
): Record<string, unknown> {
if (!teamId || !isUUID(teamId)) {
throw new Error("`ownerIds` filter requires a teamId");
}
if (!userIds.every((id) => isUUID(id))) {
throw new Error("`ownerIds` values must be UUIDs");
}
@@ -90,7 +96,7 @@ function ownedByWhere(userIds: string[]): Record<string, unknown> {
{
collectionId: {
[Op.in]: Sequelize.literal(
`(SELECT "id" FROM "collections" WHERE "ownerIds" && ARRAY[${ids}]::uuid[])`
`(SELECT "id" FROM "collections" WHERE "teamId" = '${teamId}' AND "ownerIds" && ARRAY[${ids}]::uuid[])`
),
},
},
@@ -98,7 +104,10 @@ function ownedByWhere(userIds: string[]): Record<string, unknown> {
};
}
function leafToWhere(condition: FilterCondition): Record<string, unknown> {
function leafToWhere(
condition: FilterCondition,
teamId?: string
): Record<string, unknown> {
const { field, operator, value } = condition;
// `userId` is the public-API name for the denormalized collaboratorIds array
@@ -120,10 +129,10 @@ function leafToWhere(condition: FilterCondition): Record<string, unknown> {
// through the collection that contains them.
if (field === "ownerIds") {
if (operator === "eq" && value !== undefined) {
return ownedByWhere([String(value)]);
return ownedByWhere([String(value)], teamId);
}
if (operator === "in" && Array.isArray(value)) {
return ownedByWhere(value.map(String));
return ownedByWhere(value.map(String), teamId);
}
throw new Error("`ownerIds` only supports `eq` or `in` operators");
}
@@ -187,17 +196,19 @@ export function combineFilters(
* Convert a filter DSL expression into a Sequelize WhereOptions clause.
*
* @param filter the filter expression to convert.
* @param teamId the team that the query is scoped to, required for `ownerIds`.
* @returns a Sequelize-compatible where clause.
*/
export function buildWhere<M extends object = object>(
filter: Filter
filter: Filter,
teamId?: string
): WhereOptions<M> {
if (isGroup(filter)) {
const subWheres = filter.filters.map((f) => buildWhere<M>(f));
const subWheres = filter.filters.map((f) => buildWhere<M>(f, teamId));
const op = filter.operator === "AND" ? Op.and : Op.or;
return { [op]: subWheres } as WhereOptions<M>;
}
return leafToWhere(filter) as WhereOptions<M>;
return leafToWhere(filter, teamId) as WhereOptions<M>;
}
/**
@@ -303,7 +314,8 @@ export function mapFilterFields(
}
function searchLeafToWhere(
condition: FilterCondition
condition: FilterCondition,
teamId?: string
): Record<string, unknown> {
// documentId must be expanded to descendant ids by the route handler
// before reaching here — the expansion is async and authorization-
@@ -313,7 +325,7 @@ function searchLeafToWhere(
"documentId leaves must be expanded to `id` leaves before buildSearchWhere"
);
}
return leafToWhere(condition);
return leafToWhere(condition, teamId);
}
/**
@@ -325,18 +337,20 @@ function searchLeafToWhere(
* handles that.
*
* @param filter the filter expression.
* @param teamId the team that the query is scoped to, required for `ownerIds`.
* @returns a Sequelize WHERE clause.
* @throws if the filter references documentId (unexpanded).
*/
export function buildSearchWhere<M extends object = object>(
filter: Filter
filter: Filter,
teamId?: string
): WhereOptions<M> {
if (isGroup(filter)) {
const subWheres = filter.filters.map((f) => buildSearchWhere<M>(f));
const subWheres = filter.filters.map((f) => buildSearchWhere<M>(f, teamId));
const op = filter.operator === "AND" ? Op.and : Op.or;
return { [op]: subWheres } as WhereOptions<M>;
}
return searchLeafToWhere(filter) as WhereOptions<M>;
return searchLeafToWhere(filter, teamId) as WhereOptions<M>;
}
interface LegacyParams {
+2 -2
View File
@@ -304,7 +304,7 @@ router.post(
collections: collection ? [collection] : undefined,
});
const mapped = mapFilterFields(filter, { documentId: "id" });
where[Op.and].push(buildWhere<Document>(mapped));
where[Op.and].push(buildWhere<Document>(mapped, user.teamId));
}
if (!backlinkDocumentId && !collectionScopeDropped) {
@@ -565,7 +565,7 @@ router.post(
const filter = combineFilters(rawFilters);
if (filter) {
await authorizeFilterFields(user, filter);
where[Op.and].push(buildWhere<Document>(filter));
where[Op.and].push(buildWhere<Document>(filter, user.teamId));
}
const documents = await Document.scope([