Can I save database from backend?

What I’m trying to achieve is to increase the download counter of an object whenever a person presses the download button. I’m doing this as the database is set to be updated by the author only. Is my code wrong or we can’t save the database from the backend.

Any help is appreciated. Thank you.

Backend code:

import wixData from ‘wix-data’;
.
.
export function setDownloadCounter (downloadCounter, dynamicDataset, setField){
//to add one to the original value
let count = parseInt(downloadCounter.text, 10) + 1;

dynamicDataset.setFieldValue(setField, count); 
dynamicDataset.save(); 
return count; 

}

Front end code:

import { setDownloadCounter } from ‘backend/counters.jsw’
.
.
setDownloadCounter ($w(‘#downloadCounter’), $w(“#dynamicDataset”), “downloadCounter”)
.then ((count) => {
$w(‘#downloadCounter’).text = count.toString();
});

You can write to a database in the backend, but only by using the wix-data API. Datasets, and the wix-dataset API, are not available in the backend. As stated in the wix-database API documentation:
“The wix-dataset API can only be used in your site’s front-end code.”

In the backend, you can use the wix-data save API with the suppressAuth option.

Thank you! So any advice on how to go about updating an element (download counter) on a dynamic item page which is connected to a database with permission set to able to be updated by the author only?

@iamhapot You can use the wix-data save API with the suppressAuth option.

@yisrael-wix Awesome! :v:t2: Would you mind making that another answer, as others might think one can’t save from backend if I mark the above as the best answer.

@iamhapot I edited my answer above.