switched the complete javascript logic to use primary keys