How can I add a future date and time to a firestore document and add a function when that time expires? For example today I create a document and record that time and then I want to register that fifteen days later the publicado
field will be false
. It's possible?
I am creating a document in firestore in the following way:
component.ts
import { Component, OnInit } from '@angular/core';
import { FirebaseService } from '../../../services/firebase.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
forma: FormGroup;
constructor( fb: FormBuilder, private fs: FirebaseService ) {
this.forma = fb.group ({
fechaCreacion: [ firebase.firestore.FieldValue.serverTimestamp() ],
publicado: [ true ],
})
}
agregarAviso() {
this.fs.addAviso(this.forma.value);
}
firebase.service.ts
constructor( private afs: AngularFirestore ) {}
addAviso(aviso){
this.afs.collection('avisos').add(aviso);
}
As a result I get the following:
Firestore doesn't have a concept of a TTL (time to live) for documents. Documents exist forever until some code directly deletes them.
You could write some code to periodically query for documents that should be expired, and you will have to accept that they may finally be deleted after the timestamp, by up to the amount of time your process runs periodically. You will likely want to run this on a backend you control, with a scheduler that you control, using the Firebase Admin SDK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With