title | summary | toc | |
---|---|---|---|
Build a TypeScript App with CockroachDB and TypeORM |
Learn how to use CockroachDB with the TypeORM framework. |
true |
false |
This tutorial shows you how run a simple application built with TypeORM.
{% include {{page.version.version}}/app/start-cockroachdb.md %}
{% include {{page.version.version}}/app/create-a-database.md %}
Clone the code's GitHub repository.
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.
Open a terminal window, and install the Node.js pg driver:
{% include copy-clipboard.html %}
$ npm install pg --save
Navigate to the top directory of the application project (e.g., hello-world-typescript-typeorm
), and initialize the project:
{% include copy-clipboard.html %}
$ npm i
Start the application:
{% include copy-clipboard.html %}
$ 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 }
Read more about using the TypeORM.
{% include {{page.version.version}}/app/see-also-links.md %}