spences10
spences10

Reputation: 590

Iterate through LevelDB

Can someone please educate me on this:

Trying to use levelDb in a node.js instance, to store the name key and the ID string against it, I want to be able to check if the ID is already in the DB so...

Looking at the documentation I don't know what I'm doing wrong.

I have db.js here:

var level = require('level');  
var path = require('path');

var dbPath = process.env.DB_PATH || path.join(__dirname, 'mydb');  
var db = level(dbPath);

module.exports = db;

And I want to be able to query the db, so my rudimentary try in app.js here:

var db = require('./db', {
  valueEncoding: 'json'
})

db.put('name', 'ID001')
db.put('name', 'ID002')
db.put('name', 'ID003')
db.put('name', 'ID004')
db.put('name', 'ID005')
db.put('name', 'ID006')
db.put('name', 'ID007')

db.createReadStream()
  .on('data', function (entry) {
    console.log(entry.value);
  })

I thought that I was only getting the last value in the db but after running it repeatedly I got output like this:

spences10:~/workspace/level-db $ node app.js
ID006
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID005
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID006
spences10:~/workspace/level-db $ node app.js
ID005
spences10:~/workspace/level-db $ node app.js
ID003
spences10:~/workspace/level-db $ node app.js
ID007

I just want to be able to either iterate through or query the db to see if a value needs to be put in there.

I have also tried using .get with the same sort of result

var db = require('./db', {
  valueEncoding: 'json'
})

db.put('name', 'ID001')
db.put('name', 'ID002')
db.put('name', 'ID003')
db.put('name', 'ID004')
db.put('name', 'ID005')
db.put('name', 'ID006')
db.put('name', 'ID007')

db.get('name', function(err, value) {  
  if (err) {
    return err;
  }
  console.log('value:', value);
});

Output...

spences10:~/workspace/level-db $ node app.js
value: ID007
spences10:~/workspace/level-db $ node app.js
value: ID004
spences10:~/workspace/level-db $ node app.js
value: ID005
spences10:~/workspace/level-db $ node app.js
value: ID007
spences10:~/workspace/level-db $ node app.js
value: ID003
spences10:~/workspace/level-db $ node app.js
value: ID007

Upvotes: 2

Views: 3387

Answers (1)

cjungel
cjungel

Reputation: 3781

The put function is asynchronous and the code is executing the put operation several times sequentially. Then invoking get gives you the last value that was actually written. This is why you get different outputs every time you read run your code.

In the example from the docs you can see that data is read in the callback of the put operation to make sure that it was actually written.

var levelup = require('levelup')

// 1) Create our database, supply location and options.
//    This will create or open the underlying LevelDB store.
var db = levelup('./mydb')

// 2) put a key & value
db.put('name', 'LevelUP', function (err) {
  if (err) return console.log('Ooops!', err) // some kind of I/O error

  // 3) fetch by key
  db.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err) // likely the key was not found

    // ta da!
    console.log('name=' + value)
  })
})

Upvotes: 2

Related Questions