refactor and more intelligently handle sqlite-javascript callbacks