it seems to me that as soon as I do any query on the model, all of the relations are loaded.
Couldn't objection.js defer the loading of related models to the point when I use eager or related query?
Probably, but why would it?
We're using Objection.js on amazon lambda and we're sharing some DB models with our API. In the API we have many relations which aren't really needed for lambdas. On lambda every extra required file is an extra time to boot up the node.js process.
We'll sort it out for now by splitting our models into two and only defining relations we're actually using in both cases, but it sure would be nice if objection 2.0.0 or 3.0.0 could do this by itself.
I'll see if I can do that easily. I don't think it's worth too much effort and changes to add this, but if It's easy, I'll implement it right away.
@capaj The relation names need to be loaded for insert and update operations because they use fromJson which needs to know how to handle loaded relations. So if you make your requires inside a relationMappings getter, it will get called. For select operations it could be removed, but I don't know if that's enough.
If we supported modelClass "thunks" like this:
static relationMappings = {
things: {
modelClass: () => require('./Things')
}
}
we could get the relation names without invoking the require operations.
I could already optimize the case where modelClass is a file path so that the relations are not "inflated" before needed. Instead only the relationMappings keys are accessed for insert and update.
Are you using file paths or require's inside a getter?
file paths-for example:
groups: {
relation: Model.ManyToManyRelation,
modelClass: path.join(__dirname, '/GroupModel'),
join: {
from: 'users.id',
through: {
from: 'user_groups.user_id',
to: 'user_groups.group_id'
},
to: 'groups.id'
}
},
For select operations it could be removed, but I don't know if that's enough.
It would definitely solve a cyclic dep problem we currently have on one of our lambdas.
@capaj Could you try out this commit to see if it works as you'd expect?
I added tests. Relations should now be loaded lazily. I also added support for modelClass thunks:
groups: {
modelClass: () => require('./GroupModel')
}
aaaand now 1.5.1 is released with this fix.
oh my you are fast! I just updated to 1.5.1 from 1.4.0 and aside from the two errors our whole test suite passed and hopefully the lambda will be up and running in just a few minutes. Again-great work.
Most helpful comment
aaaand now 1.5.1 is released with this fix.