|
|
|
**Modelo Conceitual do Banco de Dados**
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
**Introdução ao Cloud Firestore**
|
|
|
|
|
|
|
|
Cloud Firestore é um banco de dados NoSQL em tempo real oferecido pelo Firebase. Diferente de bancos relacionais, ele organiza os dados em coleções e documentos, permitindo consultas flexíveis e escalabilidade automática.
|
|
|
|
|
|
|
|
**Estrutura de Dados**
|
|
|
|
|
|
|
|
_Coleções_: Contêm documentos relacionados a um mesmo contexto.
|
|
|
|
|
|
|
|
_Documentos_: Unidades individuais de armazenamento de dados (JSON-like).
|
|
|
|
|
|
|
|
_Subcoleções_: Coleções dentro de documentos para dados hierárquicos.
|
|
|
|
|
|
|
|
_Campos_: Propriedades dentro de um documento.
|
|
|
|
|
|
|
|
**Estrutura do Banco de Dados do Projeto**
|
|
|
|
|
|
|
|
`User
|
|
|
|
{
|
|
|
|
email (string) PK
|
|
|
|
experience (number)
|
|
|
|
last_lesson_concluded (number)
|
|
|
|
level (number)
|
|
|
|
money (number)
|
|
|
|
notifications (boolean)
|
|
|
|
pet (reference)
|
|
|
|
}`
|
|
|
|
|
|
|
|
`Pet
|
|
|
|
{
|
|
|
|
id (string) PK
|
|
|
|
name (string)
|
|
|
|
color (string)
|
|
|
|
type (string) *Enum "Gato" ou "Cachorro"
|
|
|
|
items(array)(map)
|
|
|
|
is_active (boolean)
|
|
|
|
item_id (reference)
|
|
|
|
quantity (number)
|
|
|
|
well-being (map)
|
|
|
|
clean (number)
|
|
|
|
fun (number)
|
|
|
|
hunger (number)
|
|
|
|
thirst (number)
|
|
|
|
}`
|
|
|
|
|
|
|
|
`Item
|
|
|
|
{
|
|
|
|
id (string) PK
|
|
|
|
image (string) *Link da imagem no bucket
|
|
|
|
name (string)
|
|
|
|
price (number)
|
|
|
|
}`
|
|
|
|
|
|
|
|
`Minigame
|
|
|
|
{
|
|
|
|
id (string) PK
|
|
|
|
given_experience (number)
|
|
|
|
given_money (number)
|
|
|
|
name (string)
|
|
|
|
}`
|
|
|
|
|
|
|
|
`Lesson
|
|
|
|
{
|
|
|
|
id (string) PK
|
|
|
|
concluded (boolean)
|
|
|
|
name (string)
|
|
|
|
questions (array)(reference)
|
|
|
|
}`
|
|
|
|
|
|
|
|
`Question
|
|
|
|
{
|
|
|
|
id (string) PK
|
|
|
|
answers (array)(string)
|
|
|
|
description (string) *Pergunta
|
|
|
|
right_answer (number) *Index da resposta correta
|
|
|
|
}`
|
|
|
|
|
|
|
|
**Como Utilizar o Firestore no Código**
|
|
|
|
|
|
|
|
Para acessar e manipular os dados no Firestore, utilizamos a SDK do Firebase para Typescript (no caso do projeto).
|
|
|
|
|
|
|
|
**Instalação**
|
|
|
|
|
|
|
|
`npm install firebase`
|
|
|
|
|
|
|
|
**Configuração do Cloud Firestore**
|
|
|
|
|
|
|
|
```
|
|
|
|
import { initializeApp } from "firebase/app";
|
|
|
|
import { getFirestore, doc, setDoc, getDoc, collection, addDoc } from "firebase/firestore";
|
|
|
|
|
|
|
|
const firebaseConfig = {
|
|
|
|
apiKey: "SUA_API_KEY",
|
|
|
|
authDomain: "SEU_DOMINIO.firebaseapp.com",
|
|
|
|
projectId: "SEU_PROJECT_ID",
|
|
|
|
storageBucket: "SEU_BUCKET.appspot.com",
|
|
|
|
messagingSenderId: "SENDER_ID",
|
|
|
|
appId: "SUA_APP_ID"
|
|
|
|
};
|
|
|
|
|
|
|
|
const app = initializeApp(firebaseConfig);
|
|
|
|
const db = getFirestore(app);
|
|
|
|
```
|
|
|
|
|
|
|
|
**_Criar um Usuário_**
|
|
|
|
|
|
|
|
```
|
|
|
|
const createUser = async (email, money, level, experience, notifications) => {
|
|
|
|
await setDoc(doc(db, "User", email), {
|
|
|
|
money,
|
|
|
|
level,
|
|
|
|
experience,
|
|
|
|
notifications
|
|
|
|
});
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
**_Obter um Usuário_**
|
|
|
|
|
|
|
|
```
|
|
|
|
const getUser = async (email) => {
|
|
|
|
const userRef = doc(db, "User", email);
|
|
|
|
const userSnap = await getDoc(userRef);
|
|
|
|
|
|
|
|
if (userSnap.exists()) {
|
|
|
|
console.log("Usuário encontrado:", userSnap.data());
|
|
|
|
} else {
|
|
|
|
console.log("Nenhum usuário encontrado.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
``` |