Skip to content

Node.js example using the PlanetScale serverless driver

Learn the PlanetScale serverless driver by running a local Node.js project.

Overview

This guide will cover how to use the provided Node.js sample application using the PlanetScale serverless driver for JavaScript. Before following this guide, make sure you've enabled the feature and created a password to use with the driver.

Note

This guide will be using VS Code as the IDE, but you may use your preferred IDE.

Use the sample repository

We offer a sample repository that can be used as an educational resource. It is an Express API that can be run locally with sample SELECT, INSERT, UPDATE, and DELETE statements mapped to the proper API endpoints.

To follow along, you’ll need the following:

  • A PlanetScale account, as well as knowing how to create a database.
  • The PlanetScale CLI is installed on your computer, which will be used to seed data.

Start by creating a database in PlanetScale by clicking "New database" > "Create new database".

How to create a new database.How to create a new database.

Name the database travel_db. Click "Create database".

The New database modal.The New database modal.

Wait for the database to finish initializing before moving on.

The travel_db initializing.The travel_db initializing.

Enable the PlanetScale serverless driver for JavaScript feature by clicking on "Settings" > "Beta features", then clicking "Enroll" in the PlanetScale serverless driver for JavaScript section.

Enable PlanetScale serverless driver for JavaScript for travel_db.Enable PlanetScale serverless driver for JavaScript for travel_db.

Generate a set of credentials by clicking the "Connect" button.

The Connect button in the PlanetScale dashboard.The Connect button in the PlanetScale dashboard.

Select "@planetscale/database" from the options in the "Connect with" dropdown. Copy the text from the ".env" tab as we'll be putting this in the project after it's pulled down from GitHub.

The Connect modal.The Connect modal.

On your workstation, open a terminal and clone the repository to your computer by running the following command:

Copied
git clone https://github.com/planetscale/database-js-starter

Navigate to the scripts folder and run the seed_database.sh script to populate a small database simulating a travel agency.

Copied
cd database-js-starter/scripts
./seed-database.sh
Note

If you are using Windows, run this command through the Windows Subsystem for Linux (WSL)

Create a new file file named .env in the root of the project and paste in the sample provided from PlanetScale.

The .env example.The .env example.

To run the project, run the following commands from the root of the project.

Copied
npm install
npm start

If the project is running properly, you should receive a message stating that the API is running.

The terminal running the sample project.The terminal running the sample project.

The tests.http file is designed to work with the VS Code Rest Client plugin, but can be used as a reference when testing with the tool of your choosing. If you are using the plugin, you may click the "Send request" button that appears above each request to see the API in action.

An example of a POST request to the sample project.An example of a POST request to the sample project.

If you check the terminal where the API was started, the response from the execute function is logged out for review.

The console output after a hotel is added.The console output after a hotel is added.

Need help?

Get help from the PlanetScale support team, or join our GitHub discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on August 18, 2022
Help us improve this page
© 2022 PlanetScale Inc.