0

I want to use orbit-db to create database using Nodejs but every time I create a database and try to add a new data it deletes the last element and I only can read the new one. I tried different types of databases (docs, Keyvalue, log, feed) but it's the same problem every time.

Here is my code to create a database:

 async function createDb() {
  // Create IPFS instance
  const ipfsOptions = { repo: './ipfs', }
  const ipfs = await IPFS.create(ipfsOptions)

  // Create OrbitDB instance
  const orbitdb = await OrbitDB.createInstance(ipfs)

  // Create database instance
  const db = await orbitdb.docs('IOFY')
  console.log("address", db.address.toString())
  const identity = db.identity
  console.log("identity ", identity.toJSON())
  console.log("put data")
  await db.put({ _id: 'ip2', name: 'shamb0t', followers: 600 })
  console.log("get Data")
  const profile = db.get('')
  console.log("profile = ", profile)
  console.log("close")
  await db.close();
  console.log("disconnect")
  await orbitdb.disconnect();
}

It creates the database and displays its address. This is the read function, we need to provide the address of the database as an argument:

async function readDb(fullAdress) {
  console.log("read function")
  // Create IPFS instance
  const ipfsOptions = { repo: './ipfs', }
  const ipfs = await IPFS.create(ipfsOptions)

  // Create OrbitDB instance
  const orbitdb = await OrbitDB.createInstance(ipfs)

  const db = await orbitdb.open(fullAdress);
  console.log("address", db.address.toString())
  await db.load()
  console.log("get data")
  const value = await db.get('')
  console.log("value = ", value)
  console.log("close")
  await db.close();
  console.log("disconnect")
  await orbitdb.disconnect();
}

It displays only the last element I added to the database, it's like the last element overwrites the past elements. Why does this happen?

Discordian
  • 749
  • 3
  • 16
Mourad
  • 101
  • 1

0 Answers0