firebase database write only for cloud function code example

Example: firebase functions add to database

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);

exports.onMsgAdded = functions.database.ref('/users/{uid}/messages/{to_uid}/{msg}').onCreate((snap, context) =>{
    const data = snap.val();
    const msgRoot = admin.database().ref('/users/' + context.params.to_uid + '/messages/' + context.params.uid);
    return msgRoot.set(data);
});