kossnocorp / typesaurus

🦕 Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
412 stars 34 forks source link

[Feature Request] ~"addIfNew"() #97

Closed SrBrahma closed 2 years ago

SrBrahma commented 3 years ago

If it already exists, won't do nothing, no error is thrown. Else, the doc is created.

Here is how I am doing it right now, with transaction.

It just check if it already exists, if not, set().

I ain't sure if the !data?.data is required or just !data would do it.

const userAuth = await auth.getUser(caller.uid);

await transaction(
  ({ get }) => get(users, caller.uid),
  ({ data, set }) => {
    if (!data?.data) {
      return set(users, caller.uid, {
        // myData
      });
    }
  },
);
SrBrahma commented 2 years ago

I am no longer interested in this as my code has changed.