|
| 1 | +// _____________________________Removing Data_____________________________ |
| 2 | +function deleteCollection(db, collectionPath, batchSize) { |
| 3 | + let collectionRef = db.collection(collectionPath); |
| 4 | + let query = collectionRef.orderBy('__name__').limit(batchSize); |
| 5 | + |
| 6 | + return new Promise((resolve, reject) => { |
| 7 | + deleteQueryBatch(db, query, resolve, reject); |
| 8 | + }); |
| 9 | +} |
| 10 | + |
| 11 | +function deleteQueryBatch(db, query, resolve, reject) { |
| 12 | + query.get() |
| 13 | + .then((snapshot) => { |
| 14 | + // When there are no documents left, we are done |
| 15 | + if (snapshot.size === 0) { |
| 16 | + return 0; |
| 17 | + } |
| 18 | + |
| 19 | + // Delete documents in a batch |
| 20 | + let batch = db.batch(); |
| 21 | + snapshot.docs.forEach((doc) => { |
| 22 | + batch.delete(doc.ref); |
| 23 | + }); |
| 24 | + |
| 25 | + return batch.commit().then(() => { |
| 26 | + return snapshot.size; |
| 27 | + }); |
| 28 | + }).then((numDeleted) => { |
| 29 | + if (numDeleted === 0) { |
| 30 | + resolve(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Recurse on the next process tick, to avoid |
| 35 | + // exploding the stack. |
| 36 | + process.nextTick(() => { |
| 37 | + deleteQueryBatch(db, query, resolve, reject); |
| 38 | + }); |
| 39 | + }) |
| 40 | + .catch(reject); |
| 41 | +} |
| 42 | + |
| 43 | +function removeNestedCollection(payload) { |
| 44 | + const collectionRef = payload.db.collection(payload.collectionPath); |
| 45 | + let responseWithId = []; |
| 46 | + |
| 47 | + collectionRef |
| 48 | + .get() |
| 49 | + .then((res) => { |
| 50 | + responseWithId = res.docChanges().map((el) => { |
| 51 | + return { |
| 52 | + ...el.doc.data(), |
| 53 | + id: el.doc.id |
| 54 | + } |
| 55 | + }) |
| 56 | + return responseWithId; |
| 57 | + }) |
| 58 | + .finally(() => { |
| 59 | + const obj = findObjwithNestedCollection(responseWithId, payload); |
| 60 | + deleteCollection(payload.db, `${payload.collectionPath}/${obj.id}/${payload.nestedCollectionPath}`, 500); |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +function findObjwithNestedCollection(array, payload) { |
| 65 | + return array.find((item) => { |
| 66 | + return item[payload.targetObj.key] === payload.targetObj.value; |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +// __________________________Inserting Documents__________________________ |
| 71 | + |
| 72 | +function createCollection(payload) { |
| 73 | + payload.collection.forEach((obj, i) => { |
| 74 | + payload.db.collection(payload.collectionPath) |
| 75 | + .add(obj) |
| 76 | + .then((docRef) => { |
| 77 | + console.log("Document written with ID: ", docRef.id); |
| 78 | + |
| 79 | + if (payload.nestedCollectionPath) { |
| 80 | + createNestedCollection(obj, docRef, i, payload); |
| 81 | + } |
| 82 | + |
| 83 | + }).catch((error) => { |
| 84 | + console.error("Error adding document: ", error); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +function createNestedCollection(obj, docRef, i, payload) { |
| 90 | + obj.id = docRef.id; |
| 91 | + const { id } = payload.collection[payload.nestedCollectionParentIndex]; |
| 92 | + if (i === 0) { |
| 93 | + |
| 94 | + payload.nestedCollection.forEach((obj) => { |
| 95 | + payload.db.collection(`${payload.collectionPath}/${id}/${payload.nestedCollectionPath}`) |
| 96 | + .add(obj) |
| 97 | + .then(() => { |
| 98 | + console.log("sub collection written: ", obj); |
| 99 | + }) |
| 100 | + .catch((error) => { |
| 101 | + console.log('sub collection', error); |
| 102 | + }) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +function populateDB(payload) { |
| 109 | + const collectionRef = payload.db.collection(payload.collectionPath); |
| 110 | + |
| 111 | + collectionRef |
| 112 | + .get() |
| 113 | + .then((response) => { |
| 114 | + if (!response.empty) { |
| 115 | + |
| 116 | + if (payload.nestedCollectionPath) { |
| 117 | + removeNestedCollection(payload); |
| 118 | + } |
| 119 | + |
| 120 | + deleteCollection(payload.db, payload.collectionPath, 500) |
| 121 | + .then(() => { |
| 122 | + createCollection(payload); |
| 123 | + }); |
| 124 | + |
| 125 | + } else { |
| 126 | + createCollection(payload); |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +module.exports = { |
| 133 | + populate: populateDB |
| 134 | +} |
| 135 | + |
0 commit comments