So, let's continue working on this REST API and there are different places where we
could work on Next, but I want to start by making sure that we're actually using a
database to store the submitted data when creating a user, for example.
And of course, therefore, we will need to add some code here to create user and also
to find user by email, but first I need some code to set up that database, to install the
database too.
And for that, I'll now use the chat that's integrated in Cursor.
We could also use Composer again, but to mix things up and show you these different features
in action, I'll use the chat.
I'll remove the gitignore file as context here, don't need that.
And instead, I'll add this user.js file in the models folder as context.
And the open file, at least at the point of time when I'm recording this, is automatically
added here as context, as you can tell by looking at this.
If it were not added, you could do it manually here with a plus, you could add files like
this, or you can also add at files and then pick a file you want to add as context.
And as shown before, you could add other contexts as well, like the web search, for example.
And I'll do that here.
And I'll say, I want to store all data in a SQLite database.
How do I add one to this application?
And actually, I'll now confirm this by hitting this button or by using this shortcut here
so that it takes my entire code base into account and therefore understands that it's
a Node Express application and so on.
So let's see what it gives me.
Of course, it starts by analyzing all the files and then it tells me that I could install
the SQLite3 package, tells me how to do that.
And then how I can adjust my code, that I can add a new database.js file here in which
I initialize the database and where I then also in this case have some code to create
an initial table and how to update the other files to call this database initialization
code and also to use the database, for example, when creating users.
Now that's all fine, but I already know that I don't want to use that SQLite3 package because
I'm an experienced Node Express developer.
I have built applications like this before and I know that I prefer the better SQLite
package or better SQLite3 is the actual name.
So I'll send this as a follow-up message.
I want to use the better SQLite3 package instead of the SQLite3 package.
Update your response.
Accordingly, that sentence is probably not required, but I'll still add it here.
And with that, it hopefully gives me some updated instructions.
For example, how to install this package.
Well, it's just a different name, of course, but then also regarding initialization that
I could add this code here and that I should call some of that code from inside user.js
folder and that I also need to call initialize database in the app.js folder.
Again, we could have used composer for that as well, but here I'll go with the chat and
I'll start by installing the package.
So I'll press apply here, but no, I don't want to apply this to user.js.
So at least right now, it's not understanding that this is a terminal command.
So I'll instead copy it and then run it in my terminal here in the project folder to
install this package.
That's the first step like this.
And then let's see, I then want to apply these changes here to the to be created database.js
file.
But if I click apply, it wants to apply to user.js.
So that's not working because it will essentially always apply it to the file I have currently
open at least right now when I record this.
So what I'll do instead is I'll add a database.js file next to app.js in my case.
We could also store it in a subfolder.
That's up to you.
And then with that newly added database.js file, with that open, I'll click apply.
Now apply to that file like this.
And I'll accept this.
and then again I'll take a look whether I want to tweak this but this looks
almost good to me though I'll remove auto increment here no reason to add
this with SQLite this is all we need it will automatically be incremented by
default so the ID will automatically be generated by default even without auto
increment and auto increment can actually lead to worse performance so
I'll not add it here email should be unique password to be
text and I don't need anything else here so that's looking good now then I have
to get database function which will give me access to this database object which
is initialized when calling initialize database so that's looking good next I
want to update my models user.js file I can click on that link to be taken there
and then I'll click apply to update this file and there it will add this
import to the get database function so that I get access to the database and
then it will try to insert data into the database and here in find user by email
it does the same thing but tries to find a user with a given email address so
I'll accept this as well looks good to me we'll test it soon anyways and if
there are any problems we'll fix them but on first sight this looks good and
that's by the way the flow I prefer you could analyze the code in greater depth
before you accept it I just tend to take a brief look across it and then just
test it and tweak it thereafter but there are definitely different ways or
different approaches you could take regarding this now as a last step I'll
go to app.js and apply the changes here as well and here it's the import of this
initialized database function that's added and then this is called here and
if that fails the server is shut down so that makes sense and then thereafter we
have this code so that here will run to initialize the database though I'm not
100% happy with that but I'll change that manually so for now I'll accept
this and then tweak it manually so that's the general logic added now and
with that I'll now make some manual tweaks