refactor db code in server.py