This tutorial shows you how run a simple application built with TypeORM.
Step 1. Start CockroachDB
Choose whether to run a temporary local cluster or a free CockroachDB cluster on CockroachCloud. The instructions below will adjust accordingly.
Create a free cluster
- If you haven't already, sign up for a CockroachCloud account.
- Log in to your CockroachCloud account.
- On the Clusters page, click Create Cluster.
On the Create your cluster page, select the Free Plan.
Note:This cluster will be free forever.
(Optional) Select a cloud provider (GCP or AWS) in the Additional configuration section.
Click Create your free cluster.
Your cluster will be created in approximately 20-30 seconds.
Set up your cluster connection
Once your cluster is created, the Connection info dialog displays. Use the information provided in the dialog to set up your cluster connection for the SQL user that was created by default:
- Click the name of the
cc-ca.crt
to download the CA certificate to your local machine. Create a
certs
directory on your local machine:$ mkdir certs
Move the downloaded
cc-ca.crt
file to thecerts
directory:$ mv <path>/<to>/cc-ca.crt <path>/<to>/certs
For example:
$ mv Users/maxroach/Downloads/cc-ca.crt Users/maxroach/certs
Copy the connection string provided, which will be used in the next steps (and to connect to your cluster in the future).
Warning:This connection string contains your password, which will be provided only once. If you forget your password, you can reset it by going to the SQL Users page.
- If you haven't already, download the CockroachDB binary.
Run the
cockroach demo
command:$ cockroach demo \ --empty
This starts a temporary, in-memory cluster and opens an interactive SQL shell to the cluster. Any changes to the database will not persist after the cluster is stopped.
Take note of the
(sql/tcp)
connection string in the SQL shell welcome text:# Connection parameters: # (console) http://127.0.0.1:61009 # (sql) postgres://root:admin@?host=%2Fvar%2Ffolders%2Fk1%2Fr048yqpd7_9337rgxm9vb_gw0000gn%2FT%2Fdemo255013852&port=26257 # (sql/tcp) postgres://root:admin@127.0.0.1:61011?sslmode=require
In this example, the port number is 61011. You will use the port number in your application code later.
Step 2. Create a database
In the SQL shell, create the
bank
database that your application will use:> CREATE DATABASE bank;
Create a SQL user for your app:
> CREATE USER <username> WITH PASSWORD <password>;
Take note of the username and password. You will use it in your application code later.
Give the user the necessary permissions:
> GRANT ALL ON DATABASE bank TO <username>;
- If you haven't already, download the CockroachDB binary.
Start the built-in SQL shell using the connection string you got from the CockroachCloud Console earlier:
$ cockroach sql \ --url='postgres://<username>:<password>@<global host>:26257/<cluster_name>.defaultdb?sslmode=verify-full&sslrootcert=<certs_dir>/cc-ca.crt'
In the connection string copied from the CockroachCloud Console, your username, password and cluster name are pre-populated. Replace the
<certs_dir>
placeholder with the path to thecerts
directory that you created earlier.In the SQL shell, create the
bank
database that your application will use:> CREATE DATABASE bank;
Step 3. Get the code
Clone the code's GitHub repository.
Step 4. Update the connection parameters
Open the ormconfig.ts
file, and edit the ORM configuration parameters:
- Replace the value for
port
with the port to your cluster. - Replace the value for
username
with the user you created earlier. - Replace the value for
password
with the password you created for your user.
At the top of the file, uncomment the
import * as fs from "fs";
line.This line imports the
fs
Node module, which enables you to read in the CA cert that you downloaded from the CockroachCloud Console.Replace the value for
host
with the name of the CockroachCloud Free host (e.g.,host: 'free-tier.gcp-us-central1.cockroachlabs.cloud'
).Replace the value for
port
with the port to your cluster.Replace the value for
username
with the user you created earlier.Replace the value for
password
with the password you created for your user.Replace the value for
database
with the database that you created earlier, suffixed with the name of the cluster (e.g.,database: '{cluster_name}.bank'
).Remove the
ssl: true
key-value pair.Remove the
extra
object and its contents.Uncomment the
ssl
object with theca
key-value pair, and edit thefs.readFileSync('certs/cc-ca.crt').toString()
call to use the path to thecc-ca.crt
file that you downloaded from the CockroachCloud Console.
Step 5. Run the code
Open a terminal window, and install the Node.js pg driver:
$ npm install pg --save
Navigate to the top directory of the application project (e.g., hello-world-typescript-typeorm
), and initialize the project:
$ npm i
Start the application:
$ npm start
You should see the following output in your terminal:
Inserting a new account into the database...
Saved a new account.
Printing balances from account 1.
Account { id: 1, balance: 1000 }
Inserting a new account into the database...
Saved a new account.
Printing balances from account 2.
Account { id: 2, balance: 250 }
Transferring 500 from account 1 to account 2.
Transfer complete.
Printing balances from account 1.
Account { id: 1, balance: 500 }
Printing balances from account 2.
Account { id: 2, balance: 750 }
What's next?
Read more about using the TypeORM.
You might also be interested in the following pages: