node-firestore-import-export icon indicating copy to clipboard operation
node-firestore-import-export copied to clipboard

Nonexisiting parents should not be created after export/import

Open arthabus opened this issue 2 years ago • 4 comments
trafficstars

Context: Firestore has a concept of "non-exisitng" nodes in path

For example "collection/nonexsitingdoc/subcollection/doc1" It's possible to create a doc1 where "nonexsitingdoc" will not existing - in such case firestore UI in browser shows it as greyed out.

Expected behavior

Library doesn't create "nonexsisting" docs after export/import

Actual behavior

Library creates docs that are "nonexsisting" after export/import

Steps to reproduce the behavior

  1. Create a doc under the following path without creating intermediate docs/collections: "collection/nonexsitingdoc/subcollection/doc1"
  2. Export the whole collection
  3. Clean up database
  4. Import it back

My usecase

I have a collection of client with the counter in production like this:

Clients: users/{userId}/clients Client counter: users/{userId}/clients/-counter-doc-/-conter-col-/counter

Both -counter-doc- and -conter-col- don't exisit in the database which is expected

But after exporting and importing to the dev project (for debug and troubleshooting) the lib creates both -counter-doc- and -conter-col-

Now when I fetch client list it also returns -conter-col- document as a client which breaks the internal logic.

arthabus avatar Sep 18 '23 10:09 arthabus

Why need to keep that orphan child collection? This lib handles the "nonexsisting" document as an empty document. In JSON file, I believe there is no way to distinguish an empty parent document and a "nonexsisting" parent document.

cuong0993 avatar Sep 18 '23 12:09 cuong0993

@cuong0993 ok got you, firebase does distinguish them somehow thoughm, specifically in their admin UI.

I guess it should be possible to store a flag inside of that empty json and then skip creation of such docs/collections on import.

As to why, I've described my use case above, I basically store counter for the number of clients inside of the clients collection.

arthabus avatar Sep 18 '23 13:09 arthabus

Maybe you can change this line to check if documentData is empty the skip set document (all empty documents will be not existed) https://github.com/cuong0993/node-firestore-import-export/blob/7b98376476b3b70eb0609577e837342dec2a24ef/src/lib/import.ts#L89

cuong0993 avatar Sep 18 '23 14:09 cuong0993

@cuong0993 thanks bro! I'll give it a try

arthabus avatar Sep 19 '23 08:09 arthabus