Duck-Tech-Dev / api-winnerduck

GNU General Public License v3.0
2 stars 0 forks source link

Local PostgreSQL Integration #7

Closed erthium closed 8 months ago

erthium commented 8 months ago

Moving forward #1

What are we doing with this PR?

This PR is for general integration of PostgreSQL package, connecting to an up and running local database and retrieving data from it.

In the script src/postgres/postgres.service.ts we are adding pg and connecting to a pool with the database information. Then defining a general function to send SQL queries to manipulate the database.

For specific tables, we will use different modules. This PR creates a user module and a service to send user information queries. These function will most probably be deleted in the future but left in the PR to show how it works.

About PostgreSQL

For easy local database creation, you can follow the guides in the following this and this page for Arch Linux.

Then create an .env file to add the information necessary as below:

database=test_db # database name
host=localhost # currently we are running on local
port=5432 # default for psql
user=<change_me> # user that has all the permissions for the selected db 
password=<change_me>

Then you can use the Makefile rules to create a database, a users table and add some random users.

Do not forget that your psql database server need to be running while working with it, use make start and make shut, hopefully that works on your machine as well...

What will we do after?

Currently we can access a local database without any ORM, to be able to complete DB journey, we need to:

Notes

After running db server and adding random users, you can run the nest app and go to localhost:3000/user/all to check if you can access databse in NestJS as well.

Please do not forget to yarn install before you start the day

Drink water as well.