Last active
March 31, 2022 22:00
-
-
Save FelixEhuan/daf216842098cd626046b2dd4c1a9805 to your computer and use it in GitHub Desktop.
Angular Firestore boilerplate service for basic CRUD operations
This file contains hidden or 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
import { Injectable } from '@angular/core'; | |
import { AngularFirestoreCollection, AngularFirestoreDocument, AngularFirestore, DocumentReference, DocumentSnapshot } from '@angular/fire/firestore'; | |
import { Productos } from '../interfaces'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class ProductosService { | |
/** Colecciones */ | |
public productosCollection: AngularFirestoreCollection<any>; | |
/** Documentos */ | |
public productoDocument: AngularFirestoreDocument<any>; | |
constructor( private afs: AngularFirestore ) { | |
// Obten la colección con todos las clientes: | |
this.productosCollection = this.getCollection('productos'); | |
} | |
/** Método que devuelve la colección de referencia que le es asignado */ | |
getCollection(reference: string): AngularFirestoreCollection<any> { | |
return this.afs.collection(`${reference}`, | |
(ref) => ref.orderBy('createdOn', 'desc')); | |
} | |
/** ASYNC Crea un nuevo cliente con un uid generado automáticamente. */ | |
async create(data: any): Promise<any> { | |
data.createdOn = new Date(); | |
const ref = await this.productosCollection.add(data); | |
data.id = ref.id; | |
await this.update(data); | |
return this.get(data.id); | |
} | |
/** Obtiene cliente de forma no destructiva, usa el método getCliente. */ | |
get(id: string): Promise<any> { | |
return this.productosCollection.doc(id).get().toPromise(); | |
} | |
/** Actualizar cliente de forma no destructiva, usa el método getCliente. */ | |
update(data: any): Promise<void> { | |
return this.productosCollection.doc(data.id).update(data); | |
} | |
/** Borrar un producto por id */ | |
delete(id: string): Promise<void> { | |
return this.productosCollection.doc(id).delete(); | |
} | |
// ------------------------------------------------------------------- | |
// Sección de Batch para borrado múltiple y creación múltiple | |
// ------------------------------------------------------------------- | |
/** Batch para set productos */ | |
BatchSet(data: Array<Productos>): Promise<void> { | |
const batch = this.afs.firestore.batch(); | |
data.forEach(producto => { | |
batch.set(this.afs.firestore.collection(`productos`).doc(producto.id), producto); | |
}); | |
return batch.commit(); | |
} | |
/** Batch para actualizar productos */ | |
BatchUpdate(productos: Array<Productos>): Promise<void> { | |
const batch = this.afs.firestore.batch(); | |
productos.forEach(producto => { | |
batch.update(this.afs.firestore.collection(`productos`).doc(producto.id), producto); | |
}); | |
return batch.commit(); | |
} | |
/** En lugar de borrado se cambia el estatus a deleted */ | |
BatchDelete(productos: Array<Productos>): Promise<void> { | |
const batch = this.afs.firestore.batch(); | |
productos.forEach( producto => { | |
batch.delete(this.afs.firestore.collection(`productos`).doc(producto.id)); | |
}); | |
return batch.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment