Many Netlify users have asked us for an easier way to deploy more complex and data intensive applications. Netlify’s newest Integration with PlanetScale, enables users to easily connect Jamstack sites and apps to a production-grade data backend without worrying about the underlying infrastructure or issues with data scalability.
For example, if you’re building an e-commerce website, you could use PlanetScale to handle your inventory management, customer data, and other data-intensive tasks. Similarly, if you’re building a social media site, you could use PlanetScale to handle the storage and retrieval of user data and content, ensuring data availability even as you scale up to support more users. And because this new integration leverages the recently launched PlanetScale API and OAuth applications, users can closely integrate PlanetScale branches, deploy requests, passwords, and other features into the existing workflow. Other benefits for Netlify users include the ability to more easily connect PlanetScale accounts to Netlify sites, assign database branches to different deploy contexts, and use the withPlanetscale
function to seamlessly insert a connection into the database call.
Fluid and functional integration support with data backends are especially relevant for users running web apps and SaaS on Netlify, and this new integration with PlanetScale is a first step in that direction. With more automated processes that lead to powerful workflows and scalable data, users will benefit from a richer developer experience with both PlanetScale and Netlify.
Creating a Function to Call PlanetScale
Before you begin, make sure you have created a PlanetScale account and a database to connect to a Netlify site. For detailed instructions on connecting your site and database in the Netlify UI, refer to our docs.
Once the connection is established, you can trigger calls to your PlanetScale database from your code using the withPlanetscale
function.
In the example below, the planetscale
property on the context
object contains the connection
object. This enables a connection to the database using the credentials stored in environment variables. Once connected, the SQL command executes to insert the email
and name
properties into the users
table (this is a required component for this example) of the PlanetScale database.
import { Handler } from "@netlify/functions";
import { withPlanetscale } from "@netlify/planetscale";
export const handler: Handler = withPlanetscale(async (event, context) => {
const {
planetscale: { connection },
} = context;
const { body } = event;
if (!body) {
return {
statusCode: 400,
body: "Missing body",
};
}
const { email, name } = JSON.parse(body);
await connection.execute("INSERT INTO users (email, name) VALUES (?, ?)", [
email,
name,
]);
return {
statusCode: 201,
};
});
We will also follow up with an in-depth tutorial that further explains how the integration can be used. Stay tuned!