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)

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