Skip to content

⚙️ Connecting the Rest-API to the database (PostgreSQL 🐘 in Docker 🐳) using Prisma lib.

Notifications You must be signed in to change notification settings

imerso-io/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