A simple TypeScript database service for your RequestBin clone using PostgreSQL + MongoDB.
- Install dependencies:
npm install- Set up environment variables (copy
.env.exampleto.envand fill in your values):
cp .env.example .env-
Make sure PostgreSQL and MongoDB are running.
-
Create the PostgreSQL database (MongoDB creates its database automatically):
createdb requestbin
# If you get a permission error, try: createdb -U <your_username> requestbinNote: If your PostgreSQL user isn't postgres, update PG_USER in your .env file. On macOS with Homebrew, it's usually your system username.
- Build the TypeScript:
npm run build- Run the test script to verify everything works:
npm testimport db from './db';
// Initialize connections and create tables
await db.init();
// Create a bin
const bin = await db.bins.create('abc123', 'secret-token-xyz');
// Create a request (body goes to MongoDB, rest goes to Postgres)
const request = await db.requests.create(bin.id, {
method: 'POST',
parameters: { foo: 'bar' }, // query parameters
headers: { 'content-type': 'application/json' }, // headers
body: { message: 'Hello!' } // body (stored in MongoDB)
});
// Get all requests for a bin (bodies automatically fetched from MongoDB)
const requests = await db.requests.getByBinId(bin.id);
// Clean up
await db.close();Connects to both databases and creates tables if they don't exist.
Closes all database connections.
Creates a new bin with the given route and token.
Gets a bin by its route (e.g., "abc123").
Gets a bin by its UUID.
Deletes a bin and all its requests (cascades in Postgres).
Creates a new request. The body is stored in MongoDB, everything else in PostgreSQL.
RequestData interface:
interface RequestData {
method: string;
parameters: Record<string, unknown>;
headers: Record<string, unknown>;
body: unknown;
}Gets all requests for a bin by its ID, automatically fetching bodies from MongoDB.
Gets all requests for a bin by its route (e.g., "abc123"). Returns empty array if bin not found.
Gets a single request by ID with its body.
Deletes a request and its body from MongoDB.
- Creating a request: Body → MongoDB → get
body_id→ Rest → PostgreSQL withbody_id - Reading requests: Fetch from PostgreSQL → Use
body_idto fetch from MongoDB → Combine - Deleting: Delete from PostgreSQL → Delete body from MongoDB by
body_id
Update PG_USER in your .env to match your system username (run whoami to find it).
Run createdb requestbin to create the database.
Make sure your MONGODB_URI includes credentials if your MongoDB requires auth:
MONGODB_URI=mongodb://username:password@localhost:27017/requestbin
The previous test run didn't clean up. Reset the database:
dropdb requestbin && createdb requestbininterface Bin {
id: string;
bin_route: string;
created_at: Date;
token: string;
}
interface RequestData {
method: string;
parameters: Record<string, unknown>;
headers: Record<string, unknown>;
body: unknown;
}
interface RequestWithBody {
id: string;
bin_id: string;
method: string;
parameters: Record<string, unknown>;
headers: Record<string, unknown>;
body_id: string;
created_at: Date;
body: unknown; // from MongoDB
}