0:01 Hey guys, in this video I'm going to 0:02 show you how to use the Neo4j driver for 0:06 NodeJS. All right, so if you don't know 0:08 what Neo4j is, it's a graph database, 0:11 and I have a a a pretty decent video on 0:15 it um called Neo4j Graph Database and 0:20 Cipher. So if you want to check that 0:22 out, it's about 40 minutes long, and I 0:24 show you I go over what a graph database 0:26 is. I show you how to install it in 0:28 Windows and how to get a simple database 0:31 up and running much like this one. All 0:33 right, so right now I'm in Neo4j browser 0:36 and I just created some nodes, some 0:38 movie nodes, actors and directors and 0:42 just added some really simple 0:43 relationships between them. So, for 0:45 example, we have Alpuccino right here, 0:47 which is an actor node, and we have some 0:51 movie nodes that he's he has a 0:53 relationship to using the actedin 0:56 relationship. All right. And then we 0:58 have some directors. You'll see right 1:00 here we have Francis Ford Capola, and he 1:03 has a directed relationship with the 1:05 movie The Godfather. All right. And then 1:08 we also have Robert Dairo, which is 1:10 another actor related to the Godfather 1:12 movie with the acted in relationship. 1:15 All right, so I'm not going to go over 1:17 this stuff too much. I just want to show 1:19 you how to use the driver. Um, we're not 1:22 going to create an in-depth application. 1:24 I just want to get an Express NodeJS and 1:27 Express server set up and implement the 1:30 driver and interact with the database. 1:34 So, what I'm going to do is I'm going to 1:37 go to where I want to create my 1:39 application, which is going to be in my 1:42 C drive, and then projects. And I'm 1:44 going to create a new 1:46 folder. And let's call this 1:50 um Neo 1:53 movies. Okay. Now, I want to open my 1:56 command 1:59 line as administrator. 2:04 Okay. And then we're going to go to that 2:13 folder. All right. Now, what I want to 2:16 do is generate a package.json file. To 2:20 do that, I can run npm innit. And if you 2:23 don't have node nodejs installed and 2:26 npm, you just want to go to node.js.org. 2:28 go ahead and download it and install it 2:30 and you should be all set. All right, so 2:33 let's go ahead and just click enter 2:36 here. For the entry point, I'm going to 2:38 do 2:42 app.js. The rest is fine. Okay, so that 2:45 should have created a package.json file. 2:48 I'm going to go ahead and open my 2:51 editor and let's let's see. I want a new 2:57 window. And I'm using Visual Studio 2:59 Code, which is a a nice free editor if 3:02 you want to use that or whatever you 3:05 like. All right. So, I'm going to go 3:06 ahead and open folder. And I want to 3:09 open 3:11 NeoV. And you can see we have a 3:14 package.json file. All right. Next thing 3:16 we're going to do is set up our 3:18 dependencies. So, let's 3:21 go right here. 3:25 and say 3:31 dependencies. Okay, we're going to be 3:32 using 3:34 express. We use express for our routes 3:37 and things like 3:39 that. Want the latest 3:42 version. We also want body parser. 3:52 Okay, we're going to be using E.JS for 3:54 our 3:55 template, which is embedded JavaScript. 3:58 And for that, I'm going to say version 4:05 2.4.2. And we're also going to use 4:07 Morgan, which is a 4:10 logger. And then finally, we need the 4:12 Neo4j driver, which is going to be 4:18 Neo4j driver. 4:22 we'll get the latest version. All right, 4:24 so those are our dependencies. Let's go 4:26 ahead and save 4:28 that and go back into the command prompt 4:31 and we're going to run npm 4:36 install. Okay, that'll get all those 4:38 dependencies set up. All right, so now 4:41 that that's set up, what we're going to 4:42 do is create an app.js file. 4:51 Okay. And let's add 5:01 express. We're also going to add 5:05 path which is a core 5:11 module and logger. 5:21 which is 5:23 Morgan and body 5:37 parser. Okay, we'll add the node the 5:40 Neo4j driver after I set after the 5:43 server set up. All right. So now we're 5:45 going to initialize our app with 5:48 express. Okay. We also want to set up 5:52 the view 5:54 engine. So for that we're going to do 5:56 app 6:00 set 6:02 views and we're going to use path.join 6:05 here. 6:08 What we're doing here is just saying 6:10 that we want to use a folder called 6:12 views to hold our views. All right. Then 6:15 we need to set the 6:21 engine. So view engine is going to be 6:26 EJS. All right. Now we get some some 6:29 other 6:30 um middleware that we want to add here. 6:33 Let me just grab that real quick. 6:36 just your standard middleware for body 6:39 parser. This line here, we're saying 6:42 that our public folder is going to be a 6:45 folder called public. All right? Or our 6:47 static folder. So, I don't think that 6:50 we'll need that, but I mean, it's good 6:53 to have just in case you want to use 6:55 Bootstrap or jQuery, something like 6:57 that, images, something where you need a 6:59 static 7:00 folder. All right. Now, let's um let's 7:04 see. We want 7:06 to what do we want to do? We want to 7:08 listen on a port here. So, we're going 7:10 to say app do 7:13 listen and let's do port 7:17 3000. And then we'll just do a 7:27 console.log. All right. And then we just 7:29 want to export 7:35 There we go. All right. So, the server 7:38 should now run. Let's save that. And 7:40 we're going to go back over here and say 7:42 node app. And you can see that it's 7:45 running on port 3000. So, if we go to 7:49 localhost 7:53 3000. Okay. So, the server is running, 7:55 but we're getting this message cannot 7:57 get slash. And that's because we don't 7:59 have a route set up for slash, which is 8:02 the homepage. All right. So, let's go 8:05 right here and say 8:08 app.get and we'll put in a slash and 8:11 then a 8:14 function. Okay, that function is going 8:16 to get a request and response. And all 8:19 we're going to do is say 8:22 res.end and we'll 8:25 say it works. 8:28 All right. Now, if I go and I reload, we 8:31 have no change. And that's because we 8:33 need to restart the server. Now, if you 8:35 don't want to have to keep restarting 8:37 it, what you can do is you can install 8:39 nodemon. So, to do that, we can say npm 8:44 install nodemon. And you want to add - g 8:47 for 8:49 global. That'll make it so that you can 8:51 use it from anywhere. 8:57 All right. So once that's done, we 8:58 should be able to just say nodemon and 9:01 that'll just kind of watch it for us. So 9:03 if we reload, we get it 9:06 works. All right. So now that that's up 9:09 and running, I'm just going to make this 9:19 smaller. All right. So now that that's 9:21 up and running, what I'm going to do is 9:23 implement the driver. So let's go right 9:26 up here and create a variable called 9:30 Neo4j. And we're going to set that to 9:36 require 9:38 Neo4j dash 9:40 driver. Okay. So once we have that, we 9:44 need to let's go right above where our 9:46 route is and create a variable called 9:51 driver. And this is where we can connect 9:53 to Neo4j. Now we connect using the bolt 9:56 protocol which is uh I believe it was 9:59 new to version 3.0 of Neo4j. So what to 10:03 do that we're going to say 10:07 Neo4j. driver and in here we're going to 10:10 pass in 10:14 bolt slash 10:18 localhost and let's see sec next 10:21 parameter is going to be your um 10:24 authentication for your neo4j server. So 10:28 we can say neo4j.basic 10:32 dot basic and then we're going to pass 10:35 in the username which is neo4j for me 10:39 and for you if you didn't change it and 10:41 then whatever you you made your 10:44 password. All right. And that should 10:46 allow you to 10:48 connect. Okay. Then after that we need 10:50 to create a session 10:52 variable. So that's going to be set to 10:56 driver session. 11:00 All right. Oh, one thing I did forget is 11:03 up here where we require Neo4j driver. 11:06 We actually want to add 11:09 V1. All right. At least at this time we 11:11 need to do 11:13 that. Okay. So now we should be able to 11:17 start making queries. All right. Now let 11:20 me just bring over a page from the 11:23 documentation 11:25 here. All right. So we've done this. 11:29 We've done this and this. So now to to 11:32 run a query, what we need to do is use 11:34 that session variable and then use run 11:37 the run method. You can see here they're 11:39 creating a person node. Um I don't want 11:43 to create anything just yet. I just want 11:45 to fetch some data. So let's go into our 11:48 route here, our home route and we'll say 11:56 session dot 11:59 run and then after that will be dot 12:04 then let me just check this out. So 12:09 then and then catch that's where we'll 12:13 catch any 12:14 errors. All right. So let's go into 12:18 run and we're going to put in query. 12:22 We'll say 12:24 match. Let's get some movies. So we'll 12:26 say 12:28 n movie and then we're going to 12:33 return n limit 12:37 25. All right. So that's just the query 12:40 is as simple as that. Now what we need 12:42 to do is add a function callback 12:44 function in 12:46 here and that's going to take a 12:53 result. And then we should be able to 12:55 loop through that 12:57 result by saying result 13:00 do 13:02 records for 13:05 each. All right. And then inside here 13:07 we'll 13:08 have a 13:11 function and that's going to take a 13:14 parameter called 13:16 record. And then let's just do 13:20 console.log 13:22 record. All right. So um before we run 13:26 that, let's just go down to our 13:29 catch and put in 13:33 function. It's going to take an error. 13:38 And we're just going to console log the 13:40 error if there is 13:41 any. All right. So, we'll save it. And 13:44 then let's 13:46 go to our console and let's see what's 13:50 this. No such file or 13:55 directory. Huh. Not sure what that 13:58 means. Let's restart this. 14:09 Hm. Okay. So, it looks like it's 14:11 working. Now, what we want 14:16 is in fields, this underscore fields. 14:19 You can see there's an object. Uh we 14:22 want the properties object. So, let's 14:25 go right here and we'll say console log 14:29 record. 14:36 zero.properties. Let's try 14:40 that. And there we go. So, you can see 14:43 we're fetching the movies. We're getting 14:45 the year and the title. All right. And 14:48 if we wanted to change this query, let's 14:53 say 14:58 actor reload. And there we get the 15:00 actors. If we want, we could take just 15:02 take off the um label completely. If we 15:06 want to get all 15:10 nodes and there we go. We can get we're 15:13 getting movies, actors, and 15:16 directors. All right. So, we know the 15:19 driver is installed and set up 15:21 correctly. Now, you probably um don't 15:25 want to just console log this stuff. You 15:26 want to bring it into your application. 15:29 All right. So, we're going to have to 15:30 render a view for 15:31 that. So, let's go down to where we have 15:34 res 15:36 send. 15:39 And let's 15:41 see. We don't even excuse me. We don't 15:44 even want this down here anyway. We want 15:46 this stuff to happen up in the then call 15:49 back. All right. So, let's 15:53 go right after the for each, which ends 15:57 right here. So, we'll go right here and 15:59 say 16:03 res. And let's see, let's render 16:09 index. Okay, save that. And then we need 16:11 to create a folder called 16:14 views. And inside there, we're going to 16:17 create a file called 16:21 index.js. And let's just do let's put a 16:25 heading in here. We'll say 16:28 movies. Save it. Make sure that 16:31 works. All right. And then what we want 16:34 to do is the the way that that it's 16:38 constructed your results is a little 16:40 weird. So what I'm going to do is I want 16:43 to create a new object with uh with the 16:47 movies in it. All right? Or an array. So 16:50 let's go right under dot then and create 16:54 a new variable. We'll call it movie 16:57 array and set that for now just to an 17:00 empty 17:01 array. All right. And then down here in 17:03 the for each 17:06 uh what we want to do is we want to add 17:08 to that array. Okay. Through each 17:10 iteration we want to add each movie. So 17:12 we'll say movie array.push 17:17 push and let's pass in an 17:21 object. All right. Now we can actually 17:23 get the 17:25 ID using 17:29 record 17:31 fields 17:34 zero dot 17:39 identity. What is it? 17:42 Identity.lo. And that'll give us the 17:44 actual 17:45 ID. All right. And then we want the 17:48 name. So what I'm going to do is copy 17:50 this right here or cut it and then just 17:53 get rid of 17:55 that. And let's see, 18:02 title. And then we'll just say 18:05 title. And I want to I actually want to 18:08 change this back to 18:11 movie. All right. And then we also want 18:14 the 18:15 year. So for 18:17 that we'll grab 18:20 this, paste that in doty year. All 18:24 right. So we're constructing this array 18:26 of movies. And then what we want to do 18:28 is we want to pass that array into our 18:31 view. So let's go right here and we'll 18:34 say movies is going to equal movie 18:38 array. All right. So we'll save that. 18:42 And then let's go into our 18:45 view and we're going to use EJS syntax 18:48 which is less than 18:51 percent. And then we'll say 18:55 movies for 18:57 each and in this for each goes a 19:04 function. Okay. And let's pass in what 19:08 we want to use as a variable. We use 19:10 movie and we need to make sure we end 19:13 these both of these 19:19 lines. All right. And then we should 19:21 have access to each movie. So let's go 19:23 ahead and just 19:25 do let's do a ul 19:31 here. And then inside here we'll have 19:36 li and we should be able to 19:40 say 19:44 movie. 19:46 Hopefully. Okay, let's save that and 19:50 reload. And there we go. We're bringing 19:52 in all the movies from 19:55 Neo4j. Now, let's say we want to get the 19:57 actors as well. If we wanted if we were 19:59 going to do that, let's go back to 20:01 App.js. JS and we're going to have to 20:03 nest our set our run calls our session 20:06 runs. So let's go down to where we have 20:09 our 20:10 view and 20:14 start going say 20:18 session 20:26 run. Okay. And then let's see in 20:29 here we'll say 20:34 match and 20:36 actor and then 20:40 return and 20:42 limit 20:43 25. Okay. And then in the 20:47 then we're going to have a 20:51 function and we're going to get the 20:53 result. 20:55 Now, for this, I'm going to use result 20:57 two, just because I mean, we probably 20:59 could use result even though we used it 21:02 up here, but just to to kind of um so 21:05 it's not so confusing, we'll use result 21:08 two. All right. And then we're going to 21:10 do the same thing. We're going to create 21:12 an array. So, this one will be actor 21:18 array. And then we can take result 21:24 two and say 21:27 dot records for 21:41 each. And then I'm just going to copy 21:43 what we have right here and put that 21:46 here. 21:50 Okay, except we want to change this to 21:52 actor 21:53 array. And we only we don't want a year 21:57 or a title. I'm going to change title to 22:06 name. Okay. Okay. And then what we'll do 22:08 is I'm going to take the the render 22:10 right here and cut that 22:15 out and put that up here right under the 22:19 for 22:24 each. All right. Right. And then we 22:26 should be able to also pass along 22:49 actors. All right. So, let's save that 22:52 and then go back to our view and let's 22:56 see. I'm going to copy this whole 22:59 thing. Okay, we'll change that to 23:03 actors and we're going to do 23:11 actors. And this will be 23:19 actor.name. All right. So now we we're 23:21 getting the movies and the actors. All 23:24 right. So the next thing I want to do is 23:26 I want to be able to add movies from our 23:27 application. So let's go right above 23:31 here and let's put a 23:39 form. Actually, we'll just 23:42 have Oh, that's fine. We'll put a label. 23:45 I don't care about this looking good. 23:47 You guys can can take can use Bootstrap 23:49 or something to make it look a little 23:51 better, but um I'm just going to focus 23:53 on the functionality. So, it's not going 23:54 to be very 23:55 pretty. All right. So, we'll say movie 24:05 name. We'll give it a type of text and a 24:10 name of name. Actually, let's 24:15 do movie 24:22 name. Okay. Then we'll put a line 24:27 break. And we're also going to want the 24:41 year. Then we want a submit 24:53 button. Okay, let's look at 24:56 that. Okay, it doesn't look very good, 24:58 but that's all right. Let's actually put 25:01 a line break here and 25:12 here. All right. 25:15 So, in the form tag, we want to add a 25:18 method of post because it's going to be 25:20 a post request. and the 25:23 action we'll say 25:25 slashmov slash add. All right. So now we 25:29 want to go on the other side and create 25:31 that 25:32 route. So let's go to uh 25:38 app.js and we'll go down here and say 25:47 app.post. Okay, that's going to take a 25:49 request and 25:53 response. And let's see, what do we need 25:55 to do? We need to grab the the values 25:58 that are in the form field. So, let's 26:00 say 26:01 var name equals request 26:06 dobody do name. 26:10 And same with the 26:21 year. Okay, just to test it out, let's 26:24 do 26:31 console.log. We'll say 26:35 name. And then we're just going to do a 26:37 redirect. We'll say res 26:39 do 26:40 redirect to the 26:45 homepage. All right. So, let's see if 26:47 that 26:56 works. Okay. So, let's just say 27:01 test cannot post movie 27:04 ad. Oh, I didn't put the 27:19 route. Okay, let's take a look at the 27:21 console. And we get test. So, we know 27:24 that it's grabbing that 27:26 input. So, what we want to do 27:30 now is the same thing. We need to have a 27:32 session with the run method. So, what 27:35 I'll do is just I'm not going to copy 27:37 it. I'll just retype 27:39 it. So, 27:51 session.run. Okay. So, in 27:55 here, let's do 28:00 create and this is going to be a movie. 28:04 So create and movie and let's put 28:08 in the 28:11 title and we're going to put a 28:13 placeholder here. We'll just say title 28:16 param. Okay, you don't want to put the 28:19 thing. You don't want to put it directly 28:21 in. And let's put a comma. And then 28:23 we'll say what else is it? Year. 28:30 year 28:33 param and then we're going to 28:37 return we'll return n dot 28:41 title. All right. And then over here 28:44 we're going to need another 28:47 parameter and we'll say 28:49 title 28:51 param is going to 28:55 equal the movie. Odd. I put name, didn't 28:58 I? 29:00 Yeah. You know what? I'm going to change 29:01 this to 29:05 title. I'm going to do it here. And back 29:08 in the 29:10 view right 29:17 here. Actually, I'm going to get rid of 29:19 movie. So, it'll just be title and year 29:22 just to try to avoid 29:25 confusion. All right. And then down 29:27 here, we'll just change that to 29:30 title 29:32 year. All right. And then over here, 29:34 that can just be 29:38 title. All right. And we also want the 29:40 year params. So we're going to put a 29:42 comma 29:47 there. All 29:49 right. So that's our query. Now and 29:53 then we need our 29:57 function. Function takes a 30:00 result and we don't need to really do 30:03 anything here. I'm just going to 30:09 redirect. Okay. Okay. And then we just 30:11 want to close the session with session 30:13 dot 30:28 close. All right, let's go ahead and 30:31 save 30:33 it. And let's 30:36 reload. And let's 30:40 put Independence 30:44 Day 30:45 2016. Submit. And there it is. 30:48 Independence Day. If we reload, it's 30:51 still there. And if we go to our Neo4j 30:54 browser, we should see it as well. So, 30:56 let's 30:57 reload and click on this asterisk over 31:01 here. And there it is. Independence Day. 31:04 You can see it has its year, too. All 31:06 right. Okay. So, we can now add we can 31:08 now read data from our Neo4j database 31:11 and we can add 31:13 data. All right. And as you probably 31:15 expect, you can do the same exact thing 31:17 with actors and 31:19 directors. So, let's go ahead and do 31:22 that. Let's add the actors 31:26 form. So, what I'll 31:32 do, let's put a heading up here. 31:42 change these to 31:47 H3s. Okay. And then let's copy this 31:51 form. And this will be add 31:55 actor. We'll have it go to 31:58 slash actor slash add 32:08 And I think they just have a name 32:09 property. So we don't need 32:18 this. All right. So there's our active 32:20 form. Let's go ahead and go to 32:23 app.js and we're going to do the same 32:25 kind of thing. I'm just going to copy 32:27 this 32:35 Okay, we want to get the 32:41 name and let's change this query to 32:45 create. If you don't understand these 32:48 queries at all, then I would suggest go 32:50 watching the other Neo4j video that I 32:53 have because that'll explain all of 32:57 this. Change this 33:00 Okay, we don't need the 33:03 year. And then over here, let's return 33:06 the 33:10 name. Get rid of 33:19 that. All right, so that should do it. 33:21 Let's save 33:23 it and let's 33:25 reload. We'll add an actor here. Let's 33:28 say 33:29 Jeff Gold 33:31 Plum. Submit. And there we go. So, we 33:35 can now add movies and 33:37 actors. 33:39 Now, obviously, Neo4j and graph 33:42 databases are they're the best when 33:44 you're you're working with data that's 33:46 related to each other. So, what I'm 33:48 going to do is I'm going to add another 33:50 form to add an actor to a movie. All 33:54 right. So, let's go ahead and go to our 33:57 view. And I'm going to just copy 34:06 this. Let's 34:09 say add 34:11 actor to 34:13 movie. And that's going to go to movie 34:17 slash actor slash 34:21 add. Okay. So, we want to add the 34:27 movie. We're not going to need the 34:31 um years, but we we are going to need 34:34 the the actor 34:49 name. All right. So, now 34:52 Let's go 34:53 to 34:56 app.js. And I'm just going to copy 34:59 this. Hope this isn't getting too 35:03 confusing. And what was the route? I 35:05 think it was movie 35:08 slash 35:10 actor. And then we want to get both the 35:13 title of the movie and the name of the 35:14 actor. 35:21 Okay, now this is going to be a little 35:22 different. We're adding a relationship. 35:25 So, let's totally clear this 35:28 out, this 35:31 query. Okay, so we know we're going to 35:33 have a name and a 35:37 title. So, we can add 35:45 those. And then for the query we're 35:47 going to do 35:51 match. Okay. So 35:54 match a 35:57 actor and we want the 36:01 name that's going to be set to name 36:05 param. All right. And then we're going 36:06 to put a comma here and then another set 36:10 of parentheses. And then B 36:16 movie that's also going to have that's 36:18 going to have a title which will be 36:20 title 36:25 param. All right. And then we need to 36:27 go right here and do merge. So we want 36:32 to say merge 36:36 a and it's going to have a relationship 36:40 of acted 36:43 in to 36:46 b. All right. And then we're just going 36:48 to 36:50 return a 36:53 b. All right. So that's how we can add a 36:58 relationship. All right. So hopefully 37:00 that works. Let's go ahead and save 37:06 it. All right. Now, what I want to do is 37:08 I want to add Jeff Goldblum to 37:11 Independence Day because if we look at 37:13 the graph right now, they shouldn't be 37:18 related. Okay, so you can see right 37:21 here. Okay, there's no relationship. So 37:24 let's go ahead and try to add that. So, 37:28 movie Independence Day 37:31 actor Jeff Goldblum submit. And then if 37:35 we go back to our 37:42 graph. No, that didn't work. Let's see 37:45 what the console says. Name param not 37:49 defined. 37:57 Not to 38:07 find Oh, I didn't put I didn't put uh 38:10 curly braces around 38:15 this. That should be Yeah, like that. 38:18 Same thing 38:19 here. You guys probably noticed that. 38:24 All right, let's try it 38:34 again. 38:41 Okay. And there we go. Okay. So, you can 38:44 see now we have an actedin relationship 38:47 between Jeff Goldblum and Independence 38:50 Day. All right. So we can now read data. 38:54 We can add it and we can also add 38:56 relationships. All right. So I'm going 38:58 to go ahead and stop here now that you 39:00 can see how this works, how you can use 39:03 the Neo4j driver and write queries. I 39:06 would suggest that you keep going and 39:09 maybe make it so that you can click on 39:11 one of these movies. It'll take you to a 39:13 page and list all the actors. I might do 39:16 that in the future. I'm not sure. If you 39:19 guys want me to do that, let me know in 39:20 the comment section. Uh, but that's 39:22 going to be it for now.