Skip to content

PlanetScale

PlanetScale once provided a hosted MySQL database with 5GB of storage included in the free tier. On April 8, 2024, the free plan was retired and the cheapest plan starts at $39 with 10GB storage.

1. Sign up to PlanetScale

Go to https://auth.planetscale.com/sign-up

2. Create a new database

Select New database on the dashboard.

Create a database.

Screenshot 2023-06-25 at 21.43.25.png

Screenshot 2023-06-25 at 21.15.01.png

3. Create a password

Go to SettingsPasswords and click New password.

Screenshot 2023-06-25 at 21.15.41.png

Save the host, username, and password values as Val Town environment variables - use planetScaleHost, planetScaleUsername, and planetScalePassword respectively.

Screenshot 2023-06-25 at 21.16.18.png

4. Create your first table

Copy and paste this val to create a table with the given schema.

Table creationRun in Val Town ↗
import { queryPlanetScale } from "https://esm.town/v/vtdocs/queryPlanetScale";
await queryPlanetScale(
{
host: Deno.env.get("planetScaleHost"),
username: Deno.env.get("planetScaleUsername"),
password: Deno.env.get("planetScalePassword"),
},
`CREATE TABLE stock (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
price varchar(255) NOT NULL
);`
);

5. Insert an item

Insert one or more items.

Data insertionRun in Val Town ↗
import { queryPlanetScale } from "https://esm.town/v/vtdocs/queryPlanetScale";
await queryPlanetScale(
{
host: Deno.env.get("planetScaleHost"),
username: Deno.env.get("planetScaleUsername"),
password: Deno.env.get("planetScalePassword"),
},
`INSERT INTO stock (name, price) VALUES (?, ?);`,
["banana", 15]
);

6. Query an item

Use the rows property to get your query results - or log the entire results object to see what other data is available.

Query stock tableRun in Val Town ↗
import { queryPlanetScale } from "https://esm.town/v/vtdocs/queryPlanetScale";
const results = await queryPlanetScale(
{
host: Deno.env.get("planetScaleHost"),
username: Deno.env.get("planetScaleUsername"),
password: Deno.env.get("planetScalePassword"),
},
`SELECT id, name, price FROM stock WHERE name=?`,
["banana"]
);
console.log(results.rows[0]);

7. Do more than queries!

Read the source of the @vtdocs/queryPlanetScale helper, see how it uses the @planetscale/database SDK, refer to the driver’s documentation, and extend it!