Configure the Advanced IO Function

Now, we will begin coding the task manager application by configuring the function component.

The function’s directory, functions/task_manager_function contains:

You will be adding code in the index.js file.

The Advanced I/O function contains the following functionalities:

Request Method Endpoint Purpose
POST /addtask To create a new task.
GET /filtertask To filter created tasks by UserID, TaskName or Status fields.
DELETE /deletetask To delete tasks that are no longer required.
POST /updatetask To update the content of an existing task.

Install Required Dependencies

We will be using the Express and Axios framework to code the Advanced I/O function. To import the Express and Axios package in your function’s code, you must install the the required dependency in your system.

Package Name Purpose
Express To route HTTP requests.
Axios To send and handle HTTP requests

To install them in your local machine, navigate to the function’s home directory (functions/task_manager_function) in your terminal and execute the following command:

copy
$
npm install express axios --save

catalyst_tutorials_nosql_taskmanager_func_cli_pack_install

This information will also be updated in the package.json file. catalyst_tutorials_nosql_taskmanager_packagejson

Now, let’s begin coding the Advanced I/O function.

Copy the code given below and paste it in the index.js in the functions/task_manager_function directory of your project, and save the file. You can use any IDE of your choice to work with the application’s files.

Note: Please go through the code in this section to make sure you fully understand it.
index.js
copy
'use strict';
var express=require('express');
var catalyst=require('zcatalyst-sdk-node');
const{NoSQLItem}=require('zcatalyst-sdk-node/lib/no-sql');
const{NoSQLReturnValue,NoSQLConditionGroupOperator}=require('zcatalyst-sdk-node/lib/no-sql/enum');
const{NoSQLMarshall,NoSQLEnum}=require('zcatalyst-sdk-node/lib/no-sql');
const{NoSQLOperator}=NoSQLEnum;
var app=express();
app.use(express.json());
app.use(express.static('public'));
// Add a task
app.post('/addtask',async(req,res)=>{
  let{userID,taskName,dueDate,priority,status}=req.body;
  var capp=catalyst.initialize(req);
  const nosql=capp.nosql();
  const table=nosql.table('YOUR_TABLE_ID');
  try{
    await table.insertItems({
      item:NoSQLItem.from({UserID:userID,DueDate:dueDate,TaskName:taskName,Priority:priority,Status:status}),
      return:NoSQLReturnValue.NULL
    });
    res.send({message:"Thanks! Your task has been successfully inserted."});
  }catch(error){
    res.status(500).send({error:"Internal server error occurred. Please try again in some time."});
  }
});
// Filter task
app.get('/filtertask',async(req,res)=>{
  const query=req.query;
  const parsedData={index_data:{userID:query.userID,taskName:query.taskName,status:query.status}};
  const userId=parsedData.index_data.userID;
  var capp=catalyst.initialize(req);
  const nosql=capp.nosql();
  const table=nosql.table('YOUR_TABLE_ID');
  try{
    if(userId!=null&&parsedData.index_data.taskName===''){
      if(parsedData.index_data.status==='')return res.status(400).json({error:"Oops! You forgot to select a status."});
      const groupOpInsert=await table.queryTable({
        key_condition:{attribute:'UserID',operator:NoSQLOperator.EQUALS,value:NoSQLMarshall.makeString(parsedData.index_data.userID)},
        consistent_read:true,
        limit:10,
        forward_scan:true
      });
      let responseData=[];
      groupOpInsert.getResponseData().forEach(data=>{
        responseData.push({
          UserID:data.item.get("UserID"),
          TaskName:data.item.get("TaskName"),
          DueDate:data.item.get("DueDate"),
          Priority:data.item.get("Priority"),
          Status:data.item.get("Status")
        });
      });
      const filteredData=await filterByStatus(responseData,parsedData);
      res.status(200).send(filteredData);
    }else if(userId!=null&&parsedData.index_data.taskName!=null){
      if(parsedData.index_data.status==='')return res.status(400).json({error:"Oops! You forgot to select a status."});
      const groupOpInsert=await table.queryTable({
        key_condition:{
          group_operator:NoSQLConditionGroupOperator.AND,
          group:[
            {attribute:'UserID',operator:NoSQLOperator.EQUALS,value:NoSQLMarshall.makeString(parsedData.index_data.userID)},
            {attribute:'TaskName',operator:NoSQLOperator.EQUALS,value:NoSQLMarshall.makeString(parsedData.index_data.taskName)}
          ]
        },
        consistent_read:true,
        limit:10,
        forward_scan:true
      });
      let responseData=[];
      groupOpInsert.getResponseData().forEach(data=>{
        responseData.push({
          UserID:data.item.get("UserID"),
          TaskName:data.item.get("TaskName"),
          DueDate:data.item.get("DueDate"),
          Priority:data.item.get("Priority"),
          Status:data.item.get("Status")
        });
      });
      const filteredData=await filterByStatus(responseData,parsedData);
      res.send(filteredData);
    }else return res.send("Kindly enter partiyion key user ID and sort key Taskname");
  }catch(error){
    res.status(500).send({error:"Internal server error occurred. Please try again in some time."});
  }
});
// Delete Task
app.delete('/deletetask',async(req,res)=>{
  const query=req.query;
  const parsedData={index_data:{userID:query.userID,taskName:query.taskName}};
  var capp=catalyst.initialize(req);
  const nosql=capp.nosql();
  const table=nosql.table('YOUR_TABLE_ID');
  try{
    await table.deleteItems({keys:NoSQLItem.from({UserID:parsedData.index_data.userID,TaskName:parsedData.index_data.taskName})});
    res.status(200).json({message:"Data deleted successfully!"});
  }catch(error){
    res.status(500).send({error:"Internal server error occurred. Please try again in some time."});
  }
});
// Update Task
app.post('/updatetask',async(req,res)=>{
  let{UserID,TaskName,DueDate,Priority,Status}=req.body;
  var capp=catalyst.initialize(req);
  const nosql=capp.nosql();
  const table=nosql.table('YOUR_TABLE_ID');
  try{
    await table.insertItems({
      item:NoSQLItem.from({UserID:UserID,DueDate:DueDate,TaskName:TaskName,Priority:Priority,Status:Status}),
      return:NoSQLReturnValue.NULL
    });
    res.send({message:"Items updated successfully!"});
  }catch(error){
    res.status(500).send({error:"Internal server error occurred. Please try again in some time."});
  }
});
async function filterByStatus(data,parseData){
  return data.filter(task=>task.Status.toLowerCase()===parseData.index_data.status.toLowerCase());
}
module.exports=app;
View more
Note: Ensure you provide your table ID in lines 16, 34, 93, and 106.

The function directory is now configured. We can now proceed to configuring the client directory.

Last Updated 2025-11-26 15:38:32 +0530 IST