Skip to content

Commit 864eecf

Browse files
committed
rebuilt db and added cloud function to get collection size: done
1 parent ff946b0 commit 864eecf

File tree

16 files changed

+2234
-121
lines changed

16 files changed

+2234
-121
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "ng-mosh-project"
4+
}
5+
}

section_19-project/project/db/actions.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,23 @@ function createCollection(payload) {
8888

8989
function createNestedCollection(obj, docRef, i, payload) {
9090
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-
})
91+
92+
for (let j = 0; j < payload.nestedCollectionParentIndex.length; j++) {
93+
const { id } = payload.collection[payload.nestedCollectionParentIndex[j]];
94+
95+
if (i === payload.nestedCollectionParentIndex[j]) {
96+
payload.nestedCollection[j].forEach((obj) => {
97+
payload.db.collection(`${payload.collectionPath}/${id}/${payload.nestedCollectionPath[j]}`)
98+
.add(obj)
99+
.then(() => {
100+
console.log("sub collection written: ", obj);
101+
})
102+
.catch((error) => {
103+
console.log('sub collection', error);
104+
})
105+
})
106+
107+
}
104108
}
105109
}
106110

@@ -131,5 +135,4 @@ function populateDB(payload) {
131135

132136
module.exports = {
133137
populate: populateDB
134-
}
135-
138+
}

section_19-project/project/db/init-db.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,25 @@ firebase.initializeApp({
1616

1717
const db = firebase.firestore();
1818

19-
const collections = [];
20-
21-
const coursePayload = {
22-
db: db,
23-
collection: vegetables.collection,
24-
collectionPath: 'vegetables',
25-
// nestedCollectionPath: 'lessons',
26-
// nestedCollection: vegetables.nestedCollection,
27-
// nestedCollectionParentIndex: 0,
28-
// targetObj: {
29-
// key: 'url',
30-
// value: 'serverless-angular'
31-
// }
32-
};
3319

3420
const categoriesPayload = {
3521
db: db,
3622
collection: categories.collection,
3723
collectionPath: 'categories',
24+
nestedCollectionPath: ['bread', 'vegetables'],
25+
nestedCollection: categories.nestedCollection,
26+
nestedCollectionParentIndex: [0, 4]
3827
};
3928

40-
const breadPayload = {
41-
db: db,
42-
collection: bread.collection,
43-
collectionPath: 'bread',
44-
};
29+
30+
firestore.populate(categoriesPayload);
31+
4532

4633
// UPLOAD TO FIRESTORE
47-
collections.push(coursePayload, categoriesPayload, breadPayload);
48-
collections.forEach((payloadCollection) => {
49-
firestore.populate(payloadCollection);
50-
});
34+
// collections.push(coursePayload, categoriesPayload, breadPayload);
35+
// collections.forEach((payloadCollection) => {
36+
// firestore.populate(payloadCollection);
37+
// });
5138

5239

5340

section_19-project/project/db/seeds/bread.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let collection = [
1+
let collectionBread = [
22
{
33
title: 'Pizza Slice',
44
imageUrl: 'https://www.dropbox.com/s/0dymh0nkdhccz6j/119627_01.jpg?raw=1',
@@ -38,38 +38,16 @@ let collection = [
3838
]
3939

4040

41-
collection = collection.map((item, index) => {
41+
collectionBread = collectionBread.map((item, index) => {
4242
return {
4343
...item,
4444
seqN: index + 1,
45-
categories: [
46-
{
47-
name: 'Bread',
48-
id: 1
49-
},
50-
{
51-
name: 'Dairy',
52-
id: 2
53-
},
54-
{
55-
name: 'Fruits',
56-
id: 3
57-
},
58-
{
59-
name: 'Seasoning and Spice',
60-
id: 4
61-
},
62-
{
63-
name: 'Vegetables',
64-
id: 5
65-
}
66-
]
6745
}
6846
})
6947

7048

7149
module.exports = {
72-
collection
50+
collectionBread
7351
}
7452

7553

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
1+
const { collectionVegetable } = require("./vegetables");
2+
const { collectionBread } = require("./bread");
3+
14
let collection = [
25
{
36
categoryName: 'Bread',
47
isActive: false,
8+
path: '',
9+
collectionSize: collectionBread.length
510
},
611
{
712
categoryName: 'Dairy',
813
isActive: false,
14+
path: '',
15+
collectionSize: 0
916
},
1017
{
1118
categoryName: 'Fruits',
1219
isActive: false,
20+
path: '',
21+
collectionSize: 0
1322
},
1423
{
1524
categoryName: 'Seasoning and Spice',
1625
isActive: false,
26+
path: '',
27+
collectionSize: 0
1728
},
1829
{
1930
categoryName: 'Vegetables',
2031
isActive: false,
32+
path: '',
33+
collectionSize: collectionVegetable.length
2134
},
22-
]
23-
24-
collection = collection.map((item, index) => {
25-
return {
26-
...item,
27-
seqN: index + 1
28-
}
29-
})
35+
];
3036

31-
// const nestedCollection = [];
37+
const nestedCollection = [
38+
collectionVegetable,
39+
collectionBread
40+
];
3241

3342
module.exports = {
34-
collection
43+
collection,
44+
nestedCollection
3545
}

section_19-project/project/db/seeds/vegetables.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let collection = [
1+
let collectionVegetable = [
22
{
33
title: 'Oaklands Selection Pointed Red Peppers ',
44
imageUrl: 'https://www.dropbox.com/s/3uvy83gkftwtmln/82632_01.jpg?raw=1',
@@ -32,38 +32,16 @@ let collection = [
3232
]
3333

3434

35-
collection = collection.map((item, index) => {
35+
collectionVegetable = collectionVegetable.map((item, index) => {
3636
return {
3737
...item,
3838
seqN: index + 1,
39-
categories: [
40-
{
41-
name: 'Bread',
42-
id: 1
43-
},
44-
{
45-
name: 'Dairy',
46-
id: 2
47-
},
48-
{
49-
name: 'Fruits',
50-
id: 3
51-
},
52-
{
53-
name: 'Seasoning and Spice',
54-
id: 4
55-
},
56-
{
57-
name: 'Vegetables',
58-
id: 5
59-
}
60-
]
6139
}
6240
})
6341

6442

6543
module.exports = {
66-
collection
44+
collectionVegetable
6745
}
6846

6947

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"functions": {
3+
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
4+
}
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Compiled JavaScript files
2+
**/*.js
3+
**/*.js.map
4+
5+
# Typescript v1 declaration files
6+
typings/
7+
8+
node_modules/

0 commit comments

Comments
 (0)