Skip to content

Instantly share code, notes, and snippets.

@guiseek
Last active March 30, 2020 01:44
Show Gist options
  • Save guiseek/9dd0f185e81b1602f543a187f6987a19 to your computer and use it in GitHub Desktop.
Save guiseek/9dd0f185e81b1602f543a187f6987a19 to your computer and use it in GitHub Desktop.
Firebase útils
@Inj.....
export class ClientService {
//....
async saveMeasures({ id, ...data }: PatientMeasures) {
let path = `${this.document.ref.path}/measures`;
path = id ? `${path}/${id}` : path;
const date = this.timenow;
if (isColl(path)) {
return this.afs.collection(path)
.add({ ...data, created: date });
} else {
return this.afs.doc(path)
.set(
{ ...data, updated: date },
{ merge: true }
);
}
}
// ....
}
this.service.document
    .snapshotChanges().pipe(
      map(snapLoad),
      map(snapDate('birthday'))
    )```
import { toDate } from '@vibe/shared/util/formatting';
export const snapLoad = ({ payload }) => ({ ...payload.data(), id: payload.id });
export const snapDate = <T = any>(column: string) => {
return (payload: T): T => {
return ({ ...payload, [column]: toDate(payload[column]) });
}
}
export const isColl = (path: string) => {
return path.split('/').filter(v => v).length % 2;
}
import { firestore } from 'firebase/app';
export const toDate = (value: Date | firestore.Timestamp): Date => {
if (!value) return;
return value instanceof firestore.Timestamp ? value.toDate() : value;
};
export const orderBy = (prop: string | firebase.firestore.FieldPath, order: firebase.firestore.OrderByDirection) => {
return (ref: CollectionReference) => ref.orderBy(prop, order)
}
// Uso
this.afs.collection<Interface>('path', orderBy('date', 'desc')).valueChanges();
import { DatePipe } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { toDate } from '@vibe/shared/util/formatting';
@Pipe({
name: 'toDate'
})
export class ToDatePipe implements PipeTransform {
datePipe = new DatePipe('pt-br');
transform(value: any, arg: string = null): string {
return this.datePipe.transform(
toDate(value),
!!arg ? arg : 'mediumDate'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment