Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Ticket Service

Simple TypeScript + Express service that generates auto-incrementing random ticket IDs using sharded Postgres pools.

Features

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

Prerequisites

  • Node.js (v18+ recommended)
  • npm

Setup

  1. Install dependencies: npm installs

  2. Copy or edit the .env file 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)
  3. Build (TypeScript): npm run build

  4. Development (auto-build + run): npm run dev

  5. Type-check: npm run typecheck

.env example

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

Scripts

  • npm run dev — watch, build and run the compiled output
  • npm run build — compile TypeScript to ./dist
  • npm run typecheck — run tsc --noEmit

API

  • GET /randomID
    • Response: { status: 'Success', randomID: number } or errors (-1 used in code to indicate failures)
  • GET /
    • Simple health/log endpoint

Database

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 UPDATE within a transaction, checks current vs end, increments current, then returns the previous current.
  • Ensure each shard connection string (SHARD_0..2) points to a database that has the tickets table and appropriate rows.

Troubleshooting

  • If you see errors about missing env vars, ensure .env is present and loaded.
  • For DB connection problems verify the SHARD_* connection strings and network access.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages