Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nachonavarro opened this issue Jan 19, 2024 · 2 comments
Closed

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

nachonavarro opened this issue Jan 19, 2024 · 2 comments

Comments

@nachonavarro
Copy link

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
Copy link
Member

fgatti675 commented Feb 2, 2024

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants