Good morning. Let's go ahead and get started. I just started
So the agenda today. Just so, assignment one is due on Monday, October 7.th
I'm planning on discussing solutions.
In class.
On Tuesday.
You'll note. Oh, here's a cool.
See that works.
So one of the things you're supposed to do is.
Download this template.
And then uses validator page.
Make sure you answer. Probably named it.
So.
Here's the template page.
So what are you supposed to do? You're supposed to put your name here, student. Id. Here.
And then put your answers.
In these spaces, and you know.
Exit. Now, one thing is, this is a unix text file.
The validator checks to see whether it's a unix text file.
Depending on what you use to edit it.
You may or may not get it now. So like, if you're on.
But with textedit on a Mac.
You the default format, for it is rich. It's a rich text file.
Which text format which has got all this.
Markup and stuff in it. Basically, if you can set the font.
On a per line basis.
Yeah, yeah.
So Macos defaults nowadays to unix files because it's unix.
So you shouldn't have a problem on windows. The default text file format is a whit is a Dos text file.
Which is slightly different format, but modern text, that it should be able to handle it.
Okay, so I may. I may feel.
If it works, listen, if it works in the check. That's fine.
That's that's no problem.
Yeah, yeah. Not bad, plus plus can be used for windows before it can be changed. The settings from Unix. Yep, exactly.
To be honest, it is possible to make my script.
Automatically like change, carriage, turn line, feed into line, feed.
Things. But I particularly want you all to understand.
This difference.
Because it can lead to lots of weird things.
So!
Yes, I'm gonna talk about T. 4 today. Yes. So you see the character like tool to line feed and windows. Notepad, that should do it. Yes.
You just downloaded it with W. Get and edit with Vs code on the Vm.
That's perfectly fine.
You. You can edit it in Vs code, locally.
Vs. Codes a perfectly good text editor, and shouldn't mess anything up.
But notice, yeah, so you don't have to edit it remotely in the Vm, if you're doing your answers, you can actually have.
Local, but whichever.
Whatever works,
But if you're doing a lot of work in the class vms.
That's great, but.
They aren't backed up.
They've got some other things, but they're not backed up, and various things can happen to them.
Cloud, storage.
You know, can go wrong and go wrong horribly. So.
Make sure you have backups.
So using
I can show you guys how to do some stuff with Ssh.
Things back up. But if you're doing things in Vs code.
You should be able to save a local copy. I mean just.
Be careful.
You can use scp.
There's a great tool called Oursink.
In the past I've set up.
Class fms with scripts for that.
You could use get well.
Get by itself would just be local.
Get just as version control get does not store nothing else. Now, if you use.
Github. Yes.
If you store it to Github. But then, of course, make sure your stuff's private.
But, Github, now let's have one, you know.
Set up private repositories so, but.
Yes, do. Backups.
Anyway, so.
If so. Now let's go back to.
Where were we?
We are. So here's yes, that's there. So now this. So assignments due on Monday, we'll go over on Tuesday.
And then I'm planning on having assignment.
Be released ideally by the end of this weekend.
So assignment 2 is going to be based on.
Basically the first.st
4 tutorials tutorial will come out before the midterm exam.
But it's content. I'm playing on shifting.
Tutorial. 5 to client side stuff.
That won't be on the midterm.
But I'm hoping the material in it will help.
Validate your
Yeah, help with your mental model, and so should help you on the midterm.
Was if the openstack vm, you know.
Go down other. Vm. I don't quite know what you're saying.
Or can you use other? Oh, okay, can you use other Pm's.
Yes, of course. I mean, actually, you can still use other vms for this class. That's not. It's not a requirement. We just highly encourage you to use.
The other step was because we support those.
And because we, you know, we know how they're set up.
But.
I'm not testing you on that offering points is just you work with the tas and go like.
What's got? Why, isn't your local setup working.
You know, but also, I mean, I kind of want you to use these get used to using development with the remote machine and all the things that that entails.
One of the things that entails is.
Keeping backups in some way.
So that if that machine goes down you have something else.
Because things go wrong.
Again. The lecture of the midterm is going to be right after the midterm.
The midterm right after the fall break.
We are sitting here. Lecture 8.
And today I wanted to talk about.
Tutorial for.
I just found out in my.
Emacs browser, and I can hit capital. R.
And I can get a nice little thing here, although it cuts off too much sometimes. That's.
It's not good.
So actually, I have to do it this way.
So what's in
Tutorial, for here are the tasks.
But I wanted to show you the code. Here, let's just.
Let's load the code.
And
So this is running on my, on my classm.
This all look fine. By the way, notice, there's Sp. Dx license identifier.
This is just like I put in there to identify the license.
This is something good to do when you actually have code is to.
Put the license in. And now there's this nice.
It used to be, your classic license would be this.
Long
Bit of tax that you need to have there to say what license it is. But now.
This Spd Dx license identifier. If you look up Spd access whole standard forings.
Licenses.
You pick the license you want.
Put this as your 1st line, and everyone knows what license your code is under.
So here we've got dB, demo I mentioned I showed to you very briefly. I'm gonna go through it line by line.
But before I do that I want to talk a bit about
I will talk about the Sq. Light.
Adapter in a bit, but that does.
That is definitely something we should talk about.
How should you be doing 2 trials? Your goal should be to understand every line of code that is signed.
So this is one thing I don't think I've emphasized this enough with the tutorials, and this gets more and more important as the course goes on.
Is you can't just.
Do the tasks.
And call it done when you're doing the tutorials. Now, if you do the task and you feel like I understand this code. I know what it does.
Sure.
But if there are parts of the code that you look at and kind of go.
I'm not quite sure what's going on here.
You should try it out.
Because it's not much code.
And underneath that code are very important concepts.
So you know, computer systems are complex nowadays. So there's.
If you really want to know everything that's going down, it's a rabbit hole that goes very deep.
But you should have a basic understanding of why every line is there in the code. I specifically.
Have developed this code.
So the best thing is, if you don't know why's there? Why, something is written in a certain way.
You're probably missing something.
And so you should try it out. So one of my.
My philosophies. Here is.
To develop a mental model.
Of anything. But especially a program.
You can't do it passively.
You have to do it actively.
Confront yourself with it, and really.
Think about what it is.
The beautiful thing about working with.
Computers and code, that these are artifacts that we can.
Mess with. We can extend, we can manipulate, we can.
Break in. We can debug, we can.
So understanding.
Is there for the taking.
If you're willing to put in the time to look at the code.
And not just read it, but like wrestle with it, and.
Until you do.
And that's what I want you to do, because I'm not giving you that much code. But the code is doing very.
Interesting things.
So that's what I want you to.
Come up with that. So.
So. Along those lines.
So what? So what we have is dB, demo.
And dB. Demo is.
Basically form demo.
With database support.
And the database support is this thing called sqlite, and I spent last time talking about Sq.
Now normally.
So where we have a database in an app and a web application.
That database is normally separate.
It's normally separate, either in a separate process running on the same computer or.
Running a completely different computer. And either way you talk to it over a network connection.
And you send messages.
With sqlite. That's not the case.
It is part of the application.
Times.
SQL. Is so fast.
It's often fascinated.
Than regular. I/O.
Now.
Here's a weird thing I want to show you. This is actually, we're switching.
My sharing here, so to understand what's going on here.
So here is
This is demo sqlite.
So this is the Github page.
Of of the of this.
Dental sqlite, so like go to
So if we go here.
Looking at.
Tutorial for.
And you go to the code here, and you look at.
This dental land X.
Sqlite, MoD. TS.
You could only just take this.
This is the demo sqlite module.
If you go to that thing that you go here.
This is the same page.
So you go to the Github page. It tells you about all these things here.
What's cool about this now, of course, you can look at the code downloaded, because when you.
When you actually run a program like this, they actually will download code.
From here and store it in.
The Cash Directory, so it'll be in cash. Deno.
So you can look at that. Not it's kind of annoying to look around. Bother with it too much, but.
It's downloaded onto your computer. So the 1st time you're asking it's there and subsequent acts, it just accesses the local copy.
This is the code. It's downloading.
And if you go to.
You know the source.
And you go.
And you. And you look at this.
Where is it?
Build? No, no, no, that's not.
Ha! Here we go!
So if you look at this thing in here.
There is this file called.
Sqlite. Js, and you notice it's only 55 lines.
Long, but it's.
1 MB in size.
How is it this short.
And that long well.
Look at this!
That string.
Right, there is the entire implementation.
Of sqlite.
Which I find kind of.
This right here.
Is an entire database.
In this one line. It's 1 MB.
Line, but it's.
One line,
Now understand what SQL is.
Is. It's it's written in.
It's a it's it's a small thing, you know. It's the full source, like.
3 MB. So the compiled version being a megabyte, actually, I think it goes even smaller.
Where can we can we also connect.
Version here.
Source code. Let's see.
Version control. Yeah. The other. Okay.
Oh, yeah, anyway.
As a piece of software engineering. Sqlite is very interesting.
Let's share my
Window again.
So here's the bizarre thing. Sqm.
Was it built for battleships.
It might have been 1st written as as a contract for
Defense. I haven't looked at that.
Okay.
Anyway. But yeah, but it's it's a.
I'd be, I'd be kind of curious to look at the the history of it, but.
Up.
So sqlite's really cool as a thing.
So just want to pull. This is a little thing about Webassembly here. So Webassembly.
Is.
This. It's a language that's designed like an assembler like it's like, it's almost like machine code.
It's got some other neat security properties and things that's formally specified and stuff. But.
Web. Modern web browsers understand Javascript.
And Webassembly. Now, Javascript, it's Webassembly still a second class citizen.
If you have code, and Webassembly can't access all the Apis.
You can access Javascript. You have to go through a Javascript.
To get a lot of things that is changing.
Web sellers became more and more functional. So you can say, I really hate Javascript. Maybe I should just write it. Can I write it, and see or write it in Java, whatever. Well, you can.
If you can compile it to Webassembly.
And loaded in. Now the compile version of Webassembly, will it be as small and sufficient.
As the Javascript code. Maybe not. Depends on how it was written.
But
Webassembly is not a
Just like Javascript gets more capable webassemblies becoming more capable. It's a much newer technology.
I might do some research related to it. It's.
It's cool stuff, but.
Why do I mention? This is because.
When you do this, when you include this module, what you're actually doing is loading the code.
Of sequel, light.
Your program.
As Webassembly, and then you're calling it.
Which I just find just like.
I just loaded entire SQL database.
You know this is normally not something you think you can do.
Now.
So one of the big limitations of sqlite.
Is, it expects only one program to access it as a time.
So.
So what about different instances of the same program? No.
You don't want different.
But if you need more than one program to access the database at the same time.
Don't use sqlite.
Basically any other database is good, because that's what they're designed for.
That's actually the entire purpose, really, traditionally, of a database engine.
Is to allow for concurrent access.
To data sqlite, isn't it? Gives you the same interface. Those databases.
But it's just one entity now.
Escalate, so fast.
It can be part of the main event. Loop.
And so it can.
So.
Because so fast it can be part of the main event. Loop. You can just.
Block on access to it.
And get things back.
I've seen some basic benchmarks and web servers using sqlite can often be ridiculously fast.
Using things and things like that or something. I mean, it's it's really quite remarkable. But.
You still gotta be careful what you do with the database, because if you try loading too much work into the database, you could make.
You could make things very slow. So you.
That's why.
That's why. In fact, if you look at the calls.
To add, record and get all records and stuff you'll notice.
Their functions.
Not asynchronous functions.
These run to completion.
So again, this is a teaching course.
Outside. My priority has been for simplicity. If you understand them, things.
But this isn't actually, entirely.
Dumb because sqlite is so fast.
You can do things like.
You know, put in these little queries and and do things. I mean, you might have to optimize things a little bit if you.
You might want to benchmark your application. Is it running slow, or something like that? But.
In general, this is actually pretty sane.
And it's kind of us. It's kind of an interesting design pattern.
Again, if you wanted to scale this like, you needed to have many web servers running into candle load, and them all acts in the same database that might be shunted across multiple machines.
Don't do it this way, but because you're using SQL.
Porting, to use a different database. Engine is not.
That hard because the commands.
Basically the same. That's the nice thing about sequel. It works across.
Embedded things like sqlite all the way up to giant oracle databases and larger.
Like the cloud databases.
All having the same Api.
So that's the
Let's walk through the code.
Let's now start. Start with it. Let's say, how does this actually work.
Because in terms of how it runs.
Everyone's exactly like I'm almost exactly like
Form, Demos. There's no point in showing that right now.
But.
So let's start here. Let's start at the bottom. Here.
So what do we have here? This stuff here is.
Different.
So here's here is how form demo is at the end. So as this IC function handler.
And then it has this.
I made the end fancier.
Instead of that one line. We've got all these lines.
Does anyone know? Why?
I did this.
Why they turn one line into like.
What is this? So this is 2, 76.
To 2, 58.
Almost 20 lines.
Readability.
I replace one line with 18 lines that doesn't generally help readability.
This adds this, this change, add certain capabilities.
By making so one capability, it adds, is, I've now made explicit.
The port, and the hostname.
Yep, yeah. The port, the port, and the hosting explicit. Yes.
And then add a handler for signat.
Yes.
Why?
Why? Well, 1st of all, what is second?
They don't know what sign is.
Let's rephrase it.
So so Sig is means it's a unix interrupt signal.
So this is what the program receives.
When you hit control C.
In the command line. So you hit control. C.
That's how you stop a program from running.
This is the signal that's sent.
So by default, the signaler for signing.
Just kills the program.
Here because we have a signal handler.
So we have to, because we have the signaler.
So so the flow is weird.
Yeah, the the flow here is a little weird. So first, st what we're adding is the signal handler. So this means that when the program gets a control C message.
It goes 1st thing it does it. Logs say, oh, I got.
And then it sets.
This A/C aboard.
So this, there's 2 kinds of signals here. There's unix signals, and there's dento signals.
You know, and
So this, this is a unix single coming in. I convert the unix signal into a demo signal which I have set up here.
I have set up a
If there's an A/C
A single A/C dot signal.
And so what this is, that when this signal is.
You know this is a single handway, so I get a message when it's finished. So here, I now say, if server is finished.
Then I do this. So this is again using promises. Whenever you see this dot notation.
Cool. Well, sorry. So this is server finished. That's a method of server.
And so when that.
Finishes and basically. And it's it's things.
Then call this function.
So this is an asynchronous thing we're saying, the server finished. Method.
Then do this. You can just treat it the way it reads. But there's some interesting magic happening.
Then close the database. The whole purpose of this.
Was I didn't want to shut down.
Without closing the database.
And that's.
Probably not strictly necessary in this case.
But it's really bad.
Because if you're especially having it in memory, database.
Like you're having sqlite.
There can be.
State in the database that hasn't necessarily been written to disk.
And you really want to make sure it has been committed to disk, because otherwise it would be.
Persistent. So dB closed, basically saying, I'm done using the database. Do whatever cleanup you have to do.
Again. I don't think it. Actually, I think the chance of data loss in this kind of situation are relatively low. But I wanted to show you how.
How you should do things normally.
Make sure things go with the database. Now, if the database was remote, you don't have to worry about these things, because there, although you may want to close, still, close the database connection, because.
Your web server may be waiting to send some data out.
So you may want to pause, shut down until that date is all been transmitted.
So the flow is, we start. We, you know, this.
You know. This is what actually gets a server running.
This.
Although it then runs in the background.
It, just, you know, sets it off and runs the background.
And then this code executes, which defines a signal handler.
And then this code which sets up.
This function to be called when the server terminates.
In response, the you know. And this thing the serial finished because it gets this abort message from this thing, and it gets this because.
We've done this again.
Kind of convoluted, took me a little while to figure this out.
But I really wanted to be able to close the database properly when you interrupted it.
So I've got some still some questions here about this, but I think I address them just now.
That that? Yes, let me. I can explain that I could
We'll sort of finish then, only be called an aco A board is called.
Yes, so yes, this will be.
This will be called.
Oh, well, sorry! This is called when the server finishes.
But by default the server runs forever.
And now you might be going.
If the server runs forever, I hit control. C. Why do I need this? Well, it turns out if I just hit control C on the program. It doesn't actually.
Finish it just gets killed.
Which is why I'm worried about all the sort of ungraceful thing.
I'm waiting by catching the signal.
Go like oh, Jerry!
It's time to shut down.
And then let me shut down properly, because by default the operating system would just go. Oh, you're done.
And you're and the programs, you know, terminated.
This is saying, oh, I'm shutting down. Just give me a sec. Let me clean up.
Now do that. So that's typically why you put signal handlers for catching things that will cause it to.
Terminate this, do that. So? Cause I noticed I tried like 1st doing this.
The server finished. Then it seems like when the server finished to do that, and I hit control C. And it didn't finish. I never saw that, messer like. Oh, I've got to catch the signal. Fine.
So I made a signal.
And then call this, which then causes this causes this to be run.
So there's some question about function syntax.
So this whole, so in this.
What you're having here is this.
Syntax of segent. It's a string, and then.
There's this open, close parentheses, and this little.
Do, Hickey.
What? What is that called
So.
Rocket. No, no! What! What is this little.
Thing, called.
So.
Is it called a lambda doesn't look like, yeah. Anyway, lambda is actually a proper term. But I'm like the operator like it's a hat.
It's an arrow we'll call an arrow.
Sorry.
So, and and.
In Javascript all functions are closures.
In the sense that.
The variables are enclosed within there. It's a
Function. That's something I probably should go through. Actually, I think, in tutorial 5.
He's talking about closures.
That will actually help you guys, I think, understand some of this and fit into other things with the other code.
Arrow function like this. So it's an okay. There's an arrow function. Okay, arrow function.
That makes more sense. So this arrow operator is used. So what you do is you have.
The parentheses.
So what? What you're having here, you notice, here is.
This is the arguments to the function. There's no arguments. So you just have to parentheses.
And then you have the operator, and then you have the.
Close of it. So this you know, this close brace.
Here.
Matches up.
There we go! Those are slow, save.
Anyway.
So I've just defined 2 functions. So this sort of syntax. We haven't seen much of it, because.
In classic
Server-side Javascript in general, but we.
Find a lot of functions.
On the fly, because we're defining callbacks.
Now this exact thing. What I could like, I could replace this equivalently.
I could have written, and this is absurd. I could have said.
Create that, and then I could have had.
So this is essentially the same as this.
This is the same function. I'm just naming the function. But I'm going like I'm just defining it once.
And I'm using it right after. And it's really short. So it's like.
Do I need to do that? That's why we have anonymous functions for functions that it's not worth our time to make an explicit name for, because they're just being.
Passed on to someone else who's going to call.
We're never gonna call it that finisher will have its own name for it.
I guess I finish up no signs. The the then will have some name for it.
We don't care about that.
So a consumer type parameter which is the consumer type parameter.
I'm not following that question. I'm happy to address that question here. Now.
There was another question, what's the difference between the server finishing and getting killed?
Good question, so.
Let's let me show you what the difference is.
Oh, darn it!
Change my emacs, default.
I wanna do
Just give me a second here. Rearrange windows.
Okay, so
I'm gonna show the
Support.
So I just wanted to get this running here because I wanted to.
Oh!
Thanks for.
1, 2, 2. Tutorial, 4.
Video.
Back to the code here. We've got now a terminal here, ready to run.
And so now I can go into.
And now, if I go into dB demo.
And I do.
So if I run it there.
So here I'm loading the form. Demo. Here.
Now, if I hit control C.
Here, right now.
You'll notice it, says Sigue, terminating.
Server terminating, closing, database.
Now let's instead.
Let's just get rid of the signaler.
Keep everything else, the same.
Got rid of the signal handler. Otherwise functionality is the same.
Now we've got this here, we can.
So it's running. And now we hit home.
What's the difference?
Between these 2.
You see the difference now, when I hit control C, the program.
Stops. It's not going to do it once. That single is not able to do anything after it. We don't exactly. You don't get the finishing message.
So what's happened now.
Is without the signal handler.
When the control, when the interrupt happens.
It doesn't get a chance to do anything afterwards.
And I'm saying I want to do something afterwards. I want to shut down gracefully.
Want to handle the problem that has arisen.
So in order to handle it.
You know what I wanted to do was call dB's.
And I thought that oh, this.
You know, this abort controller would let me do that, and I'm saying like, here I set up the abort signal and say signal, and then.
You know it would call.
You know it would. It would do this, but.
You know, actually, I was actually, I was. So I was thinking that.
I didn't think I needed this A/C signal thing. I didn't think I needed the abort control. I thought I could just.
Just say when it's finished, but it turns out the finished handler.
Doesn't get run if you send it and interrupt.
When does a finished handler happen when you.
Terminate.
The server, and how do you terminate the server?
Cause it run is, does it meant to run forever.
Well, you have to send it a board signal.
So.
Oh, sorry! We'll keep this here. We'll keep this here.
Okay. Yeah.
Here, we're just.
Let's see, that's the one. Hence this is the. This is the functionality as it was.
So now we go back here.
We run it again.
And now we hit control. C. You see, you know.
Control. C. You see that me typing that there and then the message terminating.
It sends a message and you get started. And closing.
Exactly. Instead of stopping the promo drop abruptly. It does. It's just like.
Think about your text editor.
Let's say you're in the middle of typing something.
And let's say, for some reason the operating system decides. You have to.
Terminated like actually in particular. Let's say you're remotely connecting to it.
Text. Editor, you're editing something. There's some stuff in memory, but you haven't saved your work.
Do you want your text editor to potentially save a recovery file before the program terminates? Or would you like it just to be terminated.
Normally you'd like it to save a recovery file, maybe not overwrite the original files there, but at least.
Have something, a backup file place that you can say. Oh, I want to recover that file. What it was.
That's the sort of thing that you're enabling with this sort of.
Functionality is that is.
Graceful, shutdown.
Do something with the state you have.
And with whatever other cleanup you have.
Important thing for programs like web servers. They might need to do some cleanup on shutdown.
So. So I was just showing you that, and in particular, a good thing to do is.
Close the database, especially in memory database.
Just in case everything else out to disk.
There was a question here about. I'm not calling anonymous function.
So.
We talk about anonymous functions.
So we can
So here with Demo, we can say.
Main function.
Of X.
Okay, that's fine.
Now.
Let's say I have a function.
G.
Why?
That does.
Be of.
Pulse, F.
Getting very confusing, proper editor.
So let's find.
So.
F, of.
So what can you do with.
I'm just gonna show you a bit of this.
Okay, that's very guaranteed.
Silly landlines.
Okay, so that you understand.
So now we've got 2 functions F and G.
Now.
And I can make a separate function.
Now, let's say I just want to do something that.
But let's say I've got these functions here.
Remember what we load a file here.
So.
So I've got F as a function G as a function. H is a function.
So if I call H. Of.
5.
What's that gonna do? That's an error.
H. Expects a function.
So I can say so. I can say F. Of.
Works. Gee of 5.
Works.
H, of F.
Tab.
Why is that? Because I've passed into the function.
H, another function, f.
And that then calls it with the argument of 5.
So now see, H. Wants a function. So I'm now naming there. But I could also do.
So you see here, I defined a function here.
That H. Was willing to take.
It took, called 5. Of that return, 5, divided by 3, which is 1 point.
So now there's a question. Here is, what if you call hf, 1st for calling.
Does the order. So here's a question. Does the order of execution.
Here matter between these things.
HF.
H. Of F is always going to be 10.
Because it's calling.
And in fact,
And H, of G.
Is always going to be 15, because this always calls the function that it's given with the argument.
So this will always return 15. This will always return 10. It doesn't matter the order that's.
These are completely separate. Notice the F here. This is part of what means be a closure.
Is the F here? Well, we'll talk more about it later. But this.
Here is completely distinct.
From this F here. This is locally defined as lexically scoped.
Here.
So.
There's a lot more to do that. I will. We will go more.
Function scoping and things later. But
But the main thing to know is, you can define these functions on the fly.
That will be given a name. So when I did this.
When I define this H of X. And if did this.
What name was this function given.
When it was called, it was given the name F.
It was called here. But again, that F name is completely different from this F.
So
I think. Eden, are you clear? Now on, okay. Yeah, okay. You're good. Okay. You're good now. So.
Moving on.
So we're going through this code. I was explaining this, we kind of got into this. So this, basically, this is all a bunch of elaboration.
So you can set the port, set the host name. It shuts down gracefully.
Wait. So H is basically taking a function, but does nothing with it.
Oh, wait a second.
No, no, no! H takes in a function and calls it.
That's what h does. H. Takes that function and calls that function within the body.
Now.
What happens if I had instead called this G.
If I call this G here, then the F. Wouldn't be defined here, it would have to go to the outer scope. It would have to look up.
F. Over here and call it that.
So.
Yeah. So so that's that's key thing to know. It's worth.
Playing with these kind of things, and I will give you some more.
Complicated examples later, I think that'll be a good use of tutorial 5 is just.
Javascript.
Javascriptiness.
Something I didn't offer that.
But moving up here.
Handler works the same as before.
It calls
What is it doing? It does a path it place with index.
Logs. This, if it's not the route, doesn't handle it.
The routing function. You know that this loads the file.
This handles routing for get and post.
If it's a post, it does, you know it checks an ad. If it's.
That calls add record. This calls, get, and does list record. But list, record and add record are different.
Because they are calling
They're calling. This is calling. Add record. dB.
Instead of just pushing it onto.
The state, array.
And
List records, calls.
Get all records here.
Which gets it from the database.
So so sorry. One quick questions here. Do you assign anonymous function to a regular function?
You can assign an anonymous function.
To any variable name.
Because functions are 1st class things, and so you can use it. Then once you've signed that name, you can use that as a function as you would otherwise.
But when you define things as function here, it's a little, it's more like a const.
It doesn't change.
So!
Yeah, you define this.
So. So what was I saying?
You use? Constant is used like, why am I using constant here? And why am I using variable here?
We could also be using lat.
That's kind of newer, and let.
I prefer function, scopes, function, scope.
Makes sense to me. Block scopes like, where's the block and stuff.
It can be useful, but I find vars like.
It's global to the function.
So when I say Var is defined for the entire scope of the function.
And this is what the.
But but this is really so when I say var here.
What that really means.
Is that the value of these could change inside of the function.
When I say Const, I mean the value of this cannot change.
Inside of it. Now, technically, what Kant says is, whatever this thing refers to.
Doesn't change so like, for example, you could have a.
I'm not sure about that.
I'm going to check that.
Let's let's let's also what I'm talking about here.
Okay. So what I'm I'm curious about. So let's say, if I say, Const.
You know.
4, 5.
Let's say a equals 6.
Gives me an error constant, variable. You're not allowed to change it.
Booked say, const.
B equals.
So I can't say B equals.
3. Now it says it's constant. I can't do that. But can I say B.
Be of 1 0, 7.
Yes.
So, notice.
When I say Const, it means the association of this name.
With its value, will not change.
But that does not mean.
The value itself cannot change, particularly if it's like a object.
Or an array. It's constant. It's contents can change so.
This can't be that does not say that this array is constant.
It says that the reference.
To this array like. I cannot now say that.
B equals.
I can't do that.
If I was going to reassign B to some other name.
Then it doesn't.
Can you push to be.
Yep. You can push to be.
Yeah. Constant array is exactly constant. Address value. Exactly.
So I was wondering. I'm pretty sure that was the case. But 1st I was like, Wait does coms actually enforce on the tip? I don't think so.
That's why I want to check so.
Other languages have other semantics for constant. I'm not saying this is the best I'm just saying. That's what this is.
Cost is one of those things like.
Why bother even using it? I mean, really what you're saying.
When you say constant.
Is this really to help you.
Catch errors, either as a developer.
Talking so other development, or like, if you accidentally assigned to this very like, I shouldn't have assigned to it.
Now.
On these programs are so short.
These folks are so short you don't really have much of a scope of reassigning things.
The one variable that is actually significant in this code.
Here here, I kind of think it's a variable. It could change, I mean, like the sort of semantically.
Conscious eye, and changing.
But yeah, what the one thing that's.
Place where a variable is needed. This var path is not.
Constant. This has to be a variable.
And the reason why.
Is because of this.
I reassign, path.
Here, under some circumstances.
So that's why that is not constant.
I could have a ridge path like if I want to make sure I could make that constant.
And that'll be fine if I make path constant.
I would get runtime error.
Okay, so
I think we're. I think I've caught up on the questions.
So that's the.
So this this codes all.
As we expect, it's the one as before.
I'm happy to answer questions specifically about it, but the the new part.
Is this part.
Where we're doing sequel. Now.
I am no master of sequel.
By any means.
And it's funny because I actually did a.
Startup, where I was.
Who's doing some monitoring of databases.
But
Yeah, that's that was a whole other thing.
I am no master of sequel. I don't claim to be.
I'm not really a master of any of these things.
Because I don't do research on them. But I can tell you about I mean.
It's always important to know.
But you know what you don't know.
And these technologies are very rich things.
And truly mask them. You have to use them.
Regularly for years, and even then you won't know everything.
Up.
But what's important, especially when starting out is understanding what is a conceptual framework you have. So you know what you can and can't do with these things.
And where you should do different things.
So I just hit control. D, that's always a good way of getting all these things.
If you look in your directory here now, I have a.
The Tilde version of this. This means that I made a modified version. That's a backup.
Version of dB. Demojs.
That's what these Tilda files are.
Is always the last version that you did when you started. The editor.
And then there's people. dB, this is the thing that was created.
And notice that there's a line in there that will create it if it doesn't exist.
But this database is there. This database has some entries.
Or we can see if this database is entries. Now you can do this through your program and insert sqlite things, but we can also use a command line version.
Sqlite to edit this. Now, you wouldn't want to do this.
While your website was running at least as long as you could.
If you, as long as you didn't modify the database, it should be fine, but.
Get concurrent access can be dangerous.
So.
You if you are just using a regular Vm.
May have to install this, but actually, I think, in the standard.
24, one Pm. Even.
In the 24 6 Pm. This is already.
Installed but just apt install.
Analytic your app install. SPY. 3.
And that's how you get it installed.
But these are built in. So I'm not having, you know these are included, not built in.
They're installed by Se. Support.
So
So one thing to know. If you're the command line 1st line, you can do things.
Dot commands are.
Non standard commands. So like, that's what you do when you want to like.
Talk to the implementation.
Of SQL. As opposed to putting it.
Statement. And so that's where, like you say, Doc, quit.
To quit.
That's why dot help is the help thing.
So now I can say things like select.
Star, from.
People.
So the people table in the people database.
And here I've got listed these things.
These are the records. And so that's the id.
That's the name that's there.
You know you can also
There's a way to enable headers.
Which I forget. But there's a like, there's a way that would actually show you the field names.
I could also do
Select City.
People.
So what is the star mean? It means star bit is a wildcard kick.
Character. It's, you know. Remember, if you're using the shell that just matches anything.
And that's the same thing here in sequel. When you say Star, it means.
Match, everything.
So what this is saying. Show, select.
All fields, from people.
Now there's various ways of.
Of restricting. So like, how do I.
If I'm gonna start doing SQL programming, you know what I do.
I look it up, I'll be, you know, completely honest.
The way I do sequel is, I look it.
And I find the particular command, but the things to know.
About sequel just like any database.
So if I wanted to add things to the database, I could.
If you're wondering.
What are these commands doing here.
You can just type these commands.
Into the command line.
And play with the database.
Completely separate from
From deno, and just.
See what it does.
And I strongly encourage you to do that if you're trying to develop things, and then you can embed it here.
Just to give you an idea of how this intersection works.
I'm going to
Switch back to my regular web browser.
Almost out of time. Here.
Okay? So you can see my browser. So this was.
Remember this as well, showing you there before.
Let's go back here.
So here's so one thing at the bottom here, it says.
Who uses this.
I found the small RM. Sqlite.
So object. Relational model, I think, is what's stands for
Very small object, relational map or bare, essential to quickly set up embedded database.
Tesla. So why would you use something like this? And people use these kind of interfaces.
All the time when interacting with databases.
So you include this.
And then you say, you know, user extendable. So here's a table here.
Here's this, you could do things like.
Set things so you.
You do things in a much more Javascript way. You basically.
Add things, the database increment. Look things up.
All using more Javascript.
Like interfaces where there's a mapping between objects.
And records in the database.
If you look at the implementations. MoD, t, so it's module.
And it's up. So when we say, MoD, ts, it's really neat.
It's loading. Lib dot TS, okay, so lib.
Small sqlite. Ts, here's the actual code.
If you look at this code.
So, by the way, whenever you see typescript versus we're teaching, we're doing through Javascript in this course.
Type script is Javascript types.
So it's.
Almost all the same, except in certain places. You'll have.
Type declarations. Now, kind of point that out.
Why do we have those type declarations? Because it can help you catch errors.
I'm not. I think it's good to.
Get used to Javascript without types, because.
That's what Javascript actually is underneath.
Like, for example, here bool string int.
Same Boolean string number that's.
Type script for
Javascript just says.
Give me 3 things. I'll figure it out later.
So it's got all these like interface here. But if you look in this code.
You'll start seeing things like integer primary key auto, not know.
Altar table.
Add column, create table, if not exists.
Insert into.
Mean this, things creating SQL. Commands.
To access the database.
By manipulating strings. So you.
Create a little Javascript thing and have their little fields and stuff. And then this thing.
Converts it into a string.
And then sends it off to the database.
Can Javascript do this pretty fast? Yes, I don't think this is a particularly inefficient, but.
It's important to note. This is what's happening under the hood.
That it's just sequel.
And so, if you go like, oh, my little interface isn't doing what I want, then maybe do that, or maybe.
You should just define your own Api.
How you want to use the database.
Figure out the particular SQL. Commands you want to use.
And then the rest of your code doesn't have to care.
I the main reason why I'm I said you should think about these things, because sometimes these.
Mappings break in weird ways.
And
Spend a lot of time debugging, but if you understand, you're converting to another language, you can make things a lot.
Clear.
Why don't we migrate all SQL. Related queries to a separate Javascript file. That is.
Definitely, a good idea.
That is good software engineering practice.
I am not following good software engineering practice.
Because I want you all to see everything.
At once.
Like ideally what you, the way you should organize dB, demo is, it should be a few.
Modules, and second half the course.
I will factor these things into separate modules.
But right now we're sitting at.
300 lines.
It's not that much code.
So is it? We're separating the files. It tends to get more confusing when we get to more than this, though. Yeah, it's probably good to.
Organize things.
My idea is, I want you to be able to read through this entire file and going. Oh, this is what this doing! Oh, this is what this is doing.
Notice here.
There's this funny thing here where you know we're creating the string. So I have insert into table.
This is using standard Javascript. String literals that can do.
Interpolation. So we're taking this Javascript variable here, sticking it into. Here is the table.
Tables here to find his people.
But then this other thing is this weird.
SQL. Thing.
So insert into table.
Name, city birthday email. So that's the names.
Of the fields in the database.
And then I'm adding, so I'm just dividing this over multiple lines. That's why I'm doing this plus.
And I say values. And I say, question, mark, question, I'm saying.
There are going to be 5 values which will go into these 5 fields.
And then, so that so.
And then this coming afterwards, it will.
Passing the values.
So.
That's how we go from the, you know.
The record we give those things. This is how it gets put into there.
This? dB, query.
Interface.
Basically let associate will handle. The replacement of the question marks with this.
Not sure exactly how it works under the hood, but.
But this is a common SQL. Syntax.
You say these? Well, actually.
The classic sequel syntax is, you'd actually have the values.
You put the actual values right there. So I must assume dB, query.
Does the string substitution for us.
Anyway, I'm out of time.
Up.
Play with us.
Extended, fix the code, reorganize it, put it into modules.
Add extra buttons, add extra fields, add validation.
Make it into a real web app.
You know that that way. You really know what it's doing.
Or just read through it, make very minor modifications. It's really whatever is. But.
Understand this code, know what it does.
Anything is confusing you.
Do experiments until you understand.
Or or look it up.
Thank you all very much. Good luck with assignment one. We in our next class. We will go through it.
And I'll stop recording here, and I'll stick around to answer questions.
Thank you very much.