forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead.js
More file actions
119 lines (110 loc) · 3.95 KB
/
Read.js
File metadata and controls
119 lines (110 loc) · 3.95 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
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
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }] */
/* globals FirestoreQuery_, getCollectionFromPath_, regexPath_, unwrapDocumentFields_ */
/**
* 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.
*
* @private
* @param {string} path the path to the document or collection to get
* @param {string} request the Firestore Request object to manipulate
* @return {object} the JSON response from the GET request
*/
function get_ (path, request) {
return getPage_(path, null, request)
}
/**
* Get a page of results from the given path.
* If null pageToken is supplied, returns first page.
*
* @private
* @param {string} path the path to the document or collection to get
* @param {string} request the Firestore Request object to manipulate
* @param {string} pageToken if defined, is utilized for retrieving subsequent pages
* @return {object} the JSON response from the GET request
*/
function getPage_ (path, pageToken, request) {
if (pageToken) {
request.addParam('pageToken', pageToken)
}
return request.get(path)
}
/**
* 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).
*
* @private
* @param {string} path the path to the collection
* @param {string} request the Firestore Request object to manipulate
* @return {object} an array of Firestore document objects
*/
function getDocumentResponsesFromCollection_ (path, request) {
const documents = []
var pageToken = null
do {
var pageResponse = getPage_(path, pageToken, request.clone())
pageToken = pageResponse.nextPageToken
if (pageResponse.documents) {
Array.prototype.push.apply(documents, pageResponse.documents)
}
} while (pageToken) // Get all pages of results if there are multiple
return documents
}
/**
* Get a list of all IDs of the documents in a collection.
* Works with nested collections.
*
* @private
* @param {string} path the path to the collection
* @param {string} request the Firestore Request object to manipulate
* @return {object} an array of IDs of the documents in the collection
*/
function getDocumentIds_ (path, request) {
const documents = query_(path, request).select().execute()
const ids = documents.map(function (doc) {
const ref = doc.name.match(regexPath_)[1] // Gets the doc name field and extracts the relative path
return ref.substr(path.length + 1) // Skip over the given path to gain the ID values
})
return ids
}
/**
* Get a document.
*
* @private
* @param {string} path the path to the document
* @param {string} request the Firestore Request object to manipulate
* @return {object} an object mapping the document's fields to their values
*/
function getDocument_ (path, request) {
const doc = get_(path, request)
if (!doc['fields']) {
throw new Error('No document with `fields` found at path ' + path)
}
return unwrapDocumentFields_(doc)
}
/**
* Set up a Query to receive data from a collection
*
* @private
* @param {string} path the path to the document or collection to query
* @param {string} request the Firestore Request object to manipulate
* @return {object} A FirestoreQuery object to set up the query and eventually execute
*/
function query_ (path, request) {
const grouped = getCollectionFromPath_(path)
const callback = function (query) {
// Send request to innermost document with given query
const responseObj = request.post(grouped[0] + ':runQuery', {
structuredQuery: query
})
// Filter out results without documents and unwrap document fields
const documents = responseObj.reduce(function (docs, fireDoc) {
if (fireDoc.document) {
docs.push(unwrapDocumentFields_(fireDoc.document))
}
return docs
}, [])
return documents
}
return new FirestoreQuery_(grouped[1], callback)
}