firecmsco / firecms

Awesome Firebase/Firestore-based CMS. The missing admin panel for your Firebase project!
https://firecms.co
Other
1.12k stars 184 forks source link

Set ID based off of other documents in the collection #611

Closed nachonavarro closed 3 months ago

nachonavarro commented 5 months ago

Hi!

I have a collection where I want the ID of each document to be a number that increases by 1 on each new document. So far I have a callback that does the following:

callbacks: buildEntityCallbacks({
    onPreSave: async ({ collection, values, status, context }) => {
      if (status === "new") {
        const query = { path: "questions", collection, orderBy: "id" };
        const docs = await context.dataSource.fetchCollection(query);
        if (docs.length > 0) {
          values.id = docs[docs.length - 1].id + 1;
        } else {
          values.id = 1;
        }
      }
      return values;
    },
})

Problem

This generates two IDs, the auto-generated one by Firestore and the one I generated. How can I only use the one from Firestore? I saw the onIdUpdate callback but it's synchronous and I don't think it's intended to fetch other documents.

Thanks!

fgatti675 commented 5 months ago

Hi Nacho, You are right, the onIdUpdate is sync only right now, and it is the method you need. Changing the id prop in the values does not change the id of the document, but the id value inside it (which probably you don't want) Adding a feature request: make onIdUpdate async. By the way, we are also based in Madrid, you should join our discord community :) https://discord.gg/fxy7xsQm3m

fgatti675 commented 3 months ago

I just realised onIdUpdate can be async as well if necessary, so I am closing this ticket