This is a step by step that I think it may help you to create your first API. While doing that, I'll try to compare both ways to do it. Which one is easier? Let's get this answer together! (I'll assume that you already know some steps of creating a database, k?)
Alright, first let's do it WITHOUT TypeScript. The libs I'll use to build this REST API are: Express.js and Sequelize. Both are frequently used to create APIs. To create my database, I'll use MySQL as a dialect. Use the commands in your CLI:
npm install express
And use:
npm install sequelize
I'm assuming that you already did your 'npm init -y' and did your first steps. This is more about presenting you steps with these libs w/ TS and without.
After your first commands on you CLI, create you .env for your enviroment settings. Then you'll install a package called 'sequelize-CLI':
npm install sequelize-cli
Also, install MySQL too.
Right after that, use the command:
npx sequelize-cli init
This command will create four directories: config, models, migrations and seeders.
On your root directory, create your .sequelizerc to resolve your paths between these directories, alright?
You'll notice that you config file is the connection made with your database. Pretty simple. You'll notice too that it has your enviroment settings created previously. Congratulations! You made so much already.
Modify you index.js model file to point to your config.js. Instead of pointing to config.json, it'll point now to config.js with this change.
Well, I hope you have some knowledge about DevOps too. Let's say you already have your dockerfile, docker compose, all your container settings made. Then you'll check your DATABASE through that container I mentioned by running your container.
Use the command:
docker container run --name container-mysql _(it can be any name for your container)_ -e MYSQL_ROOT_PASSWORD=_.env password_ -d _(this is telling your CLI to run this container on background)_ -p _(port)_ 3306:3306 mysql:8.0.29 _(your version of database)_
Now we'll finally create our database using our .env file. User the command:
env $(cat .env) npx sequelize db:create
This command reads your .env file and uses the necessary command in it for Sequelize to create our database.
Nice. If you did that, then execute your container, so you can enter you enviroment that you created (or DevOps created). Now we can start our CRUD without TypeScript using Express and Sequelize.
Get into your database:
mysql -u root -p
And then see your databases:
show databases; _(this is a MySQL command)_
And then, finally create your Model (and Migration too):
npx sequelize-cli model:generate --name User
This command creates your Model in your model directory and also a Migration with a timestamp.
Follow the steps on:
https://sequelize.org/docs/v7/cli/
The documentation is pretty awesome and pretty simple to do it. You'll get your API easily using Express and Sequelize without TypeScript.
This is it. We've done much to set our API without TypeScript. We know that TS offers security in writing our code. As we type, we'll know exactly what to do. Or better, the user will have rules to interact with your creation. And your machine will have propers steps to follow.
But... does it make creating API with Express and Sequelize easier? Let's see.
As good developers, we are always trying to learn and use miminum efforts to solve problems. This is that nature law called 'least effort'. Let's say you DO have a solid background on JavaScript and TypeScript too. You'll see that creating an API with TypeScript is actually harder than raw JavaScript because you'll have to deal with types that you'll set throughout your development.
The first step to create a Model WITH TypeScript is (again I'm assuming you're with your project already set):
- To create your types in types directory;
- You'll have to MANUALLY write your Migrations to set your Models; (see it on https://sequelize.org/docs/v7/models/defining-models/)
- MANUALLY, write your Seeders;
- Execute your Seeders to populate your DB's;
- Create your Models MANUALLY;
- And then, and then...
So, just by these steps, we can see it takes TIME. As developers, we are dealing with time also as a problem to be solved. My sincere opinion on using TypeScript with Express and Sequelize is: don't. It's too expensive! Think about the least effort law. Be a part of nature and use Sequelize-CLI with raw JavaScript. If you have any difficulties while doing it, I suggest studying a little bit more of primitive types in JavaScript. It gets into your mind like riding a bike. I promise.
Sincerely, Juan Pinheiro - WEB DEV.
Top comments (0)