Using JavaScript
You can use XMLWriter to create an XML. Then you can pass this XML as a string to your web server. Check this out for XML Writer.
Here is a tutorial for the available methods for writing an XML using XMLWriter - Tutorial
Using Nodejs
You can write a node js function for generating the sitemap from the generated URLs. Please refer to this link for a sample.
The better way is to create a script in the backend to generate it periodically to update sitemaps. Query the tables, create URLs and generate XML files. You can configure cron jobs in the server to generate the sitemaps periodically.
There will be libraries and tutorials available for each BE technology for generating the sitemap.
For example:
Ruby on Rails - Nokogiri
Elixir - sitemap
.Net - Tutorial
Java - Tutorial
Express JS - express-sitemap