Skip to content

Instantly share code, notes, and snippets.

@molavec
Created March 20, 2024 16:21
Show Gist options
  • Save molavec/f7ec23cff3e71134e461d91eea0c64be to your computer and use it in GitHub Desktop.
Save molavec/f7ec23cff3e71134e461d91eea0c64be to your computer and use it in GitHub Desktop.
import FirestoreManager, { type FirestoreDataType } from "./FirestoreManager";
export enum ImageType {
IMAGE,
THUMB
}
export interface ImageDataType {
name: string,
fullPath?: string,
bucket?: string,
order?: number,
type?: ImageType
}
export interface ArtworkType extends FirestoreDataType {
name: string,
userId: string,
images?: ImageDataType[]
description?: string,
dimensions?: string,
authorId?: string,
ownerId?: string,
ownerType?: string,
price?: number,
discount?: number,
}
const DB_NAME = 'artworks';
export default class ArtworkManager extends FirestoreManager<ArtworkType> {
static instance: ArtworkManager;
constructor(dbName: string) {
if (ArtworkManager.instance) {
return ArtworkManager.instance;
}
super(dbName);
ArtworkManager.instance = this;
}
// Método estático para obtener la instancia
static getInstance() {
if (!ArtworkManager.instance) {
ArtworkManager.instance = new ArtworkManager(DB_NAME);
}
return ArtworkManager.instance;
}
}
import { firebaseApp } from "../../../config/firebase";
import { getFirestore, doc, addDoc, connectFirestoreEmulator, collection, updateDoc, Firestore, getDoc, deleteDoc } from "firebase/firestore";
// Initialize Cloud Firestore and get a reference to the service
function getDiffProps(obj1: Record<string, any>, obj2: Record<string, any>) {
const result: Record<string, any> = {};
Object.keys(obj2).forEach(key => {
if (obj1[key] !== obj2[key]) {
result[key] = obj2[key];
}
});
return result;
}
export interface FirestoreDataType {
id?: string;
// [x: string]: unknown;
}
export default class FirestoreManager <DocType extends FirestoreDataType> {
dbName!: string;
docState!: DocType | FirestoreDataType;
db!: Firestore;
constructor(dbName: string) {
this.db = getFirestore(firebaseApp);
// Emulator in dev
if (import.meta.env.DEV === true)
connectFirestoreEmulator(this.db, 'localhost', 5002);
this.dbName = dbName;
this.docState = {};
}
// Método para crear un nuevo elemento en Firestore
async create<DocType>(item: Object ) {
// Asumiendo que 'this.db' es una referencia a tu colección en Firestore
if (this.db && this.dbName) {
const docRef = await addDoc(collection(this.db, this.dbName), item );
this.docState = { id:docRef.id, ...item }
return this.docState as DocType;
} else {
return undefined;
}
}
// Obtiene los datos actuales
get() {
return this.docState;
}
// Método para obtener datos de la base de datos y actualizar el estado local
async fetch<DocType>(id: string) {
// Lógica para obtener datos de Firebase
// Supongamos que obtenemos un objeto data desde Firebase
const docRef = doc(this.db, this.dbName, id);
const docSnap = await getDoc(docRef);
// docSnap.data() will be undefined in this case
if (docSnap.exists()) {
this.docState = { id:docRef.id, ...docSnap.data() };
return this.docState as DocType;
} else {
return undefined;
}
}
// Método para actualizar el estado local y la base de datos solo con los cambios
async update(updatedValues: Object) {
// Aquí iría la lógica para actualizar la base de datos
if(this.docState?.id !== undefined) {
// TODO verificar que retorna el docRef
const docRef = await updateDoc(
doc( this.db, this.dbName, this.docState.id ),
getDiffProps(this.docState, updatedValues)
);
return docRef;
} else {
return undefined;
}
}
// Método para eliminar un elemento en Firestore
async delete(id: string) {
await deleteDoc(doc(this.db, this.dbName, id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment