-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
123 lines (107 loc) · 2.73 KB
/
index.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
/*!
* firestore-bulk-loader
* Copyright(c) 2019 Marcos A. Vidolin de Lima
* MIT Licensed
*/
"use strict";
const admin = require('firebase-admin');
const csv = require('csvtojson');
/**
* Configures the Firestore object.
*
* @param {string} serviceAccount
* @returns {Object} firestore
*/
const configureFirestore = (serviceAccount) => {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
let firestore = admin.firestore();
firestore.settings({
timestampsInSnapshots: true
});
return firestore;
};
/**
* Converts the given data into valid documents to be imported.
*
* @param {*} data
* @returns documents
*/
const checkData = (data) => {
if (!Array.isArray(data)) {
throw new Error("Invalid data. You must inform an array.");
}
};
/**
* Converts CSV data into JSON.
*
* @param {*} data
*/
const convertCsvToJson = async (data) => {
return await csv().fromString(data);
};
/**
* Loads the data with a specific ID.
*
* @param {*} data
* @param {*} collectionKey
* @param {*} firestore
* @param {*} documentKeyProperty
*/
const loadDataWithId = (data, collectionKey, firestore, documentKeyProperty) => {
Object.keys(data).forEach(docKey => {
let document = data[docKey];
let id = document[documentKeyProperty];
firestore.collection(collectionKey).doc(id).set(document).then((ref) => {
// console.log('Added document with ID: ', id);
});
});
};
/**
* Loads the data without ID. Google Firestore will auto generate the ID.
*
* @param {*} documents
* @param {*} collectionKey
* @param {*} firestore
*/
const loadDataWithoutId = (documents, collectionKey, firestore) => {
Object.keys(documents).forEach(doc => {
firestore.collection(collectionKey).add(documents[doc]).then(ref => {
// console.log('Added document with ID: ', ref.id);
});
});
};
/**
* A simple Firestore loader.
*
* @param {*} data
* @param {*} collectionKey
* @param {*} serviceAccount
* @param {*} options
*/
const loadData = async (data, collectionKey, serviceAccount, options) => {
if (!data) {
throw new Error("No data to load.");
}
let opts = options || {};
const docKeyProperty = opts.documentKeyProperty;
const isCsv = opts.csv;
let documents = data;
if (isCsv) {
documents = await convertCsvToJson(data);
}
checkData(documents);
let firestore = configureFirestore(serviceAccount);
if (docKeyProperty) {
loadDataWithId(documents, collectionKey, firestore, docKeyProperty);
} else {
loadDataWithoutId(documents, collectionKey, firestore);
}
};
/**
* Exports.
*/
module.exports = {
load: loadData
};