Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

โš™๏ธ Connecting the Rest-API to the database (PostgreSQL ๐Ÿ˜ in Docker ๐Ÿณ) using Prisma lib.

Notifications You must be signed in to change notification settings

somosimerso/rest-api-database-connection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ‡ง๐Ÿ‡ท

Conectando a API Rest com o Banco de Dados ๐Ÿ—ƒ๏ธ

Siga os requisitos para que a aplicaรงรฃo rode em sua mรกquina:

1. Instale a biblioteca NodeJS em: https://nodejs.org/en/download

2. Na pasta "restAPI-database-connection" abra um terminal (CLI) e instale os pacotes necessรกrios:

- npm i express

- npm i cors

3. Baixe DBeaver ou outro sistema para gerenciar o banco de dados, e confira se sua tabela estรก recebendo dados.

- Saiba como criar o banco de dados pelo DBeaver clicando aqui.

4. Instale o Docker (https://docs.docker.com/)

5. No terminal, verifique a versรฃo do seu Docker digitando: docker -v

6. Modificar o arquivo chamado: docker-compose.yml colocando uma senha (ou se preferir pode manter a mesma configuraรงรฃo)

7. Com uma conta criada no Docker, vรก no terminal e digite: docker compose up

8. Instale e inicialize as bibliotecas necessรกrias para o Prisma:

- npm install prisma --save-dev

- npx prisma

- npx prisma init

9. Vรก na pasta prisma, e em seguida acesse o arquivo schema.prisma

10. Crie seu model a partir desse cรณdigo:

// Setup
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

// Model
model users {
  id        Int      @id @default(autoincrement())
  name      String   @db.VarChar(80)
  email     String   @db.VarChar(100) @unique
}

11. Vรก no arquivo .env e altere a URL alterando seus dados como no exemplo a seguir:

- DATABASE_URL = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"

- Em nosso caso ficaria: DATABASE_URL="postgresql://postgres:restAPI-ino@localhost:5433/users?schema=public" (Sendo o USER o nome que vocรช define no DBeaver).

12. Crie seu prรณprio migrate dessa maneira (O migrate vai criar seu model como uma tabela no PostgreSQL automaticamente, entenda mais sobre clicando aqui.):

- npx prisma migrate dev --nome-do-seu-commit init

13. Realize os seguintes comandos:

- npx prisma db push

- npx prisma db pull

Sobre Mรฉtodos HTTP e RestAPI

Saiba mais.

DOCS

๐Ÿ‡บ๐Ÿ‡ธ/๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ

Connecting Rest API with PostgreSQL database ๐Ÿ—ƒ๏ธ

Follow the requirements for the application to run on your machine:

1. Install the NodeJS library from: https://nodejs.org/en/download

2. in the folder "restAPI-database-connection" open a terminal (CLI) and install the necessary packages:

- npm i express

- npm i cors

3. Download DBeaver or another system to manage the database, and check if your table is receiving data

- Learn how to create the database by DBeaver by clicking here.

4. Install Docker (https://docs.docker.com/)

5. In the terminal (CLI), check your Docker version by typing:: docker -v

6. Modify the file called: docker-compose.yml putting a password (or if you prefer you can keep the same configuration)

7. With an account created in Docker, go to the terminal and type: docker compose up

8. Install and initialize the necessary libraries for Prisma:

- npm install prisma --save-dev

- npx prisma

- npx prisma init

9. Go to the prisma folder, and then access the schema.prisma file.

10. Create your model from this code:

// Setup
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

// Model
model users {
  id        Int      @id @default(autoincrement())
  name      String   @db.VarChar(80)
  email     String   @db.VarChar(100) @unique
}

11. Go to the .env file and change the URL by changing its data as in the following example:

- DATABASE_URL = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"

- In our case it would be: DATABASE_URL="postgresql://postgres:restAPI-ino@localhost:5433/users?schema=public" (USER being the name you define in DBeaver).

12. Create your own migrate like this:

- npx prisma migrate dev --your-commit-name init

13. Run the following commands:

- npx prisma db push

- npx prisma db pull

Tecnologias usadas

About

โš™๏ธ Connecting the Rest-API to the database (PostgreSQL ๐Ÿ˜ in Docker ๐Ÿณ) using Prisma lib.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published