@shubhamrasal/groundline
Version:
Groundline GraphDB with IPFS persistence
66 lines • 2.02 kB
JavaScript
import { nodes, edges } from './graph-crdt.js';
import { jsonLdContext } from './jsonld-context.js';
/**
* Converts a single entity to a JSON-LD node
*/
function entityToJsonLd(entity) {
const jsonLd = {
"@id": `https://groundline.dev/entity/${entity.id}`,
"@type": "Entity",
"name": entity.name,
"entityType": entity.entityType
};
if (entity.observations?.length) {
jsonLd.observations = entity.observations;
}
if (entity.properties) {
jsonLd.properties = entity.properties;
}
return jsonLd;
}
/**
* Converts a single relation to JSON-LD statements
*/
function relationToJsonLd(relation) {
return {
"@id": `https://groundline.dev/relation/${relation.id}`,
"@type": "Relation",
"from": `https://groundline.dev/entity/${relation.from}`,
"to": `https://groundline.dev/entity/${relation.to}`,
"relationType": relation.relationType,
...(relation.properties ? { "properties": relation.properties } : {})
};
}
/**
* Converts the entire graph to a JSON-LD document
*/
export function serializeGraphToJsonLD() {
const graph = [];
// Convert all nodes to JSON-LD
for (const [id, node] of nodes.entries()) {
graph.push(entityToJsonLd(node));
}
// Convert all edges to JSON-LD
for (const [id, edge] of edges.entries()) {
graph.push(relationToJsonLd(edge));
}
return {
"@context": jsonLdContext["@context"],
"@graph": graph
};
}
/**
* Optional: Validate and compact JSON-LD using jsonld library
* Note: This requires the jsonld package to be installed
*/
export async function validateAndCompactJsonLD(jsonLdDoc) {
try {
const { default: jsonld } = await import('jsonld');
return await jsonld.compact(jsonLdDoc, jsonLdDoc['@context']);
}
catch (error) {
console.error('Failed to validate/compact JSON-LD:', error);
return jsonLdDoc;
}
}
//# sourceMappingURL=graph-jsonld.js.map