Fix ifdefs
Initial design of database API

Far from stable. For example search interface is wrong for now, but we will
need to figure out how to handle it correctly.