GraphletDB

Extensible property graph database stored in JSON-LD.

Function Definitions

Helper Functions

CRUD Node Handling Functions

Validation Functions

Reference Management Functions

API: Helper Functions

🔧 aboutGraphletDB()

This helper function logs the current version of GraphletDB to the console.

aboutGraphletDB();  // Outputs "GraphletDB v0.0.1"

🔧 getRandomToken(nodes, len)

This helper function creates a random hexadecimal token of the length specified by len. It ensures the uniqueness of the token within the provided nodes array. Each node in the array is expected to have an id property, which is used to check for uniqueness. The function repeatedly generates new tokens until it finds one that is not already present in the nodes array.

Parameters:

Returns a unique lowercase hexadecimal string of the specified length.

let nodes = [{ id: 'a1b2' }, { id: 'c3d4' }];
    let tokenLength = 4;
    let newToken = getRandomToken(nodes, tokenLength);
    console.log(newToken); // Outputs a unique 4-character hexadecimal string

🔧 getListOfLabels(nodes, objOrIds)

This helper function returns a list of all nodes with the label Label. If the objOrIds parameter is obj, the function returns a list of Label node objects. If the objOrIds parameter is id, the function returns a list of id strings.

Parameters:

Returns an array of either id strings or full node objects, depending on the objOrIds parameter value.

let nodes = [
        { id: 'node1', label: 'Label', data: {...} },
        { id: 'node2', label: 'Label', data: {...} },
    ];
    
    // Example 1: Get list of node objects
    let labelObjects = getListOfLabels('obj');
    console.log(labelObjects);
    
    // Example 2: Get list of id strings
    let labelIds = getListOfLabels('id');
    console.log(labelIds);

CRUD Node Handler Functions

🔧 initList()

This function initializes returns a predefined minimal list that meets the GraphletDB schema. This can be used to set up or reset a list to a known default state that contains one Label object.

Returns an array containing a single Label object. The structure and content of the Label object are determined by the initLabelNode.

let newList = initList();
    console.log(newList); // Outputs the content of initLabelNode

🔧 initNode(nodes, label)

If a Label node exists for that label, create and return a list containing the new node using the Label node as a template.

If a Label node does not exist for that label, create the Label node, then create and return an array of both node

Return a single object of type Label, if that label exists.

🔧 getNodeByKeyPair(nodes, key, value, boolFirstOnly)

This function retrieves one or more nodes from a list based on a specified key-value pair. If boolFirstOnly is true, the function returns the first node that matches the key-value pair. If false, it returns all nodes matching the key-value pair.

Parameters:

Returns an array containing the matching node(s). If boolFirstOnly is true, the array contains only the first matching node or is empty if no match is found. If boolFirstOnly is false, the array contains all nodes that match the key-value pair or is empty if no matches are found.

let nodes = [
        { id: 'node1', type: 'Label', data: {...} },
        { id: 'node2', type: 'Label', data: {...} },
    ];
    
    let firstLabelNode = getNodeByKeyPair(nodes, 'type', 'Label', true);
    console.log(firstLabelNode);
    
    let allLabelNodes = getNodeByKeyPair(nodes, 'type', 'Label', false);
    console.log(allLabelNodes);

🔧 addNode(nodes, nodeToAdd)

🔧 removeNode(nodes, nodeToRemove)

🔧 updateNode(nodes, nodeToUpdate)

Validation Functions

🔧 validateList(nodes, doFix)

Optional: if doFix is true, return a fixed list.

Reference Management Functions

🔧 convertNodeToCslJson(nodes, nodeId)

🔧 convertNodeFromCslJson(nodes, nodeId)