A complete Node.js project demonstrating Sequelize ORM with MySQL database integration.
- Start XAMPP and ensure MySQL is running
- Open phpMyAdmin (usually at
http://localhost/phpmyadmin) - Create a new database named
sequelize_test
npm installCopy .env.example to .env and update the database settings:
cp .env.example .envUpdate .env with your MySQL settings:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=sequelize_test
DB_PORT=3306node init.jsThis will:
- Create the
UserActivitiestable - Insert initial sample data
- Display the created records
| File | Description |
|---|---|
db.js |
Sequelize setup and UserActivity model definition |
init.js |
Creates table and inserts initial data |
insertRandom.js |
Inserts random data every 10 seconds |
insertRandomAdvanced.js |
Bonus: Advanced version with 50 record limit |
queryAll.js |
Displays all records with statistics |
node init.jsnode insertRandom.js- Adds random user activity every 10 seconds
- Shows data summary every 30 seconds
- Press
Ctrl+Cto stop
node queryAll.jsnode insertRandomAdvanced.jsSame as insertRandom.js but stops at 50 records
| Column | Type | Description |
|---|---|---|
id |
INTEGER | Primary key (auto-increment) |
name |
STRING | User name (required) |
activity |
STRING | Activity type (required) |
createdAt |
DATETIME | Record creation time |
updatedAt |
DATETIME | Last update time |