WEBVTT

1
00:00:01.799 --> 00:00:08.720
So, let's continue working on this REST API and there are different places where we

2
00:00:08.720 --> 00:00:13.400
could work on Next, but I want to start by making sure that we're actually using a

3
00:00:13.400 --> 00:00:19.000
database to store the submitted data when creating a user, for example.

4
00:00:20.660 --> 00:00:24.840
And of course, therefore, we will need to add some code here to create user and also

5
00:00:24.820 --> 00:00:30.820
to find user by email, but first I need some code to set up that database, to install the

6
00:00:30.820 --> 00:00:31.580
database too.

7
00:00:32.900 --> 00:00:37.320
And for that, I'll now use the chat that's integrated in Cursor.

8
00:00:37.420 --> 00:00:42.980
We could also use Composer again, but to mix things up and show you these different features

9
00:00:42.980 --> 00:00:44.980
in action, I'll use the chat.

10
00:00:45.480 --> 00:00:48.660
I'll remove the gitignore file as context here, don't need that.

11
00:00:49.380 --> 00:00:53.540
And instead, I'll add this user.js file in the models folder as context.

12
00:00:54.800 --> 00:00:59.380
And the open file, at least at the point of time when I'm recording this, is automatically

13
00:00:59.380 --> 00:01:03.680
added here as context, as you can tell by looking at this.

14
00:01:03.960 --> 00:01:09.820
If it were not added, you could do it manually here with a plus, you could add files like

15
00:01:09.820 --> 00:01:16.700
this, or you can also add at files and then pick a file you want to add as context.

16
00:01:18.260 --> 00:01:22.900
And as shown before, you could add other contexts as well, like the web search, for example.

17
00:01:23.940 --> 00:01:24.800
And I'll do that here.

18
00:01:25.260 --> 00:01:31.580
And I'll say, I want to store all data in a SQLite database.

19
00:01:32.899 --> 00:01:37.940
How do I add one to this application?

20
00:01:39.820 --> 00:01:46.900
And actually, I'll now confirm this by hitting this button or by using this shortcut here

21
00:01:46.860 --> 00:01:51.140
so that it takes my entire code base into account and therefore understands that it's

22
00:01:51.140 --> 00:01:52.940
a Node Express application and so on.

23
00:01:54.880 --> 00:01:56.680
So let's see what it gives me.

24
00:01:57.220 --> 00:02:01.060
Of course, it starts by analyzing all the files and then it tells me that I could install

25
00:02:01.060 --> 00:02:04.400
the SQLite3 package, tells me how to do that.

26
00:02:05.000 --> 00:02:13.240
And then how I can adjust my code, that I can add a new database.js file here in which

27
00:02:13.180 --> 00:02:18.980
I initialize the database and where I then also in this case have some code to create

28
00:02:18.980 --> 00:02:24.280
an initial table and how to update the other files to call this database initialization

29
00:02:24.280 --> 00:02:28.800
code and also to use the database, for example, when creating users.

30
00:02:29.840 --> 00:02:36.700
Now that's all fine, but I already know that I don't want to use that SQLite3 package because

31
00:02:36.700 --> 00:02:39.660
I'm an experienced Node Express developer.

32
00:02:39.820 --> 00:02:45.680
I have built applications like this before and I know that I prefer the better SQLite

33
00:02:45.680 --> 00:02:50.720
package or better SQLite3 is the actual name.

34
00:02:51.320 --> 00:02:53.240
So I'll send this as a follow-up message.

35
00:02:53.440 --> 00:03:01.340
I want to use the better SQLite3 package instead of the SQLite3 package.

36
00:03:02.140 --> 00:03:04.440
Update your response.

37
00:03:05.800 --> 00:03:10.200
Accordingly, that sentence is probably not required, but I'll still add it here.

38
00:03:11.720 --> 00:03:14.860
And with that, it hopefully gives me some updated instructions.

39
00:03:15.160 --> 00:03:17.420
For example, how to install this package.

40
00:03:18.440 --> 00:03:24.060
Well, it's just a different name, of course, but then also regarding initialization that

41
00:03:24.060 --> 00:03:31.560
I could add this code here and that I should call some of that code from inside user.js

42
00:03:33.239 --> 00:03:38.600
folder and that I also need to call initialize database in the app.js folder.

43
00:03:39.800 --> 00:03:45.000
Again, we could have used composer for that as well, but here I'll go with the chat and

44
00:03:45.000 --> 00:03:46.860
I'll start by installing the package.

45
00:03:47.800 --> 00:03:54.080
So I'll press apply here, but no, I don't want to apply this to user.js.

46
00:03:54.520 --> 00:03:58.300
So at least right now, it's not understanding that this is a terminal command.

47
00:03:58.660 --> 00:04:05.640
So I'll instead copy it and then run it in my terminal here in the project folder to

48
00:04:05.640 --> 00:04:06.900
install this package.

49
00:04:07.380 --> 00:04:09.640
That's the first step like this.

50
00:04:11.420 --> 00:04:21.440
And then let's see, I then want to apply these changes here to the to be created database.js

51
00:04:21.440 --> 00:04:21.860
file.

52
00:04:22.960 --> 00:04:25.900
But if I click apply, it wants to apply to user.js.

53
00:04:27.100 --> 00:04:31.840
So that's not working because it will essentially always apply it to the file I have currently

54
00:04:31.840 --> 00:04:35.360
open at least right now when I record this.

55
00:04:35.780 --> 00:04:40.660
So what I'll do instead is I'll add a database.js file next to app.js in my case.

56
00:04:41.680 --> 00:04:43.680
We could also store it in a subfolder.

57
00:04:43.980 --> 00:04:44.740
That's up to you.

58
00:04:45.400 --> 00:04:50.760
And then with that newly added database.js file, with that open, I'll click apply.

59
00:04:51.260 --> 00:04:55.940
Now apply to that file like this.

60
00:04:57.960 --> 00:05:00.000
And I'll accept this.

61
00:05:00.000 --> 00:05:06.620
and then again I'll take a look whether I want to tweak this but this looks

62
00:05:06.620 --> 00:05:10.780
almost good to me though I'll remove auto increment here no reason to add

63
00:05:10.780 --> 00:05:16.400
this with SQLite this is all we need it will automatically be incremented by

64
00:05:16.400 --> 00:05:20.680
default so the ID will automatically be generated by default even without auto

65
00:05:20.680 --> 00:05:25.260
increment and auto increment can actually lead to worse performance so

66
00:05:25.240 --> 00:05:29.580
I'll not add it here email should be unique password to be

67
00:05:29.580 --> 00:05:35.620
text and I don't need anything else here so that's looking good now then I have

68
00:05:35.620 --> 00:05:40.000
to get database function which will give me access to this database object which

69
00:05:40.000 --> 00:05:46.800
is initialized when calling initialize database so that's looking good next I

70
00:05:46.800 --> 00:05:51.680
want to update my models user.js file I can click on that link to be taken there

71
00:05:51.600 --> 00:05:58.360
and then I'll click apply to update this file and there it will add this

72
00:05:58.360 --> 00:06:04.340
import to the get database function so that I get access to the database and

73
00:06:04.340 --> 00:06:11.080
then it will try to insert data into the database and here in find user by email

74
00:06:11.080 --> 00:06:17.560
it does the same thing but tries to find a user with a given email address so

75
00:06:17.520 --> 00:06:22.240
I'll accept this as well looks good to me we'll test it soon anyways and if

76
00:06:22.240 --> 00:06:27.740
there are any problems we'll fix them but on first sight this looks good and

77
00:06:27.740 --> 00:06:31.860
that's by the way the flow I prefer you could analyze the code in greater depth

78
00:06:31.860 --> 00:06:37.720
before you accept it I just tend to take a brief look across it and then just

79
00:06:37.720 --> 00:06:42.880
test it and tweak it thereafter but there are definitely different ways or

80
00:06:42.880 --> 00:06:48.000
different approaches you could take regarding this now as a last step I'll

81
00:06:48.000 --> 00:06:54.640
go to app.js and apply the changes here as well and here it's the import of this

82
00:06:54.640 --> 00:07:00.840
initialized database function that's added and then this is called here and

83
00:07:00.840 --> 00:07:05.780
if that fails the server is shut down so that makes sense and then thereafter we

84
00:07:05.760 --> 00:07:13.860
have this code so that here will run to initialize the database though I'm not

85
00:07:13.860 --> 00:07:18.520
100% happy with that but I'll change that manually so for now I'll accept

86
00:07:18.520 --> 00:07:24.919
this and then tweak it manually so that's the general logic added now and

87
00:07:24.919 --> 00:07:27.380
with that I'll now make some manual tweaks

