Injection MgSO4

Currently in 22 week . I am 41 yrs old and this is my first pregnancy. My dr prescribed for MgSO4 injection at 30 th week. Does anyone know the purpose of this injection. I don’t have high blood pressure complaint so far.. I have gestational diabetics. I came to know that I need to be in hospital for 2 days after injection. I am scared.. anyone who have experienced this pls share. Thanks