joist-orm / joist-orm/joist-orm

Feature Request: Polymorphic Reference Folding

Open
#756 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
385
Forks
26
Avg merge
8h 55m
Merged PRs (30d)
42

Description

It'd be cool to be able to both load and fold on Polymorphic references. Something like...

```js
const author = await em.findOne(
Author,
{},
{
populate: {
// Poly on Book | Blogpost | Article
latest: {
_book: ["name", "chapters", "store"],
_blogpost: ["siteName", "link"],
_article: ["publication", "url"],
},
},
},
);

const currentBlurb = author.latest.fold({
Book: (b) => `${b.name} now available at ${b.store.get.name}`, // `b` is Loaded
Blogpost: (bp) => `See the latest from ${author.name} at ${bp.siteName} (${bp.link})`,
Article: (a) => `${a.publication.get.name} recently wrote about ${author.name}. See what they had to say! (${a.link})`,
});
```

[This](https://stackoverflow.com/a/64528509/12022691) TypeScript type might be helpful.

It might be nice to avoid `.get` if possible, to save a few chars. Not sure how that might play out in Arrays, i.e. if "Reviews" was a poly array on `UserReview | PublicationReview` would the preferred syntax be `Book.reviews.get.map((poly) => poly.fold({ ... }))`?

I think this would scale pretty well to Async Properties, derived fields, maybe some Persisted stuff, Hooks, etc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.