Skip to content

Latest commit

 

History

History
95 lines (69 loc) · 3.14 KB

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

File metadata and controls

95 lines (69 loc) · 3.14 KB
title summary toc twitter referral_id
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

This tutorial shows you how build a simple Node.js application with CockroachDB and Knex.js.

Step 1. Start CockroachDB

{% include {{ page.version.version }}/app/sample-setup.md %}

Step 2. Get the code

Clone the code's GitHub repo:

{% include_cached copy-clipboard.html %}

$ git clone https://github.com/cockroachlabs/example-app-node-knex

Step 3. Run the code

To start the app:

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

  2. Install the app requirements:

    {% include_cached copy-clipboard.html %}

    $ npm install
  3. Run the app:

    {% include_cached copy-clipboard.html %}

    $ npm run run

    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' }
    

What's next?

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