1. Yes, it is possible .
JavaScript code is text only. Technically, there is no problem with storing in the database. Whether it is recommendable or the best solution is another question.
2. It's safe? It depends.
It completely depends on what you intend to do with that code. Where it comes from, where it goes, and how it is used.
In the database , there is no JavaScript execution environment. It's safe. As stated above, it is just another string of text. You will need to take the precautions common to any other text content, notably escape , avoiding vulnerabilities that are independent of the text being "JavaScript code" or any other type of text.
In application , it depends a lot, varying from extreme to extreme: depending on whether the application is dangerous or secure.
To analyze, you have to start considering: How do you intend to use this code? Who can supply the bank with this code?
The danger of using user-generated JavaScript code is that a malicious user can easily program for an HTTP request to be triggered by triggering other functions of your application (for example, simulating the effect of a click on "Like ", send the authentication cookie to a remote server, et cetera ).