Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
On this page

How to use Planetscale with Deno

Planetscale is a MySQL-compatible serverless database that is designed with adeveloper workflow where developers can create, branch, and deploy databasesfrom the command line.

View source here.

We'll use the Planetscale serverless driver,@planetscale/database, to workwith Deno. First we want to createmain.ts and import the connect method fromthis package:

import{ connect}from"npm:@planetscale/database@^1.4";

Configuring our connectionJump to heading

The connection requires three credentials: host, username, and password. Theseare database-specific, so we first need to create a database in Planetscale. Youcan do that by following the initial instructionshere.Don't worry about adding the schema—we can do that through@planetscale/database.

Once you have created the database, head to Overview, click "Connect", andchoose "Connect with@planetscale/database" to get the host and username. Thenclick through to Passwords to create a new password for your database. Once youhave all three you can plug them in directly, or better, store them asenvironment variables:

exportHOST=<host>exportUSERNAME=<username>exportPASSWORD=<password>

Then call them usingDeno.env:

const config={  host: Deno.env.get("HOST"),  username: Deno.env.get("USERNAME"),  password: Deno.env.get("PASSWORD"),};const conn=connect(config);

This will also work on Deno Deploy if you set the environment variables in thedashboard. Run with:

deno run --allow-net --allow-env main.ts

Theconn object is now an open connection to our Planetscale database.

Creating and populating our database tableJump to heading

Now that you have the connection running, you can conn.execute() with SQLcommands to create tables and insert the initial data:

await conn.execute("CREATE TABLE dinosaurs (id int NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(255) NOT NULL, description varchar(255) NOT NULL);",);await conn.execute("INSERT INTO `dinosaurs` (id, name, description) VALUES (1, 'Aardonyx', 'An early stage in the evolution of sauropods.'), (2, 'Abelisaurus', 'Abels lizard has been reconstructed from a single skull.'), (3, 'Deno', 'The fastest dinosaur that ever lived.')",);

Querying PlanetscaleJump to heading

We can use sameconn.execute() to also write our queries. Let's get a list ofall our dinosaurs:

const results=await conn.execute("SELECT * FROM `dinosaurs`");console.log(results.rows);

The result:

[{    id:1,    name:"Aardonyx",    description:"An early stage in the evolution of sauropods.",},{    id:2,    name:"Abelisaurus",    description:"Abels lizard has been reconstructed from a single skull.",},{ id:3, name:"Deno", description:"The fastest dinosaur that ever lived."},];

We can also get just a single row from the database by specifying a dinosaurname:

const result=await conn.execute("SELECT * FROM `dinosaurs` WHERE `name` = 'Deno'",);console.log(result.rows);

Which gives us a single row result:

[{ id:3, name:"Deno", description:"The fastest dinosaur that ever lived."}];

You can find out more about working with Planetscale in theirdocs.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp