Skip to content

Commit fe7b696

Browse files
committed
uploading seed file to firestore db: done
1 parent 0e080ad commit fe7b696

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const firebase = require("firebase");
2+
// Required for side-effects
3+
require("firebase/firestore");
4+
5+
const firestore = require('./actions');
6+
const vegetables = require('./vegetables');
7+
8+
// Initialize Cloud Firestore through Firebase
9+
firebase.initializeApp({
10+
apiKey: "AIzaSyCOqWcUyqJnHKxdXlZluXoYoWkpabbTbdA",
11+
authDomain: "ng-mosh-project.firebaseapp.com",
12+
projectId: "ng-mosh-project",
13+
});
14+
15+
const db = firebase.firestore();
16+
17+
const coursePayload = {
18+
db: db,
19+
collection: vegetables.collection,
20+
collectionPath: 'vegetables',
21+
// nestedCollectionPath: 'lessons',
22+
// nestedCollection: vegetables.nestedCollection,
23+
// nestedCollectionParentIndex: 0,
24+
// targetObj: {
25+
// key: 'url',
26+
// value: 'serverless-angular'
27+
// }
28+
}
29+
30+
firestore.populate(coursePayload);
31+
32+
33+
34+
35+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const collection = [
2+
{
3+
title: 'Pizza Slice',
4+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/1950_01.jpg?raw=1',
5+
price: 0.69,
6+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
7+
category: 'Bakery'
8+
},
9+
{
10+
title: 'Italian Style Panini Roll',
11+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/6742_01.jpg?raw=1',
12+
price: 0.25,
13+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
14+
category: 'Bakery'
15+
},
16+
{
17+
title: 'French Baguette',
18+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/6817_01.jpg?raw=1',
19+
price: 0.69,
20+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
21+
category: 'Bakery'
22+
},
23+
{
24+
title: 'Maple and Pecan',
25+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/119627_01.jpg?raw=1',
26+
price: 0.49,
27+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
28+
category: 'Bakery'
29+
},
30+
{
31+
title: 'Double Chocolate Cookie',
32+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/121278_01.jpg?raw=1',
33+
price: 0.39,
34+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
35+
category: 'Bakery'
36+
},
37+
{
38+
title: 'All Butter Croisssant',
39+
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/5207811_01.jpg?raw=1',
40+
price: 0.37,
41+
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
42+
category: 'Bakery'
43+
},
44+
]
45+
46+
const nestedCollection = [
47+
48+
]
49+
50+
51+
52+
53+
module.exports = {
54+
collection,
55+
nestedCollection,
56+
}
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+

0 commit comments

Comments
 (0)