refactor dynamic schema and middlweare declaration