Get Started

A shared folder with AI prompts and code snippets

From workspace: Cockroach Labs

Team: Main

Total snippets: 5

Cockroach Labs

Get Started

5 snippets

Init SQL Schema

The dbinit.sql file initializes the database schema that the application uses:

CREATE TABLE accounts ( id UUID PRIMARY KEY, balance INT8 );

Set Database URL

Set the DATABASE_URL environment variable to the connection string for your cluster. Where {connection-string} is the connection string you copied earlier.

export DATABASE_URL="{connection-string}"

Clone App Repo

Clone the example Node.js app with Postgres from CockroachDB's official GitHub repository.

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

Initialize Database

To initialize the example database, use the cockroach sql command to execute the SQL statements in the dbinit.sql file:

cat dbinit.sql | cockroach sql --url $DATABASE_URL

Retry Transaction Wrapper

The app.js file contains the code for INSERT, SELECT, UPDATE, and DELETE SQL operations:

const { Pool } = require("pg"); const { v4: uuidv4 } = require("uuid"); var accountValues = Array(3); // Wrapper for a transaction. This automatically re-calls the operation with // the client as an argument as long as the database server asks...

Cockroach Labs - Get Started - AI Prompts & Code Snippets | Snippets AI