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)
- 00:07the module i'll be going over today is
- 00:09level up so what level up is is a high
- 00:13level wrapper around a database called
- 00:15leveldb
- 00:16and
- 00:18leveldb is actually a database that was
- 00:20created by google it's an embedded
- 00:22database it's just a key value store
- 00:24it's actually what backs indexeddb
- 00:27on the google chrome browser it also i
- 00:30think is in mariadb as kind of like a
- 00:32cache layer
- 00:34for their values and also is in this
- 00:36really cool project called
- 00:38dat
- 00:40which is all about sharing information
- 00:42between servers and multiple databases
- 00:44uses it behind the scenes really cool
- 00:46project i suggest checking it out
- 00:48so level up to install it you do npm
- 00:52install level up
- 00:53but it does have a dependency of level
- 00:55down that doesn't include inside of its
- 00:57package.json because you can actually
- 00:59swap it out for different things so to
- 01:01install it you need to do
- 01:04npm install level up level down and then
- 01:07save those or you could just do npm
- 01:09install level
- 01:10um i already have installed because it
- 01:13level down is does have some c-scripts
- 01:15so it takes a little bit to install
- 01:17but i do have installed in this repo um
- 01:20so what i'm going to do is start
- 01:21building a an application with it so the
- 01:24first thing you do
- 01:25is that you include level up into
- 01:29your app
- 01:36and then
- 01:39we're going to create a database and
- 01:41this is real simple you just say
- 01:47level up
- 01:50and there might be a little bit of lag
- 01:51in between my typing and what's going on
- 01:54is
- 01:55on the screen
- 01:57uh
- 01:58we just pointed towards a local
- 01:59directory now this will actually build a
- 02:01database if it's not there already but
- 02:03if it will hook up to the existing
- 02:04database if there is one i've actually
- 02:06created one and has a little bit of data
- 02:08in it uh not too much um so now since we
- 02:11have an empty database we'll essentially
- 02:13have an empty database um
- 02:15we're gonna want to do some uh actions
- 02:18against it's like put information inside
- 02:20of it so i'm gonna create this little
- 02:21log file so that way we can see the
- 02:23information coming out
- 02:25and i'm going to use that crazy bind
- 02:26method
- 02:29to make a little logging function
- 02:33and then
- 02:34so we want to put information in so with
- 02:37level uh up it's really easy i do db
- 02:40put
- 02:41and then
- 02:42a key
- 02:44and then
- 02:45a value
- 02:46um
- 02:48so i'm going to say jsla is effing
- 02:52awesome
- 02:53and then that takes a callback function
- 02:55because after the write is done it it
- 02:58lets me know so i'm just going to pass
- 03:00in a log
- 03:02and then i'm going to run it
- 03:06um and as you can see well you will see
- 03:09it didn't spit out anything which is
- 03:10good because it actually the only thing
- 03:12that it will pass back is an error if it
- 03:14happens
- 03:15so it looks like we're all good so we
- 03:16want to see you know now the next thing
- 03:18you want to do with
- 03:19a database is to actually read that
- 03:21information out that you've stored with
- 03:23it so what i'm going to do
- 03:27is i'm going to
- 03:28push this down and then say db.get
- 03:32and then we want to get jsla and then
- 03:34i'm going to pass it that log function
- 03:35we only have to pass the one uh two
- 03:37parameters with this one um
- 03:40the the key and the callback
- 03:43so i'm gonna save
- 03:46and then we'll go back to the console
- 03:53so
- 03:54now
- 03:55you see it brings back nola's effing
- 03:57awesome it's all right i mean those
- 03:59pretty cool but knows where the value of
- 04:01the error would be
- 04:03uh and then that is the the value of
- 04:05that that key that we stored
- 04:07um
- 04:08so let's say i've been to jsla and i
- 04:11don't think it's it's super effing
- 04:12awesome
- 04:13so
- 04:14let's say we're going to do a delete
- 04:17on this key
- 04:20don't worry don't worry
- 04:23so we're going to save that and then
- 04:25we're going to run it
- 04:26and this doesn't have anything as well
- 04:28that comes out because it's supposed to
- 04:29just bring back an error
- 04:31um
- 04:32so we're all good so we've actually
- 04:34written things uh read things and then
- 04:36we've deleted things from the database i
- 04:38do think jsla is effing awesome so we're
- 04:41gonna we're gonna we're gonna run that
- 04:43again um just to put that back in the
- 04:45database
- 04:46uh so another cool thing
- 04:48about this module is that uh it has
- 04:50streaming
- 04:51um so what i'm gonna do is i'm gonna
- 04:54create a second database so db2 is level
- 04:57up
- 04:58and then we're just going to put in a
- 05:00random database name
- 05:03so that way you see that i don't you
- 05:05know i didn't
- 05:06pre-fill this
- 05:08so i'm going to create a
- 05:10read stream
- 05:12which to do this all you do is db dot
- 05:16create
- 05:17read stream
- 05:23and i'm also going to create a right
- 05:25stream because a level db or
- 05:28level up has both of them
- 05:30so db2
- 05:32dot
- 05:33create
- 05:38right stream
- 05:40cool
- 05:41so now i have a restream and a right
- 05:42stream and what i want to do is i want
- 05:44to essentially copy my whole entire
- 05:46first database and then pump it into the
- 05:49second database
- 05:50and we can do this real easy by saying
- 05:52read stream dot pipe
- 05:55into our right stream
- 05:58so i'm gonna run this it's not gonna do
- 06:00anything in the console because we're
- 06:01not catching any errors
- 06:03but we're gonna assume that everything
- 06:04went okay i mean didn't throw any really
- 06:07bad errors
- 06:09so now we want to see uh that
- 06:11information
- 06:12to see if it actually uh put it into
- 06:15that second database so i'm just going
- 06:17to go up into this read stream and
- 06:20change it to the second database
- 06:23and then i'm going to take my read
- 06:24stream and say on
- 06:26data
- 06:28and then we're just going to
- 06:30use the log function as our callback
- 06:33now i'm gonna go back over and run it
- 06:38as you can see i had a couple little
- 06:40bits of information already in there
- 06:43in the first database but now this is
- 06:44the second database that we just created
- 06:46on the fly and pumped all the
- 06:47information from the first database into
- 06:48the second database using streaming
- 06:51so that's a level up i encourage you
- 06:53guys to to use it it's a very cool
- 06:55module
- 06:56and thank you
- 07:03you