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

transaction function not available on repos #38

Open
hschk opened this issue Oct 6, 2023 · 5 comments
Open

transaction function not available on repos #38

hschk opened this issue Oct 6, 2023 · 5 comments

Comments

@hschk
Copy link

hschk commented Oct 6, 2023

In the docs it says that every repo provides a transaction function, but it seems that it's not there.
Am I doing something wrong, or are the docs not up to date?

@dominicbartl
Copy link
Member

Hi @hschk

That's correct, the docs aren't up-to-date with transactions. They've been made independent of repos. Here's an example:

await firestore.runTransaction(async (t) => {
  const trx = new FirestoreTransaction(firestore, t);
  const user = await trx.get(userRepo, { userId: '123' });
  trx.update(userRepo, { id: user.id, email: '[email protected]' });
});

Depending on your setup you can use this helper function to run transactions

function runTransaction(cb: (trx: FirestoreTransaction) => Promise<any>) {
  return firestore.runTransaction((trx) => {
    return cb(new FirestoreTransaction(firestore, trx));
  });
}

If you have anymore questions, just let me know

@hschk
Copy link
Author

hschk commented Oct 6, 2023

Hi @dominicbartl

Thank you very much for your answer!
However, I still have a problem with the create method of the firestore-storage transaction.

const repo = new Retail7TransactionRepository();

await db.runTransaction(async (t) => {
  const tx = new FirestoreTransaction(db, t);
  const transactionDoc = await tx.find(repo, {id: transactionId});

  if (transactionDoc) {
    throw new TransactionExistsError();
  }

  const repoTransaction = {
    ...payload,
    id: transactionId,
  };

  tx.create(repo, repoTransaction);
});

The create method requires an argument for 'ids'. Why and how do I have to pass this? With the "normal" create method of the repo I don't have to pass this.

@dominicbartl
Copy link
Member

If your collection is a root collection and not a subcollection, it could be that the typing is not correct. Try passing undefined or null as ids

@hschk
Copy link
Author

hschk commented Oct 13, 2023

Yes, it does work with undefined, even though that doesn't feel perfect ^^ Why would the typing not be correct? Did I configure something wrong here?

I have another question, that is more important to me: Is it somehow possible to query a FirestoreTransaction not only by id but as "normal" query like you can do on a normal firestore-storage repo?
It's even weird that the function name is "find" on the Transaction and not findById like it is on a normal repo. So I would need something like this:

const tx = new FirestoreTransaction(db, t);
const transactionDoc = await tx.find(repo, {someField: 'value'});

@dominicbartl
Copy link
Member

@hschk thanks for your patience. No, you didn't configure anything wrong, it's just a typing issue with the package itself.

Regarding your seconds question: You're actually correct. The transaction methods should match the repo methods. I deprecated find() and get() and added findById() and getById(). I also added the query() method

const accounts = await trx.query(
  accountRepo,
  (qb) => {
    return qb.whereAll({
      someField: 'value',
    });
  },
  undefined
);

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