A Node.js REST API for managing contract data using Express.js and Prisma ORM with PostgreSQL.
- Node.js (v16 or higher)
- pnpm package manager
- PostgreSQL database
- Framework: Express.js 5.1.0
- Database: PostgreSQL with Prisma ORM 6.12.0
- Language: TypeScript
- Package Manager: pnpm
pnpm installContact the DATA team to obtain creds.json:
- This file contains the Google Cloud service account credentials
- Required for IAM authentication to Cloud SQL in the application
- Place the file in the project root directory as
creds.json - The file is already in
.gitignoreto prevent accidental commits
Configure the following environment variables:
DB_NAME- Your database namePOSTGRES_CONNECTION_NAME- Cloud SQL instance connection namePOSTGRES_IAM_USER- IAM database user emailGOOGLE_APPLICATION_CREDENTIALS- Path tocreds.json(e.g.,./creds.json)DATABASE_URL- PostgreSQL connection string (see options below)
Important Notes:
- This API uses IAM authentication via Google Cloud SQL Connector for database connections in the application controllers
- The application uses
creds.json(viaGOOGLE_APPLICATION_CREDENTIALS) for IAM authentication DATABASE_URLis not directly used by the application controllers but is required by Prisma CLI tools for:- Running migrations (
pnpm migrate) - Using Prisma Studio (
pnpm prisma:studio) - Database introspection (
npx prisma db pull)
- Running migrations (
Two options for using Prisma CLI tools:
Use the Cloud SQL Auth Proxy to connect with IAM authentication:
-
Download the Cloud SQL Proxy from Google Cloud
-
Run the proxy with IAM authentication:
./cloud-sql-proxy --auto-iam-authn <INSTANCE_CONNECTION_NAME>
-
Set your DATABASE_URL to use the proxy:
DATABASE_URL="postgresql://<YOUR_IAM_EMAIL>@localhost:5432/<DATABASE_NAME>?host=/cloudsql/<INSTANCE_CONNECTION_NAME>" -
Run Prisma commands:
npx prisma db pull pnpm prisma:studio
Contact the data team to get your IP address allowlisted for direct database access:
DATABASE_URL="postgresql://username:password@localhost:5432/contract_data"This option is useful for Prisma Studio and running migrations locally.
npx prisma db pull
pnpm prisma:generate
# or
npx prisma generatepnpm migrate
# or
npx prisma migrate devpnpm devpnpm dev- Run development server with nodemonpnpm dev:watch- Run development server with file watchingpnpm build- Build TypeScript to JavaScriptpnpm start- Run production serverpnpm typecheck- Run TypeScript type checkingpnpm prisma:generate- Generate Prisma clientpnpm migrate- Run database migrationspnpm prisma:studio- Open Prisma Studio
- Reset database:
npx prisma migrate reset - Deploy migrations:
npx prisma migrate deploy - Pull database schema:
npx prisma db pull
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/${network}/contracts/:id/storage |
Get contract data by ID |
curl http://localhost:3000/api/{network}/contract/{contract_id}/storage
- ?sort_by=durability&order=desc - Sort by durability descending
- ?sort_by=ttl&order=asc - Sort by TTL ascending
- ?sort_by=updated_at&order=desc - Sort by updated timestamp descending
src/
├── controllers/ # Route handlers
├── routes/ # API routes
├── utils/ # Utility functions
└── index.ts # Main application entry
prisma/
├── schema.prisma # Database schema
└── migrations/ # Database migrations