Okay first of all.......ALL puppies get worms, about 99% of them are born with them, what you described as spaghetti is round worms..... 2nd of all OTC dewormers are the worst and when the label says don't give them to puppies under 6 weeks, sadly that is there for a reason. The second you spotted the worms you should have taken them to your vet, if they were infested that bad, your vet could of given and injection for the worms, which would of cleared them up in about 72 hours for the most part, then usually you go back about 10-21 days later for either another injection, pill or syrup form... depending on the exact med's your vet would of used, would depend on the time period between doses.
Sadly there is no real way to prevent puppies from getting worms, it's almost mother nature "sort of speaking" I know that could be worded differently, but that is what popped in my head.
No it is not safe to de-worm a pregnant dog, if you are planning on breeding again, then I would do some research and sit down and talk with your vet on what options are best for you and your dog for future breeding. Also keep in mind, if your going to breed make sure you have a good vet and money put away for emergencies, as you can never be to sure what could happen. Please don't think I'm lecturing you, because I'm not, I'm only trying to help
I'm sorry to hear about the loss of the 2 puppies