title | summary | toc | |
---|---|---|---|
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-orms
repository.
{{site.data.alerts.end}}
{% include {{page.version.version}}/app/before-you-begin.md %}
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
{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %}
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
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)
{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %}
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)
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 %}