惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
IT之家
IT之家
Vercel News
Vercel News
C
Check Point Blog
Google DeepMind News
Google DeepMind News
月光博客
月光博客
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog

Netlify Developers

AI model comparison using Netlify's AI Gateway | Netlify Developers Build an AI agent to automatically process and summarize form submissions | Netlify Developers Build an AI agent to automatically generate relevant images for blog posts | Netlify Developers Prerendering SPA content pages to enable AI agent access | Netlify Developers Identify image optimization opportunities with Netlify Observability | Netlify Developers Fix top 404 page not found errors with Netlify Observability | Netlify Developers Introducing AI Gateway: built-in AI model access on Netlify | Netlify Developers Introducing Observability: your project insights on Netlify | Netlify Developers From first publish to first update with AI agents | Netlify Developers How to use Agent Runners on Netlify | Netlify Developers Add forms to your project with AI + Netlify | Netlify Developers Track Real User Metrics | Netlify Developers Lighthouse performance scores on Netlify | Netlify Developers Review your AI project before you ship | Netlify Developers Using environment variables in AI Projects on Netlify | Netlify Developers Build prototypes and MVPs fast with AI + Netlify | Netlify Developers Pause auto-publishing on Netlify | Netlify Developers Run serverless functions, storage, and more natively in Nuxt dev | Netlify Developers Netlify Office Hours: The AX Arcade challenge | Netlify Developers Netlify Office Hours: Prompting with style | Netlify Developers Netlify Office Hours: RAG Apps with OpenAI + Netlify DB | Netlify Developers How to build a RAG application with Neon, Netlify & OpenAI | Netlify Developers Netlify Office Hours: Netlify DB (powered by Neon) | Netlify Developers Netlify Office Hours: Netlify MCP Server | Netlify Developers Netlify Office Hours: Netlify Vite Plugin | Netlify Developers Building MCPs with Netlify | Netlify Developers Deploying sites from your AI tool | Netlify Developers Adding your domain using Netlify API | Netlify Developers Build a context driven chat bot with Netlify Blob and OpenAI | Netlify Developers Simplify deployments with Netlify’s branch-matching environment variables | Netlify Developers
Write data to Turso from a Netlify site | Netlify Developers
2024-10-03 · via Netlify Developers

The Turso extension for Netlify enables you to read and write data to Turso in a site deployed to Netlify. The extension provides a seamless way to connect your Netlify site to your Turso project, for use in all Netlify environments.

#TL;DR

We’re going to install and configure the Turso extension, fill the database with data, and then set up an example site that reads and writes data to Turso.

The example project is a simple site that lists pets from our Turso database and enables users to “snuggle” pets.

#Deploy example site

Before we can install and apply the Turso extension, we need to have a Netlify site. Deploy the example project by clicking the button below.

Deploy to Netlify

The first build will likely fail because the extension hasn’t been installed yet. Let’s move on to the next section and then come back to re-deploy.

#Set up Turso database

Let’s create a new Turso project and set up a database so that we have something to work with.

#Create a new group

After signing up or signing in to your Turso account, navigate to Databases (1) and choose Create Group (2). Use Fly.io as the provider, then give your group a name and choose a primary data close to you.

Create a new Turso group

#Create a new database

This will lead you to the group page. Click Create Database to open the new database modal. Fill in the details and click Create Database.

Create a new Turso database

#Create the pets table

Once the database is provisioned, we can create the pets table. From your database dashboard, choose Edit Data > Outerbase.

Open Outerbase application

Then navigate to the Queries tab (1), add the query below (2), then choose Run (3).

Run query to create pets table

The query for creating the pets table is as follows:

CREATE TABLE

pets (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT NOT NULL,

greeting TEXT NOT NULL,

bio TEXT NOT NULL,

image TEXT NOT NULL,

snuggles INTEGER NOT NULL DEFAULT 0,

created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

);

#Add some starter data

Let’s add some seed data to the pets table. While still in the Queries tab, run the following query to add some data.

INSERT INTO

pets (name, greeting, image, bio)

VALUES

('Bubbles', 'Blub blub!', 'bubbles.jpg', "Hey there, I'm Bubbles! I swim around all day and blow bubbles. I might be small, but I have a big personality."),

('Chirpy', 'Tweet!', 'chirpy.jpg', "Hello, I'm Chirpy! I love singing and hopping around. My favorite thing is to wake everyone up with my cheerful tweets."),

('Fluffy', 'Woof!', 'fluffy.jpg', "Hi, I'm Fluffy! I love chasing my tail and barking at the mailman. My favorite pastime is napping on the couch."),

('Nibbles', 'Squeak!', 'nibbles.jpg', "Hi, I'm Nibbles. I love running on my wheel and nibbling on treats. I'm always up for an adventure in my little world."),

('Rex', 'Roar!', 'rex.jpg', "Roar! I'm Rex, the tiny dinosaur. I might be small, but I have a big roar. I love stomping around and pretending I'm a giant."),

('Shadow', '...', 'shadow.jpg', "Hello, I'm Shadow. I'm mysterious and love hiding in the shadows. I enjoy quiet moments and observing everything around me."),

('Spike', 'Arrf!', 'spike.jpg', "Hi, I'm Spike. Do I look prickly? I'm not. I'm a softie at heart. I love exploring and curling up into a ball."),

('Whiskers', 'Meow!', 'whiskers.jpg', "Hello, I'm Whiskers. I enjoy long naps in the sun and knocking things off the counter. I'm a master of the purr and the silent meow.");

You’re welcome to change the data to whatever you’d like, but note that the images have already been added to the site and unless you use the name of an existing image for the image field, the pet will not have an image to display.

Now if you navigate to the Tables tab (1) and choose the pets table (2), you should see the data we just added (3).

Preview data in pets table

#Install the Turso extension

Now that we have the database set up, we can install the Turso extension.

Back in the Netlify UI, go to your team’s dashboard, navigate to Extensions. Locate and select the Turso extension. Click the install button to install the extension.

#Add Turso API token

Once installed, you’ll see a configuration option for an API token. This gets set at the team level. If you don’t have an API token ready to go, follow the link to get an API token. This link directs you to the Turso API token page.

Netlify Turso extension team config

Create an API token and give it a name. Copy the value and paste it into the Turso extension configuration.

Create Turso API token

#Configure the Turso extension

After the extension is installed and configured for the team, navigate to the example site that you just deployed, and go to Site configuration. In the General settings, you will see a new Turso section. Choose the database that you created earlier and save the settings.

Select Turso database for site config

#Deploy the site again

Now that the extension is configured, we can deploy the site again. Click the Deploy site button to deploy the site.

Once the build is complete, navigate to your production URL and you should see the pets that we just added to the database.

Pets to snuggle on the home page

Give them some snuggles and watch the count go up!

#Working locally

You now have what you need to start working with Turso in a Netlify site. The extension handled injecting environment variables into your Netlify site. You can apply the same approach to any Netlify project.

You can also work locally without dealing with environment variables. To do this, all you have to do is to use Netlify Dev rather than Astro’s default development server.

Install the Netlify CLI.

npm install -g netlify-cli

Then link the site to your production site.

The easiest way to do this is to find the site ID, which is available in the general section of you site configuration in the Netlify UI.

Then start the development server. Your environment will be automatically configured, and you’ll be able to use your Turso database locally.