Skip to main content

Node Class

Represents a node entity in the graph.

Import

import { Node } from 'grafio';

Properties

id

readonly id: string

Unique identifier (auto-generated UUID).

type

readonly type: string

The node type label (e.g., 'Person', 'Course').

properties

readonly properties: Readonly<Record<string, Primitive>>

The node's key-value properties. Properties are deep-frozen for immutability.

Methods

toJSON()

toJSON(): NodeData

Converts the node to a JSON-serializable object.

Returns: NodeData

interface NodeData {
id: string;
type: string;
properties: Record<string, Primitive>;
}

Example

const node = await graph.addNode('Person', { name: 'Alice', age: 30 });

console.log(node.id); // 'uuid-xxxx-xxxx'
console.log(node.type); // 'Person'
console.log(node.properties); // { name: 'Alice', age: 30 }

// Serialize
const json = node.toJSON();
/*
{
id: 'uuid-xxxx-xxxx',
type: 'Person',
properties: { name: 'Alice', age: 30 }
}
*/

Immutability

Node properties are deep-frozen to prevent accidental mutation:

const node = await graph.addNode('Person', { name: 'Alice' });
node.properties.name = 'Bob'; // TypeError: Cannot assign to read only property

To modify, use Graph's property update methods:

await graph.updateNodeProperty(node.id, 'name', 'Bob');