Saturday, November 3, 2018

Split and Deploy Google Cloud Functions Code (Simple Way)

1- Suppose you already created your new app and have index.ts file contain the following:

import * as functions from 'firebase-functions';

export const helloWorld = functions.https.onRequest((request, response) => {
const message = request.query.message || request.body.message || 'Hello World!';
response.status(200).send(message);
});


2- Create a new file called groupA.ts contain the following :

import * as functions from 'firebase-functions';

exports.groupA = {
function1: functions.https.onRequest((request, response) => {
response.send('Hello from function1!');
}),
function2: functions.database.ref('path').onWrite((change, context) => {
response.send('Hello from function2!');
})
};

exports.modifyUser = functions.firestore
.document('users/{userID}')
.onWrite((change, context) => {
// Get an object with the current document value.
// If the document does not exist, it has been deleted.
const document = change.after.exists ? change.after.data() : null;

// Get an object with the previous document value (for update or delete)
const oldDocument = change.before.data();

// Get the user object with the new changes,
const newData = change.after.data();

});


You have now 3 functions:
- function1 &  function2 inside groupA 
- modifyUser as separate one

3- Inside index.ts file add :
exports.groupA = require('./groupA');

4- Now you can deploy to your Google cloud using one of the following:
    firebase deploy --only functions
    firebase deploy --only functions:groupA
  firebase deploy --only functions:groupA.function1,modifyUser
    firebase deploy --only functions:function1,functions:function2


No comments: