save-order-datastore

2.1244.0 • Public • Published

Cloud function Save order raw data to datastore

What is it

This cloud function is here to register raw data from microservices and the api inside datastore.

Dependencies

Install

  1. Install node dependencies
yarn lerna bootstrap
  1. Authenticate with Google Default Credential
gcloud auth application-default login

Your user will be used to deploy the serverless stack, you must have at least the following roles on the targeted project:

  • Deployment Manager Editor
  • Storage Admin
  • Logging Admin
  • Cloud Functions Developer
  1. copy .env.exemple

Development workflow

In your local machine, functions are run individually in tests. They are never run in a local stack.

Working on an existing function

  1. Write your code changes

  2. Test it with unit tests

  3. Trigger the function locally in integration with the dev stack (cf local invocation)

  4. Deploy the stack: yarn sls deploy

  5. Test in integration with the dev stack

    You can use

    yarn sls invoke -f <functionName> -p <path/to/an/event/mock>
    

    to trigger your function with a mocked payload

    or trigger the service which will trigger the function with a real payload

Create a new function

  1. Write an empty handler which log the event

  2. Configure the triggers of the function

  3. Deploy the stack: yarn sls deploy

  4. Trigger the function with the trigger

  5. Get the logged event and put it in a mock.json to mock the event to help the local development

  6. Write a test per possible trigger event

  7. Write your code changes

  8. Test it with unit tests

  9. Trigger the function locally in integration with the dev stack (cf local invocation)

  10. Deploy the stack: yarn sls deploy

  11. Test in the dev stack

    You can use

    yarn sls invoke -f <functionName> -p <path/to/an/event/mock>
    

    to trigger your function with a mocked payload

    or trigger the service which will trigger the function with a real payload

Local invocation

🚧 WIP: this will be simplified 🚧

Functions Framework is used to locally invoke the functions. It starts a development server which handles the http request as CGP handle it and trigger the local cloud function in the closest way of the reality. Functions Framework only handle compiled JS.

  1. Compile and start the function in the development server
FUNCTION_NAME=<functionName> TYPE=<event|http> yarn local

invoke-local

This will build the code and start it in an express server

  1. Request the development server with a http request

You can use Curl or PostMan for example

curl --location --request POST 'http://localhost:8080/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "orderId": "77b787d4-e9eb-4797-b78f-53da679a831c",
    "restaurantId": 623
}'

Good to know:

  • To invoke a http triggered function with json body, you need to provide the header Content-Type with application/json
  • To invoke an event triggered function, call the server with a json body composed of two attributes:
    • data (required): the event
    • context (optional): the context
  • You will really interact with the real stack. Never set production variables in your .env
  • The GCP libraries will authenticate them self with the Google Default Credential
    • credentials from GOOGLE_APPLICATION_CREDENTIALS if exist
    • credentials created by gcloud auth application-default login otherwise (recommended)
  • You need to rebuild (yarn local) between each code modifications. There is currently no watch.

Next steps

Automate this in serverless-google-cloudfunctions to run it in one command

sls invoke local -f $FUNCTION_NAME -p $PAYLOAD_PATH

Readme

Keywords

none

Package Sidebar

Install

npm i save-order-datastore

Weekly Downloads

1

Version

2.1244.0

License

ISC

Unpacked Size

363 kB

Total Files

25

Last publish

Collaborators

  • kdescamps