-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (116 loc) · 3.22 KB
/
app.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const firebase = require('firebase-admin')
const fs = require('fs')
firebase.initializeApp({
credential: firebase.credential.cert(require('./service-account')),
})
const firestore = firebase.firestore()
const mode = process.argv[2]
async function deleteCollection(collectionPath, batchSize = 25) {
const collectionRef = firestore.collection(collectionPath)
const query = collectionRef.orderBy('__name__').limit(batchSize)
return new Promise((resolve, reject) => {
deleteQueryBatch(query, resolve).catch(reject)
})
}
async function deleteQueryBatch(query, resolve) {
const snapshot = await query.get()
const batchSize = snapshot.size
if (batchSize === 0) {
// When there are no documents left, we are done
resolve()
return
}
// Delete documents in a batch
const batch = firestore.batch()
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref)
})
await batch.commit()
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(query, resolve)
})
}
const downloadJson = async () => {
const devices = (await firestore.collection('devices').get()).docs.map(
(doc) => {
const { type, ...device } = doc.data()
return device
}
)
const rooms = (await firestore.collection('rooms').get()).docs.map((doc) => {
const { name } = doc.data()
return name
})
const sourceTypes = {}
const sourceTypeDocs = (
await firestore
.collection('devices')
.doc('definitions')
.collection('sourceTypes')
.get()
).docs
sourceTypeDocs.forEach((doc) => {
sourceTypes[doc.id] = doc.data()
})
const targetTypes = {}
const targetTypeDocs = (
await firestore
.collection('devices')
.doc('definitions')
.collection('targetTypes')
.get()
).docs
targetTypeDocs.forEach((doc) => {
targetTypes[doc.id] = doc.data().entries
})
fs.writeFileSync(
'data.json',
JSON.stringify({ rooms, devices, sourceTypes, targetTypes })
)
}
const uploadJson = async () => {
await deleteCollection('devices')
await deleteCollection('rooms')
await deleteCollection('devices/definitions/sourceTypes')
await deleteCollection('devices/definitions/targetTypes')
const { devices, rooms, sourceTypes, targetTypes } = require('./data.json')
for (const room of rooms) {
await firestore.collection('rooms').add({ name: room })
}
for (const device of devices) {
const type = sourceTypes[device.sourceType].targetType
await firestore.collection('devices').add({ ...device, type })
}
for (const [name, targetType] of Object.entries(targetTypes)) {
await firestore
.collection('devices')
.doc('definitions')
.collection('targetTypes')
.doc(name)
.set({ entries: targetType })
}
for (const [name, sourceType] of Object.entries(sourceTypes)) {
await firestore
.collection('devices')
.doc('definitions')
.collection('sourceTypes')
.doc(name)
.set(sourceType)
}
await firestore
.collection('settings')
.doc('firestore-sync')
.update({ restart: Date.now() })
}
switch (mode) {
case '--download': {
downloadJson()
break
}
case '--upload': {
uploadJson()
break
}
}