-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.js
More file actions
48 lines (39 loc) · 1.56 KB
/
import.js
File metadata and controls
48 lines (39 loc) · 1.56 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
40
41
42
43
44
45
46
47
48
const admin = require('firebase-admin');
// Replace with the path where `credentials.json` is located
const serviceAccount = require('/path/to/your/credentials.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
async function importData(documentIds, collectionPath, subCollections, subDocuments) {
try {
const collectionRef = db.collection(collectionPath);
for (const docId of documentIds) {
const docRef = collectionRef.doc(docId);
// Create the document with no data
await docRef.set({});
// Create empty sub-documents under sub-collections
for (const subDoc of subDocuments) {
const subDocRef = docRef.collection(subDoc.subCollection).doc(subDoc.docId);
await subDocRef.set({});
}
}
console.log('Import completed successfully!');
} catch (error) {
console.error('Import failed:', error);
}
}
const collectionPath = 'participants';
// Add a list of prolific IDs here
const documentIds = [
'ProlificID1',
'ProlificID2',
'ProlificID3'
];
const subCollections = ['Day1', 'Day2', 'Day3']; // Array of sub-collection names
const subDocuments = [
{ subCollection: 'Day1', docId: 'questionnaire' },
{ subCollection: 'Day2', docId: 'questionnaire' },
{ subCollection: 'Day3', docId: 'questionnaire' }
]; // Array of sub-document names and their corresponding sub-collection names
importData(documentIds, collectionPath, subCollections, subDocuments);