Skip to content

feat(content): filter indexed custom fields - #2213

Merged
ascorbic merged 11 commits into
emdash-cms:mainfrom
logelog:feat/indexed-custom-field-filtering
Aug 16, 2026
Merged

ascorbic merged 11 commits into
emdash-cms:mainfrom
logelog:feat/indexed-custom-field-filtering

Conversation

@logelog

@logelog logelog commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

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 through in, 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

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable)
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: indexed filtering is awaiting maintainer review.

AI-generated code disclosure

  • This PR includes AI generated code: Claude Opus 5, GPT-5.6 (via private dev orchestra)

Screenshots / test output

Validated on the current main base and the current sorting PR head:

  • focused SQLite: 416 tests passed, 3 PostgreSQL-only tests skipped
  • PostgreSQL 17 filtering: 34 tests passed
  • PostgreSQL 17 merged search control: 12 tests passed
  • required workspace builds and core typecheck passed
  • quick lint and changed-file type-aware lint passed
  • format check passed for 2,661 files

GitHub CI runs the complete branch matrix.

@changeset-bot

changeset-bot Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d7b94b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
emdash Minor
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Patch
@emdash-cms/plugin-mcp-smoke Major
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This 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.

@pkg-pr-new

pkg-pr-new Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@2213

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@2213

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@2213

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@2213

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@2213

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@2213

emdash

npm i https://pkg.pr.new/emdash@2213

create-emdash

npm i https://pkg.pr.new/create-emdash@2213

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@2213

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@2213

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@2213

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@2213

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@2213

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@2213

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@2213

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@2213

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@2213

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@2213

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@2213

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@2213

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@2213

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@2213

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@2213

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@2213

commit: d7b94b4

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...) after validateIdentifier, filter values are parameterized, in lists are built with sql.join, and the partial expression index design is correct. No injection vectors found.
  • Authorization/logged-out paths: fieldFilters flows through handleContentList and plugin content: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 locale when supplied; indexed field values are per-row, so this stays correct.
  • Index discipline: createFieldIndex matches the deleted_at IS NULL filter used by list/count queries.
  • API envelope: responses remain { items, total, nextCursor? }.

Blocking issues found: none. Issues to fix before merge:

  1. A new repository test asserts new Set(...).toHaveLength(5), which will fail because Sets have size, not length.
  2. 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.listColumns shape but not that the slugs are declared fields in the collection.
  • createFieldIndex could be idempotent with IF NOT EXISTS to 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 ?? [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/core/src/seed/validate.ts Resolved
Comment thread packages/core/src/schema/registry.ts Resolved
@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch from 80b9da5 to abd8982 Compare July 22, 2026 23:39
@github-actions github-actions Bot added review/needs-rereview New commits since the last review overlap and removed review/needs-review No maintainer or bot review yet labels Jul 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch from 1ed426a to b79d7d7 Compare August 12, 2026 17:35
@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch 2 times, most recently from ad0d640 to 4ec1b5f Compare August 13, 2026 12:44
@ascorbic

Copy link
Copy Markdown
Member

@logelog this needs rebasing now that 2212 is in

@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch from 4ec1b5f to 0ded635 Compare August 13, 2026 13:45
@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch from 0ded635 to 3437913 Compare August 13, 2026 18:50
logelog added 10 commits August 14, 2026 19:46
`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.
@logelog
logelog force-pushed the feat/indexed-custom-field-filtering branch from 3437913 to 6f229d6 Compare August 14, 2026 17:53
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Aug 16, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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:

Suggested change
// 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.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Aug 16, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ascorbic
ascorbic enabled auto-merge (squash) August 16, 2026 11:47
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Aug 16, 2026
@ascorbic
ascorbic merged commit e5cda04 into emdash-cms:main Aug 16, 2026
46 checks passed
@emdashbot emdashbot Bot mentioned this pull request Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants