ReviewOS

stacks/bun-query-builder

Eager loading silently returns nothing for typos and unimplemented morph relations

#1068
Closed glennmichael123 opened this 22 days ago · 0 comments
22 days ago

orm.ts:2530 resolves an eager-load name and returns silently when it finds nothing:

if (!rel) return

So .with('typo') loads nothing and reports nothing — no throw, no warning, and the property is simply absent from the result. A misspelled relation name is indistinguishable from a relation with no rows.

The same silence covers the polymorphic relations. ModelDefinition accepts morphOne, morphMany, morphTo, morphToMany and morphedByMany (orm.ts:231-235), and the type system encourages declaring them, but only some are resolved at load time. Declaring the others is accepted, type-checks, and does nothing at runtime.

Fix: throw from that branch with the available relation names in the message, the way an unknown column already fails. Note the null caching just above at orm.ts:2524 — a naive throw will need to account for it, or the second call will behave differently from the first.

Either implement the remaining morph kinds or reject them at defineModel() time; accepting a declaration that silently does nothing is the worst of the three options.

Found while auditing after #1001.

Sign in to comment on this issue.