forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead.js
138 lines (117 loc) · 4.71 KB
/
Read.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
/**
* Get the Firestore document or collection at a given path. If the collection
* contains enough IDs to return a paginated result, this method only
* returns the first page.
*
* @param {string} path the path to the document or collection to get
* @param {string} authToken an authentication token for reading from Firestore
* @param {string} projectId the Firestore project ID
* @return {object} the JSON response from the GET request
*/
function get_(path, authToken, projectId) {
return getPage_(path, projectId, authToken, null);
}
/**
* Get a page of results from the given path. If null pageToken
* is supplied, returns first page.
*/
function getPage_(path, projectId, authToken, pageToken) {
var baseUrl = "https://firestore.googleapis.com/v1beta1/projects/" + projectId + "/databases/(default)/documents/" + path;
const options = {
'muteHttpExceptions': true,
'headers': {'content-type': 'application/json', 'Authorization': 'Bearer ' + authToken}
};
if (pageToken) {
baseUrl += "?pageToken=" + pageToken;
options['pageToken'] = pageToken;
}
var responseObj = getObjectFromResponse_(UrlFetchApp.fetch(baseUrl, options));
checkForError_(responseObj);
return responseObj;
}
/**
* Get fields from a document.
*
* @param {string} path the path to the document
* @param {string} authToken an authentication token for reading from Firestore
* @param {string} projectId the Firestore project ID
* @return {object} an object mapping the document's fields to their values
*/
function getDocumentFields_(path, authToken, projectId) {
const doc = get_(path, authToken, projectId);
checkForError_(doc);
if (!doc["fields"]) {
throw new Error("No document with `fields` found at path " + path);
}
return getFieldsFromFirestoreDocument_(doc);
}
/**
* Get a list of the JSON responses received for getting documents from a collection.
*
* The items returned by this function are formatted as Firestore documents (with
* types). This is a helper method, not meant to return documents to a user of the
* library.
*
* @param {string} pathToCollection the path to the collection
* @param {string} authToken an authentication token for reading from Firestore
* @param {string} projectId the Firestore project ID
* @return {object} an array of Firestore document objects
*/
function getDocumentResponsesFromCollection_(pathToCollection, authToken, projectId) {
const initialResponse = get_(pathToCollection, authToken, projectId);
checkForError_(initialResponse);
if (!initialResponse["documents"]) {
return [];
}
const documentResponses = initialResponse["documents"];
// Get all pages of results if there are multiple
var pageResponse = initialResponse;
var pageToken = pageResponse["nextPageToken"];
while (pageToken) {
pageResponse = getPage_(pathToCollection, projectId, authToken, pageToken);
pageToken = pageResponse["nextPageToken"];
if (pageResponse["documents"]) {
addAll_(documentResponses, pageResponse["documents"]);
}
}
return documentResponses
}
/**
* Get a list of all IDs of the documents in a collection.
*
* @param {string} pathToCollection the path to the collection
* @param {string} authToken an authentication token for reading from Firestore
* @param {string} projectId the Firestore project ID
* @return {object} an array of IDs of the documents in the collection
*/
function getDocumentIds_(pathToCollection, authToken, projectId) {
const documentResponses = getDocumentResponsesFromCollection_(pathToCollection, authToken, projectId);
const ids = [];
// Create ID list from documents
for (var i = 0; i < documentResponses.length; i++) {
var document = documentResponses[i];
var path = document["name"];
var id = getIdFromPath_(path);
ids.push(id)
}
return ids;
}
/**
* Get a list of all documents (in field-value JSON format) in a collection.
*
* @param {string} pathToCollection the path to the collection
* @param {string} authToken an authentication token for reading from Firestore
* @param {string} projectId the Firestore project ID
* @return {object} an array of the documents in the collection
*/
function getDocuments_(pathToCollection, authToken, projectId) {
const documentResponses = getDocumentResponsesFromCollection_(pathToCollection, authToken, projectId);
const documents = [];
// Create ID list from documents
for (var i = 0; i < documentResponses.length; i++) {
var documentResponse = documentResponses[i];
var document = getFieldsFromFirestoreDocument_(documentResponse);
documents.push(document)
}
return documents;
}