This project demonstrates how to implement document versioning in MongoDB using TypeScript. We use bun.sh
to manage our project dependencies and MongoClient
for MongoDB. Docker is used to create a local MongoDB instance for testing.
Ensure you have the following installed on your system:
- Bun (v1.1.17)
- Docker
- Docker Compose
First, clone this repository to your local machine:
git clone <repository-url>
cd my-versioning-project
Install the required dependencies using Bun:
bun install
Create a .env
file in the project root and add the following variables:
MONGODB_URL="mongodb://root:example@localhost:27017"
DB_NAME="my-versioning-project"
Create a docker-compose.yml
file in the project root to set up MongoDB and Mongo Express:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
networks:
default:
name: mongodb_network
Start the MongoDB and Mongo Express services:
docker-compose up -d
Verify the containers are running:
docker ps
Run the project using Bun:
bun run src/index.ts
src/index.ts
: Main entry point for the project.src/services/listingService.ts
: Contains the logic for connecting to MongoDB, creating listings, updating listings, and retrieving listing versions.src/models/listing.ts
: Defines the TypeScript types for listings and listing versions.src/services/listingService.test.ts
: Contains tests for the listing service functions.
To run tests:
bun test
In src/services/listingService.ts
, we define a connectDB
function to connect to the MongoDB instance.
We define functions to create and update listings in src/services/listingService.ts
.
Refer to the src/services/listingService.test.ts
file for detailed test cases that ensure the listing service works correctly. The test file includes cases for creating a new listing, updating a listing, retrieving listing versions, and getting a listing by ID.