- Podcasts
- CodingCat.dev Podcast
- Code with CodingCat.dev: Next.js Amplified: Full-Stack Web Apps on AWS Amplify Gen2
Code with CodingCat.dev: Next.js Amplified: Full-Stack Web Apps on AWS Amplify Gen2
CodingCat.dev Podcast
- June 30, 2024
- 1 h 27 min
- Season 2, episode 3
About this episodehttps://codingcat.dev/podcast/cwcc-1-2-aws-amplify
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Code with CodingCat.dev: Next.js Amplified: Full-Stack Web Apps on AWS Amplify Gen2
What's up, Eric? Hey, how's it going? How's it going? I'm doing good. I'm doing good. Happy Wednesday. I guess I've been, I've been like out of it. My, I don't know why, but 2024, I feel like I've been sicker than I have any other year, which has been awesome. So I'm just kind of rolling back in, so bear with me if my mental game is completely off. Well.
Thanks for having me on. Let me and. I I think you sound fine. Oh good. I'll throw up the old AWS Amplified Gen. 2 banner. So that's what we're talking about today. But I just wanted to say hi to to Eric for a little bit. First get some people in here. I shared some links. I don't know Eric, do you have any other spots to share this fun live coding session with? No, so I didn't see which links you shared. Was it the docs links?
Sorry, I I shared them out on my own Twitter and stuff like that but. Let me. Yeah, let. Me send you the YouTube and that way you have it as well let. Me. See. Oh yeah, I got it. Cool. Sweet, get some joiners in here checking out Gen. 2. So what have you been up to? I think I saw you last at Vegas for Reinvent.
Have you been doing anything fun since then? Yeah, no, yes, it's Reinvent was just a whirlwind event at the end of November, beginning of December. That was the first time I had been at a Reinvent where I was speaking and I was an employee. I'd been there as a customer before, so that was really fun meeting you, Alex, and, and meeting so many other great people in the AWS community, a bunch of other developer advocates and solution architects. And yeah, it, it was great.
And what's really nice about our my work schedule. So a little bit about me. I'm AI used to be doing a front end engineering on the UI team. And then recently I moved over to doing developer advocacy for the AWS Amplify team. Is that usually December like right after reinvent things slow down a whole lot. It's it's really great.
So I was able to deep dive into some old projects that I wanted to get done, create some content that I hadn't done in the past, deep dive even further into Gen. 2, which we're going to talk about today and, and and look at a demo app. So that that was mostly all of December and then my other passion, I have quite a few passions life coding, teaching, but is football. And so I was all into the post season. I'm a big 49ers fan.
And then I had the heartbreak last Sunday when we were so close and we just came up short. But you know, I'm also me and my wife are big Taylor Swift fans. So I think during the game, every time Taylor Swift came on, we we were cheering. But it was fun. For those who don't know who that that's this is American football.
Not not the football. Yeah, and I'm, I'm, I'm sure worldwide, you know who Taylor Swift is. If you don't know, Taylor Swift has been dating Travis Kelsey from the Kansas City Chiefs, who, who beat the 49ers. Unfortunately for Eric, I had, I had the amazing trip to be, you know, a huge Detroit Lions fan. So you guys?
Got to. To beat me slightly earlier, I was actually, I was flying back from Singapore, I landed in San Francisco. I was going to, I was going to think about going to the game. So I was going to hit up the Detroit, San Francisco game. But I started doing math on like re booking a flight, a hotel room, $1500 for the ticket to actually go. And I'm like, can't justify this, but it was fun. Like I got to the trip back from, so I flew from San Francisco to Houston.
And the the flight back from Houston, there's a big group of us Lions fans. So we're all cheering together. And then we promptly lost Wi-Fi so we couldn't watch the end of the game, which was the worst part. Maybe that's a good thing. It was. It was good. That's so funny. I saw so many like the day after that game, which I think 49ers, I think half the nation wanted more than half the nation wanted Lions win. It's probably like 7030.
And so there's all these videos of like people watching them, like Lions fans watching the game. And they were so happy during the the first half. And then the second-half, it didn't work out. And there was also a couple I saw where people were on an airplane and just like you. But yeah, there was like cheering on the plane. And then the second-half, yeah, I feel bad for the Lions fans 'cause now that's how I feel now, losing us. So I guess what comes around goes around. Yeah, yours was definitely a lot closer in the end. There it was.
It was a little more heartbreaking. I was cheering for the 49ers. I wanted to see, was it Mr. Irrelevant Rock Purdy make his way out of the stage, but Oh well. He's got a lot of years left, hopefully. He can, yeah. He's a young guy, so. I'll figure it out. Somebody's got to beat Mahomes one of these days.
From an AWS perspective, Gen. two came out. I know you guys pinged me on it a while back when it was kind of in early alpha stage. So where are we at with Gen. 2 these days? Is it officially released? Yeah. So Gen. 2 is our is still in preview right now. So we're hoping to release it soon, probably in the next quarter or two.
And the way we Amplify Gen. 2 is kind of a rethinking of the way we handle our creating apps using Amplify. So if you've used Amplify in the past, you've probably used either our command line interface or CLI tool, or you've probably used something called Studio. And I think me and a couple people were on the podcast, Heather, a few months ago, maybe like, gosh, maybe been a year ago right now where we really deep, yeah, we deep dive into studio, we deep dive into our UI components framework, which we we still have, by the way. We're still, we have all these products and you can still use them today.
We're still supported. But we thought we've heard over and over again that people just really like these infrastructure as code tools. They like CDK, they like other cloud, they like some people like cloud formation, some people don't like people like Terraform SST, like people like kind of deep diving in the code. And if you create the right abstracts for it, then it makes a a really great experience.
So we rethought of like how can we take what we do best? And you can see here from the screenshot and how can we create a really great user experience to create the back end. But also at the same time, we wanted to integrate it really well in with our app sync service, our managed graph QL service. So then we came up with this idea of of this kind of dot notation, this TypeScript first approach and this code first approach, which makes it much more straightforward to create these apps and then connect to databases, things like that. I definitely love it from a a standpoint, a lot of what you know, we're not all of it, but many of the tooling that we used in the past was always kind of more on the server or not server generated code, just kind of being in the code itself and less on the the UI side.
I thought it was cool once we kind of got into the studio and things like that, but I still know massive projects that are running today and it's, it's all code based. So it was kind of cool to see this jump over to, to Gen. 2 and where it's headed to. I I really love it. Yeah, we, we looked at and you like I said, Studio's still going strong and and we're still adding to it.
But if if you look at some of the features people really liked about Studio, which is our kind of drag and drop interface, people really like the Figma plug in so that that is now available as like a command line tool that you can use to generate your forms to grab Figma files and convert them over to React code. So a lot of those things are still there. They're just now just there's different ways to do it. You don't have to log into the Studio console to to get there. Yeah.
And so we brought all those over. There's now command line tools that you can do to generate those forms. And then it still uses the tried and true UI React library that we created. And we're still actually even adding in more components to that. That's amazing that you guys are able to continue building this out too.
Yes, yes, looks like. Hi, Chat. Hi, Abjit. Hi, Best Jerry. Yeah. And if anybody has questions, yeah, feel free to. Yeah, throw them, throw them in the chat. We'll get to them as we go. So I think with all of that said, what we're here to talk about today is this fun part. This is probably the best page to start with. So it's, these are the new docs, newish docs for Amplify AWS. And what we're looking to do is this piece right here for Gen. 2.
And we're gonna, we're gonna kind of deep dive. I, I hope we can work on the, the one I want to work on. We'll see if Eric agrees. But I'm looking looking forward to either like the next JS app robber, server components or client components if you're if your games are creating something. Yeah, let's do it.
Yeah, I'd love to. Sweet. I might have to let me share my screen and we can get started. Let me open up my We can just start for Zachly from scratch and then go form it and. Perfect. And we'll do. Do you want to just coach me through it like someone who doesn't know anything? You want to go that route. I I was just gonna maybe I can show it, but. I I'm totally game I I love a day off. That sounds great.
Like I said, I haven't been feeling my best anyway, So what up? What up Focus Adder I? Like the? Name Focus Adder. That is our Mike Holiendo. He is our App Sync expert DA and always a pleasure to talk to him. It's good to have him in the chat. It's always fun relating screen names back to to users, people to people. Messes me up all the time.
Well, Eric's clicking around over there just to to show folks on the dock side of things. So you can still there. There are these kind of like newer docks and you can flip between like frameworks and things too. So just keep that in mind. Like if you want to follow along but you're like, I'm not ready for Gen. 2, you can always pop into next GS and just go version six.
You can do the whole like getting started and stuff like that. Typically what you're going to see after that if I actually open up the console, not Firebase, sorry folks, sorry. They're both console, so once you kind of get in here you can start to create different apps too within this so. Yeah, it's, it's pretty powerful there. And also, yeah, we redid our whole Gen. 2 site. So we, we did pretty much a a fresh coat of paint on our whole dock site because we heard it was time for it. So we're really, we're really glad. I think it looks much more nicer, cleaner, easier to navigate.
We did a whole infrastructure clean up on it. Couldn't agree more. It's great. Now, I know you're going to hate this. You're probably going to have to bump up at least one, maybe two. It gets a little light. OK, yes. That's good, I think. Let us let us know if you can't see anything, folks. Yeah, so here's my sample folder. I have just a bunch of sample apps in here, but let's let's just start from scratch.
Let's create a next app. So I'm going to do NPX, create next app at latest, and then we'll put some, we'll call it Code Cat live. We're going to use TypeScript, yeslint. I love tailwinds, so we'll leave that on. So last time I was here, we showed everything using, using our UI components library, our UI React library. I think today we'll just use Tailwind just to kind of speed things up a little bit, but certainly you can do most lot. We have a lot of really nice UI components in as well. And we'll also use Authenticator. This is a interesting question I think it's OK to answer. Trying to build a surveillance app for something like a whistleblower within my state.
So if you have some particular use case, I'm not sure what you need for surveillance, you can then maybe use some A to B services for that. So I would say that set that could happen. Things like protecting your app with off and everything are are already there. Yeah, I'll put a link in. I just did a little update.
So we have this new thing called community dot AWS out there. So that's part of I think community builders and other folks can write content on there. I'll drop the the link in, but I just did ACW or CWK compare CDK comparison. There we go to amplify and kind of when to use which and both and all those fun things.
So definitely jump in and check that out too. Nice. That's awesome. All right, so I'm gonna install a couple of libraries or UI Rack library and AW, Amplify library, and there's one more. Let me check while that's installing. Yeah, that's a great point, folks, Otter, when when the original Amplify came about, there's kind of like this black magic that was occurring and and no one actually understood what was going on. And then it kind of opened up further and you're able to extend a lot of things.
So great point if if AWS can do it, pretty much Amplify can do it. So. I'm also going to install one more uninstall adapter Next JS. And this is this is specific adapter we created exactly for a Next JS if we're using server components and server rendering. So let's just well, before we, well, we'll start the server and before we get too far, I don't know.
Actually, before we do that, let me reopen this in the same folder. Dash R We were just talking about this. I can. Never remember the dash. What it? Which one opens in the same? Man I I forget to sometimes so it's glad. Britney's always like you're you need Dash R I'm like, oh, sorry. And we'll just close some of these things, OK? All right, so now we're in our app and we can search files. Let's go.
It's all like you're using Tailwind, but you don't have any Tailwind utility classes. What are you doing? That's what I just told us. All right, so here is very ugly, but very simple. Hello world and we'll we'll bump that font up in a second, but let's say we want to do flex flex column items center. And then for this H1 we'll do to class name text. I don't know, three XL also add heading 4. OK, so we got a hello world here. So I think what we should do before we jump directly into into setting up Gen. 2, I figure we want to protect this whole app by our authenticator.
Well, you know what? What? I'll do two things. First, let's get the since it takes a few minutes to provision and create all the resources, let's do that first, and then we'll jump into adding the authenticator. How's that sound? Sorry, that sounds great. I I'm kind of thinking, should we tell people that haven't used amplify at all, maybe what's about to occur or maybe while it's occur?
Yeah, yeah, let's sure, sure, one second here. So what I'm thinking is, so we have this next app, it's just a basic Hello world next app using Tailwind. And the next steps here would be to connect it to a back end. So what I'm thinking is, let's say we create a kind of like a meal planning prep app. So we're gonna try to see how far we can get on that. So we're gonna have like, I'm thinking like three boxes here on the screen.
But yeah, it's hungry. So maybe it's a, excuse me, maybe it's like a way to to save some data to the back end, kind of like a CRUD type app. But it, I think it'll just show how straightforward is to get started with Gen. 2. And then on top of that, we want to, we want to make sure that only logged in authenticated users are able to connect and make these changes and add these, these these values for their own user. So that's kind of the goal of today. Nice, love it.
All right, So we we're open up another terminal and what we can do is if we it's in the docs here, so we can mirror what it says in in the docs. But if you go to docs dot amplified dot AWS and you click on the get started and you get started here, you can do this one right here. You're kind of saying you, you want to take a look at this next just app server components and it gives you the instructions.
So the first thing it tells us right here is to after you create the app is to run this NPM create amplify at latest. Also, by the way, I'm think I'm using Node 20, so make sure. Well, it looks like I'm using 18.17. I think that's fine, but you need to have like at least 18 or 20 to do this. Otherwise you might get an error. So I'm gonna just copy and paste this here. And I just paste this in the main and it's gonna ask me a few questions.
I'm gonna create a project on the route and this is just going to scaffold some things out. So if we look at here, we have our public, our source folder, and it's going to create a new Amplify folder when it's done here. So it's just going to install some required dependencies while this is going. Do we have any other questions?
I think we just got the the dash R thing which we covered. SO-R opens VS Code in the same window, so you don't have to like open a whole another one. I didn't actually see you use any vim things, but there's something about vim key bindings, so. Oh yeah, I I love Vim. I'm a big Vim fan, so I use. It my my lead, Dan Moore at Fusion Auth, huge vim guy, like to the point where I'm like, it's easier and then he's like, no, it's not it's.
Like. OK, I feel like I have to open the VS Code for everything. All right, so it looks like it it created it. So I believe, and you can correct me of this at this point, you actually created a project up in the cloud on AWS as well, right? Not yet. We're not quite there yet. So we have this new amplify folder here and inside here we have this off and data. And so let's take a look at him.
So if you look at off here, so it's scaffold all this out. And what this is saying is this is a kind of out-of-the-box Cognito which is our identity provider for AWS service and you can make some changes to it but I'm probably going to leave it as default. It says users are going to login through e-mail. If I want to I can add in like Federated logins like login with Amazon or Google.
If I needed to. I can also add in multi factor authentication. I can add user attributes. So this is going to scaffold out and create a brand new Cognito back end for us, with some very sane defaults that we can use. This, this is reminding me I have an e-mail out to Renee. I haven't heard back. So if you if you see him poking around, send him over my way 'cause we're supposed to chat about Auth 0, fusion auth kind of stuff.
So you can do things like that. So we we do work with those other providers. I'm just gonna leave it all default for now. And then in the resource, this is the resource one. And this is where we create our schema and we give a bunch of steps to for people who've never used it before or you can look through the documentation.
So you can think of this as creating this like like a database. But really we're creating app sync or Graph QL service. And when we use this dot model, it is creating not just this model, but it's creating all the resolvers. So usually in Graph QL you have to have create these resolvers that connect to your database to like the queries you're putting in.
And this does everything for you. So with these few lines, it creates the create, read, update and delete in this case like list get all those ones create, delete for you and just with a few lines of code. So and it also has this dot notation so you can add in validation rules. So maybe this content has to be a string and if you try to push up a number into this content, it's not going to work. You can also make it required.
So when you're doing a create, you have to have this available. And we have all the all these other types too, like enums, dates, things like that and I'll show you. Is there a choice to go API Gateway anymore or is that kind of fall into the wayside? So if you want the API gateway now, then which? Which to your point, like a Restful service, you can drop down to the CDK layer and then create that manually. So you can still create a REST service if you want. You can also drop down to create functions. So we're in developer preview right now. We really focused on two use cases, that is auth, authentication and data.
However, we're adding in like storage and functions, which are going to be like these other high level kind of constructs. You can add them now with CDK, but it's a little bit more of work. Makes sense? And then we're also looking at adding in like that Restful API if you weren't, if you didn't want to use Graph QL.
Nice. And and so this also had these authorization rules too. So like in this this case, this is only the owner of it. Whoever creates it can update, create, read and update and delete. But public users can read. And then we can also set authorization modes like we can default it to API key if we wanted to. And then in this one file, the back end file, this is where we define the back end.
So we take those two files, we put it in here. And this is also where you can do additional things to get down to the CDK layer, but we probably won't do that in this video today. All right, so I think what the next step is, let's create a schema for our new meal prep app. And I think we need a few things here. So first, I'm not going to call it to do, but I'm going to call it meal.
And then first name, I'm going to have a meal name. And then I'm going to use this A and this is, by the way, imported in from this a divis amplified back end. And if you've used Zod, have you used the Zod? So this might seem kind of familiar with you kind of might sounds a little bit familiar. But if you hit dot, I have all these different types I can use and I'm going to use enum for this one. And then I'm going to add in a few in here, breakfast, lunch, dinner and let's say a snack.
So we'll assume that you know everyone, you have to choose what what it is. We'll add in a food which is just going to be a string or make it required. And actually, I think I'm gonna make this required too. Actually with an enum, I'm just gonna leave it like this. But this, this string's gonna be required something called food.
I'm gonna have something called calories. And we wanna kinda every single meal, we wanna track how many calories it is. And you see Amazon Q trying to give me some suggestions here, which I don't need. And then I'll have a date, so 8 date, and then we'll make that required as well. And for authorization, I'm just gonna leave it the owner and read.
I'm gonna change this API key. Nice thing too. This is all has intellisense behind it. So I can just say, oh, here are the options I can choose. I'm gonna make sure it's user pool expires in 30 days. That's fine. I think that's it. So I'm just going to keep this changed. Now I have this new model and it's also has this this we just saw this data for this or not this resource.
It's the off. So we also have this off here, which I'm just going to leave default and now I want to deploy it. Now, if you don't have an AWS account, there is instructions in this Amplified documentation which it says right here, configure your aatabase account to use Amplify. So if you use the AWSCLI or if you've done this in the past, you may not need to use this part, but this kind of tells you what you need to do.
You have to run this configure command. I've already done that and so I should be able to just do it. Now I'm going to stop sharing my screen for one SEC because I need to run a command kind of locally for my environment. So give me one SEC. I'm basically telling my since we have a lot of AWS accounts, I have to do these commands to say hey use this one. Yeah, it's always fun.
What it does in the background, it uses cloud formation scripts and it creates the cloud formation scripts and it's also an ephemeral environment. So what I mean by that is this is really meant to only be used for testing locally. And it's, you can do this for any project that you want and it takes probably a few minutes to get it all up and running, but it will go ahead to my AWS account and provision everything in the back end for it. Hopefully if we don't have to see any errors. A lot of people say this red text and they get worried. Don't worry, that's normal.
It's just saying that we're creating this for the first time. All right, So it's now on its merry way. It's going to start provisioning the back end. And what'll happen is it'll create this new, well, it's going to create this new Amplify configuration file, which if you've used Amplify in the past, used to be called AWS exports. Now it's called Amplify configuration, Jason, and we'll be able to import that in and that'll have all the settings and URLs and everything that we need, all the keys to be able to talk to the back end.
So in the in the past, and maybe this is just my experience, when when we push up a sandbox like that, other developers could also work off our same code base. So they would bring this in and they would have an amplify environment within it. But in order to make like database changes, you would have to re push and make sure cloud formation does its thing. And like the database gets set and, and app sync gets set and like all those things finished and they're all nice and happy together.
With Gen. 2, from what I've noticed so far is it actually lets you just update like the modeling side of it and it doesn't require some other pieces. So those are not directly In Sync. And later on you can make them kind of come back In Sync so that they're guaranteed. Is that Maybe I'm far off on this, but. Well, we have hot swapping, so you can make individual changes to let's say the schema that we just looked at, and then it will automatically detect those changes cause the IT, the sandbox is running in the background at all times looking for file changes and it'll only make certain changes. So once you get the sandbox up and running, it's much quicker to make changes.
You have ephemeral environments usually for each individual developer on your team. But though you can share sandboxes. So I've seen that. I haven't seen what you're talking about with drift though, but I'll I'll look into that it. It might be the hot swap I'm I'm thinking of, I just know in the past it was always very slow to like make model changes and it seems much faster now.
Yes, yeah, we, we, we have made some updates so it it can do that kind of fancy CDK hot swap stuff that makes it easier. Nice. Nice. OK, well, let's continue on. So we have kind of an ugly app, but what I want to do is I want to surround it all by Cognito or authentication provider. And since we're using Next 14, we have to do, we have to, and this is kind of a new paradigm with Next. If you've used the old 13 with Pages router, server, router's a little different.
We need to kind of make sure that we're dealing with server components and kind of client components a little bit differently. So what I like to do is I'm going to create a new folder. I'm going to call it components and I'm gonna create a new one. We're gonna call auth dot TSX. So we're gonna I'm gonna do some live coding here.
So we're gonna export default function auth and it's gonna provide children and then children trin type React, React node, React node, Yep. And then we're going to import a few things in. First, this needs to be a client component, use client. And then we're going to import in the authenticator. And then we have to import these in for the styles. And we're we haven't quite yet.
There it is. And then also, since we're using server components, we need to do this SSR, true. And what this does is it makes so that we also have we're saving the authentication and job tokens inside our cookies and that way the back end can retrieve those if it needs to do some authentication check UPS. So even though this is because we used use client, this is client side component.
We're actually using SSR as well for the the cookie set. Yes, yeah. So when you configure the authenticator, which is kind of a, for those who don't know, it's a drop in login system that you can add to your apps with a few lines of boilerplate. We have to do it on the client side. It doesn't work on the server side yet. And by the way, oh, looks like our server, our ephemeral environment's up and running.
And if we look back here now, we do have the Amplify configuration file. So now I don't have any errors and I can do this. I'll return return the authenticator if I spelt that wrong. Oops, authenticator. Yep, there it is. And then inside here we're going to put in our children and then we'll surround this whole thing. OK, that looks right.
And now I'm going to put in my layout or import in that. It's the default export from at slash components slash off. And now where it says children here, I'm going to surround the whole app by it. So this is a different way to do it. If you've used the old authenticator, you may have used something called with authenticator you. This is the way you do it with with here. All right, so I still have my app running, I believe, so now we should be able to log in.
So let's just see if we can do that. I'm back to my app here, refresh. It might have to stop and restart the server. We'll see. And this, my computer's a little slow too, I think, because I'm live streaming and but here it is all right. And we have our create account and let's, I'll just create account with an old e-mail address. I have, let's see here, put some fake password in there. And now I have a confirmation code and look on my phone for it.
OK, cool. So I'm I'm logged in here. We can add in logout buttons and things like that. But for now we're logged in and if we can kind of prove that too if we look inside or inspect and then go into our application. This is really small, probably can't see it. I'll make it so now we have all these tokens down here that's being saved. That's.
Cookies. Just a word from someone that's done this. If you wrap your full app in auth, you could run into issues where you're getting data from like the server. So that's where the server side pieces come from. So if you're like, let's say putting A blog out and you need an OG image or something like that, make sure that's not inside of your off pieces because that won't appear right away and so it'll blow things up. Just just keep that in mind as we're going through this example.
We're good. I think we have about 45 minutes left. OK, so I can do some more coding here. So I think what I'm going to do in this components folder. So I'm going to create a new file and I'm going to call it meal dot TSX and this file will be our meal file and this one will be used client and we'll export default function meal and I think I'll have some props passed in. But before we do that, I suppose we'll just leave it empty and let's just create kind of the scaffolding here. I'm probably just to save some time I might some copy and paste some stuff from my other screen because I have a feeling it's gonna take a while to type everything. Old Whisper is in full effect.
I like. It yeah, Yep, we'll have a section here, last name width 1/4 and then we'll have a form and we'll use server actions in a second, but I'll just delete this for now. Try to delete it. There we go. And for this form, we'll have I'll have the class name flex, flex column order 2P 5 rounded gap 4. And then we'll have an input type equals text, we'll call it meal name and we'll make it read only. So what I'm going to try to do is, is, well, we don't have read only, I'm just having this type here. This is actually gonna be hidden. I'm going to try to reuse this between lunch, breakfast and dinner.
So I think what I'm gonna have to have is a value here. And we're going to import in from here something called. Yeah, we'll use meal name, and that will be a middle name, will be type string, OK. And then I can do meal name to uppercase. All right, So that's just a hidden input that we're going to use later when we use our server actions. But I think what we really want is a couple labels for where to enter your food in and then also where to enter the calories in. So this is where I'm do a little copy and paste action. You're probably going to answer this question, but is there any way of reading in your schema so that you can use like TypeScript and pull that E NUM in off of them? Oh, that's a good point.
Probably there is inside the schema itself in this resource file, in this resource file right here. I think what I probably want to do if I had more time, is maybe refactor this into like its own constant and then have that in here and then export the constant out. Is that what kind of what you're thinking?
Yeah, I was kind of thinking like this. You could export this schema as a whole and possibly like go schema dot meal dot whatever and like actually pull those out, but I don't know, I'm just thinking that alone. Yeah, yeah. Well, later on we're going to be doing that in a similar way to something similar.
I'll, I'll show you. All right, so we have now this label for our meal name. We're going to enter meal name info. We have this input here. And then we need a button, and I'm going to just say delete some of these since we're not quite there. So we have this class name, this button, and I think let's just add this in and see what it looks like.
So we're going to go back to our page TSX file and in here we'll probably want to do a few more changes. And once again, I'm just going to copy and paste a few things from my other screen so you don't have to see me typing. So I'm going to have my super amazing fitness, I guess be a meal plan app, maybe better description meal plan app.
And then the other one would be lunch and we won't worry about a snacks right now, but we can have that. All right, come back to here, refresh it. So let's see if we have a little bit of HTMLCSS. OK, we have it here. Looks a little weird wonder I do have it zoomed in, but maybe we can add a few more things in here so. Maybe like a gap two up there would be good? Yeah, let's do a gap 6 right here, width full. Let's see what it looks like if we do width full here. And OK, I got the gap going.
Wonder what the width is set on this. Do a little live troubleshooting. So we have the gap that's full H1, H2 section. Oh, because this is width 1/4, I wonder if I do with 100%. OK, so I could change it here. Let's go back to the code and let's do 1/2. There we go. I think that looks a little nicer.
That's cool. OK, so we have breakfast estimated calories. Yeah, breakfast estimated calories. Lunch has to be in dinners. OK, so the idea now is that we want to be able to enter your breakfast, like I had pizza, toast and whatever, and then 300. And then we want to kind of send it to our back end and then lock this up so that way users can't do anything to make changes or they can delete it and then re enter it in and it's being saved in the back end.
Does that make sense? Yeah, totally. Yep. And by the way, we're not using next. So in this case you can put this in a separate file, but I'm just going to leave it on this one. So we'll do an on submit and remember server actions and next always have async in front of them. So and they have this form data, form data.
And then oh, one thing I forgot is we need some way to talk. Since we're on the server here, we have ways, we have our new JavaScript V6 library that can do client side and server side, but we need to kind of set up the server side to to do it. So what the way I like to do that is I create a new file, a utils folder. So a new file in the utils folder. So my app have components actually in the source should say I'll create a new folder here, we'll call it utils and then new, new inside new Tils, I'll have amplify utils dot TS. And the purpose of this is to be able to work with our server components and be able to talk back to the server. And we have to do a little set up. This is actually all in the documentation too. So this is nothing crazy.
So we're going to export construction cookie based client and then we're going to do something called generate server client using cookies. And you mentioned before about using the schema. We're going to have the schema right here and the schema is going to be imported directly in find it from our resource file. So from that resource file before. So this is how we can get this kind of end to end type safety because now we're using this the schema that we generated and this is going to be used with our server components. You can also use this with client components as well. And then we need a, we need three things here. First we need a config which I'm going to have to import in this. Is that the the amplified configuration file? Let's see here, I guess put from, is it going to load it? Yeah, here it is Amplify configuration. Then we need cookies and this is directly from next. And by the way, you can do this with Knucks too if you're a Knucks fan from next headers.
Oh, it's spelled. Yeah, we, we're, we're looking at svelte kit right now, but I don't think we have official documentation on that put. On the whole list. I'm just so cookies and then off mode and then we'll have it user pool. So now we have a a way to run on the server using this cookie based client.
It has the schema so it knows what it should run. And so if you want to talk to our, our Graph QL, our manage Graph QL service, we can now use this new cookies based client to do it. And it's going to know because it's going to look through the cookies to see if someone's logged in or not. So I think the next thing to do then is to go back to the page TSX file and use it.
So we have this on submit here. We're going to do construction, just open bracket here. And now we're going to use await our cookie base client. Yep, there it is. We're and you can see here, which is nice. We have this auto completion. So we know we have meal here. And then we can do all those create, read, update and delete functions. So we can do create, delete, get list, update. It's all here for us.
And since we're doing a, we want to create something, we'll do create. And now it's going to tell us what we need. We have to add in the calories, the data, the food, the ID, meal name and owner. I guess these are aren't required. We could make them required, but they're not. So I I suppose we can start with calories and then this is supposed to be a number. So if you're familiar with oh looks like it, it actually detects some of them.
So this is saying form data dot git is the way inside next when you're using server actions that you can grab the data. This is basically the same thing if like you do a a normal submit, like an on submit and then you have to get the form element out of it. You can do something similar by grabbing the form data. Yeah, that's part of the new Web API. I think it couple years ago, old now. Yeah.
And then we have didn't quite get all this right. So let's do this. We got food. This is gonna be the form data dot git and we're gonna call this meal info. If you don't know well I'll come back to it. So but this dot git, these are lately the names that you have on the inputs 2 string as string. And then we have the date, which we kind of could whisper kind of got a little bit of it here, but we're in the two ISO string and this date actually doesn't it only takes it in the year, year, month to month date, date format. If you kind of look at it here or if you go back to the resource world, it tells you it doesn't accept the, the time. So you have to, to remove that from it. It's a little bit of a, a hack here, but works. And then the then the last one is the Neil Neil name. I'm just going to copy and paste this from my other screen because it's a lot to type.
So this right here, you're probably thinking like this is already inside this resource data file. So this is where I say I'm thinking you can't. I wasn't. When I was doing this demo earlier, I couldn't grab it directly from the schema file 'cause I do have the schema which was exported out and you can like grab like meal data from it.
So now we have our on submit. What we need to do though is we need to pass it on to this meal. So I'm just going to create something called on submit equals on submit. But we don't have an on submit in here. So go back to meals. And right now we only have a meal name. So we'll add in on submit, probably kept doing think of a better name.
And we have to type it, which is on submit, which is going to take a form data of type form data. And then it's going to return void, voidy void. All right, I don't see any errors. And now I can add an action on here. So if you're normal in React you do on submit, but with next 14 we can just do action equals and then we just pass in the on submit. That's so weird.
Have you used server actions before? I have, I don't think I've used it where you pass the function through to the component like this. So that's that's interesting to me. Yeah, let's see. Oh one thing I missed if we go back to page. I think mainly I was using it for like log in, log out type stuff.
I don't know if I had well, I used it in any form so well, no, I must have cause e-mail password would have come through. Yeah, interesting. Yeah. Yeah, I kind of like this. So we can have client components talk directly over to our server components. But one thing you have to do, a lot of times people refactor this into their own actions file, but yeah, you can just put it in here.
Yeah, I should clarify, I haven't used it in 14, only 13, so. OK, and while it's kind of thinking there, another thing I want to do is soon as it loads, I want to. Since this is on the server, I don't need to like run a function or anything, and I could just have it do something as soon as the app loads, so I'm gonna have it. 1st I need to go up here and make sure this is async, which this is a dead giveaway.
This is a server component if you have async as the component name. So I'm going to create a response here. I'm going to do a wait. I'm going to use that cookie based client again, client dot models dot meal dot. This time I'm going to do a list. And what I want to try to do is make sure I only get values from today.
So I'm going to filter on data, excuse me, date not date to date. And now I can do make sure it equals and it already sent it for me. So this is saying make sure that the date equals today's date. And then I guess I could just call this current meal. I can do this a couple ways. I'm going to call this current meal. Actually, let's do this.
I'll leave this response. Is there any like advantage disadvantage for storing dates like? I think this is going to end up as a string type I. Think yes, you're right. Instead of doing that like just saving the normal like numeric version of that date represented from like calendar days. Like saving it as like just doing. Yeah, exactly.
Yeah. Yeah, I'm sure there is. I, I kind of the date object requires this very specific format. OK. So it has to be this year, year, year, month, month, day, day format to save and everything? Gotcha. All right. So that's kind of why I was doing it that way. So I can do here have this equal date. You know what, it's actually easier. You're probably thinking the same thing I am. If you have data, I can rename it to current meal. There's a little ES6 trick now in current meal. I can now I can maybe just console logging out for now, but we can use that in our app to check to see if there actually is data in there.
All right, so if we. OK, so we're back up and running again and I'm gonna open up here. So current meal says it's empty. There's no current meals right now on the server. Well, let's see if we can add 1. So we'll add breakfast. OK, so let's say I eat toast and Peaches and grapefruit, grapefruit and let's say that's three hundred 400 calories. I'll hit submit.
OK, cool. So now you see my data changed. I have now in my database I have my data and I have my current meal, so I know it's saving right now. I didn't see any errors. Looks like it's working, but the next step would be to we want to kind of maybe after you enter this in and you click submit, I want to disable this whole screen here and then have a button to delete this in case I accidentally messed up like or like oops, I would actually need to add something else. So that's the next step.
All right, so if I go back to my meal TSX file, oh, first, I think since we know this current meal has the data that we want, let's add, let's pass in a couple things. So we have this on submit, which we already did, but let's pass in current meal and we're going to use a schema and it's going to be meal. So this is imported directly in from the oops. This is not how you do this.
All right, so now we have this new current meal. And now we can do all sorts of things because current meal if once it does that filter and everything, if there's no meal for that day, it'll be undefined. So we can check right away and do some things. So right behind, right underneath here, I'm gonna do a current meal. So I'm gonna check to see if it's not undefined. I'm going to put some information in here and I'm just gonna copy and paste it.
And by the way, if it is, if it is undefined, it should just return null, OK. And we're going to add a delete button. We haven't done add delete yet, but we're going to add it here soon. But we'll just have a delete button. But most important thing is, is that we'll have some that says submitted at the top and it should be submitted.
What does it say? Did we not extend oatmeal? Oh, oh, you know what? We have to get the correct current meal because this this current meal is for everything that has this date. But we're gonna have one for breakfast and and a few others. So we'll need to do this. We'll do a find meal, meal dot meal name equal, equal equals breakfast. And so we'll have to do something similar for all of them.
And then, oh, and then it could be undefined. So we have to do something like this. There it is. I never think about. I never think about the explanation like that. That's that's good. Yeah, this is just a a quick trick 'cause this could be undefined and then that's an issue, yeah. I I've never would have thought about that hacking the TypeScript.
Yeah, so people who are TypeScript experts that are watching this now live or on replay, are probably cringing. Put just put in the comments what it really should be and pretend we did that. And so we have this one should be lunch, which one should be dinner, And oh, I accidentally did the on submit, so on submit, on submit.
So this would be this one. I think we can add it into this input here. I think we might have just broken down our TypeScript issue here, right? Because technically, we'll never get a empty current meal. Well, we definitely will. Within the the meal itself we might want to allow undefined, I don't know.
Yeah, I think I yeah, that's a good point. I think we're definitely maybe the types need to be changed a little bit for that undefined case, because we're right here. This, it only accepts meal. But maybe we should do and or undefined because it does, it is sometimes undefined. Let's see what it looks like here.
I refresh it. Oops. OK, so now it's all kind of grayed out and so you can't hit the submit button again. Yep. So it's doing what you want. That's good. Yeah. And the other two are still open, correct? Like breakfast or lunch and dinner. Yeah, let's see. Let's let's add some lunch. We'll have pizza 200.
I'll submit it. Yep. Now it added it in there. We can do dinner. We had pork chops, 800 calories and it takes a second. We can add a loading spinner too, if you want. OK, so now they're all set. So now let's say we want to there's two things. One is we want it to delete it and then clear this data from here.
So first, maybe let's let's just try to see if we can delete it and it's not clearing the text first. So I need to go back into my pages file and then I have my on submit. But now I need to create a delete and this is also going to be a server action that should work for us. And to construction on delete,
click async. And this time you don't have to do a form data, it doesn't have to be connected to a form. It could just be a function that you just call. Just really neat but you just have to make sure you put in use server and make it async. And now I can say, let's grab the data out of here and await that cookie based client models dot meal capital N And now we can do delete.
If this was production, you'd probably want to like check for errors and do something if ever happened. This is demo and we'll do revalidate, validate path and that'll like just refresh this path, this root path here we'll go back to meal and let's see here we'll add in on delete and this will be on delete. Instead of adding this, it'll be ID string. And it's complaining, I think because there it is. I didn't have the type.
Cannot find. Oh, that's a weird error. I think it's just kind of slow. Let's see if I get an error here. Is there something on the end of that class name like not double coded? Well, that's a good .0. There it goes. I think it's just resolved. Yeah, sometimes I have to I don't know if anybody's done this in in when you're in BS code. Sometimes I just had to like reload the window, reload TypeScript, things just don't work. Query.
All right, so all these are here. So if I just do delete entry, I get an error undelete. Oh you know what? Because I didn't pass it in. So I come back here. Now these are all complaining. So on delete equals on delete, pass it in, pass it in. Those are all happy. Now refresh, you still see these are all grayed out and click Delete entry.
All right, so now it's back, but it does have still has the text here. No, no it doesn't. I thought it did for a second. I don't know that I don't know that are other two that have this submitted. I don't think they have our text. Oh, you're right. So it never saves the text. So that's the next thing we need to do to make sure it saves the text so we can delete the entry. I believe this one not deleted.
Let's see if we can get the text showing up here. So we are passing this whole meal current meal which should have the name in it. So we have to just set some form values in there. Exactly, you are 100% correct. So we have these inputs like where this text area and what I think what I want to do is have AU state for both calories and food. Since this is a client component, we can use use states.
This is a way we can keep track of of our app. So we're going to have these two calories set calories and I'm going to import this from React. And what this is saying is that soon as these the this component loads, it's going to look at the calories, the current meal dot calories and it's going to set to 0 if it's undefined. And we're going to have the food set to empty if it's undefined. Otherwise we're setting the food and calories here. I'm not I'm not a React expert, but is there any advantage of just passing the full object in instead of like the individual value?
What do you mean? So I I think your current meal has the entire data that you're looking for. So instead of just having that like like why, why do we use the the state here? Well, Oh yeah, that's because we what I'm thinking of is when we hit this delete button, we want to set the forms to empty. OK, So we want to show and to do that you have to kind of use use state because it's going to update the forms and change the input.
So we're going to add a value and an on change to it. So I'll show you what I mean by that. So in this text area here at the bottom, I'm going to create an on change, and this is going to be this. So what this says is soon as anytime you press something on the keyboard it's going to set the food to this, but in the same one we want it to default to food.
I like view better in this respect 'cause you can use something called V model, but inside react you always have to do this on change in value, which I don't think is so. Is how Angular was too. It's like a 2. Way binding, Yeah, the Ng model exactly with the two way data binding. But you have to do it all manually here in your ACT. So we can do the same thing here, set calories. It's parsing the int there and then value equals calories. So now it should default with the name. So let's OK, if I refresh it here, I think I just saw it. Oh, I have pork chops.
The estimate calories show 0. Oh, which we'll check in a second. There might be a bug there with the with that. I'm not sure why it's doing that, but we'll look in a second. So now, but when I click delete here, it still says pork chops, which I guess could be OK, but if we wanted to delete it, we can do is. And I, I did this, I created a new function. I'll copy and paste it.
It's called on deleting and then it just sets everything back to zero. And then it calls that server action on delete. So instead of doing on delete here, we can do on delete ING. So we'll delete this and we'll do on delete ING. OK, And if we did that right, let's see what it looks like just refreshing it.
OK, so these are all deleted. So if I'll put in here, I'll do food will be cereal 300. If you submit it now, it's kind of set in stone. It's saved in our database. If we refresh it, refresh the page, it's still saved. Looks like the calories. There's a bug there. I need to fix that. But if I click delete entry, it deletes both of them and now they're fine there.
and then delete, delete entry works just if you refresh the page, it doesn't seem to work. All right. Yeah. So that's that's pretty much my demo. I guess we could troubleshoot the bug. I, I think that's actually like a, a really good stabbing point 'cause I feel like there's maybe some more amplify ish things, I'll call them instead of next JS things like we'll, we'll get this repo out to you folks and and get that updated just like we normally do out in coding cat dot devs GitHub space. And we'll show that.
I am kind of curious when people think of like Gen. 2 and how we've been like hot reloading and like creating things. Can we also show do you have the console handy where we could like look at what this created out in AWS? And I think sure. Probably wrap it up after that. Yeah. So what typically happens, we've kind of spent a lot of time on the app part of it, but the the next part of this is like, let's say this is the app with that we want to deploy and we've been using this kind of Infirmary environment the whole time. And once we hit Control C here, it'll ask us, do we want to delete all the resources or we're not going to delete them right now.
So I'll do no. So by the way, they're still there. I haven't deleted them. I can run MPX sandbox again and it's it's just going to restart everything. But everything's still running in the cloud. It hasn't deleted anything. But the next step if you want to do a deploy and like you want to get this up, is we have this new console experience.
So what I'd have to do is I'd have to add this repo to GitHub and then inside GitHub I can then connect it up to our console experience and deploy it. And I think like even right now you can show off your sandbox and like the the actual files and stuff, right? The database I should say. Yeah, let's, yeah, we so, but we can also just look at what's in the ephemeral environment.
That's true. Yeah. Let me on the other screen here. Let me see if I can bring that up. Yeah. And while Eric's doing that, I will just reshare. I had my screens right here. There you go. So I'll just share mine in a minute here. So everything that we covered here just again, if you are out in Amplify, so let me remove the Gen. 2.
So if you come up to Amplify docs, Amplified AWS currently Gen. 2 is still in its previous state. So you can click the get started and it'll drive over to slash Gen. 2, which has all of this information that we're talking about right now. We actually are working through the next GS version of this on server components and that's the app that Eric build out. So a lot of resources in here that you can dig through and check this whole thing out.
So go ahead and go over there. I think Eric's ready for us. Yeah. So here's here's the AWS console on my account and then I can always go to like if I want to look at one of those resources, it created a Cognito resource. I could I can look at the amplify configuration Jason file and get more details about it. But if I wanted to bring up the console, like I'll come here. I have some like sample ones.
So if I just do created time, this one I created about 51 minutes ago. This is the one I just created and here's all the data on it. And I'll delete it after this live stream. But here's the users that I created and I can go in here in the console and check it out. If I want to do the same thing with app sync, I can just go in the app sync here I. Think you can go all the way back to If you do like amplify, you can jump into the amplify.
I think we call it codcat something maybe? Yeah, it was Codcat. Oh, you know what you have to do is it doesn't show up here because I haven't added it to the console yet, but it's under managed sandboxes. Yes. If we go to managed sandboxes and left, these are the ephemeral environments that we have. And then I can look at.
Well, yeah, so you can look it in the command line or really the way to do this is the ephemeral environment is just kind of meant to to be deleted and used. And if you want to, you can look at it, you can try to search for the app sync or Cognito. But what you really want to do is you click the try, it doesn't just try Amplify Gen. 2 and this will bring you to this screen here.
And this is where you would want to connect it up. And once you do this, it'll create a production environment that or development environment, whatever you want based on the branches. So that's another big thing with Gen. 2 is now you have branch based deployments. So everything you you do is based on the branch.
Cool. So you can. It has full stack branch deployments and I'll show you here. So like for example, you would click repository settings, you can add a new branch, you can add a new, you can do whatever you want there. We could try taking this app and deploying it but. Yeah, I'm thinking we're we're kind of up on time.
I mean, we kicked that apps behind. I got to take a day off. This was such a pleasure. No, thank you. Any other last minute thoughts? Jen 2 Where's it headed? Out of preview soon? Question mark. Exactly. Yeah, we're, we're really excited. Hopefully soon it'll be out of preview, so keep an eye out for that. We're adding in more these.
You saw in the demo today we had auth and data, but we're looking forward to like adding storage and functions and other things, more high level constructs to make it even more simpler. The console experience right now that you kind of briefly saw, that's where you connect up your GitHub repo right now it doesn't it it, it has a lot of features, but it doesn't support all the features like custom domains. So we're like adding that very shortly and that that'll really superpower people and be able to host your whole website and just have it every time you have a new branch to create your back end.
