Build a Node.js App with CockroachDB

2020-02-16

This tutorial shows you how build a simple Node.js application with CockroachDB using a PostgreSQL-compatible driver or ORM.

We have tested the Node.js pg driver and the Sequelize ORM enough to claim beta-level support, so those are featured here. If you encounter problems, please open an issue with details to help us make progress toward full support.

{{site.data.alerts.callout_success}} For a more realistic use of Sequelize with CockroachDB, see our examples-ormsrepository. {{site.data.alerts.end}}

Before you begin

  1. Install CockroachDB.
  2. Start up a secure or insecure local cluster.
  3. Choose the instructions that correspond to whether your cluster is secure or insecure:

Step 1. Install the Sequelize ORM

To install Sequelize, as well as a CockroachDB Node.js package that accounts for some minor differences between CockroachDB and PostgreSQL, run the following command:

$ npm install sequelize sequelize-cockroachdb
## Step 2. Create the `maxroach` user and `bank` database {% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} ## Step 3. Generate a certificate for the `maxroach` user Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. {% include copy-clipboard.html %} ~~~ shell $ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key ~~~ ## Step 4. Run the Node.js code The following code uses the [Sequelize](https://sequelize.readthedocs.io/en/v3/) ORM to map Node.js-specific objects to SQL operations. Specifically, `Account.sync({force: true})` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.bulkCreate([...])` inserts rows into the table, and `Account.findAll()` selects from the table so that balances can be printed. Copy the code or download it directly. {% include copy-clipboard.html %} ~~~ js {% include {{ page.version.version }}/app/sequelize-basic-sample.js %} ~~~ Then run the code: {% include copy-clipboard.html %} ~~~ shell $ node sequelize-basic-sample.js ~~~ The output should be: ~~~ shell 1 1000 2 250 ~~~ To verify that funds were transferred from one account to another, start the [built-in SQL client](use-the-built-in-sql-client.html): {% include copy-clipboard.html %} ~~~ shell $ cockroach sql --certs-dir=/tmp/certs -e 'SELECT id, balance FROM accounts' --database=bank ~~~ ~~~ +----+---------+ | id | balance | +----+---------+ | 1 | 1000 | | 2 | 250 | +----+---------+ (2 rows) ~~~
## Step 2. Create the `maxroach` user and `bank` database {% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} ## Step 3. Run the Node.js code The following code uses the [Sequelize](https://sequelize.readthedocs.io/en/v3/) ORM to map Node.js-specific objects to SQL operations. Specifically, `Account.sync({force: true})` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.bulkCreate([...])` inserts rows into the table, and `Account.findAll()` selects from the table so that balances can be printed. Copy the code or download it directly. {% include copy-clipboard.html %} ~~~ js {% include {{ page.version.version }}/app/insecure/sequelize-basic-sample.js %} ~~~ Then run the code: {% include copy-clipboard.html %} ~~~ shell $ node sequelize-basic-sample.js ~~~ The output should be: ~~~ shell 1 1000 2 250 ~~~ To verify that the table and rows were created successfully, you can again use the [built-in SQL client](use-the-built-in-sql-client.html): {% include copy-clipboard.html %} ~~~ shell $ cockroach sql --insecure -e 'SHOW TABLES' --database=bank ~~~ ~~~ +------------+ | table_name | +------------+ | accounts | +------------+ (1 row) ~~~ {% include copy-clipboard.html %} ~~~ shell $ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank ~~~ ~~~ +----+---------+ | id | balance | +----+---------+ | 1 | 1000 | | 2 | 250 | +----+---------+ (2 rows) ~~~

What's next?

Read more about using the Sequelize ORM, or check out a more realistic implementation of Sequelize with CockroachDB in our examples-orms repository.

You might also be interested in using a local cluster to explore the following CockroachDB benefits: