Node.js With Neo4j - Freestyle Coding [2]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how to use the neo4j driver for nodejs alright so if you don't know what neo4j is it's a graph database and I have a pretty decent video on it called neo4j graph database and cipher so if you want to check that out it's about 40 minutes long and I show you I go over what a graph database is I show you how to install it in Windows and how to get a simple database up and running much like this one alright so right now I'm in neo4j browser and I just created some nodes some movie nodes actors and directors and just added some really simple relationships between them so for example we have al pacino right here which is an actor node and we have some movie nodes that he's he has a relationship to using the acted in relationship all right and then we have some directors can see right here we have Francis Ford Coppola and he has a directed relationship with the movie The Godfather alright and we also have Robert Nero which is another actor related to the Godfather movie with the acted in relationship alright so I'm not going to go over this stuff too much I just want to show you how to use the driver we're not going to create an in-depth application I just want to get and Express a nodejs and Express service set up and implement the driver and interact with the database so what I'm going to do is going to go to where I want to create my application which is going to be in my C Drive and then projects and I'm going to create a new folder and let's call this neo movies ok now I want to open my command line as administrator okay then we're going to go to that folder all right now what I want to do is generate a package.json file to do that I can run NPM in it and if you don't have node no js' installed and NPM you just want to go to node.js org go ahead and download it and install it and you should be all set all right so let's go ahead and just click enter here for the entry point I'm going to do a BAS the rest is fine okay so that should have created a package.json file I'm going to go ahead and open my editor and let's see I want a new window and I'm using Visual Studio code which is a nice free editor if you want to use that or whatever you like alright so I'm going to go ahead and open folder and I want to open Neal movies and you can see we have a package.json file all right next thing we're going to do is set up our dependencies so let's go right here and say dependencies ok we're going to be using Express use Express for our routes and things like that want the latest version we also want body parser okay we're going to be using eg a s for our template which is embedded JavaScript and for that I'm going to say version two point four point two and we're also going to use Morgan which is a logger and then finally we need the neo4j driver which is going to be neo4j - driver we'll get the latest version all right so those are our dependencies let's go ahead and save that and go back into the command prompt and we're going to run npm install okay that'll get all those dependencies set up alright so now that that's set up what we're going to do is create an app j/s file ok let's add Express we're also going to add path which is a core module and logger which is Morgan and body posture okay we'll add the node in the neo4j driver after I set after the server setup alright so now we're going to initialize our applet Express okay we also want to set up the view engine so for that we're going to do app dot set views and we're going to use path dot join here what we're doing here is just saying that we want to use a folder called views to hold our views all right then we need to set the engine so view engine is going to be ejs all right now we get some some other middleware that we want to add here let me just grab that real quick just your standard middleware for body parser this line here we're saying that our public folder is going to be a folder called public alright our our static folder so I don't think that we'll need that but I mean it's good to have just in case you want to use bootstrap or jQuery something like that image is something where you need a static folder all right now let's sum let's see we want to we want to do we want to listen on a port here so we're going to say app dot listen let's do port 3000 and then we'll just do a console dot log all right and then we just want to export there we go all right so the server should now run let's save that and we're going to go back over here and say node app and you can see that it's running on port 3000 so if we go to localhost 3000 okay so the server is running but we're getting this message cannot get slash and that's because we don't have a route set up for slash which is the home page all right so let's go right here and say app dot get and we'll put in a slash and then a function okay that function is going to get a request and response and all we're going to do is say res dot send and we'll say it works all right now if I go when I reload we have no change that's because we need to restart the server now if you don't want to have to keep restarting it what you can do is you can install node Mon so to do that we can say npm install node mon and you want to add - g for global that'll make it so that you can use it from anywhere alright so once that's done we should be able to just say node mon and that'll just kind of watch it for us so if we reload we get it works alright so now that that's up and running I'm just going to make this smaller all right so now that's up and running what I'm going to do is implement the driver so let's go right up here and create a variable called neo4j and I'm going to set that to require neo4j - driver okay so once we have that we need to let's go right above where our route is and create a variable called driver and this is where we can connect to neo4j now we connect using the bolt protocol which is I believe it was new to version 3.0 of neo4j so what to do that I'm going to say neo4j dot driver and here we're going to pass in bolt : / / localhost and so let's see second next parameter is going to be your authentication for your neo4j server so we can say neo4j dot auth dot basic and then I'm going to pass in the username which is neo4j for me and for you if you didn't change it and then whatever you made your password alright and that should allow you to connect ok then after that we need to create a session variable so that's going to be set to driver dot session all right oh one thing I did forget is up here where we require neo4j driver we actually want to add dot v1 all right at least at this time we need to do that okay so now we should be able to start making queries all right so now let me just bring over a page from the documentation here all right so we've done this we've done this in this so now to run a query what we need do is use that session variable and then use run the run method you can see here they're creating a person node I don't want to create anything just yet I just want to fetch some data so let's go into our route here our home route and we'll say session dot run and then after that will be dot then let's check this out so then and then catch that's what we'll catch any hours all right so let's go into run and I'm going to put in query will say match let's get some movies so we'll say n movie and then we're going to return and limit 25 all right so that's just the query as as simple as that now we need to do is add a function call back function in here and that's going to take a result and then we should be able to loop through that result by saying result dot records dot for each alright and then inside here we'll have a function and that's going to take a parameter called record and then let's just do console dot log record alright so before we run that let's just go down to our catch and put in function it's going to take an error and we're just going to console.log the error if there is any alright so we'll save it and then let's go to our console and let's see what's this no such file or directory huh not sure what that means let's restart this okay so it looks like it's working now what we want is in fields this underscore fields you can see there's an object we want the properties object so let's go right here and we'll say console.log record dot underscore fields 0 dot properties let's try that and there we go so you can see we're fetching the movies we're getting the year and the title alright and if we wanted to change this query let's say actor reload and then we get the actors if we want we could take just take off the label completely if we want to get all nodes and there we go we can get we're getting movies actors and directors all right so we know the driver is installed and set up correctly now you probably don't want to just console.log this stuff you want to bring it into your application all right so we're going to have to render a view for that so let's go down to where we have res send and let's see we don't even use me we don't even want this down here anyway we want this stuff to happen up in the then call back alright so let's go right after the for each which ends right here so we'll go right here and say res dot render and so let's see let's render index okay save that and then we need to create a folder called views and inside there we're going to create a file called index dot ejs and let's just do let's put a heading in here we'll say movies save it make sure that works alright and then what we want to do is though the way that it's constructed your result is a little weird so what I'm going to do is I want to create a new object with with the movies in it alright are an array so let's go right under dot then and create a new variable we'll call it movie array and set that for now just to an empty array all right and then down here in the for each what we want to do is you want to add to that array okay through each iteration we want to add each movie so we'll say movie array dot push and let's pass in an object alright now we can actually get the ID using record dot underscore fields 0 dot identity what is it identity dot Lo and I'll give us the actual ID all right and then we want the name so what I'm going to do is copy this right here or cut it and then just get rid of that and let's see title and then we'll just say dot title and I want to actually want to change this back to movie all right and then we also want the year so for that we'll grab this paste that in dot year all right so we're constructing this array of movies and then what we want to do is we want to pass that array into our view so let's go right here and it'll say movies is going to equal movie array all right so we'll save that and then let's go into our view and we're going to use ejs syntax which is less than percent and then we'll say movies dot for each and in this for each goes a function okay and let's pass in what we want to use as a variable will use movie and we need to make sure we end these both of these lines all right and then we should have access to each movie so let's go ahead and just do let's do a ul here and then inside here we'll have allies and we should be able to say movie dot title hopefully ok let's save that and reload and there we go we're bringing in all the movies from neo4j now let's see we want to get the actors as well if we wanted if we're going to do that let's go back to AB KS and we're going to have to nest our set our run calls our session runs so let's go down to where we have our view and start look let's say session dot run okay and then let's see in here we'll say match ten actor and then return and limit 25 okay and then den de then we're going to have a function and I'm going to get the results now for this I'm going to use result to just because I mean we probably could use result even though use it up here but just to kind of so it's not so confusing we'll use result to alright and then we're going to do the same thing we're going to create an array so this one will be actor array and then we can take result two and say dot records dot for each and then I'm just going to copy what we have right here and put that here okay except we want to change this to actor array and we only we don't want a year or a title I'm going to change title to name okay and then what we'll do is I'm going to take the the render right here and cut that out and put that up here right under the for each right and then we should be able to also pass along actors all right so let's save that and then go back to our view and let's see I'm going to copy this whole thing ok we'll change that to actors and we're going to do actors dot for each and this will be actor dot name alright so now we're getting the movies and the actors alright so the next thing I want to do is we want to be able to add movies from our application so let's go right above here and let's put a form so we'll just have that's fine we'll put a label I don't care about this looking good you guys can can take can use bootstrap or something to make it look a little better but I'm just going to focus on the functionality so it's not going to be very pretty alright so say movie name and give it a type of text and a name of name let's do movie name okay then we'll put a line break and we're also going to want the year then we want a submit button okay let's look at that it doesn't look very good but that's alright let's actually put a line break here and here alright so in the form tag we want to add a method of post it's going to be a post request and the action will say slash movie slash ad all right so now we want to go on the other side and create that route so let's go to app J s and go down here and say app dot post okay that's going to take a request and response and let's see what do we need to do we need to grab the values that are in the form field so let's say var name equals request body not movie name and same with the year okay just to test it out let's do console dot log I'll say name and then we're just going to do a redirect will say res dot redirect to the home page alright so let's see if that works OOP okay so let's just say tests cannot post movie ad oh I didn't put the route okay let's take a look at the console and we get tests so we know that it's grabbing that input so what we want to do now is the same thing we need to have a session with the run method so what I'll do is just I'm not going to copy it I'll just retype it so session dot run okay so in here let's do create and and this is going to be a movie so create an movie and let's put in the title and we're going to put a placeholder here just say title / am okay you don't want to put that you don't want to put it directly in and let's put a comma and then we'll say what else does a year year program and then we're going to return will return end dot title all right and then over here we're going to need another parameter and we'll say title / Ram is going to equal the movie thought I put name didn't I yeah you know what I'm going to change this to title I'm going to do it here and back in the view right here I'm just going to get rid of movie so it'll just be title in year just to try to avoid confusion all right and then down here we'll just change that to title yeah all right and then over here that can just be title all right and we also want the year params so we're going to put a comma there all right so that's our query now and then we need our function function takes a result and we don't need to really do anything here I'm just going to redirect and we just want to close the session with session dot close alright let's go ahead and save it and let's reload and let's put Independence Day 2016 submit and there it is Independence Day if we reload it's still there and if we go to our neo4j browser we should see it as well so let's reload and click on this asterisk over here and there it is Independence Day you can see it has its year too alright so we can now add we can now read data from our neo4j database and we can add data all right and as you probably expect you can do the same exact thing with actors and directors so let's go ahead and do that let's add the actors form so what we'll do let's put a heading up here change these to H threes okay and then let's copy this form and this will be add actor will have a go to slash actor slash ad I think they just have a name property so we don't need this alright so there's our active form let's go ahead and go to app J s and we're going to do the same kind of thing I'm just going to copy this okay we want to get the name and let's change this query to create if you don't understand these queries at all then I would suggest got watching the other Neil 4j video that I have because that'll explain all of this change this okay we don't need the year and then over here let's return the name get rid of that all right so that should do it let's save it and let's reload we'll add an actor here let's say Jeff Goldblum submit and there we go so we can now add movies and actors now obviously neo4j and graph databases are the best when you're working with data that's related to each other so what I'm going to do is we're going to add another form to add an actor to a movie all right so let's go ahead and go to our view and I'm going to just copy this let's say add actor to movie and that's going to go to movie slash actor slash add okay so we want to add the movie we're not going to need the years but we are going to need that the actor name all right so now let's go to app J s and just going to copy this hope this isn't getting too confusing and what was the route I think it was movie slash actor slash ad and then we want to get both the title of the movie and the name of the actor okay now this is going to be a little different we're adding a relationship so let's totally clear this out this query okay so we know we're going to have a name and title so we can add those and then for the query we're going to do match okay so match a actor and we want the name that's going to be set to name / Ram alright and then we're going to put a comma here and then another set of parentheses and then B movie that's also going to have that's going to have a title which will be titled program all right and then we need to go right here and do merge so we want to say merge a and it's gonna have a relationship of acted in to be alright and then we're just going to return a.b alright so that's how we can add a relationship alright so hopefully that works let's go ahead and save it alright now what I want to do is I want to add Jeff Goldblum to Independence Day because if we look at the graph right now they shouldn't be related ok so you can see right here ok there's no relationship so let's go ahead and try to add that so movie Independence Day actor Jeff Goldblum submit and then if we go back to our graph well that didn't work let's see what the console says name per am not defined not defined oh I didn't put I didn't put curly braces around this that should be yeah like that same thing here you guys probably notice that all right let's try it again okay and there we go okay so you can see now we have enacted in relationship between Jeff Goldblum and Independence Day all right so we can now read data we can add it and we can also add relationship all right so I'm going to go ahead and stop here now that you can see how this works how you can use the neo4j driver and write queries I would suggest that you keep going and maybe make it so that you can click on one of these movies it'll take you to a page and list all the actors I might do that in the future I'm not sure if you guys want me to do that let me know in the comment section but that's going to be it for now hopefully you enjoyed it and I'll see you next time
Info
Channel: Traversy Media
Views: 41,513
Rating: 4.9281435 out of 5
Keywords: node.js neo4j, neo4j nodejs, neo4j driver, node.js cypher
Id: snjnJCZhXUM
Channel Id: undefined
Length: 39min 26sec (2366 seconds)
Published: Thu Jul 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.