The usual suggestions from doctors for first line treatment are more mild medications that have drowsiness as a side effect, prescribed at low dose. I would actually prefer many of these low dose options over some of the high dose melatonin supplements. Melatonin is a hormone and taking it can throw off natural production
Modern orexin antagonist sleep medications are not addictive and can be taken long-term.
The reason diphenhydramine Is associated with a slight increase in dementia risk is the anticholinergic properties. This risk increase is from correlational studies on other medications with anticholinergic medicines being taken for many years by elderly people, so the risk of taking Benadryl occasionally is low to none.
The prescription alternatives like doxepin have the same antihistamine properties without the anticholinergic properties when used at the prescribed dose.
This is an example where people can get themselves into the wrong outcome by assuming anything their doctor prescribes is a last resort quick fix, but anything they can source by themself is safer and superior.
It’s not a good idea to take Benadryl for many years, but occasional use or even for months while going through a difficult period isn’t going to cause dementia.
Tramadol is routinely prescribed long term where I live. I know someone with a massive bottle good for something like 6 to 12 months of daily use. (I don't know if that's a good thing but it is certainly a thing.)
It has opioid effects and can cause drowsiness but would never be prescribed for sleep.
It’s also typically a controlled substance. I don’t know where you live but I’d be surprised if anyone was handed a 12 month supply in a bottle.