Skip to content

Commit

Permalink
Sends copy of documents instead of deleting __collections__. Fixes #328
Browse files Browse the repository at this point in the history
  • Loading branch information
jloosli committed May 8, 2020
1 parent ab05bd8 commit 0b0b792
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const importData = (data: any,
throw new Error('Root or document reference doesn\'t contain a __collections__ property.');
}
const collections = dataToImport['__collections__'];
delete (dataToImport['__collections__']);
const collectionPromises: Array<Promise<any>> = [];
for (const collection in collections) {
if (collections.hasOwnProperty(collection)) {
Expand Down Expand Up @@ -53,9 +52,9 @@ const setDocuments = (data: ICollection, startingRef: FirebaseFirestore.Collecti
collection: data[documentKey]['__collections__'][collection],
});
});
delete (data[documentKey]['__collections__']);
}
const documentData: any = unserializeSpecialTypes(data[documentKey]);
const {__collections__, ...documents} = data[documentKey];
const documentData: any = unserializeSpecialTypes(documents);
batch.set(startingRef.doc(documentKey), documentData, {merge: mergeWithExisting});
});
return batch.commit();
Expand Down
8 changes: 7 additions & 1 deletion tests/import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ describe('Firestore Import', () => {
expect(true).to.be.true;
}
}
})
});

it(`should not manipulate imported data object`, async () => {
const dump = {name: 'Alice', __collections__: {jobs: {123: {name: 'doctor'}}}};
await firestoreImport({['id1']: dump}, mockFirestore.collection('user'));
expect(dump.__collections__.jobs['123'].name).to.eql('doctor');
});
});

0 comments on commit 0b0b792

Please sign in to comment.