Open tholo opened 4 years ago
You can override the static query(on: Database)
method of Model protocol directly in your User
model :
public static func query(on database: Database) -> QueryBuilder<User> {
return QueryBuilder(database: database)
.with(\.$roles)
}
It would often be very helpful if it would be possibly to hook into
ModelAuthenticatable
andModelTokenAuthenticatable
to cause related data to be eager loaded.One area where this would be very useful would be if a
User
has a relationship to e.g.Role
, which is then used to validate actual access to resources, for instance:With such a setup one would rather often want to be able to access
User.roles
to validate access to some resource, but with the current implementation ofModelAuthenticatable
andModelTokenAuthenticatable
one has to loadUser.$roles
in each and every instance one wants to check them.