Skip to content

Latest commit

 

History

History
165 lines (117 loc) · 5.35 KB

build-a-nodejs-app-with-cockroachdb-sequelize.md

File metadata and controls

165 lines (117 loc) · 5.35 KB
title summary toc twitter
Build a Node.js App with CockroachDB and Sequelize
Learn how to use CockroachDB from a simple Node.js application with the Sequelize ORM.
true
false

This tutorial shows you how build a simple Node.js application with CockroachDB and the Sequelize ORM.

We have tested the Sequelize ORM enough to claim beta-level support. 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

{% include {{page.version.version}}/app/before-you-begin.md %}

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:

{% include copy-clipboard.html %}

$ 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 %}

$ 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 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 %}

{% include {{ page.version.version }}/app/sequelize-basic-sample.js %}

Then run the code:

{% include copy-clipboard.html %}

$ node sequelize-basic-sample.js

The output should be:

1 1000
2 250

To verify that funds were transferred from one account to another, start the built-in SQL client:

{% include copy-clipboard.html %}

$ 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 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 %}

{% include {{ page.version.version }}/app/insecure/sequelize-basic-sample.js %}

Then run the code:

{% include copy-clipboard.html %}

$ node sequelize-basic-sample.js

The output should be:

1 1000
2 250

To verify that the table and rows were created successfully, you can again use the built-in SQL client:

{% include copy-clipboard.html %}

$ cockroach sql --insecure -e 'SHOW TABLES' --database=bank
+------------+
| table_name |
+------------+
| accounts   |
+------------+
(1 row)

{% include copy-clipboard.html %}

$ 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.

{% include {{ page.version.version }}/app/see-also-links.md %}