Simple TypeScript + Express service that generates auto-incrementing random ticket IDs using sharded Postgres pools.
- Picks a random shard and increments a ticket counter in a transactional-safe way.
- Exposes a small HTTP API to request a generated random ID.
- Node.js (v18+ recommended)
- npm
-
Install dependencies: npm installs
-
Copy or edit the
.envfile with your values. Example variables used by the app:- PORT (default 5000)
- Url (for logging, e.g. http://localhost)
- SHARD_0, SHARD_1, SHARD_2 (Postgres connection strings)
-
Build (TypeScript): npm run build
-
Development (auto-build + run): npm run dev
-
Type-check: npm run typecheck
Create a .env file in the project root with values like the example below. Replace usernames, passwords, hosts and database names with your real values.
PORT=5000
URL=http://localhost
# Postgres shard connection strings (replace credentials/hosts/db names)
SHARD_0=postgres://user:password@localhost:5432/shard0db
SHARD_1=postgres://user:password@localhost:5432/shard1db
SHARD_2=postgres://user:password@localhost:5432/shard2db- npm run dev — watch, build and run the compiled output
- npm run build — compile TypeScript to ./dist
- npm run typecheck — run
tsc --noEmit
- GET /randomID
- Response: { status: 'Success', randomID: number } or errors (-1 used in code to indicate failures)
- GET /
- Simple health/log endpoint
The service expects a tickets table with at least these columns:
- id (int PRIMARY KEY)
- start (int)
- end (int)
- current (int)
Notes:
- The service selects a random row id and locks it using
FOR UPDATEwithin a transaction, checkscurrentvsend, incrementscurrent, then returns the previouscurrent. - Ensure each shard connection string (SHARD_0..2) points to a database that has the
ticketstable and appropriate rows.
- If you see errors about missing env vars, ensure
.envis present and loaded. - For DB connection problems verify the SHARD_* connection strings and network access.
MIT