Node API
Work with node data from simulations
Interactive Examples
Fetch Nodes
No nodes loaded yet. Click the button to fetch nodes.
Collection Access
import { initializeSGERP } from 'sgerp-frontend-lib';
const api = initializeSGERP({
activeConnection: 'admin@localhost',
connections: {
'admin@localhost': {
id: 'admin@localhost',
environment: 'localhost',
user: 'admin',
password: 'your-password',
baseUrl: 'https://sgerp-stage.d.gcdev.swatrider.com',
},
},
});
const nodes = api.collections.node;
Fetching Nodes
// Fetch first 20 nodes
await nodes.fetch({ limit: 20 });
// Fetch with offset
await nodes.fetch({ limit: 20, offset: 20 });
// Fetch all nodes (be careful with large datasets)
await nodes.fetchAll();
Accessing Node Data
// Get all nodes
const allNodes = nodes.models;
// Get a specific node by ID
const node = nodes.get(15358831);
// Check if a node exists
const exists = nodes.has(15358831);
// Get first/last node
const first = nodes.first();
const last = nodes.last();
// Check collection state
const isEmpty = nodes.isEmpty();
const count = nodes.length;
const hasMore = nodes.hasMore;
Node Model
interface Node {
id: number;
h3: string;
lat: number;
lon: number;
created_at: string;
modified_at: string;
uid: string;
booking_uid: string;
booking_group_uid: string;
data: Record<string, any>;
assigned_vehicle_id: number;
passenger_count: number;
node_type: string;
display_name: string;
location_name: string;
location_code: string;
estimated_arrival_time: string | null;
booking_id: number;
simulation_id: number;
project_id: number;
demand: Record<string, number>;
open_time_ts: string;
close_time_ts: string;
service_time: number;
status: string;
assignment_order: number;
scheduled_ts: string;
visited_at_ts: string | null;
penalty: number;
trip_cost: number;
transfer_type: string;
slack: number;
vehicle_characteristics: Record<string, any>;
vehicle_labels: Record<string, any>;
// ... many more fields
}
Collection Utility Methods
Grouping
// Group nodes by simulation
const bySimulation = nodes.groupBy('simulation_id');
// Group by node type
const byType = nodes.groupBy('node_type');
// Group by booking
const byBooking = nodes.groupBy('booking_id');
Sorting
// Sort by scheduled time
const byScheduled = nodes.sortBy('scheduled_ts');
// Sort by ID
const byId = nodes.sortBy('id');
// Sort by custom function
const sorted = nodes.sortBy(n => n.assignment_order);
Filtering
// Filter with custom logic
const pickups = nodes.filter(n => n.node_type === 'pickup');
// Find specific node
const found = nodes.find(n => n.uid === 'be2b7adf-88b8-4fb2-ac47-991908252e5e');
// Where clause
const filtered = nodes.where({
project_id: 929,
status: 'assigned'
});
Plucking Values
// Get all simulation IDs
const simIds = nodes.pluck('simulation_id');
// Get all node types
const types = nodes.pluck('node_type');
// Get all locations
const locations = nodes.models.map(n => ({
lat: n.lat,
lon: n.lon
}));
Pagination
// Fetch first page
await nodes.fetch({ limit: 10 });
console.log(`Loaded ${nodes.length} nodes`);
console.log(`Has more: ${nodes.hasMore}`);
// Load next page
if (nodes.hasMore) {
await nodes.fetchNext();
console.log(`Now have ${nodes.length} nodes`);
}
Error Handling
try {
await nodes.fetch({ limit: 20 });
console.log('Nodes loaded:', nodes.length);
} catch (error) {
if (error instanceof APIError) {
console.error('API Error:', error.message);
console.error('Status:', error.status);
} else {
console.error('Error:', error);
}
}
Common Patterns
Get All Nodes for a Simulation
// Fetch all nodes filtered by simulation on the server
await nodes.fetchAll({ simulation_id: 211238 });
console.log(`Simulation has ${nodes.length} nodes`);
Find Pickup and Dropoff Nodes
await nodes.fetch({ limit: 100 });
const pickups = nodes.where({ node_type: 'pickup' });
const dropoffs = nodes.where({ node_type: 'dropoff' });
console.log(`${pickups.length} pickups, ${dropoffs.length} dropoffs`);
Calculate Average Service Time
await nodes.fetch({ limit: 100 });
const serviceTimes = nodes.pluck('service_time');
const avgTime = serviceTimes.reduce((a, b) => a + b, 0) / serviceTimes.length;
console.log(`Average service time: ${avgTime.toFixed(0)} seconds`);
Type Definitions
The full Node type is exported from the library:
import type { Node } from 'sgerp-frontend-lib';
const node: Node = {
id: 15358831,
lat: 1.30762282594567,
lon: 103.82859041681,
node_type: 'dropoff',
status: 'assigned',
booking_id: 7764432,
simulation_id: 211238,
project_id: 929,
// ... other fields
};