API Operations

Overview

apiOperations provides predefined functions to perform CRUD operations within Supista ERP. This includes both standard table operations and advanced relational operations, which require a parent record ID (__d3__parentId) and a relation ID (__d3__relationId) to manage data in nested or related tables.

  1. createOp
  2. readOp
  3. updateOp
  4. removeOp
  5. bulkCreateOp
  6. fetchUsers
  7. generatePdf
  8. generateCSV
  9. sendMail

createOp

Usage:

const createdData = await createOp(tableName, payload);
  • tableName: The name of the table in the PostgreSQL database where a new row will be added.
  • payload: Contains the modified userData for the Create API request.

Return Value:
Returns the result of Sequelize dataModel.create() — a single object representing the newly created record.

Sample Output:

{
  "id": 1,
  "columnName1": "columnValue1",
  "columnName2": "columnValue2",
  "columnName3": "columnValue3"
}

Example Code:

async function customizeERP(userData, apiOperations) {
  const tableName = "table_name";
  const { createOp } = apiOperations;
  return new Promise(async (resolve) => {
    const payload = {
      __d3__newData: { ...userData?.__d3__newData }
    };
    const createdData = await createOp(tableName, payload);
    if (createdData?.id) {
      resolve({ result: createdData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not create data! Please try again later"
        }
      });
    }
  });
}

Example Input:

{
  "__d3__newData": {
    "name": "Sample",
    "bio": "Sample",
    "create at": "2025-06-11T09:02:01.794Z"
  }
}

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "count": 1,
        "rows": [
          {
            "id": 3,
            "name": "Sample",
            "bio": "Sample",
            "create at": "2025-06-11T09:02:01.794Z",
            "__d3__createdBy": 575,
            "__d3__updatedAt": "2025-06-11T09:08:50.082Z",
            "__d3__createdAt": "2025-06-11T09:08:50.082Z",
            "__d3__lastUpdatedBy": null,
            "__d3__deletedAt": null
          }
        ]
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

readOp

Usage:

const rowsData = await readOp(tableName, payload);
  • tableName: The name of the table in the PostgreSQL database to query data from.
  • payload: Contains the modified userData for the Read API request. For related tables, include __d3__parentId and __d3__relationId.

Return Value:
Returns the result of Sequelize dataModel.findAndCountAll() — an object containing the total count and matched rows.

Example Code:

async function customizeERP(userData, apiOperations) {
  const tableName = "table_name";
  const { readOp } = apiOperations;
  return new Promise(async (resolve) => {
    const payload = {
      __d3__filterdata: { where: {} }
    };
    const rowsData = await readOp(tableName, payload);
    if (rowsData?.count) {
      resolve({ result: rowsData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not create data! Please try again later"
        }
      });
    }
  });
}

Example Input:

{
  "__d3__filterdata": {
    "where": {}
  }
}

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "count": 2,
        "rows": [
          {
            "id": 1,
            "name": "Sample",
            "bio": "Sample",
            "create at": "2025-06-11T09:02:01.794Z",
            "__d3__createdBy": 575,
            "__d3__lastUpdatedBy": 575,
            "__d3__createdAt": "2025-05-21T16:28:50.975Z",
            "__d3__updatedAt": "2025-06-11T09:29:18.133Z",
            "__d3__deletedAt": null
          },
          {
            "id": 2,
            "name": "Sample_2",
            "bio": "Sample_2",
            "create at": "2025-06-11T09:02:01.794Z",
            "__d3__createdBy": 575,
            "__d3__lastUpdatedBy": 575,
            "__d3__createdAt": "2025-05-29T21:28:52.319Z",
            "__d3__updatedAt": "2025-05-29T21:28:52.319Z",
            "__d3__deletedAt": null
          }
        ]
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

updateOp

Usage:

const updatedData = await updateOp(tableName, payload);
  • tableName: The name of the table where a record is updated.
  • payload: Contains __d3__id and __d3__newData. For related tables, include __d3__parentId and __d3__relationId.

Return Value:
Returns the result of Sequelize dataModel.update() — the number of records updated.

Example Code:

async function customizeERP(userData, apiOperations) {
  const tableName = "table_name";
  const { updateOp } = apiOperations;
  return new Promise(async (resolve) => {
    const payload = {
      __d3__id: userData?.__d3__id,
      __d3__newData: { ...userData?.__d3__newData }
    };
    const updatedData = await updateOp(tableName, payload);
    if (updatedData?.affectedCount?.[0]) {
      resolve({ result: updatedData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not create data! Please try again later"
        }
      });
    }
  });
}

Example Input:

{
  "__d3__newData": {
    "id": 1,
    "name": "Sample",
    "bio": "Sample",
    "create at": "2025-06-11T09:02:01.794Z",
    "__d3__createdBy": 575,
    "__d3__lastUpdatedBy": null,
    "__d3__createdAt": "2025-05-21T16:28:50.975Z",
    "__d3__updatedAt": "2025-06-01T10:00:05.594Z",
    "__d3__deletedAt": null
  },
  "__d3__id": 4
}

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "affectedCount": [1]
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

removeOp

Usage:

const deletedData = await removeOp(tableName, payload);
  • tableName: Name of the table where a record is deleted.
  • payload: Contains __d3__id. For related tables, also include __d3__parentId and __d3__relationId.

Return Value:
Returns the result of Sequelize dataModel.delete() — number of records deleted.

Example Output:

{
  "deletedObjNo": 1
}

Example Code:

async function customizeERP(userData, apiOperations) {
  const tableName = "table_name";
  const { removeOp } = apiOperations;
  return new Promise(async (resolve) => {
    const payload = {
      __d3__id: [userData?.__d3__id]
    };
    const deletedData = await removeOp(tableName, payload);
    if (deletedData?.deletedObjNo) {
      resolve({ result: deletedData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not create data! Please try again later"
        }
      });
    }
  });
}

Example Input:

{
  "__d3__id": 4
}

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "deletedObjNo": 1
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

bulkCreateOp

Usage:

const bulkData = await bulkCreateOp(tableName, payload);
  • tableName: Name of the PostgreSQL table where multiple records will be inserted.
  • payload: Contains an array of objects under __d3__bulkData. For related tables, include __d3__parentId and __d3__relationId.

Example Payload:

{
  "__d3__bulkData": [
    {
      "columnName1": "columnValue1",
      "columnName2": "columnValue2",
      "columnName3": "columnValue3"
    },
    {
      "columnName1": "columnValue1",
      "columnName2": "columnValue2",
      "columnName3": "columnValue3"
    }
  ]
}

Return Value: Returns newly created records from Sequelize dataModel.bulkCreate().

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "bulkCreated": 2
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

Example Code:

async function customizeERP(userData, apiOperations) {
  const tableName = "table_name";
  const { bulkCreateOp } = apiOperations;
  return new Promise(async (resolve) => {
    const payload = {
      __d3__bulkData: userData.__d3__bulkData
    };
    const bulkData = await bulkCreateOp(tableName, payload);
    if (bulkData) {
      resolve({ result: bulkData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not create data! Please try again later"
        }
      });
    }
  });
}

fetchUsers

Usage:

const usersData = await fetchUsers(payload);
  • payload: Contains a list of user IDs under usersList.

Example Payload:

{
  "usersList": [1, 3, 5]
}

Return Value: Returns user records from Sequelize dataModel.findAll().

Example Output:

{
  "response": {
    "__d3__result": {
      "result": [
        {
          "id": 1,
          "userID": 1,
          "emailID": "johnDoe@gmail.com",
          "role": ["Admin"],
          "ITStakeholder": ["ITAdmin"],
          "erpRole": ["Admin"],
          "firstName": "joh",
          "lastName": "doe",
          "phone": "(+011) 9999999999",
          "addedByUserID": null,
          "addedByEmailID": null,
          "userInfo": {
            "manager": [],
            "coach": [],
            "subordinates": [[2, 3]],
            "coachees": [[2, 3]]
          },
          "manager": null,
          "coach": null,
          "__d3__createdAt": "2025-04-19T10:45:47.057Z",
          "__d3__updatedAt": "2025-05-24T16:19:11.541Z",
          "__d3__deletedAt": null
        }
      ]
    },
    "__d3__error": false,
    "printConsole": []
  }
}

Example Code:

async function customizeERP(userData, apiOperations) {
  return new Promise(async (resolve) => {
    const payload = {
      usersList: userData.usersList
    };
    const rowsData = await fetchUsers(payload);
    if (rowsData) {
      resolve({ result: rowsData });
    } else {
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not read data! Please try again later"
        }
      });
    }
  });
}

generatePdf

Usage:

const pdfsGenerated = await generatePdf(payload = {});
  • payload: Contains templateID, tableName, columnName (optional), and templateData.

Example Payload:

{
  "templateID": "template-id",
  "tableName": "table_name",
  "columnName": "optional-column-name",
  "templateData": [
    {
      "columnName1": "columnValue1",
      "columnName2": "columnValue2"
    }
  ]
}

Return Value: Returns PDF filenames uploaded to S3.

Example Output:

{
  "fileIds": ["invoice1.pdf", "invoice2.pdf"]
}

Example Code:

async function customizeERP(userData, apiOperations) {
  const { generatePdf } = apiOperations;
 
  const pdfPayload = {
    templateID: "your-template-id-here",
    tableName: "your-table-name",
    templateData: {
      row: userData
    }
  };
 
  const pdfResult = await generatePdf(pdfPayload);
 
  return {
    result: pdfResult,
    popupMsg: {
      type: "message",
      message: "PDF generated successfully."
    }
  };
}

generateCSV

Usage:

const csvGenerated = await generateCSV(payload = {});
  • payload: Contains title, tableName, columnName (optional), and an array of rows.

Example Payload:

{
  "title": "Customer List",
  "tableName": "customers",
  "rows": [
    {
      "name": "John",
      "email": "john@example.com"
    },
    {
      "name": "Jane",
      "email": "jane@example.com"
    }
  ]
}

Return Value: Returns CSV filenames uploaded to S3.

Example Output:

{
  "response": {
    "__d3__result": {
      "result": {
        "response": {
          "fileIds": [
            "table_name_Name of the CSV file_2025-06-12_vGGfWhHwb3.csv"
          ]
        },
        "__d3__success": true,
        "time": 1749719785038,
        "status": 200
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

Example Code:

async function customizeERP(userData, apiOperations) {
  if (userData && userData.length > 0) {
    const csvPayload = {
      title: "Name of the CSV file",
      tableName: "table_name",
      rows: userData
    };
    const csvResult = await generateCSV(csvPayload);
    return { result: csvResult };
  }
}

sendMail

Usage:

const emailSent = await sendMail(payload = {});
  • payload: Contains email template data and recipients.

Example Payload:

{
  "templateID": "id-of-the-template",
  "templateData": {
    "row": {
      "columnName1": "columnValue1",
      "columnName2": "columnValue2"
    }
  },
  "attachments": [],
  "recipients": {
    "to": ["recipient@example.com"]
  }
}

Return Value: Returns confirmation of email dispatch.

Example Output:

{
  "message": "Email sent successfully."
}

Example Code:

async function customizeERP(userData, apiOperations) {
  const { sendMail } = apiOperations;
 
  const emailPayload = {
    templateID: "your-email-template-id",
    templateData: {
      row: userData
    },
    attachments: [],
    recipients: {
      to: ["recipient@example.com"]
    }
  };
 
  const emailSent = await sendMail(emailPayload);
 
  return {
    result: emailSent,
    popupMsg: {
      type: "message",
      message: "Email sent successfully."
    }
  };
}
Last updated on
Transform Data into Decisions with Supista – Your Intelligent Data Partner
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. Learn More.