Level DB is a database that works with no SQL which can be used to store data not only for Bitcoin any project can make use of it, as the database is leveled on choosing what key and value you want to store in the program or get from the database. A running Bitcoin node saves Bitcoin related data on block or chainstate directories, then the leveldb can be used to call those files saved in the directory. For instance we can retrieve a bunch of key values from the blocks index using one operation.
const level = require('level')
const db = level('my-db')
const ops = [
{ type: 'get', key: 'block' },
{ type: 'get', key: 'b'+32, value: 'byte block hash' },
{ type: 'get', key: 'F+4', value: 'byte file number' },
{ type: 'get', key: 'I-4', value: 'byte file number' },
{ type: 'get', key: 't+32', value: 'byte transaction hash' }
]
db.batch(ops, function (err) {
if (err){
return console.log(err)
}
console.log('Great success')
})
We have an ops array with a bunch of objects to define the operations.
The type is the type of the operation we want to do. They are the same as the method names.
The key is needed for getting the items to delete.
And the value is what we insert as the value of the key
Other additional functions include;
Get- get a key from the database
Put- put a value from the database
Del - delete a key in the current range
L.S- get all the keys in the current range
Start - defines the start of the current range
End - denfine the end of the current range
This will help you get to know leveldb better
https://bitcoindev.network/understanding-the-data/amp/https://thewebdev.info/2020/09/21/node-js-basics%E2%80%8A-%E2%80%8Alevel-db/https://imil.net/blog/posts/2020/bitcoin-leveldb-debugging/