Jacob Lowe

jsla - Module of the Month: LevelUp

by Jacob Lowe on

Module of the month was a small series at jsla in which people of the community would give talks about a specific module in the nodejs ecosystem. This months talk was about LevelUp.

Transcripts (auto-generate)

  • the module i'll be going over today is
  • level up so what level up is is a high
  • level wrapper around a database called
  • leveldb
  • and
  • leveldb is actually a database that was
  • created by google it's an embedded
  • database it's just a key value store
  • it's actually what backs indexeddb
  • on the google chrome browser it also i
  • think is in mariadb as kind of like a
  • cache layer
  • for their values and also is in this
  • really cool project called
  • dat
  • which is all about sharing information
  • between servers and multiple databases
  • uses it behind the scenes really cool
  • project i suggest checking it out
  • so level up to install it you do npm
  • install level up
  • but it does have a dependency of level
  • down that doesn't include inside of its
  • package.json because you can actually
  • swap it out for different things so to
  • install it you need to do
  • npm install level up level down and then
  • save those or you could just do npm
  • install level
  • um i already have installed because it
  • level down is does have some c-scripts
  • so it takes a little bit to install
  • but i do have installed in this repo um
  • so what i'm going to do is start
  • building a an application with it so the
  • first thing you do
  • is that you include level up into
  • your app
  • and then
  • we're going to create a database and
  • this is real simple you just say
  • level up
  • and there might be a little bit of lag
  • in between my typing and what's going on
  • is
  • on the screen
  • uh
  • we just pointed towards a local
  • directory now this will actually build a
  • database if it's not there already but
  • if it will hook up to the existing
  • database if there is one i've actually
  • created one and has a little bit of data
  • in it uh not too much um so now since we
  • have an empty database we'll essentially
  • have an empty database um
  • we're gonna want to do some uh actions
  • against it's like put information inside
  • of it so i'm gonna create this little
  • log file so that way we can see the
  • information coming out
  • and i'm going to use that crazy bind
  • method
  • to make a little logging function
  • and then
  • so we want to put information in so with
  • level uh up it's really easy i do db
  • put
  • and then
  • a key
  • and then
  • a value
  • um
  • so i'm going to say jsla is effing
  • awesome
  • and then that takes a callback function
  • because after the write is done it it
  • lets me know so i'm just going to pass
  • in a log
  • and then i'm going to run it
  • um and as you can see well you will see
  • it didn't spit out anything which is
  • good because it actually the only thing
  • that it will pass back is an error if it
  • happens
  • so it looks like we're all good so we
  • want to see you know now the next thing
  • you want to do with
  • a database is to actually read that
  • information out that you've stored with
  • it so what i'm going to do
  • is i'm going to
  • push this down and then say db.get
  • and then we want to get jsla and then
  • i'm going to pass it that log function
  • we only have to pass the one uh two
  • parameters with this one um
  • the the key and the callback
  • so i'm gonna save
  • and then we'll go back to the console
  • so
  • now
  • you see it brings back nola's effing
  • awesome it's all right i mean those
  • pretty cool but knows where the value of
  • the error would be
  • uh and then that is the the value of
  • that that key that we stored
  • um
  • so let's say i've been to jsla and i
  • don't think it's it's super effing
  • awesome
  • so
  • let's say we're going to do a delete
  • on this key
  • don't worry don't worry
  • so we're going to save that and then
  • we're going to run it
  • and this doesn't have anything as well
  • that comes out because it's supposed to
  • just bring back an error
  • um
  • so we're all good so we've actually
  • written things uh read things and then
  • we've deleted things from the database i
  • do think jsla is effing awesome so we're
  • gonna we're gonna we're gonna run that
  • again um just to put that back in the
  • database
  • uh so another cool thing
  • about this module is that uh it has
  • streaming
  • um so what i'm gonna do is i'm gonna
  • create a second database so db2 is level
  • up
  • and then we're just going to put in a
  • random database name
  • so that way you see that i don't you
  • know i didn't
  • pre-fill this
  • so i'm going to create a
  • read stream
  • which to do this all you do is db dot
  • create
  • read stream
  • and i'm also going to create a right
  • stream because a level db or
  • level up has both of them
  • so db2
  • dot
  • create
  • right stream
  • cool
  • so now i have a restream and a right
  • stream and what i want to do is i want
  • to essentially copy my whole entire
  • first database and then pump it into the
  • second database
  • and we can do this real easy by saying
  • read stream dot pipe
  • into our right stream
  • so i'm gonna run this it's not gonna do
  • anything in the console because we're
  • not catching any errors
  • but we're gonna assume that everything
  • went okay i mean didn't throw any really
  • bad errors
  • so now we want to see uh that
  • information
  • to see if it actually uh put it into
  • that second database so i'm just going
  • to go up into this read stream and
  • change it to the second database
  • and then i'm going to take my read
  • stream and say on
  • data
  • and then we're just going to
  • use the log function as our callback
  • now i'm gonna go back over and run it
  • as you can see i had a couple little
  • bits of information already in there
  • in the first database but now this is
  • the second database that we just created
  • on the fly and pumped all the
  • information from the first database into
  • the second database using streaming
  • so that's a level up i encourage you
  • guys to to use it it's a very cool
  • module
  • and thank you
  • you