Using Turso with Cloudflare Workers
Presented by: Glauber Costa, Pekka Enberg, Gift Egwuenu
Originally aired on September 16 @ 2:30 PM - 3:00 PM EDT
Join Glauber Costa and Pekka Enberg Founders of Turso, and Gift Egwuenu, Developer Advocate Cloudflare as they demonstrate how to integrate Turso with Cloudflare Workers.
English
Transcript (Beta)
Hi, everyone. Welcome to this live segment. I am Gift, Developer Advocate at Cloudflare.
And with me, I have some special guests for this segment. So I'll let them do introductions.
Over to you, Kluge. Thank you, Gift. Thank you so much. First of all, happy to be here.
Thank you so much for having us. I am Glauber Costa, and I am the founder and CEO of Turso.
I will speak more about Turso later, but with me here, we have my co-founder, Pekka.
Why don't you just introduce me like you usually do?
Oh, because we promise we will be good guests, Pekka. So the funny thing is that me and Glauber know each other way back, and we actually spent the last, I think, decade doing stuff together.
But yeah, Pekka, I'm the CEO, co-founder of Turso.
And let's talk more about Turso, I guess. Yeah. Before we get into that, well, I'm very happy to have both of you on.
I would like to give a brief introduction of why we are having this segment in the first place.
And if you're not familiar, last week, we had birthday week here at Cloudflare, where we made tons of announcements.
And one of those announcements was adding additional integrations to Cloudflare marketplace.
And one of those integrations is Turso. So this segment is essentially going to be a space where we share what Turso is, and also show you how the integration works with Cloudflare Workers.
So I'll hand over to Glauber.
What exactly is... Happy birthday, Cloudflare. First and foremost, thank you for everything.
Cloudflare has been such a great partner of the ecosystem in general, right?
It's hard to imagine most of the developers' lives without Cloudflare today.
So happy birthday. How old is Cloudflare, by the way? Just around 10 years.
It's Tetsun. We're Tetsun. Yeah. Awesome. So I'll just get started telling you a little bit more about what Turso is.
And then again, now while we have this introduction that Pekka is going to be demonstrating today, Turso is a database.
It is more specifically, it's an edge database.
And our focus area is having a database that can be fast from anywhere and support workloads like multi-tenant, multi-user SaaS applications.
So each database is a SQLite file and you can create thousands of those databases.
You can start for free. And we focus a lot again in data placement and data replication with the intention of making your database fast from everywhere.
It pairs extremely well with a player like Cloudflare, which is also putting a lot of effort, especially through Cloudflare workers into having this global compute platform, so to speak.
So one of the things that Cloudflare talks a lot about that I love is region earth.
So how you can do compute from all over the earth, always close to your users.
And again, the focus of Turso is making sure that the data is going to be close to your users as well.
Yeah. That sounds great. I also find that interesting because I find that there is alignment between what Turso is doing and what Cloudflare Workers is doing.
So I can't wait to get into like the demo.
So I'll let you, Pekka, share your screen and show us exactly what you can do with Turso.
Yeah, absolutely. So let me share my screen.
So yeah, I hope you can see my browser window. So basically, we're just going to show a simple demo of how to build a REST API with Cloudflare Workers and Turso.
So it is a fictional e-commerce site selling mugs. And actually, as you can see on the right, it's work by James, who's a technical writer at us.
So I'm here just to take credit for his awesome work.
And in this example, we're going to use an object relation mapper called Drizzle.
It doesn't have to be Drizzle, there are other options as well.
But in this example, that's what we're going to use.
And basically, I was thinking that I'll just briefly show the code. We can go into more detail if needed.
And then I'm just going to walk through the steps. So this is, again, on GitHub.
And I think we also have a blog post about this. But basically, it's an example of how to get going.
And Giv, this is a stack that we see so much in the open, Cloudflare workers, Turso, and the Drizzle ORM.
We are not involved with the Drizzle project.
For those of you who want to check out, it's an ORM focused on edge environments.
We do sponsor the project, though, because we think it's important to make sure that especially the open source community is well-maintained, well-kept.
And we're users of open source, we benefit from open source, so we're always ready to give back whenever possible.
But this is a stack that if you go on Twitter, for example, you're going to see mentions of that all the time.
I have seen a couple of it. Yeah. Yeah, absolutely. But maybe I'll just jump into the code so you can see a little bit of Drizzle as well.
So as I mentioned, this is just a simple worker.
I know if we scroll down here, you can see the exported fetch, and it just builds this router, which is essentially the REST API.
And basically, from integration perspective, it's really centered around creating the database client.
So Turso is still in this environment, a remotely accessed database.
We have an HTTP-based protocol. But basically, the way you configure it is through two things.
You give it an URL, and this is the same URL from anywhere you access it from.
So Turso deals with figuring out what's the closest replica of your database, and then also authentication token.
Now, this is actually what the Cloudflare Workers integration does for us, which I'll show you in a second.
And basically, this is creating the client based on the configuration, and then also wrapping it into this Drizzle object.
And for example, if we look at the route to get all the mugs that we're selling on this platform.
So it is really just db.select from mugs, and this is a TypeScript type that specifies it.
And without going into more detail about Drizzle, this is like essential.
And then we just return it as JSON, and there's a bunch of other routes as well.
So if we go back to the GitHub project. So first thing we're going to do in this demo, so I already installed the CLI.
So when you're working with Turso, you're almost certainly going to install the CLI.
It is there to manage your databases and so forth.
But obviously, I already have it. So the first thing we're going to do is to, we're going to create a database to use with this example.
And we're going to call the database the mug store API.
So that's the name of the database.
And the CLI really tries to guide you what the next steps are, but we already have them here.
So as I mentioned in the, or showed in the example code, two things that we need, the URL and authentication token.
I'm just going to copy paste these commands.
And here I'm basically just running this command to get the URL and capturing it into this environment variable, which we will use a bit later.
And let's do the same thing for the token. And now we have basically have the two configuration pieces that we need.
And there's two things that I wanted to show today.
So first of all, you know, you can develop locally with Cloudflare workers.
In this example, we're always going to have the database remote. But what we need to do is do a bit, a little bit of configuration.
So we're going to again, copy this command and basically creates this dot N file, which I will not show you because it will have my authentication token, which happens a lot in demos.
So let's just do that. And then also, you know, this is something I didn't manage to configure with Wrangler.
So actually it also needs this because basically what we need to do, yeah, we need to give this configuration to two entities here.
One is a drizzle kit, which is again, part of drizzle, which manages the migrations.
And then on the other hand, Wrangler, the locally running thing needs this environment or this configuration as well.
I didn't manage to, you know, make Wrangler actually eat that dot N file.
So I'm just going to copy it to an external file.
What I see also, sometimes people use the Wrangler configuration file, but again, I don't want to put it there because it's my secret authentication token.
Then also in this example, you know, if you just clone it repository, you will have to install the dependencies.
I did all of that already.
And then before we can actually get into running the code, I'm just going to, you know, run these commands, which are essentially steps that you do with drizzle.
You know, I highly encourage people to look it up themselves, but basically what it does, it takes the TypeScript type definitions of the mugs and all the stuff that like the domain model that we have generates some SQL.
And then, you know, it has support for SQLite and also Torso of course, which is SQLite based.
So it migrates the database for you. And then you can also seed some data. So some test data for this example, and that's it.
Now we can basically, unless there's any questions at this stage, Glauber, do you have any questions?
No, I actually do have one question.
Where you were running the environment, the authentication token.
I saw that you created that from the CLI. So interestingly enough, the database integration with on the Cloudflare dashboard would also automatically create that for you, right?
Yeah, absolutely. And I will show that.
And this is basically, yeah. And this is basically for the case when we are now running this locally.
So we will get to the integration for sure. And this token, by the way, is not an account token is a per database token.
So, you know, even if we were showing now, it's totally fine.
It can generate others. Every one of your databases will have a different token.
Yeah, absolutely. They're all separated like that.
So we'll get to the integration part just in a second. So now we can do basically, we can start Wrangler in dev mode.
And now we will have the function running on my machine, accessing a remote database.
So assuming everything went well.
So here we can see that sort of the route route, which just shows the different endpoints.
And for example, we can see, let's look at the marks in the system.
So it's just doing the select from markets, which I showed in the beginning.
And that's pretty much it for the local development case. So now let's go to the cool stuff that you guys announced last week.
So let's go and deploy the function on Cloudflare.
And it just takes a small, small amount of time to get there.
So now what I did, and of course, I actually had the Cloudflare project there before, but I don't have the integration configured.
So now the code that I was running locally is running on Cloudflare, but we don't have the configuration for the database, right?
Because exactly what you were pointing out.
So what we're going to do is we're going to, so if we actually go to the overview page, so this is like, this is your normal Cloudflare worker's dashboard.
We can see the project that I just deployed here.
And then the cool bit that you announced last week is this integrations tab.
And there's a bunch of different providers.
You can find Torso here as well. And at least I find this super cool.
So what you do is you click on that integration. You need to give it some permissions.
And then this is actually the step that maybe now explains. So I created the credentials from the CLI, but actually what it does, it's just connecting to our API server and getting the credentials from there.
So what we do with the dashboard is we connect to Torso.
We authorize Cloudflare to be able to access the API on my behalf.
And then now this is the part where you do the integration. So we select an organization, Torso of course supports different organizations.
I created this demo database under my personal one.
So I'm just going to pick that.
And then I select a database and it should be here. So the mug store API, which we were just using, and you can also configure access level.
So I'm just going to give it full access, but we also do support read-only databases.
And this is the final bit that you were asking about.
Now Cloudflare Workers dashboard will ask, what are the...
So this has essentially managed secrets like the URL and the authentication token.
And of course we make the demo use the same secret names as here.
But basically once I do the add integration and wait a second. So basically Cloudflare Workers under the hood does the same configuration step.
And what is cool about this is that now I don't have to manage it anymore.
It can be done actually also from the Wrangler CLI and the GitHub read me explains how to do this, but this is pretty cool in terms of basically managing the integration.
And now we can just quickly run the same commands.
So we're going to see... So this is now the code is running on Cloudflare workers, which is hopefully like 10 kilometers from my house.
You have a data center there, and then we can just check the mugs.
And on the first try, for some reason, it uses the cached version of the secrets, the Cloudflare Workers team.
I don't know why it does that the second time it works.
So yeah, but that's basically... So it's related to something around the caching of the secrets or something, but nonetheless, so this is the way where you do it and you do it once.
So you maybe even do it before you deploy and so forth.
But in the interest of this demo, these were the steps. So in a nutshell, that's the demo we wanted to show.
And again, you can find the whole thing on GitHub where you can play around.
Thank you so much for sharing all of that. Thank you.
Let me stop sharing my screen. It looks great. The combination of Tosu, Cloudflare Workers, and how it makes integration seamless when you're trying to deploy your applications to production.
I am very curious what are the kind of applications you've seen?
Because you mentioned that you've seen a lot of people building with Tosu, Cloudflare Workers, and Drizzle.
Drizzle, yeah. Yeah. What would be the best suited applications for Tosu?
Yeah. No, there are many. I would say that there is a combination of factors that make Tosu and Cloudflare Workers a very powerful combination.
And it's usually the fact that you expect users from different places in the world.
So if all of your users are in a specific location, you might not even want to reach Cloudflare Workers.
Although the DX alone and the experience of running those functions, it's worth it.
But to make it like the full power of that is realized when you expect users coming from many places in the world.
And you need those things to be served with low latency. What falls into this category?
Many things. And I think the most important, the one we see the most is the SaaS applications, e -commerce applications.
I spoke to somebody the other day who had absolutely no interest in using neither of our technology, neither Cloudflare nor Tosu in a discovery call.
And what he told me is like, look, I sell subscriptions to a service that when you sum it up, I mean, it costs $70,000 a year for consumers.
So if it takes one, you don't make a decision to purchase something that costs $7,000 a year like this.
So if the website takes a second to load, two second to load, nobody cares.
But we know that most e-commerce is not like that.
Like most e-commerce, especially when you're selling things that have a low individual value, it is a purchase that if you don't make it at that point, the user will just go away.
So those websites, I mean, those technologies are things that are prime to see this combination together.
Travel agencies is something that we see a lot for a similar reason.
Essentially things where you want to see a quick, snappy experience wherever you are in the world.
Absolutely. So I think, yeah, so just to add on that, so it's like a complementary, Torso is a great complementary thing for workers.
And the reason people usually go for workers is latency, but also the serverless aspect of it as well.
And when you're building those types of applications, I think usually it's the API layer that people really do on Cloudflare Workers and then use Torso for that as well.
Yeah, I agree. I agree.
And also just adding Torso as your data layer, as well as it being latency-free and accessible to different users makes it just a very good combination.
Cool. Well, I am very happy about the integration and I hope that people watching this or that would watch later would be happy to try out both Torso and Cloudflare Workers for building the applications.
If you haven't heard of Torso before, and this is your first time, and if you want to check out more of what we do, we are at torso.tech.
And on the website, you're going to find links to our Discord page.
You're going to find links to libsql, which is our fork of SQLite on top of which we built the database.
Everything really you need to know about is there.
And if you haven't heard of Cloudflare, I don't know, I've questioned a lot about you.
You're here, so I'll see. Yeah, exactly. I'm assuming that all of the audience knows about Cloudflare, but Torso may be news to you, in which case, we're at torso.tech.
Cool. Thank you so much for joining me. If there is any other questions, I'll possibly pass it on to you.
But also, all the links that you mentioned, I'll have the team edit, because this is going to be restreamed over the course of the few days or weeks.
I'm not sure. But I have the links attached, including the demos, the GitHub repo, so people can easily access them.
Yeah.
Thank you. Thank you so much. It was a great pleasure to be here with you. All right.
Have a wonderful rest of your day. You too. Bye. You too. Bye.