This tutorial shows you how to deploy a Netlify web application that communicates with a CockroachDB Serverless cluster.
The sample app used in this tutorial simulates a gaming leaderboard. The Netlify functions used for the app are written in TypeScript. The functions use Prisma to connect to CockroachDB. The app's frontend, also written in TypeScript, uses React, bootstrapped with Create React App.
The source code for the completed app is available on GitHub at https://github.com/cockroachdb/cockroachdb-typescript.
Prerequisites
Before starting the tutorial, do the following:
Create a CockroachDB Cloud account.
Create a Starter Netlify account. You can do this with your GitHub login credentials.
Step 1. Create a CockroachDB Serverless cluster
Organizations without billing information on file can only create one CockroachDB Basic cluster.
- If you haven't already, sign up for a CockroachDB Cloud account.
- Log in to your CockroachDB Cloud account.
- On the Clusters page, click Create cluster.
- On the Select a plan page, select Basic.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS) in the Cloud provider section.
- In the Regions section, select a region for the cluster. Refer to CockroachDB Cloud Regions for the regions where CockroachDB Basic clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, select Start for free. Click Next: Finalize.
On the Finalize page, click Create cluster.
Your cluster will be created in a few seconds and the Create SQL user dialog will display.
After the cluster is created, the Connection info window appears. Click the Connection string tab and copy the connection string to a secure location. You will use this connection string to connect to CockroachDB later in the tutorial.
The connection string is pre-populated with your username, cluster name, and other details, including your password. Your password, in particular, will be provided only once. Save it in a secure place (we recommend a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users
.
Step 2. Get the code
Clone the code's GitHub repo:
$ git clone git@github.com:cockroachdb/cockroachdb-typescript.git
The project has the following directory structure:
├── LICENSE.md ├── README.md ├── netlify │ └── functions │ ├── addScore.ts │ ├── getPlayers.ts │ └── getScores.ts ├── package-lock.json ├── package.json ├── prisma │ ├── schema.prisma │ └── seed.ts ├── public ├── src └── tsconfig.json
In this tutorial, we focus on the files in the
netlify
andprisma
directories.At the top of the repo directory, fork the repo:
$ gh repo fork --remote
To deploy your code to Netlify, you need to have your own repo or your own fork of the existing repo.
Step 3. Initialize the database
In the
.env
file in your project, set theDATABASE_URL
environment variable to the connection string you obtained earlier from the CockroachDB Cloud Console:DATABASE_URL=<connection-string>
Prisma loads the variables defined in
.env
to the project environment.Install Prisma:
npm install prisma --save-dev
Run Prisma Migrate to initialize the database with the schema defined in
prisma/prisma.schema
.$ npx prisma migrate dev --name init
You should see the following output:
Your database is now in sync with your schema. ✔ Generated Prisma Client (3.11.0 | library) to ./node_modules/@prisma/client in 87ms Running seed command `ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts` ... { test_player_1: { id: 1n, name: 'Test Player 1', email: 'test_player_1@example.com' }, test_player_2: { id: 2n, name: 'Test Player 2', email: 'test_player_2@example.com' }, test_player_3: { id: 3n, name: 'Test Player 3', email: 'test_player_3@example.com' } } 🌱 The seed command has been executed.
This command initializes Prisma Client to communicate with your CockroachDB cluster, based on the configuration in the
prisma/schema.prisma
file. The command also seeds your database with some sample data, using the script defined inprisma/seed.ts
.
Step 4. Deploy the application
You can deploy web applications directly from GitHub to Netlify. In this tutorial, we use the Netlify CLI to deploy the app.
Install the
netlify
CLI:$ npm install netlify-cli -g
Sign into your Netlify account:
$ netlify login
Run the app server locally to preview your site:
$ netlify dev
If the local deployment succeeds, you should see the following message in your terminal:
┌─────────────────────────────────────────────────┐ │ │ │ ◈ Server now ready on http://localhost:8888 │ │ │ └─────────────────────────────────────────────────┘
For a preview of the site, visit http://localhost:8888.
Interacting with the site triggers the Netlify functions defined in the
netlify/functions
directory. These functions use Prisma Client to runSELECT
andINSERT
queries against the database:getScores.ts
reads all rows from theplayer_scores
table and returns values in theid
,name
, andscore
columns.getPlayers.ts
reads and returns all rows from theplayers
table.addScore.ts
writes new scores to theplayer_scores
table.
Deploy your app with the Netlify CLI:
$ netlify deploy
Choose to create a new site, and then select the default options for each of the subsequent prompts. You will be required to authorize Netlify with GitHub.
After the app is deployed, you should see the following message:
✔ Deploy is live!
Navigate to the admin URL for your site:
$ netlify open
From the Site overview page, you can manage your site. The Site overview page also provides you with a public URL for your site.
See also
- How to build a Complete Webapp with React, TypeScript & CockroachDB
- Build a Simple CRUD Node.js App with CockroachDB and Prisma Client
You might also be interested in the following pages: