質問編集履歴
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
### デバッグで確認できていること
|
50
50
|
return await admin.firestore() の行にポインタを置いてデバッグすると、1周目だと、
|
51
|
-
「path」という変数には、 (2) ['
|
51
|
+
「path」という変数には、 (2) ['users', 'user1'] という配列、
|
52
52
|
→これは、ルートノードからのパス構成を配列を持っている理解です。
|
53
53
|
「data」という変数には、{field1: 'foo', field2: 'bar'} というobjectが入っています。
|
54
54
|
→これが、登録したいデータの理解です。
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -59,5 +59,96 @@
|
|
59
59
|

|
60
60
|
…当該データにUpdateなりInsertなりが走ってほしいのです。
|
61
61
|
|
62
|
+
### 参考コード(そのまま貼り付け)
|
63
|
+
```ここに言語を入力
|
64
|
+
const admin = require('../functions/node_modules/firebase-admin');
|
65
|
+
const serviceAccount = require("./service-key.json");
|
66
|
+
|
67
|
+
admin.initializeApp({
|
68
|
+
credential: admin.credential.cert(serviceAccount),
|
69
|
+
databaseURL: "https://<your-database-name>.firebaseio.com"
|
70
|
+
});
|
71
|
+
|
72
|
+
const data = require("./fakedb.json");
|
73
|
+
|
74
|
+
/**
|
75
|
+
* Data is a collection if
|
76
|
+
* - it has a odd depth
|
77
|
+
* - contains only objects or contains no objects.
|
78
|
+
*/
|
79
|
+
function isCollection(data, path, depth) {
|
80
|
+
if (
|
81
|
+
typeof data != 'object' ||
|
82
|
+
data == null ||
|
83
|
+
data.length === 0 ||
|
84
|
+
isEmpty(data)
|
85
|
+
) {
|
86
|
+
return false;
|
87
|
+
}
|
88
|
+
|
89
|
+
for (const key in data) {
|
90
|
+
if (typeof data[key] != 'object' || data[key] == null) {
|
91
|
+
// If there is at least one non-object item in the data then it cannot be collection.
|
92
|
+
return false;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
return true;
|
97
|
+
}
|
98
|
+
|
99
|
+
// Checks if object is empty.
|
100
|
+
function isEmpty(obj) {
|
101
|
+
for(const key in obj) {
|
102
|
+
if(obj.hasOwnProperty(key)) {
|
103
|
+
return false;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
return true;
|
107
|
+
}
|
108
|
+
|
109
|
+
async function upload(data, path) {
|
110
|
+
return await admin.firestore()
|
111
|
+
.doc(path.join('/'))
|
112
|
+
.set(data)
|
113
|
+
.then(() => console.log(`Document ${path.join('/')} uploaded.`))
|
114
|
+
.catch(() => console.error(`Could not write document ${path.join('/')}.`));
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
*
|
119
|
+
*/
|
120
|
+
async function resolve(data, path = []) {
|
121
|
+
if (path.length > 0 && path.length % 2 == 0) {
|
122
|
+
// Document's length of path is always even, however, one of keys can actually be a collection.
|
123
|
+
|
124
|
+
// Copy an object.
|
125
|
+
const documentData = Object.assign({}, data);
|
126
|
+
|
127
|
+
for (const key in data) {
|
128
|
+
// Resolve each collection and remove it from document data.
|
129
|
+
if (isCollection(data[key], [...path, key])) {
|
130
|
+
// Remove a collection from the document data.
|
131
|
+
delete documentData[key];
|
132
|
+
// Resolve a colleciton.
|
133
|
+
resolve(data[key], [...path, key]);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
// If document is empty then it means it only consisted of collections.
|
138
|
+
if (!isEmpty(documentData)) {
|
139
|
+
// Upload a document free of collections.
|
140
|
+
await upload(documentData, path);
|
141
|
+
}
|
142
|
+
} else {
|
143
|
+
// Collection's length of is always odd.
|
144
|
+
for (const key in data) {
|
145
|
+
// Resolve each collection.
|
146
|
+
await resolve(data[key], [...path, key]);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
resolve(data);
|
151
|
+
```
|
152
|
+
|
62
153
|
以上、ヒントになりうることのご教示だけでも大変ありがたい現状です。
|
63
154
|
何卒よろしくお願いいたします。
|