How to handle cancellation and termination gracefully

When ever a cancellation or termination of a workflow is done from the ui or client. How can we gracefully handle it using typescript sdk?

I need to execute a db operation in the workflow before workflow is terminated or cancelled.

For workflow cancelation to run activity it needs to exec in noncancellable scope: Class: CancellationScope | Temporal TypeScript SDK API Reference

Your worker cannot react to workflow termination so preferred not to terminate executions in most if not all cases if possible.