fixed code to add entry on first write to database and then update on subsequent saves