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

Extracting collection/document path from document name #99

Closed
juliusn opened this issue Jul 9, 2020 · 5 comments
Closed

Extracting collection/document path from document name #99

juliusn opened this issue Jul 9, 2020 · 5 comments

Comments

@juliusn
Copy link

juliusn commented Jul 9, 2020

I often find myself in a situation where I have a Firestore document that I want to update or delete, but no other reference to the collection/name than Document.name, which doesn't work as is, so I wrote custom functions to extract these parts from the name string. Is this really necessary? Is there a built-in solution in the library for doing this, or if not, should there be?

function pathToRef_(path) {
  return path.match(/(?:[^\/]*\/|^)[^\/]*$/)[0];
}

const docs = fs.getDocuments("collection");

for (const doc in docs) {
  
  const data = {
    "key": value
  }
  
  fs.updateDocument(pathToRef_(docs[doc].name), data, true);
}
Library Version: 32
@LaughDonor
Copy link
Collaborator

I'm not sure I'm following accurately... Can you provide an example name and walk through the problem?

We have Util_.regexPath with the expression that is used by the method Util_.getDocumentFromPath(). Does this help at all?

@juliusn
Copy link
Author

juliusn commented Jul 9, 2020

For example if I have a collection of documents and I want to update a common property, calling updateDocument like so:

const docs = fs.getDocuments("collection");

docs.forEach(doc => fs.updateDocument(doc.name, { "key": "newVal" }, true));

throws an error saying Document name "[...]" lacks "/" at index [...]. If I want to get a working path for each document, I need to extract it from the Document.name property myself using string methods or regex match. I was just wondering if this is really necessary.

@LaughDonor
Copy link
Collaborator

Oh, I get it. Wouldn't you be able to do:

const collectionPath = "collection";
const docs = fs.getDocuments(collectionPath);
docs.forEach(doc => fs.updateDocument(`${collectionPath}/${doc.name}`, { "key": "newVal" }, true));

to accomplish the same without the extraction process?

@juliusn
Copy link
Author

juliusn commented Jul 9, 2020

Oh, I get it. Wouldn't you be able to do:

const collectionPath = "collection";
const docs = fs.getDocuments(collectionPath);
docs.forEach(doc => fs.updateDocument(`${collectionPath}/${doc.name}`, { "key": "newVal" }, true));

to accomplish the same without the extraction process?

That would create a completely new collection hierarchy under collection, because Document.name returns with the base path, like projects/project_id/databases/(default)/collection_name/document_name.

@LaughDonor
Copy link
Collaborator

LaughDonor commented Jul 9, 2020

Ok, I'll be adding a property to the Document that lets you get this data. In the meantime, you can use:

docs.forEach(doc => {
    const name = doc.name.match(/^projects\/.+?\/databases\/\(default\)\/documents\/(.+\/.+)$/)[1];
    fs.updateDocument(name, { "key": "newVal" }, true));
});

Where the RegEx is from Util_.regexPath.

LaughDonor added a commit that referenced this issue Jul 9, 2020
Update Tests to sync with Shields and Github caching.
Fix Tests that couldn't fail.
Update README and packages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants