Repository navigation
feat(content): filter indexed custom fields - #2213
Conversation
🦋 Changeset detectedLatest commit: d7b94b4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Scope checkThis PR changes 1,474 lines across 47 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This PR adds a coherent, architecture-aligned implementation of indexed custom-field filtering: it stores filterable fields as real columns, validates identifiers before they reach SQL, parameterizes values, and adds coverage across repository, handler, client, plugin, and MCP surfaces. The sorting/index plumbing and admin list-column work are related but distinct concerns, so the main judgement call is whether the PR should be split or retitled.
What I checked:
- SQL safety: dynamic column refs use
sql.ref(...)aftervalidateIdentifier, filter values are parameterized,inlists are built withsql.join, and the partial expression index design is correct. No injection vectors found. - Authorization/logged-out paths:
fieldFiltersflows throughhandleContentListand plugincontent:read, both authenticated; public loader filters via the pre-existing loader path are untouched. No new anonymous hot-path queries. - Locale filtering: content-table list queries already filter by
localewhen supplied; indexed field values are per-row, so this stays correct. - Index discipline:
createFieldIndexmatches thedeleted_at IS NULLfilter used by list/count queries. - API envelope: responses remain
{ items, total, nextCursor? }.
Blocking issues found: none. Issues to fix before merge:
- A new repository test asserts
new Set(...).toHaveLength(5), which will fail because Sets havesize, notlength. - The new admin list-column formatter emits hard-coded
"—","✓", and", ", and formats dates with the browser default locale instead of Lingui — violating the AGENTS.md localization rule.
Lower-priority suggestions:
- The PR title says "filtering" but the diff ships three separate changesets/features (filtering, sorting/indexes, list columns). Consider splitting or updating the title/description per scope discipline.
- Seed validation checks
admin.listColumnsshape but not that the slugs are declared fields in the collection. createFieldIndexcould be idempotent withIF NOT EXISTSto survive partial creates or re-runs.
Overall direction is solid; the test and localization items should be addressed.
| fields[field.slug] = entry; | ||
| } | ||
|
|
||
| const configuredListColumns = collection.admin?.listColumns ?? []; |
There was a problem hiding this comment.
[suggestion] The PR title and main changeset are about indexed custom-field filtering, but this hunk and the related schema/admin code introduce a third concern: collection-configured admin list columns (and the diff also ships a separate sorting/index feature). Per AGENTS.md scope discipline, unrelated/semi-related features should not ride along on a single PR. Consider either splitting this into focused PRs or updating the title/description/changeset set so reviewers can evaluate each feature on its own.
There was a problem hiding this comment.
This PR sits at the top of a four-PR stack, so its diff necessarily contains 2194 and 2212. Each capability is reviewed in its own PR; the stack order is in the description. Leaving this thread open since it is a judgement call rather than something the code can resolve.
80b9da5 to
abd8982
Compare
e318139 to
e00e9db
Compare
e00e9db to
3947dda
Compare
3947dda to
1afb45e
Compare
1ed426a to
b79d7d7
Compare
ad0d640 to
4ec1b5f
Compare
|
@logelog this needs rebasing now that 2212 is in |
4ec1b5f to
0ded635
Compare
0ded635 to
3437913
Compare
`findMany` races the page query against the count query. A collection whose table is missing rejects both, and `Promise.all` returns on the first while the other stays in flight holding a pooled connection. A Postgres pool destroyed inside that window never finishes closing. This predates the branch. The missing-collection regression test added here is the first to reach the path on Postgres, so the suite cannot pass without it.
Identifier validation ran first, so a request naming both a collection that does not exist and an invalid filter field answered VALIDATION_ERROR while the same request without filters answered COLLECTION_NOT_FOUND. The field names reach the lookup as bound parameters, so deferring validation past the collection check keeps the query parameterized. Drop four comments that justify decisions rather than record an invariant.
The cap on filter count threw before the collection lookup, so a request naming a collection that does not exist alongside more than the allowed number of filters answered VALIDATION_ERROR while the same request without filters answered COLLECTION_NOT_FOUND. The cap stays where it is, since it also bounds how many field slugs reach the lookup as bound parameters. Drop the comment above the settled list queries. The missing-collection test fails without them, which is where that contract belongs.
The tests around the cap already say what it protects.
3437913 to
6f229d6
Compare
There was a problem hiding this comment.
This PR is the filtering companion to the indexed-field storage work (#2212), and the implementation is solid. resolveFieldFilters / applyFieldFilters validate against _emdash_fields, normalize to the stored type, and push the predicates down through Kysely parameterised expressions (sql.ref() for identifiers, template placeholders for values). The cursor-pagination path correctly skips null-only ordering when the order field itself is filtered to null, and the integration/unit tests cover SQL-injection resistance, operand budgets, missing-collection precedence, and plugin access.
What I checked this round: the new content-list-query.ts type surface, the contentFieldFiltersSchema boundary validation, ContentRepository filter resolution/application, cursor-pagination interaction with order-field filters, the REST route and client serialization, plugin ctx.content.list forwarding, and the updated docs. I did not run the test suite, linter, or builds; the checkmarks in the PR description are unverified.
The only remaining merge blocker is process, not code: AGENTS.md requires a maintainer-approved Discussion for feature PRs. Discussion #2397 is still marked “awaiting maintainer review” in the checklist, so that approval must be confirmed before merge.
One small code-side note: the new Promise.allSettled comment in content.ts is a decision-justification narrative; it can be shortened to the invariant a future reader actually needs.
| // Settled rather than raced: a collection whose table is missing rejects | ||
| // both, and `Promise.all` returns on the first. The loser stays in flight | ||
| // holding a pooled connection, so a Postgres pool destroyed in that window | ||
| // never finishes closing. |
There was a problem hiding this comment.
[suggestion] This block justifies the switch to Promise.allSettled with a narrative about Postgres pool teardown. AGENTS.md allows comments only when they explain a non-obvious invariant a reader would otherwise get wrong; the reasoning here is legitimate, but the wording is decision-justification. Shorten it to the invariant:
| // never finishes closing. | |
| // Use allSettled so a missing content table rejects both promises; | |
| // Promise.all would leave the count query in flight after the first rejection. |
What does this PR do?
Adds typed, server-backed filtering for custom fields that are explicitly marked
indexed.The content list REST API, core client, runtime, and plugin content access contract accept AND-combined
fieldFilters. Supported conditions include exact scalar matches, missing values, membership throughin, and inclusive or exclusive numeric or textual ranges. Field names and values are schema-validated, bounded, and resolved only against indexed fields.This lets plugins and admin experiences filter complete result sets by metadata such as ticket state, priority, workflow status, or SEO score without browser-side filtering or full-table scans.
This PR is stacked on emdash-cms/emdash PR 2212, which supplies the indexed-field storage and validation contract. Both can be reviewed together, but the sorting PR should merge first; GitHub will then narrow this PR to its unique filtering changes.
Addresses the structured filtering portion of emdash-cms/emdash issue 2179.
Discussion: indexed filtering; configured content-list fields.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
Validated on the current
mainbase and the current sorting PR head:GitHub CI runs the complete branch matrix.