Support type-safe variant selection in the MongoDB query builder

Open
#30,783 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
mongodb, typescript

Research direction

Start by reading the MongoDB query builder exposed through @prisma/orm-mongo and compare its field accessors with the ORM's .variant('Expense') support. The issue does not name implementation files or tests, so first locate the query builder and its type tests. Done means variant-only fields such as categoryId can be accessed with type checking in aggregation queries.

Written by the indexing model from the issue text.

Description

What problem are you trying to solve?

Aggregations over polymorphic MongoDB collections cannot access variant-only fields with full typing, even when those fields are declared in the contract.

For example, Transaction defines kind and amount, while Expense extends it using @@base(Transaction, "expense") and adds categoryId ObjectId.

Filtering to expenses does not make categoryId accessible:

import { acc } from '@prisma/orm-mongo/query-builder';

const plan = db.query.from('transactions')
  .match((f) => f.kind.eq('expense'))
  .group((f) => ({
    _id: f.categoryId, // TypeScript error: 'categoryId' does not exist on the base model's field accessor.
    total: acc.sum(f.amount),
  }))
  .build();

The ORM provides .variant('Expense'), but the query builder has no equivalent.

Proposed solution

Support typed access to variant fields.

Alternatives considered

Use rawPath to access the variant-only field:

import { acc } from '@prisma/orm-mongo/query-builder';

const plan = db.query.from('transactions')
  .match((f) => f.kind.eq('expense'))
  .group((f) => ({
    _id: f.rawPath<{ readonly codecId: 'mongo/objectId@1'; readonly nullable: false }>('categoryId'), // Manually supply codec metadata; field-name typos are not checked.
    total: acc.sum(f.amount),
  }))
  .build();
Scope and impact

This affects the MongoDB query builder exposed through @prisma/orm-mongo.

Users could aggregate over variant-specific fields without bypassing field-name checks or manually supplying codec metadata.

Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.