@shubhamrasal/groundline
Version:
Groundline GraphDB with IPFS persistence
58 lines • 1.92 kB
JavaScript
import { nodes, edges, getProvenanceLog } from './graph-crdt.js';
import { createIPFSClient } from './ipfs.js';
export class GraphIPFSManager {
constructor(ipfsConfig) {
this.ipfsClient = createIPFSClient(ipfsConfig);
}
/**
* Initialize the IPFS client
*/
async initialize() {
await this.ipfsClient.initialize();
}
/**
* Create a snapshot of the current graph state and upload to IPFS
* @returns CID of the uploaded snapshot
*/
async snapshotToIPFS() {
// Create snapshot from current graph state
const snapshot = {
nodes: new Map(nodes.entries()),
edges: new Map(edges.entries()),
timestamp: Date.now(),
version: '1.0.0',
provenance: getProvenanceLog()
};
// Upload to IPFS
return await this.ipfsClient.uploadSnapshot(snapshot);
}
/**
* Load a graph snapshot from IPFS and apply it to the current graph
* @param cid Content identifier of the snapshot
*/
async loadFromIPFS(cid) {
// Get snapshot from IPFS
const snapshot = await this.ipfsClient.getSnapshot(cid);
// Clear current graph state
nodes.clear();
edges.clear();
// Apply snapshot data to graph
snapshot.nodes.forEach((value, key) => nodes.set(key, value));
snapshot.edges.forEach((value, key) => edges.set(key, value));
}
/**
* Get the current graph state with provenance information
*/
getGraphState() {
return {
nodes: Array.from(nodes.entries()),
edges: Array.from(edges.entries()),
provenance: getProvenanceLog()
};
}
}
// Export a factory function for creating GraphIPFSManager instances
export function createGraphIPFSManager(config) {
return new GraphIPFSManager(config);
}
//# sourceMappingURL=graph-ipfs.js.map