title | summary | toc | referral_id | |
---|---|---|---|---|
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 |
docs_hello_world_nodejs_sequelize |
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/start-cockroachdb.md %}
{% include {{page.version.version}}/app/create-a-database.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_cached copy-clipboard.html %}
$ npm install sequelize sequelize-cockroachdb
Download the sample code directly, or clone the code's GitHub repository.
Open app.js
, and edit the connection configuration parameters:
- 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
port
with the port to your cluster.
-
At the top of the file, uncomment the
const fs = require('fs');
line.This line imports the
fs
Node module, which enables you to read in the CA cert that you downloaded from the {{ site.data.products.db }} Console. -
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
host
with the name of the {{ site.data.products.db }} 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
database
with the database that you created earlier, suffixed with the name of the cluster (e.g.,database: '{cluster_name}.bank'
). -
Remove the
rejectUnauthorized
key-value pair. -
Uncomment the
ca
key-value pair, and edit thefs.readFileSync('certs/ca.crt').toString()
call to use the path to thecc-ca.crt
file that you downloaded from the {{ site.data.products.db }} Console.
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.
{% include_cached copy-clipboard.html %}
{% remote_include https://raw.githubusercontent.com/cockroachlabs/example-app-node-sequelize/main/app.js %}
To run the code:
{% include_cached copy-clipboard.html %}
$ node app.js
The output should be:
1 1000
2 250
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 %}