firebase - Cloud Functions(TypeScript) - Cum să folosești modele personalizate în loc de hărți/dicționare înnăscute


Am creat modele personalizate pentru utilizatori și postări în funcțiile mele cloud după cum urmează(models.ts):

export class UserModel {
    picture?: string;
    fullName: string;
    totalPostsCount?: number;
    recentPosts?: PostModel[];

    constructor(userMap: any) {   //the documentations usually had specific properties for the constructor instead of a map/dictionary. I thought this would be better. Am I wrong?
        this.fullName = userMap.fullName;
        this.picture = userMap.picture;
        this.totalPostsCount = userMap.totalPostsCount;
        this.recentPosts = userMap.recentPosts;     //since this is another Custom Model, do we need to convert userMap.recentPosts dictionary array to PostModel[] here?
    }
}

export class PostModel {
    title: string;
    postDate: admin.firestore.Timestamp;   //is this the right type for a date object in cloud functions? there is also a FirebaseFirestore.Timestamp
    creator_user: UserModel;

    constructor(postMap: any) {
        this.title = postMap.title;
        this.creator_user = postMap.creator_user;   //since this is another Custom Model, do we need to convert postMap.creator_user dictionary to UserModel here?
    }
}

În loc să lucrez cu obiecte de tip map/dictionary din documentSnapshots de la Firestore, vreau să completez aceste modele. În acest fel pot manipula câmpurile într-un mod mai puțin predispus la erori.

Am aflat despre FirestoreDataConverter care presupus ajută la această problemă. Există un tutorial despre Obiecte Personalizate în documentația Firestore care arată o implementare diferită. Am copiat codurile din linkurile de mai jos pentru comoditate:

class Post {
  constructor(readonly title: string, readonly author: string) {}

  toString(): string {
    return this.title + ', by ' + this.author;
  }
}

const postConverter = {
  toFirestore(post: Post): firebase.firestore.DocumentData {
    return {title: post.title, author: post.author};
  },
  fromFirestore(
    snapshot: firebase.firestore.QueryDocumentSnapshot,
    options: firebase.firestore.SnapshotOptions
  ): Post {
    const data = snapshot.data(options)!;
    return new Post(data.title, data.author);
  }
};

const postSnap = await firebase.firestore()
  .collection('posts')
  .withConverter(postConverter)
  .doc().get();
const post = postSnap.data();
if (post !== undefined) {
  post.title; // string
  post.toString(); // Should be defined
  post.someNonExistentProperty; // TS error
}

Și;

class City {
    constructor (name, state, country ) {
        this.name = name;
        this.state = state;
        this.country = country;
    }
    toString() {
        return this.name + ', ' + this.state + ', ' + this.country;
    }
}

    // Firestore data converter
  cityConverter = {
      toFirestore: function(city) {
          return {
              name: city.name,
              state: city.state,
              country: city.country
              }
      },
      fromFirestore: function(snapshot, options){
          const data = snapshot.data(options);
          return new City(data.name, data.state, data.country)
      }
  }

 // Set with cityConverter
db.collection("cities").doc("LA")
  .withConverter(cityConverter)
  .set(new City("Los Angeles", "CA", "USA"));

Ambele creează erori în codul meu Cloud Functions care este scris în TypeScript. Întrebarea mea este dacă FirestoreDataConverter este cea mai bună metodă pentru asta? Care sunt opțiunile? Poate cineva să posteze modalitatea corectă de a modela o relație utilizator<->post pentru a fi folosită în cloud functions? (ținând cont că ambele modele au celălalt model ca proprietăți și de asemenea ce s-ar întâmpla dacă mapările de pe server au mapări în interior?)