-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finishing api details and db connections with postgres
- Loading branch information
1 parent
d1ecede
commit faca2f9
Showing
12 changed files
with
208 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PGHOST='' | ||
PGDATABASE='' | ||
PGUSER='' | ||
PGPASSWORD='' | ||
ENDPOINT_ID='' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Projeto Node.js - CRUD simples sobre videos | ||
|
||
Este projeto foi desenvolvido como parte de um curso simples de Node.js, visando a prática e aprimoramento dos conhecimentos na linguagem. | ||
|
||
## Configuração do Banco de Dados | ||
|
||
Dentro do projeto, oferecemos a flexibilidade de utilizar um banco de dados local. Para isso, basta realizar a substituição no arquivo `server.js`: | ||
|
||
```javascript | ||
const db = new DatabasePostgress(); | ||
``` | ||
|
||
por | ||
|
||
```javascript | ||
const db = new DatabaseMemory(); | ||
``` | ||
|
||
## Testando as Rotas | ||
|
||
Caso deseje testar as rotas, recomendamos o uso da extensão "REST Client" do VSCode. Você pode interagir com as rotas utilizando o arquivo denominado `routes.http`. | ||
|
||
Ao lidar com rotas que possuem identificadores (IDs), lembre-se de substituir no arquivo `routes.http` o `:id` pelo ID gerado ao criar um vídeo. | ||
|
||
## Rota de Listagem com Parâmetro de Busca | ||
|
||
Destacamos que a rota de listagem de vídeos conta com um parâmetro de busca, utilizando a sintaxe `search=`. Para testar, basta adicionar um título após o sinal de igual. | ||
|
||
Exemplo: | ||
|
||
```http | ||
GET http://localhost:3000/videos?search=SeuTituloAqui | ||
``` | ||
|
||
Este projeto oferece uma oportunidade prática de manipulação de rotas em Node.js, permitindo a exploração e aprendizado da linguagem de forma interativa. Sinta-se à vontade para experimentar e personalizar conforme suas necessidades de aprendizado. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sql from "./db.js"; | ||
|
||
sql` | ||
CREATE TABLE videos ( | ||
id TEXT PRIMARY KEY, | ||
title TEXT, | ||
description TEXT, | ||
duration INTEGER | ||
); | ||
`.then(() => { | ||
console.log("Tabala criada!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { randomUUID } from "crypto"; | ||
import sql from "./db.js"; | ||
|
||
export class DatabasePostgress { | ||
async list(search) { | ||
let videos; | ||
if (search) { | ||
videos = await sql`select * from videos where title ilike ${ | ||
"%" + search + "%" | ||
}`; | ||
} else { | ||
videos = await sql`select * from videos`; | ||
} | ||
|
||
return videos; | ||
} | ||
|
||
async create(video) { | ||
const videoId = randomUUID(); | ||
const { title, description, duration } = video; | ||
await sql`INSERT into videos (id, title, description, duration) VALUES (${videoId}, ${title}, ${description}, ${duration})`; | ||
} | ||
|
||
async update(id, video) { | ||
const { title, description, duration } = video; | ||
|
||
await sql`UPDATE videos set title = ${title}, description = ${description}, duration = ${duration} WHERE id = ${id}`; | ||
} | ||
|
||
async delete(id) { | ||
await sql`DELETE from videos WHERE id = ${id}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import postgres from "postgres"; | ||
import "dotenv/config"; | ||
|
||
let { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env; | ||
|
||
const sql = postgres({ | ||
host: PGHOST, | ||
database: PGDATABASE, | ||
username: PGUSER, | ||
password: PGPASSWORD, | ||
port: 5432, | ||
ssl: "require", | ||
connection: { | ||
options: `project=${ENDPOINT_ID}`, | ||
}, | ||
}); | ||
|
||
async function getPgVersion() { | ||
const result = await sql`select version()`; | ||
console.log(result); | ||
} | ||
|
||
getPgVersion(); | ||
|
||
export default sql; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters