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

Fixed batchExecutor to not be useless #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/lib/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ const clearData = async (startingRef: admin.firestore.Firestore |
};

const clearCollections = async (startingRef: admin.firestore.Firestore | FirebaseFirestore.DocumentReference, logs = false) => {
const collectionPromises: Array<Promise<any>> = [];
const collectionPromises: Array<() => Promise<any>> = [];
const collectionsSnapshot = await safelyGetCollectionsSnapshot(startingRef, logs);
collectionsSnapshot.map((collectionRef: FirebaseFirestore.CollectionReference) => {
collectionPromises.push(clearDocuments(collectionRef, logs));
collectionPromises.push(() => clearDocuments(collectionRef, logs));
});
return batchExecutor(collectionPromises);
};

const clearDocuments = async (collectionRef: FirebaseFirestore.CollectionReference, logs = false) => {
logs && console.log(`Retrieving documents from ${collectionRef.path}`);
const allDocuments = await safelyGetDocumentReferences(collectionRef, logs);
const documentPromises: Array<Promise<object>> = [];
const documentPromises: Array<() => Promise<object>> = [];
allDocuments.forEach((docRef: DocumentReference) => {
documentPromises.push(clearCollections(docRef, logs));
documentPromises.push(docRef.delete());
documentPromises.push(() => clearCollections(docRef, logs));
documentPromises.push(() => docRef.delete());
});
return batchExecutor(documentPromises);
};
Expand Down
16 changes: 8 additions & 8 deletions src/lib/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const exportData = async (startingRef: admin.firestore.Firestore |
FirebaseFirestore.DocumentReference |
FirebaseFirestore.CollectionReference, logs = false) => {
if (isLikeDocument(startingRef)) {
const collectionsPromise = getCollections(startingRef, logs);
let dataPromise: Promise<any>;
const collectionsPromise = () => getCollections(startingRef, logs);
let dataPromise: () => Promise<any>;
if (isRootOfDatabase(startingRef)) {
dataPromise = Promise.resolve({});
dataPromise = () => Promise.resolve({});
} else {
dataPromise = (<FirebaseFirestore.DocumentReference>startingRef).get()
dataPromise = () => (<FirebaseFirestore.DocumentReference>startingRef).get()
.then(snapshot => snapshot.data())
.then(data => serializeSpecialTypes(data));
}
Expand All @@ -31,11 +31,11 @@ const exportData = async (startingRef: admin.firestore.Firestore |

const getCollections = async (startingRef: admin.firestore.Firestore | FirebaseFirestore.DocumentReference, logs = false) => {
const collectionNames: Array<string> = [];
const collectionPromises: Array<Promise<any>> = [];
const collectionPromises: Array<() => Promise<any>> = [];
const collectionsSnapshot = await safelyGetCollectionsSnapshot(startingRef, logs);
collectionsSnapshot.map((collectionRef: FirebaseFirestore.CollectionReference) => {
collectionNames.push(collectionRef.id);
collectionPromises.push(getDocuments(collectionRef, logs));
collectionPromises.push(() => getDocuments(collectionRef, logs));
});
const results = await batchExecutor(collectionPromises);
const zipped: any = {};
Expand All @@ -48,11 +48,11 @@ const getCollections = async (startingRef: admin.firestore.Firestore | FirebaseF
const getDocuments = async (collectionRef: FirebaseFirestore.CollectionReference, logs = false) => {
logs && console.log(`Retrieving documents from ${collectionRef.path}`);
const results: any = {};
const documentPromises: Array<Promise<object>> = [];
const documentPromises: Array<() => Promise<object>> = [];
const allDocuments = await safelyGetDocumentReferences(collectionRef, logs);
allDocuments.forEach((doc) => {

documentPromises.push(new Promise(async (resolve) => {
documentPromises.push(() => new Promise(async (resolve) => {
const docSnapshot = await doc.get();
const docDetails: any = {};
if (docSnapshot.exists) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/firestore-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const isRootOfDatabase = (ref: admin.firestore.Firestore |

const sleep = (timeInMS: number): Promise<void> => new Promise(resolve => setTimeout(resolve, timeInMS));

const batchExecutor = async function <T>(promises: Promise<T>[], batchSize: number = 50) {
const batchExecutor = async function <T>(promiseGenerators: (() => Promise<T>)[], batchSize: number = 50) {
const res: T[] = [];
while (promises.length > 0) {
const temp = await Promise.all(promises.splice(0, batchSize));
res.push(...temp);
while (promiseGenerators.length > 0) {
const promises = promiseGenerators.splice(0, batchSize).map(generator => generator());
res.push(...await Promise.all(promises));
}
return res;
};
Expand Down
8 changes: 4 additions & 4 deletions src/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const importData = (data: any,
throw new Error('Root or document reference doesn\'t contain a __collections__ property.');
}
const collections = dataToImport['__collections__'];
const collectionPromises: Array<Promise<any>> = [];
const collectionPromises: Array<() => Promise<any>> = [];
for (const collection in collections) {
if (collections.hasOwnProperty(collection)) {
collectionPromises.push(setDocuments(collections[collection], startingRef.collection(collection), mergeWithExisting, logs));
collectionPromises.push(() => setDocuments(collections[collection], startingRef.collection(collection), mergeWithExisting, logs));
}
}
if (isRootOfDatabase(startingRef)) {
Expand Down Expand Up @@ -57,12 +57,12 @@ const setDocuments = (data: ICollection, startingRef: FirebaseFirestore.Collecti
const documentData: any = unserializeSpecialTypes(documents);
batch.set(startingRef.doc(documentKey), documentData, {merge: mergeWithExisting});
});
return batch.commit();
return () => batch.commit();
});
return batchExecutor(chunkPromises)
.then(() => {
return collections.map((col) => {
return setDocuments(col.collection, col.path, mergeWithExisting, logs);
return () => setDocuments(col.collection, col.path, mergeWithExisting, logs);
});
})
.then(subCollectionPromises => batchExecutor(subCollectionPromises))
Expand Down
6 changes: 3 additions & 3 deletions tests/firestore-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ describe('Firestore Helpers', () => {
};

it('should resolve lists smaller then the batchsize', async () => {
const input = [toPromise(1), toPromise(2)];
const input = [() => toPromise(1), () => toPromise(2)];
let actual = await batchExecutor(input, 3);
expect(actual).to.eql([1, 2]);
});

it('should resolve lists equal to the batchsize', async () => {
const input = [toPromise(1), toPromise(2)];
const input = [() => toPromise(1), () => toPromise(2)];
let actual = await batchExecutor(input, 1);
expect(actual).to.eql([1, 2]);
});

it('should resolve lists larger then the batchsize', async () => {
const input = [toPromise(1), toPromise(2)];
const input = [() => toPromise(1), () => toPromise(2)];
let actual = await batchExecutor(input, 1);
expect(actual).to.eql([1, 2]);
});
Expand Down