Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Node.js is a popular runtime platform to create programs that run on it.
It lets us run JavaScript outside the browser.
In this article, we’ll look at how to start using Node.js to create programs.
Level DB
Most apps need to store data somewhere.
They most likely use a database to store their data.
One way to store data within a Node app is to use the Level DB database.
It’s a key-value pair database, which makes it a NoSQL database.
It supports JSON right off the bat so we won’t have to do much to get and set the data.
The database is stored in a file.
To use Level DB, we run:
npm install level
to install the package.
Then we can use it by writing:
const level = require('level')
const db = level('my-db')
db.put('name', 'level', (err) => {
if (err) {
return console.log(err)
}
db.get('name', (err, value) => {
if (err) {
return console.log(err)
}
console.log(`name=${value}`)
})
})
We get the database file by calling the level
function with the database file’s path.
Then we call db.put
with the key and value as the first 2 arguments to save the key-value pair.
The 3rd argument is a function to get the error from the err
parameter in case there is any.
The db.get
method lets us get a piece of data by the key.
The first argument is the key
.
The 2nd argument is the callback in the same format.
Then we should see the output:
name=level
displayed.
We can also store a bunch of key-value pairs with one operation.
For instance, we can write:
const level = require('level')
const db = level('my-db')
const ops = [
{ type: 'del', key: 'father' },
{ type: 'put', key: 'name', value: 'james smith' },
{ type: 'put', key: 'dob', value: '1990-01-01' },
{ type: 'put', key: 'spouse', value: 'jane smith' },
{ type: 'put', key: 'occupation', value: 'waiter' }
]
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.
We can also chain the del
and put
methods to manipulate the key-value pairs:
const level = require('level')
const db = level('my-db')
db.batch()
.del('father')
.put('name', 'james smith')
.put('dob', '1990-01-01')
.put('spouse', 'jane smith')
.put('occupation', 'waiter')
.write(function () { console.log('Done!') })
We can read our data with a read stream.
For example, we can write:
const level = require('level')
const db = level('my-db')
db.put('foo', 'bar', function(error) {
if (error){
return console.log(error)
}
const stream = db.createReadStream();
stream
.on('data', function({ key, value }) {
console.log(`${key}=${value}`);
})
.on('error', function(error) {
console.log(error);
})
.on('end', function() {
console.log('end');
});
});
We call db.createReadStream
to create the read stream.
Then we listen to the data
to get the key-value pairs from the key
and value
properties respectively.
Conclusion
We can use the Level DB database to store key-value pairs in our app.