A shared folder with AI prompts and code snippets
From workspace: Cockroach Labs
Team: Main
Total snippets: 5
5 snippets
The dbinit.sql file initializes the database schema that the application uses:
CREATE TABLE accounts ( id UUID PRIMARY KEY, balance INT8 );
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 the example Node.js app with Postgres from CockroachDB's official GitHub repository.
git clone https://github.com/cockroachlabs/example-app-node-postgres
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
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...