Skip to content

Fix: non-primary snake_case ObjectId fields not converted (issue #12)#16

Open
rathboma wants to merge 5 commits intomainfrom
fix/issue-12-non-primary-objectid-fields
Open

Fix: non-primary snake_case ObjectId fields not converted (issue #12)#16
rathboma wants to merge 5 commits intomainfrom
fix/issue-12-non-primary-objectid-fields

Conversation

@rathboma
Copy link
Contributor

Summary

  • Adds a failing integration test that reproduces the bug reported in BUG: Unable to query non-primary ObjectId fields in MongoDB #12
  • convertObjectIds() in executor/index.ts only handles _id, camelCase Id and Ids suffixes — it misses snake_case _id suffix fields like transaction_id
  • Test confirms: SELECT * FROM soldinventories WHERE transaction_id = '<hex>' returns 0 results instead of matching documents

Root cause

In packages/lib/src/executor/index.ts:234, the condition is:

key === '_id' || key.endsWith('Id') || key.endsWith('Ids')

A field like transaction_id fails all three checks, so the string value is never converted to new ObjectId(...), and MongoDB's strict type comparison returns no results.

Test plan

  • New integration test should handle ObjectId conversions on non-primary snake_case id fields (issue #12) added to edge-cases.integration.test.ts
  • Test currently fails, confirming the bug
  • Fix convertObjectIds() to also handle fields ending in _id — test should then pass

Closes #12

rathboma and others added 5 commits February 24, 2026 15:13
Query by non-primary ObjectId fields like `transaction_id` returns no results
because `convertObjectIds()` only handles `_id`, camelCase `Id`, and `Ids`
suffixes, missing snake_case `_id` suffix fields.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extend `convertObjectIds()` to also handle fields ending in `_id`
(snake_case), so queries like `WHERE transaction_id = '<hex>'` correctly
convert the string to a MongoDB ObjectId before comparison.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Instead of guessing which fields need ObjectId conversion based on field
name patterns (Id, _id, etc.), sample one document from the collection
and check which fields are actually ObjectId instances. Use that type map
to convert string values in filters before querying.

This correctly handles any arbitrarily-named ObjectId field (e.g.
transaction_id, source_ref, parent) without relying on naming conventions.

Closes #12

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ECTID

Users can now explicitly cast a string to ObjectId in any position,
regardless of field name or collection schema.

Supported syntax:
  WHERE ref = CAST('507f...' AS OBJECTID)
  WHERE ref = '507f...'::OBJECTID

Implementation:
- parser.ts: preprocess both cast forms into a '__QL_OBJECTID_hex__'
  sentinel string before the SQL AST is built
- compiler.ts: convertValue() detects the sentinel and returns a
  { __qlObjectId: hex } marker object
- executor/index.ts: resolveSentinels() walks the compiled filter after
  schema-based conversion and turns any remaining markers into
  ObjectId instances; non-plain objects (ObjectId, Date, etc.) are
  skipped to avoid destructuring already-typed BSON values

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…elds

Previously _id was hardcoded as always-ObjectId regardless of what was
actually stored. Now resolveFilterObjectIds() trusts the schema sample
for _id too — only adding it to objectIdFields if the sampled document
has an ObjectId there. The only assumption made is for empty collections,
where _id defaults to ObjectId (MongoDB's standard default).

Tests added:
- external_id stored as integer → NOT converted to ObjectId
- external_id stored as 24-char hex string → NOT converted to ObjectId
- _id stored as integer → NOT converted to ObjectId
- _id stored as 24-char hex plain string → NOT converted to ObjectId

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Unable to query non-primary ObjectId fields in MongoDB

1 participant