I'm building a simple Web3 app that generates JSON data and saves it on the IPFS network.
The JSON data saved on the IPFS network is then updated when new JSON data is generated.
Saving the data is easy.
The problem is I cannot figure out how to update the data already saved in the IPFS network.
Could you help me spot the problem?
I'm using 'ipfs-http-client' as my IPFS module.
The main code:
const { create } = require("ipfs-http-client");
let hashStorage;
async function ipfsClient() {
const ipfs = await create(
{
host: "localhost",
port: 5011,
protocol: "http"
}
);
return ipfs;
}
//saving the file
let fileHash = await saveFile(); // See the save function below
hashStorage=fileHash;
//updating the file
let updateHash = await updateFile(); // See the update function below
The data is saved onto IPFS using the function below:
async function saveFile() {
let ipfs = await ipfsClient();
let data = fs.readFileSync("./config.json")
let options = {
warpWithDirectory: false,
//progress: (prog) => console.log(`Saved :${prog}`)
}
let result = await ipfs.add(data, options);
//console.log(result.path)
return result.path
}
The IPFS data is updated using the function below:
async function updateFile() {
let ipfs = await ipfsClient();
let newData = fs.readFileSync("./newConfig.json")
let options = {
warpWithDirectory: false,
//progress: (prog) => console.log(`Saved :${prog}`)
}
let result = await ipfs.add(newData, options);
//console.log(result.path)
return result.path
}