forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirestoreDocument.js
More file actions
181 lines (154 loc) · 3.96 KB
/
FirestoreDocument.js
File metadata and controls
181 lines (154 loc) · 3.96 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }] */
/* globals isInt_, regexPath_, regexBinary_ */
/**
* Create a Firestore documents with the corresponding fields.
*
* @param {object} fields the document's fields
* @return {object} a Firestore document with the given fields
*/
function createFirestoreDocument_ (fields) {
const keys = Object.keys(fields)
const fieldsObj = keys.reduce(function (o, key) {
o[key] = wrapValue_(fields[key])
return o
}, {})
return {fields: fieldsObj}
}
/**
* Extract fields from a Firestore document.
*
* @param {object} firestoreDoc the Firestore document whose fields will be extracted
* @return {object} an object with the given document's fields and values
*/
function getFieldsFromFirestoreDocument_ (firestoreDoc) {
if (!firestoreDoc || !firestoreDoc['fields']) {
return {}
}
const fields = firestoreDoc['fields']
const keys = Object.keys(fields)
const object = keys.reduce(function (o, key) {
o[key] = unwrapValue_(fields[key])
return o
}, {})
return object
}
/**
* Unwrap the given document response's fields.
*
* @private
* @param docResponse the document response
* @return the document response, with unwrapped fields
*/
function unwrapDocumentFields_ (docResponse) {
if (docResponse.fields) {
docResponse.fields = getFieldsFromFirestoreDocument_(docResponse)
}
return docResponse
}
function wrapValue_ (value) {
const type = typeof (value)
switch (type) {
case 'string':
return wrapString_(value)
case 'object':
return wrapObject_(value)
case 'number':
return wrapNumber_(value)
case 'boolean':
return wrapBoolean_(value)
default: // error
return null
}
}
function unwrapValue_ (value) {
const type = Object.keys(value)[0]
value = value[type]
switch (type) {
case 'referenceValue':
case 'bytesValue':
case 'stringValue':
case 'booleanValue':
case 'doubleValue':
return value
case 'integerValue':
return parseInt(value)
case 'geoPointValue':
value = createFirestoreDocument_(value)
// Transform coordinates as mapValue object type
// fall through
case 'mapValue':
return getFieldsFromFirestoreDocument_(value)
case 'arrayValue':
return unwrapArray_(value['values'])
case 'timestampValue':
return new Date(value)
case 'nullValue':
default: // error
return null
}
}
function wrapString_ (string) {
// Test for root path reference inclusion (see Util.js)
if (regexPath_.test(string)) {
return wrapRef_(string)
}
// Test for binary data in string (see Util.js)
if (regexBinary_.test(string)) {
return wrapBytes_(string)
}
return {'stringValue': string}
}
function wrapObject_ (object) {
if (!object) {
return wrapNull_()
}
if (Array.isArray(object)) {
return wrapArray_(object)
}
if (object instanceof Date) {
return wrapDate_(object)
}
if (Object.keys(object).length === 2 && 'latitude' in object && 'longitude' in object) {
return wrapLatLong_(object)
}
return {'mapValue': createFirestoreDocument_(object)}
}
function wrapNull_ () {
return {'nullValue': null}
}
function wrapBytes_ (bytes) {
return {'bytesValue': bytes}
}
function wrapRef_ (ref) {
return {'referenceValue': ref}
}
function wrapNumber_ (num) {
if (isInt_(num)) {
return wrapInt_(num)
} else {
return wrapDouble_(num)
}
}
function wrapInt_ (int) {
return {'integerValue': int}
}
function wrapDouble_ (double) {
return {'doubleValue': double}
}
function wrapBoolean_ (boolean) {
return {'booleanValue': boolean}
}
function wrapDate_ (date) {
return {'timestampValue': date}
}
function wrapLatLong_ (latLong) {
return {'geoPointValue': latLong}
}
function wrapArray_ (array) {
const wrappedArray = array.map(wrapValue_)
return {'arrayValue': {'values': wrappedArray}}
}
function unwrapArray_ (wrappedArray) {
const array = (wrappedArray || []).map(unwrapValue_)
return array
}