refactor database functions into a single file to prepare for postgres migration