This project demonstrates a simple example of implementing database concurrency control using Sequelize in a Node.js/Express application with a MySQL database.
This project showcases a Node.js/Express application that interacts with a MySQL database using Sequelize. The application includes a basic API endpoint (POST /
) that increments a counter in the database.
Before running the application, make sure you have the following installed:
Clone the repository:
git clone https://github.com/your-username/your-repo.git
Install dependencies:
cd your-repo
npm install
Configure the database credentials:
Open TSroute.ts
and replace the placeholder values with your actual database credentials:
const sequelize = new Sequelize('your-database', 'your-username', 'your-password', {
host: 'localhost',
dialect: 'mysql',
});
Sync the model with the database:
npm run sync
Initialize the database with an initial count value:
Open TSroute.ts
and find the following code:
// Add this code to initialize the database with a starting count value
const initialCount = 10;
await Count.create({ count: initialCount });
Insert SQL script:
-- Add this SQL command to initialize the database with a starting count value
INSERT INTO counts (count) VALUES (10);
Save the changes and run the script:
npx ts-node src/app/server.ts
The main file TSroute.ts
includes a POST
endpoint that increments a counter in the database. It uses Sequelize for database interactions.
Count
model is defined with two columns: id
(primary key) and count
.POST
endpoint finds the row with id = 1
, increments the count
value, and updates the database.To execute the script that simulates 1000 POST requests:
chmod +x execute_1000_times.sh
./execute_1000_times.sh