質問編集履歴
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -98,7 +98,7 @@
|
|
98
98
|
|
99
99
|
return await admin.firestore() の行にポインタを置いてデバッグすると、1周目だと、
|
100
100
|
|
101
|
-
「path」という変数には、 (2) ['
|
101
|
+
「path」という変数には、 (2) ['users', 'user1'] という配列、
|
102
102
|
|
103
103
|
→これは、ルートノードからのパス構成を配列を持っている理解です。
|
104
104
|
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -120,6 +120,188 @@
|
|
120
120
|
|
121
121
|
|
122
122
|
|
123
|
+
### 参考コード(そのまま貼り付け)
|
124
|
+
|
125
|
+
```ここに言語を入力
|
126
|
+
|
127
|
+
const admin = require('../functions/node_modules/firebase-admin');
|
128
|
+
|
129
|
+
const serviceAccount = require("./service-key.json");
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
admin.initializeApp({
|
134
|
+
|
135
|
+
credential: admin.credential.cert(serviceAccount),
|
136
|
+
|
137
|
+
databaseURL: "https://<your-database-name>.firebaseio.com"
|
138
|
+
|
139
|
+
});
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
const data = require("./fakedb.json");
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
/**
|
148
|
+
|
149
|
+
* Data is a collection if
|
150
|
+
|
151
|
+
* - it has a odd depth
|
152
|
+
|
153
|
+
* - contains only objects or contains no objects.
|
154
|
+
|
155
|
+
*/
|
156
|
+
|
157
|
+
function isCollection(data, path, depth) {
|
158
|
+
|
159
|
+
if (
|
160
|
+
|
161
|
+
typeof data != 'object' ||
|
162
|
+
|
163
|
+
data == null ||
|
164
|
+
|
165
|
+
data.length === 0 ||
|
166
|
+
|
167
|
+
isEmpty(data)
|
168
|
+
|
169
|
+
) {
|
170
|
+
|
171
|
+
return false;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
for (const key in data) {
|
178
|
+
|
179
|
+
if (typeof data[key] != 'object' || data[key] == null) {
|
180
|
+
|
181
|
+
// If there is at least one non-object item in the data then it cannot be collection.
|
182
|
+
|
183
|
+
return false;
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
return true;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// Checks if object is empty.
|
198
|
+
|
199
|
+
function isEmpty(obj) {
|
200
|
+
|
201
|
+
for(const key in obj) {
|
202
|
+
|
203
|
+
if(obj.hasOwnProperty(key)) {
|
204
|
+
|
205
|
+
return false;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
return true;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
async function upload(data, path) {
|
218
|
+
|
219
|
+
return await admin.firestore()
|
220
|
+
|
221
|
+
.doc(path.join('/'))
|
222
|
+
|
223
|
+
.set(data)
|
224
|
+
|
225
|
+
.then(() => console.log(`Document ${path.join('/')} uploaded.`))
|
226
|
+
|
227
|
+
.catch(() => console.error(`Could not write document ${path.join('/')}.`));
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
/**
|
234
|
+
|
235
|
+
*
|
236
|
+
|
237
|
+
*/
|
238
|
+
|
239
|
+
async function resolve(data, path = []) {
|
240
|
+
|
241
|
+
if (path.length > 0 && path.length % 2 == 0) {
|
242
|
+
|
243
|
+
// Document's length of path is always even, however, one of keys can actually be a collection.
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// Copy an object.
|
248
|
+
|
249
|
+
const documentData = Object.assign({}, data);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
for (const key in data) {
|
254
|
+
|
255
|
+
// Resolve each collection and remove it from document data.
|
256
|
+
|
257
|
+
if (isCollection(data[key], [...path, key])) {
|
258
|
+
|
259
|
+
// Remove a collection from the document data.
|
260
|
+
|
261
|
+
delete documentData[key];
|
262
|
+
|
263
|
+
// Resolve a colleciton.
|
264
|
+
|
265
|
+
resolve(data[key], [...path, key]);
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
// If document is empty then it means it only consisted of collections.
|
274
|
+
|
275
|
+
if (!isEmpty(documentData)) {
|
276
|
+
|
277
|
+
// Upload a document free of collections.
|
278
|
+
|
279
|
+
await upload(documentData, path);
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
} else {
|
284
|
+
|
285
|
+
// Collection's length of is always odd.
|
286
|
+
|
287
|
+
for (const key in data) {
|
288
|
+
|
289
|
+
// Resolve each collection.
|
290
|
+
|
291
|
+
await resolve(data[key], [...path, key]);
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
resolve(data);
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
|
304
|
+
|
123
305
|
以上、ヒントになりうることのご教示だけでも大変ありがたい現状です。
|
124
306
|
|
125
307
|
何卒よろしくお願いいたします。
|