MgFunction

If you like to create custom function such as on AWS Lambda or Google Cloud Function, Microgen also give you that super power. Every function run on its own node or even clustered server. Function in microgen is called MgFunction.

Creating a MgFunction

Go to MgFunction menu on microgen, then create a new function via plus fab button

There are four trigger type on MgFunction such as, http, pub-sub, responder-requester, and table. Every trigger has its own unique functionality. We'll discuss it later.

Function "handler" is the main function that directly called on server. You can make another private function but, thats not will be called directly when server running, but should be calling inside the handler function.

Http Trigger

MgFunction http is a simple http protocol, or you can simply make a REST API here. You can choose GET, POST, PATCH, or DELETE method via the dropdown.

Parameter References

In every method you'll noticed that there is a dependency injection parameter such as "app", "context", and "callback".

Parameter

Description

app.getRequester

to get Event sourcing topic from responder-requester trigger

app.getPublisher

to get Publisher and publish to its topic, to be called on subscriptions

context.body

get the http body

context.cookies

get the http cookies

context.method

get the http method type

context.params

get the http params

context.query

get the http query

context.headers

get the http headers

callback

return error and response data

Creating Http Trigger

You can either choose GET, POST, PATCH, or DELETE method. In this example we'll make a simple GET method.

Because we choose Http trigger, and GET method, later you can access it directly via REST API. This function will return a response that consist of simple hello message, and context data. Click "Save & Deploy" to run this function, and you can access it via CURL or browser.

$ curl YOUR_MICROGEN_ENDPOINT/function/hellomgfunction

Creating Http Trigger (Legacy)

If you are an old user in microgen, you will be noticed that the API code hook is not there anymore. It's because we move the API code in the mgFunction, so you can use it as normal function, and not losing your old API.

On the method dropdown, choose legacy method. Then you can simply make any API that you need.

to be noticed, that you must Re-Run the microgen in order to deploy this function.

Pub-Sub Trigger

MgFunction pub-sub is a simple Subscriber to listen any Publisher that trigger an event. This can be useful for asynchronous call such as booking, transaction, etc. In MgFunction, you can choose trigger type "pub-sub", and set the topic as you need.

Parameter References

Parameter

Description

app.getRequester

to get Event sourcing topic from responder-requester trigger

app.getPublisher

to get Publisher and publish to its topic, to be called on subscriptions

context

any

Creating pub-sub trigger

We'll make simple booking function that save to transaction db when success, and sending an email to the user.

/**
 * Responds to Pub/Sub.
 *
 * @param { app, context }
 * app { getRequester, getPublisher }
 * context: any
 */

exports.handler = ({ app, context }) => {

  //save to transaction table
  await app.getRequester('transaction').send({
      type: 'create',
      body: {
          hotelId: context.hotelId
      },
  })

  //sending an email to user
  await app.getRequester("email").send({
    type: "send",
    body: {
      to: email,
      subject: "Booking Success",
      body:
        "Your Booking is success",
    },
  });
  
  console.log("success")  
}

save the function with any name, and set the topic to "booking"

Trigger the subscriptions function

You can call the subscriptions function either via GraphQL publish mutation or via http call. Here is the example how to trigger the subscriptions via GraphQL

mutation {
  publish(topic: "booking", data: {
    hotelId: "t5gbbeuy663eEcf",
    email: "someuser@email.com"
  }){
    data
  }
}

or you can make simple http function to call this subscriptions.

then you can call it via CURL or API

$ curl YOUR_MICROGEN_ENDPOINT/function/bookpub

Listening the Subscriptions (Client Side)

You can listen the subscriptions on client side using GraphQL like this

subscription {
  subscribe(topic: "booking"){
    data
  }
}

Responder-Requester Trigger

MgFunction responder-requester is a simple Responder to listen any Requester that trigger an event. This can be useful for asynchronous call such as booking, transaction, etc. In MgFunction, you can choose trigger type "responder-requester", and set the topic as you need. The difference with pub-sub is, you can return a response from the Responder.

Parameter References

Parameter

Description

app.getRequester

to get Event sourcing topic from responder-requester trigger

app.getPublisher

to get Publisher and publish to its topic, to be called on subscriptions

context

any

callback

return error and response data

Creating responder-requester trigger

We'll make simple booking function that save to transaction db when success, and sending an email to the user.

/**
 * Responds to Responder/Requster.
 *
 * @param { app, context, callback }
 * app { getRequester, getPublisher }
 * context: any
 * callback(error, response) 
 */

exports.handler = async ({ app, context, callback }) => {
  //save to transaction table
  const transaction = await app.getRequester('transaction').send({
      type: 'create',
      body: {
          hotelId: context.hotelId
      },
  })

  //sending an email to user
  await app.getRequester("email").send({
    type: "send",
    body: {
      to: email,
      subject: "Booking Success",
      body:
        "Your Booking is success",
    },
  });

  //return transaction object
  callback(null, transaction);
}

Save the file name with "bookingresponder", and set the type to "booking". As we can see, Its not like a subscriptions because we can actually send a response to the requester whenever any requester triggering this event.

Trigger the responder function

You can call the responder function either via GraphQL publish mutation or via http call. Here is the example how to trigger the responder via GraphQL

mutation {
  mgFunction(name: "bookingresponder", type: "booking", data: {
    hotelId: "t5gbbeuy663eEcf",
    email: "someuser@email.com"
  }){
    type,
    data
  }
}

or you can make simple http function to call this subscriptions.

then you can call it via CURL or API

$ curl YOUR_MICROGEN_ENDPOINT/function/bookrequester

Table Trigger

Table Trigger is work like hook on table services. Whether it afterFind, beforeFind, afterCreate, beforeCreate etc. We can do something like:

  • modify data context

  • triggering event (pubsub or requester-responder)

  • calling another API

  • validation (user, form, etc)

  • change query

Parameter References

Parameter

Description

app.getRequester

to get Event sourcing topic from responder-requester trigger

app.getPublisher

to get Publisher and publish to its topic, to be called on subscriptions

app.env

get the env variables

context.data

get the user input data

context.result

get the table data

context.params.user

get the user data

context.params.query

get the table current query

context.params.headers

get the http header (Token etc)

Creating Table Trigger

Choose a designated table and one event whether beforeFind, afterFind, beforeCreate, etc. Then we can modify the context, sending event, calling third party API, etc.

For example we'll make a beforeFind event on a table.

/**
 * Responds to Table Event.
 *
 * @param { app, context, callback }
 * app { getRequester, getPublisher, env }
 * context { data, params, query, headers }
 */
 
import axios from 'axios';

exports.handler = ({ app, context }) => {

  //modify context
  context.someData = "change the data";
  
  //sending event
  app.getRequester("email").send({
    ...
  })  
  
  //calling third party API
  axios.get(...)
  
  //check the user
  const user = context.params.user
  
  //change query
  context.params.query = {$limit: 2}
  
  //etc
  
  
  return context;
}

Creating Table Trigger (Legacy)

The legacy one actually almost the same with the normal table trigger function. The difference is we must Re-run the microgen in order to make it work, and all trigger function can be used on single place like this.

module.exports = (app) => ({
    before: {
        find: async (context) => {
            //do something before find request
        },
        get: async (context) => {
            //do something before get request            
        },
        create: async (context) => {
            //do something before create request
        },
        patch: async (context) => {
            //do something before patch request
        },
        delete: async (context) => {
            //do something before delete request
        },
    },
    after: {
        find: async (context) => {
            //do something after find request
        },
        get: async (context) => {
            //do something after get request
        },
        create: async (context) => {
            //do something after create request
        },
        patch: async (context) => {
            //do something after patch request
        },
        delete: async (context) => {
            //do something after delete request
        },
        register: async (context) => {
            //do something after register request
        },
        login: async (context) => {
            //do something after login request
        },
        requestOtp: async (context) => {
            //do something after requestOtp request
        },
        verifyOtp: async (context) => {
            //do something after requestOtp request
        }
    },
    permissions: null
})

Trigger the Table Trigger Function

Whenever any table action triggered, it will automatically call the function that you already setting up. So it can be from anywhere, whether it an event, http call, or direct table access.

Last updated