This document provides comprehensive usage examples for CephaloDB, demonstrating how to interact with the database system using both Axios (JavaScript/Node.js) and CURL commands. It covers CRUD operations, relationships, global state management, fuzzy logic, and real-time synchronization.
Description: Adds a new document containing nodes.
const axios = require('axios');
const newDocument = {
title: "Employee Data",
description: "Contains employee information and relationships",
nodes: [
{ name: "Alice", role: "Developer" },
{ name: "Bob", role: "Manager" }
]
};
axios.post('http://localhost:3000/api/documents', newDocument, {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Document Created:', response.data))
.catch(error => console.error('Error:', error));
curl -X POST http://localhost:3000/api/documents \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{
"title": "Employee Data",
"description": "Contains employee information and relationships",
"nodes": [
{ "name": "Alice", "role": "Developer" },
{ "name": "Bob", "role": "Manager" }
]
}'
Description: Retrieves all documents stored in CephaloDB.
axios.get('http://localhost:3000/api/documents', {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Documents:', response.data))
.catch(error => console.error('Error:', error));
curl -X GET http://localhost:3000/api/documents \
-H "Authorization: Bearer <your-token>"
Description: Updates an existing document by ID.
const updatedDocument = {
title: "Updated Employee Data",
nodes: [{ name: "Alice", role: "Senior Developer" }]
};
axios.put('http://localhost:3000/api/documents/<document-id>', updatedDocument, {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Document Updated:', response.data))
.catch(error => console.error('Error:', error));
curl -X PUT http://localhost:3000/api/documents/<document-id> \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{
"title": "Updated Employee Data",
"nodes": [{ "name": "Alice", "role": "Senior Developer" }]
}'
Description: Deletes a document based on its ID.
axios.delete('http://localhost:3000/api/documents/<document-id>', {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Document Deleted:', response.data))
.catch(error => console.error('Error:', error));
curl -X DELETE http://localhost:3000/api/documents/<document-id> \
-H "Authorization: Bearer <your-token>"
Description: Retrieves documents with sorting and filtering options.
axios.get('http://localhost:3000/api/documents?sort=title:asc&filter=role:Developer', {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Sorted and Filtered Documents:', response.data))
.catch(error => console.error('Error:', error));
curl -X GET "http://localhost:3000/api/documents?sort=title:asc&filter=role:Developer" \
-H "Authorization: Bearer <your-token>"
Description: Establishes relationships between nodes based on IDs.
const relationship = {
sourceNodeId: 'node1_id',
targetNodeId: 'node2_id',
relationshipType: 'reportsTo'
};
axios.post('http://localhost:3000/api/relations', relationship, {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Relationship Created:', response.data))
.catch(error => console.error('Error:', error));
curl -X POST http://localhost:3000/api/relations \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{
"sourceNodeId": "node1_id",
"targetNodeId": "node2_id",
"relationshipType": "reportsTo"
}'
Description: Removes a relationship by its ID.
axios.delete('http://localhost:3000/api/relations/<relationship-id>', {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Relationship Deleted:', response.data))
.catch(error => console.error('Error:', error));
curl -X DELETE http://localhost:3000/api/relations/<relationship-id> \
-H "Authorization: Bearer <your-token>"
Description: Retrieve and update the global state for CephaloDB.
axios.get('http://localhost:3000/api/state', {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Global State:', response.data))
.catch(error => console.error('Error:', error));
curl -X GET http://localhost:3000/api/state \
-H "Authorization: Bearer <your-token>"
const updatedState = {
maintenanceMode: true,
updatedBy: "admin_user"
};
axios.put('http://localhost:3000/api/state', updatedState, {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Global State Updated:', response.data))
.catch(error => console.error('Error:', error));
curl -X PUT http://localhost:3000/api/state \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{
"maintenanceMode": true,
"updatedBy": "admin_user"
}'
Description: Use the fuzzy logic engine to establish dynamic relationships.
axios.post('http://localhost:3000/api/relations/fuzzy', { documentId: '<document-id>' }, {
headers: { Authorization: 'Bearer <your-token>' }
})
.then(response => console.log('Fuzzy Logic Applied:', response.data))
.catch(error => console.error('Error:', error));
curl -X POST http://localhost:3000/api/relations/fuzzy \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{"documentId": "<document-id>"}'
Description: Listen for real-time updates using WebSocket.
const io = require('socket.io-client');
const socket = io('http://localhost:3000');
socket.on('connect', () => {
console.log('Connected to CephaloDB');
});
socket.on('documentUpdate', (data) => {
console.log('Document Updated:', data);
});
This document provides a comprehensive guide to using CephaloDB’s features with both Axios and CURL. For further details or assistance, feel free to reach out or consult the main documentation.