Laravel + Cloudflare D1 speedrun

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
not that long ago I made a video about my personal website ironfran thatcombines sqlite as my database and I commit it all to the git repo and ship it off to lar Val Vapor I thought this was really clever so I told my friend and podcast co-host Ian about it and he thought I was crazy so to get static site speeds I have Cloud flare which you love active invest Cloud flare oh man it's up 5% today let's go go check it every freaking day Cloud flare baby so to get the static site speeds I have this rule in Cloud flare that's just Brute Force cash everything with all the way around cash everything and then and then okay so we're we're on the straight and narrow so far SE light boring as could be lar per alignment Cloud flare active investor okay so not and so when I deploy I make a API call to Cloud flare that says blow everything up I'm coming baby and so it just it just kills the cash yep and then the next time somebody requests it it'll hit Vapor it'll run through Cloud flare it'll cash yep what is crazy about that that's all not crazy that's all it is no but you're leaving out a step in the video you had this whole other step where you do some local stuff and then you push the local stuff to the server but you don't need to do that stuff what are you talking about now I'm never going to tell Ian that he's right about anything honestly but especially not about this one thing one thing that is a little bit weird is committing that sqlite database to the git repo in that video I talked a lot about Cloud flare and how I use their aggressive caching to make my site extremely fast and they reached out and said hey we actually have a database that you can use do you want to try D1 it's generally available now do you want to do a video on it and I said you bet I do D1 is cloud player's serverless database offering it's built on top of sqlite and since it's serverless you pay by usage instead of paying a fixed amount I'll tell you more about that in a second but before we do I want to do two things the first is laravel speedrun how quickly can we get D1 working together with laravel and the second is what are some potentially interesting use cases once D1 is your database let's do the speedrun first you can see here that I have no D1 databases you could create it right here by clicking create database but we're going to hop over to the terminal and say npx Wrangler and that's their little command line tool D1 create and we're just going to call this we'll call it blog Dev so we're going to create a database it should spit out a database ID which you're welcome to try to use but it's already been deleted so I'm going to copy this database ID and come put it in my LEL environment uh file there are a few other keys in here like my uh API token and account ID I am not going to show you those because my account ID doesn't change so I'm just going to put the database ID in here and then to combine laravel and D1 there's a package that sits in the middle called L1 which is a community package great name D1 larl L1 love it I've already installed it because that part's boring so let's come over to the docs L1 usage with raw pedo interesting not what I'm after usage with laravel that is what I'm after so I'm going to copy out the D1 connection part and we're going to come over to database and right here we're going to put D1 I don't need to change anything because I already have these three environment variables set up um it does go over it does go over Cloud Flare's API which is why you need a custom driver for this so we're going to come up here and change the default from SQL light to D1 now with any luck we can hop over here clear this out PHP Artisan migrate we should be running these migrations against our D1 database if we come back to the dashboard and refresh you see we do have the blog Dev database and we look there are all the tables and if you wanted to poke around with the data you could click into one this is the laravel data I didn't populate my tables with any data so that's the only table that has anything in it not bad for a speedrun right that took I don't know maybe less than a minute which is pretty good I'm pretty happy about that I'm just testing this from my local machine so there is a little bit more latency from where I am to where the database is and I think that's an interesting thing that we might need to consider is this database is typically supposed to be used with cloudflare workers or cloudflare pages but we're using it with a full-on laravel application and this is where I think it gets interesting from a laravel application standpoint yes we just set up the database to be our laral database we have eloquent that all works fine and dandy you can also access that data from cloudflare workers which means you can manipulate or do or query the data before it ever reaches your layer Val application so you could do redirects you could do caching you could do responses and requests headers you can do all kinds of stuff before it ever gets to your Lal app with the same data that your Lal app is relying on that's what I want to hear from you on what can you possibly dream up of doing with the combination of Lal and Cloud flare workers I'm going to show you two more things before we go one is the insights that come with this database and one is time travel which is like automated backup that you never have to turn on and it's point in time recovery so let me show you let me show you the insights first their insights are great because you can query all different types of things from your database so we're just going to say insights on the database that we just created sort by time show us 10 so this is going to show us the 10 slowest queries and this is all available via API so it returns this nice Json response so you can potentially imagine how you could build this into some sort of alerting reporting notification system so you can keep track of what your slowest queries are they do have a little bit of insights in the actual uh browser here you can come over here and see the queries and the latency and the metrics that sort of thing um but I like I like using the API because you can get exactly what you're looking for out of it metrics are very cool but nothing is cooler than time travel so let's talk about time travel generally it is possible however you can only move forward through time which kind of defeats the whole point right with Cloud flares time travel for your database which is slightly different you can just travel back in time to any point in time in the past 30 days and restore your database there's nothing to configure there's nothing to enable more importantly there's nothing to forget to enable so you can just pick any point in time hit restore and you are good to go y'all I am always going to love databases maybe now more so than ever all of these books back here those are all database books I just got a bunch of new ones the sqlite revolution is upon us I am here for it I am excited what I want to hear from you in the comments is how do we combine laravel with cloudflare workers using D1 in the middle there's something interesting there and I want you to tell me what it is cloud Flair just wrapped up their developer week so they just announced a ton of stuff stuff including uh D1 going GA so I'll leave a link to that in the description thank you Cloud flare for helping me make this video until the next time see you
Info
Channel: Aaron Francis
Views: 12,365
Rating: undefined out of 5
Keywords:
Id: htAOyy3-E9c
Channel Id: undefined
Length: 7min 43sec (463 seconds)
Published: Tue Apr 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.