Skip to content

Instantly share code, notes, and snippets.

View rafasousa's full-sized avatar
:octocat:
☕️+💻 = ❤️

Rafael Sousa rafasousa

:octocat:
☕️+💻 = ❤️
  • São Paulo - Brazil
View GitHub Profile
@rafasousa
rafasousa / 1-proposal.md
Created August 21, 2018 02:58 — forked from JedWatson/1-proposal.md
Proposal: adding reverse-relationship population to Mongoose (as implemented in KeystoneJS)

I've developed a useful feature in KeystoneJS that lets you populate a relationship from either side, while only storing the data on one side, and am looking for feedback on whether it is something that could / should be brought back into mongoose itself. (It might be possible to add as a separate package but I suspect there'd be too much rewriting of mongoose internals for that to be a good idea).

I've added this as an issue in mongoose for consideration: #1888 but am leaving this gist in place because the examples are easier to read.

I've used Posts and Categories as a basic, contrived example to demonstrate what I'm talking about here; in reality you'd rarely load all the posts for a category but there are other real world cases where it's less unreasonable you'd want to do this, and Posts + Categories is an easy way to demo it.

The problem

The built-in population feature is really useful; not just for