objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Alternatives for a lifecycle hook after all eager fetching?

Open GreatSchool2 opened this issue 7 years ago • 3 comments

I'd like to be able to process a model after it's satisfied/hydrated its relations with an eager query. This is because the object as produced by following the relations is a bit more verbose than necessary.

I'm modelling the join table as a Model itself so I can have access to its extra columns. The result is that the related object is accessible only by first going through this join table model, which I'd like to be able to get rid of in a "post-eager" lifecycle hook.

GreatSchool2 avatar Apr 11 '19 16:04 GreatSchool2

Why couldn't you use the extra feature for many-to-many? https://vincit.github.io/objection.js/recipes/extra-properties.html#join-table-extra-properties

koskimas avatar Apr 11 '19 16:04 koskimas

I'll admit it didn't work at first (I didn't see the extra column in parseDatabaseJson), so I found another suggestion and just went with that.

GreatSchool2 avatar Apr 11 '19 16:04 GreatSchool2

Ok. Well the extra properties should be in parseDatabaseJson already. If you want to go with your current implementation, there isn't such hook yet. There will be soon when I release objection 2.0.

koskimas avatar Apr 11 '19 16:04 koskimas