indigotech / onboard-marcos-peter

0 stars 0 forks source link

[Track 3/9] Database setup #6

Open taki-tiler-server[bot] opened 2 years ago

taki-tiler-server[bot] commented 2 years ago

Step 1/4 - Containers everywhere

Estimated time: 2 hours

[Spoiler alert]: in the end of this onboard, you'll basically develop a CRUD of an entity called User. So, in order to store the user data in our app, let's create a real database.

The database we use on most of our projects is PostgresSQL, an open-source relational database.

Creating the database containers

There are a lot of setup so far right? In order to reduce the project setup time, we use Docker containers to create a database to work locally while developing. We'll create 2 databases: one for running server locally and another for (spoiler alert 2) the tests. Yeah, we will write tests. 🤓

First, make sure you have Docker installed on your machine. Just hit Command Space and type Docker. If not, you can download on their site.

  1. Create a docker-compose.yml file on your root project folder.
  2. Setup the two databases in the docker-compose.yml file. You can use this example if you want, just replace the values on environment fields.
  3. Run $ docker-compose up -d on terminal to mount your containers.

Note: Before run docker-compose commands, make sure that the docker is running on your mac. You can use again Command Space, type Docker and hit Enter.

Note 2: If you want to stop your containers, you can run $ docker-compose stop command in your project's root folder.

Marcos-Peter commented 2 years ago

Finish

taki-tiler-server[bot] commented 2 years ago

Step 2/4 - Using database's data

Estimated time: 3 hours

In this step, you'll connect your code with the database and create your first table using only Typescript 😎.

Connecting with the database

Do we have a library to access our database? Of course we do. For this, we use the TypeORM library. It helps us to make database operations (create tables, access data and so on) in a very simple way.

  1. Take some time to learn how this library works. It's super cool, but it's also a lot of abstraction.
  2. Follow their docs to add it to your project (remember that we're using postgres).
  3. Choose an option provided by typeorm to connect to your database. The credentials are the ones you set on docker-compose.yml.
  4. Try to connect with database before running the apollo-server start code. For now, you can connect only with your local database, leaving the test connection for later.

Note: don't forget to check if your docker container is running before connecting to the DB, otherwise it will fail.

Now you can ask: "Do they have a tool to manage the data inside the database without code?" Of course we do, young padawan. Actually, we recommend these two apps:

Feel free to choose one of these above, or any other app you want :D

Creating your first entity

After successfully connecting TypeORM to your database, it's time to see it working by creating your first table. Guess what? It will be called User (we told you so)

  1. Check on TypeORM docs how to create a TypeORM entity
  2. After creating the table, try creating a row (an user 🙆‍). You can do this either coding (it's possible) or directly on Tableplus, whatever you prefer.

Note 2: too much for one step? Don't get stuck and feel free to ask anything to any Taqtiler 👍

Open a Pull Request

Now open a pull request for your feature/setup-database branch.

Marcos-Peter commented 2 years ago

Finish

taki-tiler-server[bot] commented 2 years ago

Step 3/4 - Readme

Remember you created a README.md file. Now it's time to populate it. Fill the blank topics that were left before. Create a branch named docs/update-readme and fill in the info you left blank before. Open a pull request for this branch after finished.

  - Project's Name
  - Project's Description
  - Environment and tools *
  - Steps to run and debug *

TIP: an excellent README is that one document that is sufficient for any developer to be able to run and contribute to your project.

NOTE 2: For now on, you're going to create branches and Pull requests on your own. Pick good and clear branch names and try to open Pull Requests for one or only few steps. It's not a good practice to have too much code on a PR. 😅

Marcos-Peter commented 2 years ago

Finish

taki-tiler-server[bot] commented 2 years ago

Step 4/4 - Rebasing your branch

Here in Taqtile, before merging our revised (and approved) branches, we rebase it into the one we are attempting to merge. In this case, you'll rebase feature/setup-graphql to develop.

Too confusing? Here are some good links you can read about rebase:

Note: after your feature/setup-database is approved, you'll repeat this process.

The ideal setup we are trying to achieve with the rebases are something like below.:

git_flow

Marcos-Peter commented 2 years ago

Finish

taki-tiler-server[bot] commented 2 years ago

Click here for your next track