forked from MDxWARRIORxOP/node-firestore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (140 loc) · 4.01 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const firebase = require("firebase-admin/app");
const firestore = require("firebase-admin/firestore");
let app;
let db;
/**
* @typedef {Object} firebaseConfig
* @property {String} type
* @property {String} project_id
* @property {String} private_key_id
* @property {String} private_key
* @property {String} client_email
* @property {String} client_id
* @property {String} auth_uri
* @property {String} token_uri
* @property {String} auth_provider_x509_cert_url
* @property {String} client_x509_cert_url
*/
/**
* @param {firebaseConfig} config Your Firebase service account config
*/
const initialize = async (config) => {
if (!app || !db) {
app = await firebase.initializeApp({
credential: firebase.cert(config),
});
db = await firestore.getFirestore(app);
}
};
/**
* @param {String} collectionName name of the collection to add data to.
* @param {String} docName name of the document to add data to.
* @param {{}} dataObj JS object, the data to add to the document.
* @returns {Boolean} true if successful or false
* @example await addData("users", "test", {hello: "world"})
**/
const addData = async (collectionName, docName, dataObj) => {
if (!app || !db) {
throw new Error(
"Firebase is not initialized. Please initialize firebase before trying to access any of its service."
);
}
try {
const dataRef = await db.collection(collectionName).doc(docName);
await dataRef.set(dataObj);
return true;
} catch (error) {
console.error(error);
return false;
}
};
/**
* @returns data if all is successful, or false
* @param {String} collectionName name of the collection to get a doc from.
* @param {String} docName name of the doc ot get data from.
* @example const data = await getData("users", "Kingerious")
**/
const getData = async (collectionName, docName) => {
if (!app || !db) {
throw new Error(
"Firebase is not initialized. Please initialize firebase before trying to access any of its service."
);
}
try {
const dataRef = db.collection(collectionName).doc(docName);
const doc = await dataRef.get();
if (!doc.exists) {
return false;
} else {
return doc.data();
}
} catch (error) {
console.error(error);
return false;
}
};
/**
* @returns {Boolean} true if all is successful, or false
* @param {String} collectionName The name of the collection
* @param {String} docName The name of teh document
* @example await deleteData("cities", "LA")
* @remarks Doesn't delete sub collections.
*/
const deleteData = async (collectionName, docName) => {
if (!app || !db) {
throw new Error(
"Firebase is not initialized. Please initialize firebase before trying to access any of its service."
);
}
try {
await db.collection(collectionName).doc(docName).delete();
return true;
} catch (error) {
console.error(error);
return false;
}
};
const deleteBatches = async (query) => {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
return true;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
process.nextTick(() => {
deleteBatches(query);
});
};
/**
* @param {String} collectionName The name of the collection to delete
* @param {Number} batchSize The size of the batches to delete
* @returns {Boolean} true is successful or false
*/
const deleteCollection = async (collectionName, batchSize) => {
if (!app || !db) {
throw new Error(
"Firebase is not initialized. Please initialize firebase before trying to access any of its service."
);
}
try {
const collectionRef = await db.collection(collectionName);
const query = await collectionRef.orderBy("__name__").limit(batchSize);
await deleteBatches(query);
return true;
} catch (error) {
console.error(error);
return false;
}
};
module.exports = {
initialize,
addData,
getData,
deleteData,
deleteCollection,
};