-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
39 lines (32 loc) · 1.29 KB
/
app.js
File metadata and controls
39 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const admin = require('firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
const CATEGORIES_KEY = 'categories';
const PROJECT_ID = process.env.PROJECT_ID;
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${PROJECT_ID}-default-rtdb.firebaseio.com`
});
const db = admin.firestore();
const categories = require('./categories.json');
const fetchCategories = async () => {
return await db.collection(CATEGORIES_KEY).get();
};
fetchCategories()
.then((snapshot) => {
let dbCategories = [];
snapshot.forEach(doc => {
dbCategories.push({ id: doc.id, ...doc.data() });
});
categories.forEach(jsonCategory => {
const dbCategoryFound = dbCategories.find(c => c.name === jsonCategory.name);
if (dbCategoryFound != null) {
const dbCategoryToUpdate = db.collection(CATEGORIES_KEY).doc(dbCategoryFound.id);
dbCategoryToUpdate.set(jsonCategory);
console.log(`Updated category ${jsonCategory.name}`);
} else {
let docRef = db.collection(CATEGORIES_KEY)
.add(jsonCategory);
console.log(`Added category ${jsonCategory.name}`);
}
});
});