{"id":30802,"date":"2024-08-10T08:09:43","date_gmt":"2024-08-10T16:09:43","guid":{"rendered":"https:\/\/alexrusin.com\/?p=30802"},"modified":"2024-08-11T07:49:49","modified_gmt":"2024-08-11T15:49:49","slug":"how-to-integrate-sequelize-with-typescript-in-node-js-project","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/how-to-integrate-sequelize-with-typescript-in-node-js-project\/","title":{"rendered":"How to Integrate Sequelize with Typescript in Node.js Project"},"content":{"rendered":"\n
In my experience, many Node.js projects require access to a database. My go-to ORM (Object-Relational Mapper) for these tasks is Sequelize. In this blog post, I’ll guide you through the process of adding a MySQL database using Sequelize with Typescript in your Node.js project.<\/p>\n\n\n\n
To start, you’ll need to set up a MySQL instance using Docker. Here’s how you can do it:<\/p>\n\n\n\n
\ndocker run --name mysql-db -e MYSQL_ROOT_PASSWORD=secret -p 3306:3306 -d mysql:latest\n<\/pre><\/div>\n\n\n\n- Run the command. If you don’t have the MySQL image, Docker will automatically download it and start the container.<\/li>\n<\/ol>\n\n\n\n
Step 2: Connecting to MySQL Using TablePlus<\/h2>\n\n\n\n
Next, you’ll want to connect to your MySQL instance using TablePlus or a similar database management tool:<\/p>\n\n\n\n
\n- Open TablePlus and configure a new connection:\n
\n- Host:
localhost<\/code><\/li>\n\n\n\n- Port:
3306<\/code><\/li>\n\n\n\n- Username:
root<\/code><\/li>\n\n\n\n- Password:
secret<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n- Test the connection, and if successful, connect to the database.<\/li>\n\n\n\n
- Create a new database named
my_blog<\/code> using the UI.<\/li>\n<\/ol>\n\n\n\nStep 3: Configure Your Node.js Project<\/h2>\n\n\n\n
In your Node.js TypeScript project, you’ll need to configure the database connection settings:<\/p>\n\n\n\n
\n- Open the
.env<\/code> file and add the following configuration below.<\/li>\n\n\n\n- Ensure that your project has a configuration module to load these environment variables.<\/li>\n<\/ol>\n\n\n\n
DB_NAME=my_blog\nDB_USER=root\nDB_PASSWORD=secret\nDB_HOST=localhost\nDB_PORT=3306<\/pre>\n\n\n\n\n