Supista
Contact Us
documentation
Supista ERP
Parameters
Entity Relationships
Has One Relation

hasOne relation CRUD examples using OpWithRelation functions

In Supista ERP, a hasOne relation establishes a one-to-one relationship between two components, where one record in the parent table is linked to exactly one record in the child table.

Unlike hasMany, where one parent can have many children, hasOne ensures there is only a single child record per parent.


Example Scenario: A Member has one Profile

Parent Table: Members
Child Table: Profiles

Supista creates a column in Profiles: Members - Profiles ID

Each profile is linked to exactly one member.


createOpWithRelation

Creates a new child record (Profile) and links it to a specific Member.

Example Code

async function customizeERP(userData, apiOperations) {
 
  const parentTableName = "members";
 
  return new Promise(async (resolve, reject) => {
 
    const payload = {
      __d3__newData: { ...userData?.__d3__newData }
    };
 
    const createdData = await createOpWithRelation(
      parentTableName,
      payload,
      userData?.__d3__parentId,
      userData?.__d3__relationId
    );
 
    if (createdData?.id) {
 
      resolve({ result: createdData });
 
    } else {
 
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not read data! Please try again later"
        }
      });
 
    }
 
  });
 
}

Example input

{
  "__d3__newData": {
    "bio": "frontend developer"
  },
  "__d3__parentId": 2,
  "__d3__relationId": "254624385y"
}

Example Output

{
  "response": {
    "__d3__result": {
      "result": {
        "id": 3,
        "bio": "frontend developer",
        "members - profiles ID": 2,
        "__d3__createdBy": 575,
        "__d3__updatedAt": "2025-06-18T08:53:53.539Z",
        "__d3__createdAt": "2025-06-18T08:53:53.539Z",
        "__d3__lastUpdatedBy": null,
        "__d3__deletedAt": null
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

readOpWithRelation

Fetches the profile that belongs to a specific Member.

Example Code

async function customizeERP(userData, apiOperations) {
 
  const parentTableName = "members";
 
  return new Promise(async (resolve, reject) => {
 
    const payload = {
      __d3__filterdata: {
        where: {}
      }
    };
 
    const rowsData = await readOpWithRelation(
      parentTableName,
      payload,
      userData?.__d3__parentId,
      userData?.__d3__relationId
    );
 
    if (rowsData?.count) {
 
      resolve({ result: rowsData });
 
    } else {
 
      resolve({
        result: {},
        popupMsg: {
          type: "error",
          message: "Could Not read data! Please try again later"
        }
      });
 
    }
 
  });
 
}

Example input

{
  "__d3__filterdata": {
    "where": {}
  },
  "__d3__parentId": 2,
  "__d3__relationId": "254624385y"
}

Example Output

{
  "response": {
    "__d3__result": {
      "result": {
        "count": 1,
        "rows": [
          {
            "id": 1,
            "bio": "frontend developer",
            "members - profiles ID": 2,
            "__d3__createdBy": 575,
            "__d3__lastUpdatedBy": null,
            "__d3__createdAt": "2025-06-16T20:16:25.415Z",
            "__d3__updatedAt": "2025-06-16T20:16:25.415Z",
            "__d3__deletedAt": null
          }
        ]
      }
    },
    "__d3__error": false,
    "printConsole": []
  }
}

updateOpWithRelation

Updates a member’s profile only if it belongs to the specified Member.

Example Code

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

Example input

{
  "__d3__newData": {
    "bio": "Frontend developer and designer",
    "members - profiles ID": 2
  },
  "__d3__id": 3,
  "__d3__parentId": 2,
  "__d3__relationId": "254624385y"
}

Example Output

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

removeOpWithRelation

Deletes a profile record that belongs to the specified Member.

Example Code

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

Example input

{
  "__d3__parentId": 2,
  "__d3__id": 3,
  "__d3__relationId": "254624385y"
}

Example Output

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

---
Transform Data into Decisions with Supista – Your Intelligent Data Partner
AI-Powered Operational Intelligence for Growing Businesses

Automate your operations. Orchestrate your workflows. Scale with confidence.

Book a free strategy session with our Automation Experts and discover how Supista’s AI-Composable ERP can transform your operations into a scalable and intelligent system.