Creating Serverless Workboard with Netlify Lambda — Part Two

Jadranko Dragoje
NSoft
Published in
3 min readFeb 3, 2020

In Part One, we have explained what serverless is, how to use Netlify Lambda, and created a simple example of functions. In this article, using lambda functions, we will create an API needed to run a simple workboard.

To have a working API for client application to consume, we need to have these functionalities:

  • View and search boards
  • View, create, update and delete board
  • View, create, update and delete columns
  • View, create, update and delete tasks

Extract Database Connection Handling

Since connecting to the database will be a common task, we will extract this functionality to utility function and use it in lambda functions.

Database Utility File

Create Boards Function

This function will provide a client application with a list of boards and the ability to search them by name. We will create a new function boards.js for this purpose.

Boards Function

Now, we have a GET /boards route that returns response data consumable by a client application. We can use this data to build a workboard list.

Boards Response

We can paginate data using query parameters: limit and offset, and filter data using a query parameter name.

Modify Board Function

To get a single board detail, we will modify board function. We need to find the board by an internal identifier which we will pass using a query parameter. Since we will find the document by internal _id , we need to use ObjectId.createFromHexString method which we will add to db utility file.

Board Function

Since we need to support adding, editing and deleting actions, we will update the board function to implement POST request with different actions.

Board Actions

Implement Board Columns Manipulation

Since each board can have board columns, we will support manipulation of columns inside board documents. One board can have zero or more columns.

Manipulate with board columns

Creating function for tasks

Task is created inside a column of the board meaning that it is necessary to send the column and board inside the payload of task creation and retrieve action. Since each task has id, for delete and update actions, it is enough to pass id. We will also create a new task collection inside the Mongo database. In this example, we will add just a few properties for task document and expand them once UI is built.

Since all tasks need to be shown inside the board column, paging is not necessary, but we will keep it to limit tasks in the column to some reasonable value.

Manipulate with board tasks

Since functions need to be kept small, I did not use any utility functions like lodash for now. It is visible that we could use thedefaultTo method of library to create query parameters fallback. We will check for possible optimisations with tree-shaking in further articles.

Conclusion

Lambda functions can be used to create API in single-page applications. Every function is a single path in the API URL. We differentiate actions using HTTP verbs GET and POST and using custom action property inside the payload as we did in this case.

State of the application in this stage can be viewed on apiLambda branch of the repository: https://github.com/manico/workboard-lambda/tree/apiLambda

In Part Three, we will create a visual representation of the workboard using functions as an API and upgrade some parts of the API.

--

--