model query middleware