LevelDB & Crypto

Introducing LevelDB

LevelDB & Crypto

Check out a free preview of the full LevelDB & Crypto course

The "Introducing LevelDB" Lesson is part of the full, LevelDB & Crypto course featured in this preview video. Here's what you'd learn in this lesson:

James Halliday introduces and sets up LevelDB, a key-value storage library written at Google that provides an ordered mapping from string keys to string values.


Transcript from the "Introducing LevelDB" Lesson

>> So I really like LevelDB for a number of reasons. It's not really as good of a database for kind of boring purposes, like crud apps and things. Just whatever, use a relational database for that. What LevelDB is really good for is for being a modular database where you can kinda layer on different abstractions.

So if you wanna publish packages to NPM, if you wanna have strange experiments. Also if you wanna run your abstractions and if you wanna run your applications in the browser as well as a node, LevelDB is almost the only game in town for doing that. So I think it's really worth knowing.

I mean, not that a whole lot of people use LevelDB in the browser, for some reason. It's a fantastic way to do it. So LevelDB is embedded, which means that it exists in the same process. So if you've ever used something like SQLite or Berkeley DB, these are also embedded databases.

So it's not a daemon that you run. It's not like my SQL D where you connect to it over a socket. LevelDB just lives in your program. This is partly because LevelDB was build for Chrome by some of the same people who wrote Google Bigtable. And that host arc will come full circle when we start using LevelDB in the browser on top of IndexedDB.

Because IndexedDB is a very awkward API, and the LevelDB API is very nice. So just take what was originally LevelDB inside of Chrome and you can wrap it. Well, then it gives you IndexedDB, but then you can wrap it so that you're back at the LevelDB API. Because it's very nice and simple, and you can do pretty much anything that you would need to do with this kind of a database.

All right, so if you haven't yet, run the package JSON in the workshop directory. You can also install LevelDB by doing this, which will give you the node package for LevelDB. The interface that you get is called LevelUp. So if you want to read about that documentation as we go along, you can pull up the readme for the package called LevelUp, one word, on NPM.

This will probably compile some things, or pull down some pre-built packages, if your system is supported. So it could take a little while to install. But then, once you've done that, this is all of the setup that you need to have in your database. So whatever that DB is going to be created on disk if it doesn't exist.

If it does exist, it'll just be used. So I can show you what that looks like here. So I'll just hop into the node REPL. We can require('level'). And now we can create a new database instance by just doing level and giving level, And giving level a string.

And the string is going to be a directory on your file system that will be created if it doesn't exist. So if I see now, I've got a new directory called test-foo.db. It's got a lot of odd stuff in it, that's okay. You don't have to know what any of this means, I don't.

But you shouldn't touch any of this because it's kind of important for how LeveDB works. So now that we have a database handle, we can start to do some things. You can also configure LevelDB with this option, valuingEncoding. You can create your own valueEncodings, or there's one built in called JSON.

So if you like to store JSON values instead of just binary blocks, you can do that. It's handy for, I'd say more often than not, I use valueEncoding of JSON when I'm dealing with LevelDB. I mentioned these already, but everything that we're gonna be doing with LevelDB, you can run all of this code in the browser as well.

And I'll get more into how to do that. But basically, if you swap require level for require level-browserify, you'll get a shim that's built on top of IndexedDB in the browser. So everything that we're gonna do, you can also do in the browser. But just due it in node first because node is way easier to muck around with than the browser, which is very complicated.

Also, if you have data that doesn't really fit very well into the kind of relational style, or maybe you just have a different approach to how you like to build stuff, LevelDB is good for that because you get some low level primitives and then you're on your own.

So you can pick and choose abstractions, or you can build your own. You don't have to kind of put your put your application into this normal form or anything like that. You can just do whatever you want. It's also really good for building these things called cap architectures, which are log based.

They use logs as a source of truth and build indexes called materialized views on top of those logs. It's sort of old enterprise architecture that just so happens to be really useful for building peer-to-peer systems as well. Which is why I find them very interesting, cuz I do that a lot.

Because then you don't even need a data center. You don't even need servers of any sort. You can just use people's browsers on a phone or something to get a lot done.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now