Edit on val.town ↗

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.

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

Select New database on the dashboard.

Create a database.

screenshot_2023-06-25_at_214325.png

screenshot_2023-06-25_at_211501.png

Go to Settings → Passwords and click New password.

screenshot_2023-06-25_at_211541.png

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

screenshot_2023-06-25_at_211618.png

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

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
);`
);

Insert one or more items.

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]
);

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

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]);

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!

Last updated