title | summary | toc | referral_id | docs_area | |
---|---|---|---|---|---|
Build a Simple CRUD Node.js App with CockroachDB and Knex.js |
Learn how to use CockroachDB from a simple CRUD application that uses the Knex.js query builder. |
true |
false |
docs_node_knexjs |
get_started |
{% include {{ page.version.version }}/filter-tabs/crud-js.md %}
This tutorial shows you how build a simple Node.js application with CockroachDB and Knex.js.
{% include {{ page.version.version }}/setup/sample-setup.md %}
Clone the code's GitHub repo:
{% include_cached copy-clipboard.html %}
$ git clone https://github.com/cockroachlabs/example-app-node-knex
-
Set the
DATABASE_URL
environment variable to the connection string:{% include_cached copy-clipboard.html %}
$ export DATABASE_URL="postgresql://root@localhost:26257?sslmode=disable"
{% include_cached copy-clipboard.html %}
$ export DATABASE_URL="<connection-string>"
Where
<connection-string>
is the connection string you obtained from the {{ site.data.products.db }} Console.The app uses the connection string saved to the
DATABASE_URL
environment variable to connect to your cluster and execute the code. -
Install the app requirements:
{% include_cached copy-clipboard.html %}
$ npm install
-
Run the app:
{% include_cached copy-clipboard.html %}
$ npm start
The output should look like this:
Initializing accounts table... New account balances: { id: 'bc0f7136-7103-4dc4-88fc-102d5bbd312f', balance: '1000' } { id: '35bde7d0-29c9-4277-a117-3eb80c85ae16', balance: '250' } { id: '18cc1b2d-be61-4a8d-942c-480f5a6bc207', balance: '0' } Transferring funds... New account balances: { id: 'bc0f7136-7103-4dc4-88fc-102d5bbd312f', balance: '900' } { id: '35bde7d0-29c9-4277-a117-3eb80c85ae16', balance: '350' } { id: '18cc1b2d-be61-4a8d-942c-480f5a6bc207', balance: '0' } Deleting a row... New account balances: { id: 'bc0f7136-7103-4dc4-88fc-102d5bbd312f', balance: '900' } { id: '18cc1b2d-be61-4a8d-942c-480f5a6bc207', balance: '0' }
{% include {{page.version.version}}/app/see-also-links.md %}