firebase-rest-firestore 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +526 -66
- package/dist/client.d.ts +234 -0
- package/dist/client.js +434 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -1
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -15,6 +15,18 @@ export declare class FirestoreClient {
|
|
|
15
15
|
* 認証トークンを取得(キャッシュあり)
|
|
16
16
|
*/
|
|
17
17
|
private getToken;
|
|
18
|
+
/**
|
|
19
|
+
* コレクションリファレンスを取得
|
|
20
|
+
* @param path コレクションパス
|
|
21
|
+
* @returns CollectionReferenceインスタンス
|
|
22
|
+
*/
|
|
23
|
+
collection(path: string): CollectionReference;
|
|
24
|
+
/**
|
|
25
|
+
* ドキュメントリファレンスを取得
|
|
26
|
+
* @param path ドキュメントパス
|
|
27
|
+
* @returns DocumentReferenceインスタンス
|
|
28
|
+
*/
|
|
29
|
+
doc(path: string): DocumentReference;
|
|
18
30
|
/**
|
|
19
31
|
* Firestoreにドキュメントを作成
|
|
20
32
|
* @param collectionName コレクション名
|
|
@@ -60,6 +72,228 @@ export declare class FirestoreClient {
|
|
|
60
72
|
id: string;
|
|
61
73
|
})[]>;
|
|
62
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* コレクションリファレンスクラス
|
|
77
|
+
*/
|
|
78
|
+
export declare class CollectionReference {
|
|
79
|
+
private client;
|
|
80
|
+
private path;
|
|
81
|
+
private _queryConstraints;
|
|
82
|
+
constructor(client: FirestoreClient, path: string);
|
|
83
|
+
/**
|
|
84
|
+
* ドキュメントリファレンスを取得
|
|
85
|
+
* @param documentPath ドキュメントID(省略時は自動生成)
|
|
86
|
+
* @returns DocumentReferenceインスタンス
|
|
87
|
+
*/
|
|
88
|
+
doc(documentPath?: string): DocumentReference;
|
|
89
|
+
/**
|
|
90
|
+
* ドキュメントを追加(IDは自動生成)
|
|
91
|
+
* @param data ドキュメントデータ
|
|
92
|
+
* @returns 作成されたドキュメントのリファレンス
|
|
93
|
+
*/
|
|
94
|
+
add(data: Record<string, any>): Promise<DocumentReference>;
|
|
95
|
+
/**
|
|
96
|
+
* フィルター条件を追加
|
|
97
|
+
* @param fieldPath フィールドパス
|
|
98
|
+
* @param opStr 演算子
|
|
99
|
+
* @param value 値
|
|
100
|
+
* @returns Queryインスタンス
|
|
101
|
+
*/
|
|
102
|
+
where(fieldPath: string, opStr: string, value: any): Query;
|
|
103
|
+
/**
|
|
104
|
+
* 並べ替え条件を追加
|
|
105
|
+
* @param fieldPath フィールドパス
|
|
106
|
+
* @param directionStr 並べ替え方向('asc'または'desc')
|
|
107
|
+
* @returns Queryインスタンス
|
|
108
|
+
*/
|
|
109
|
+
orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
|
|
110
|
+
/**
|
|
111
|
+
* 取得件数の制限を設定
|
|
112
|
+
* @param limit 最大件数
|
|
113
|
+
* @returns Queryインスタンス
|
|
114
|
+
*/
|
|
115
|
+
limit(limit: number): Query;
|
|
116
|
+
/**
|
|
117
|
+
* スキップ件数を設定
|
|
118
|
+
* @param offset スキップ件数
|
|
119
|
+
* @returns Queryインスタンス
|
|
120
|
+
*/
|
|
121
|
+
offset(offset: number): Query;
|
|
122
|
+
/**
|
|
123
|
+
* クエリを実行
|
|
124
|
+
* @returns QuerySnapshotインスタンス
|
|
125
|
+
*/
|
|
126
|
+
get(): Promise<QuerySnapshot>;
|
|
127
|
+
/**
|
|
128
|
+
* ランダムなIDを生成
|
|
129
|
+
* @returns ランダムなID
|
|
130
|
+
*/
|
|
131
|
+
private _generateId;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* ドキュメントリファレンスクラス
|
|
135
|
+
*/
|
|
136
|
+
export declare class DocumentReference {
|
|
137
|
+
private client;
|
|
138
|
+
private collectionPath;
|
|
139
|
+
private docId;
|
|
140
|
+
constructor(client: FirestoreClient, collectionPath: string, docId: string);
|
|
141
|
+
/**
|
|
142
|
+
* ドキュメントIDを取得
|
|
143
|
+
*/
|
|
144
|
+
get id(): string;
|
|
145
|
+
/**
|
|
146
|
+
* ドキュメントのパスを取得
|
|
147
|
+
*/
|
|
148
|
+
get path(): string;
|
|
149
|
+
/**
|
|
150
|
+
* サブコレクションを取得
|
|
151
|
+
* @param collectionPath サブコレクション名
|
|
152
|
+
* @returns CollectionReferenceインスタンス
|
|
153
|
+
*/
|
|
154
|
+
collection(collectionPath: string): CollectionReference;
|
|
155
|
+
/**
|
|
156
|
+
* ドキュメントを取得
|
|
157
|
+
* @returns DocumentSnapshotインスタンス
|
|
158
|
+
*/
|
|
159
|
+
get(): Promise<DocumentSnapshot>;
|
|
160
|
+
/**
|
|
161
|
+
* ドキュメントを作成または上書き
|
|
162
|
+
* @param data ドキュメントデータ
|
|
163
|
+
* @param options オプション(mergeは現在サポートされていません)
|
|
164
|
+
* @returns WriteResultインスタンス
|
|
165
|
+
*/
|
|
166
|
+
set(data: Record<string, any>, options?: {
|
|
167
|
+
merge?: boolean;
|
|
168
|
+
}): Promise<WriteResult>;
|
|
169
|
+
/**
|
|
170
|
+
* ドキュメントを更新
|
|
171
|
+
* @param data 更新データ
|
|
172
|
+
* @returns WriteResultインスタンス
|
|
173
|
+
*/
|
|
174
|
+
update(data: Record<string, any>): Promise<WriteResult>;
|
|
175
|
+
/**
|
|
176
|
+
* ドキュメントを削除
|
|
177
|
+
* @returns WriteResultインスタンス
|
|
178
|
+
*/
|
|
179
|
+
delete(): Promise<WriteResult>;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* クエリクラス
|
|
183
|
+
*/
|
|
184
|
+
export declare class Query {
|
|
185
|
+
private client;
|
|
186
|
+
private collectionPath;
|
|
187
|
+
_queryConstraints: {
|
|
188
|
+
where: Array<{
|
|
189
|
+
field: string;
|
|
190
|
+
op: string;
|
|
191
|
+
value: any;
|
|
192
|
+
}>;
|
|
193
|
+
orderBy?: string;
|
|
194
|
+
orderDirection?: string;
|
|
195
|
+
limit?: number;
|
|
196
|
+
offset?: number;
|
|
197
|
+
};
|
|
198
|
+
constructor(client: FirestoreClient, collectionPath: string, constraints: {
|
|
199
|
+
where: Array<{
|
|
200
|
+
field: string;
|
|
201
|
+
op: string;
|
|
202
|
+
value: any;
|
|
203
|
+
}>;
|
|
204
|
+
orderBy?: string;
|
|
205
|
+
orderDirection?: string;
|
|
206
|
+
limit?: number;
|
|
207
|
+
offset?: number;
|
|
208
|
+
});
|
|
209
|
+
/**
|
|
210
|
+
* フィルター条件を追加
|
|
211
|
+
* @param fieldPath フィールドパス
|
|
212
|
+
* @param opStr 演算子
|
|
213
|
+
* @param value 値
|
|
214
|
+
* @returns Queryインスタンス
|
|
215
|
+
*/
|
|
216
|
+
where(fieldPath: string, opStr: string, value: any): Query;
|
|
217
|
+
/**
|
|
218
|
+
* 並べ替え条件を追加
|
|
219
|
+
* @param fieldPath フィールドパス
|
|
220
|
+
* @param directionStr 並べ替え方向('asc'または'desc')
|
|
221
|
+
* @returns Queryインスタンス
|
|
222
|
+
*/
|
|
223
|
+
orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
|
|
224
|
+
/**
|
|
225
|
+
* 取得件数の制限を設定
|
|
226
|
+
* @param limit 最大件数
|
|
227
|
+
* @returns Queryインスタンス
|
|
228
|
+
*/
|
|
229
|
+
limit(limit: number): Query;
|
|
230
|
+
/**
|
|
231
|
+
* スキップ件数を設定
|
|
232
|
+
* @param offset スキップ件数
|
|
233
|
+
* @returns Queryインスタンス
|
|
234
|
+
*/
|
|
235
|
+
offset(offset: number): Query;
|
|
236
|
+
/**
|
|
237
|
+
* クエリを実行
|
|
238
|
+
* @returns QuerySnapshotインスタンス
|
|
239
|
+
*/
|
|
240
|
+
get(): Promise<QuerySnapshot>;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* クエリ結果クラス
|
|
244
|
+
*/
|
|
245
|
+
export declare class QuerySnapshot {
|
|
246
|
+
private _docs;
|
|
247
|
+
constructor(results: Array<Record<string, any>>);
|
|
248
|
+
/**
|
|
249
|
+
* 結果のドキュメント配列
|
|
250
|
+
*/
|
|
251
|
+
get docs(): DocumentSnapshot[];
|
|
252
|
+
/**
|
|
253
|
+
* 結果が空かどうか
|
|
254
|
+
*/
|
|
255
|
+
get empty(): boolean;
|
|
256
|
+
/**
|
|
257
|
+
* 結果の件数
|
|
258
|
+
*/
|
|
259
|
+
get size(): number;
|
|
260
|
+
/**
|
|
261
|
+
* 各ドキュメントに対してコールバックを実行
|
|
262
|
+
* @param callback 各ドキュメントに対して実行するコールバック関数
|
|
263
|
+
*/
|
|
264
|
+
forEach(callback: (result: DocumentSnapshot) => void): void;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* ドキュメントスナップショットクラス
|
|
268
|
+
*/
|
|
269
|
+
export declare class DocumentSnapshot {
|
|
270
|
+
private _id;
|
|
271
|
+
private _data;
|
|
272
|
+
constructor(id: string, data: Record<string, any> | null);
|
|
273
|
+
/**
|
|
274
|
+
* ドキュメントID
|
|
275
|
+
*/
|
|
276
|
+
get id(): string;
|
|
277
|
+
/**
|
|
278
|
+
* ドキュメントが存在するかどうか
|
|
279
|
+
*/
|
|
280
|
+
get exists(): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* ドキュメントデータを取得
|
|
283
|
+
* @returns ドキュメントデータ(存在しない場合はundefined)
|
|
284
|
+
*/
|
|
285
|
+
data(): Record<string, any> | undefined;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* 書き込み結果クラス
|
|
289
|
+
*/
|
|
290
|
+
export declare class WriteResult {
|
|
291
|
+
/**
|
|
292
|
+
* 書き込み時刻
|
|
293
|
+
*/
|
|
294
|
+
readonly writeTime: Date;
|
|
295
|
+
constructor();
|
|
296
|
+
}
|
|
63
297
|
/**
|
|
64
298
|
* 新しいFirestoreクライアントインスタンスを作成
|
|
65
299
|
* @param config Firestore設定オブジェクト
|
package/dist/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FirestoreClient = void 0;
|
|
3
|
+
exports.WriteResult = exports.DocumentSnapshot = exports.QuerySnapshot = exports.Query = exports.DocumentReference = exports.CollectionReference = exports.FirestoreClient = void 0;
|
|
4
4
|
exports.createFirestoreClient = createFirestoreClient;
|
|
5
5
|
const auth_1 = require("./utils/auth");
|
|
6
6
|
const converter_1 = require("./utils/converter");
|
|
@@ -41,6 +41,28 @@ class FirestoreClient {
|
|
|
41
41
|
}
|
|
42
42
|
return this.token;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* コレクションリファレンスを取得
|
|
46
|
+
* @param path コレクションパス
|
|
47
|
+
* @returns CollectionReferenceインスタンス
|
|
48
|
+
*/
|
|
49
|
+
collection(path) {
|
|
50
|
+
return new CollectionReference(this, path);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* ドキュメントリファレンスを取得
|
|
54
|
+
* @param path ドキュメントパス
|
|
55
|
+
* @returns DocumentReferenceインスタンス
|
|
56
|
+
*/
|
|
57
|
+
doc(path) {
|
|
58
|
+
const parts = path.split("/");
|
|
59
|
+
if (parts.length % 2 === 0) {
|
|
60
|
+
throw new Error("Invalid document path. Document path must point to a document, not a collection.");
|
|
61
|
+
}
|
|
62
|
+
const collectionPath = parts.slice(0, parts.length - 1).join("/");
|
|
63
|
+
const docId = parts[parts.length - 1];
|
|
64
|
+
return new DocumentReference(this, collectionPath, docId);
|
|
65
|
+
}
|
|
44
66
|
/**
|
|
45
67
|
* Firestoreにドキュメントを作成
|
|
46
68
|
* @param collectionName コレクション名
|
|
@@ -166,7 +188,7 @@ class FirestoreClient {
|
|
|
166
188
|
structuredQuery.orderBy = [
|
|
167
189
|
{
|
|
168
190
|
field: { fieldPath: options.orderBy },
|
|
169
|
-
direction: "ASCENDING",
|
|
191
|
+
direction: options.orderDirection || "ASCENDING",
|
|
170
192
|
},
|
|
171
193
|
];
|
|
172
194
|
}
|
|
@@ -199,6 +221,416 @@ class FirestoreClient {
|
|
|
199
221
|
}
|
|
200
222
|
}
|
|
201
223
|
exports.FirestoreClient = FirestoreClient;
|
|
224
|
+
/**
|
|
225
|
+
* コレクションリファレンスクラス
|
|
226
|
+
*/
|
|
227
|
+
class CollectionReference {
|
|
228
|
+
constructor(client, path) {
|
|
229
|
+
this.client = client;
|
|
230
|
+
this.path = path;
|
|
231
|
+
this._queryConstraints = {
|
|
232
|
+
where: [],
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* ドキュメントリファレンスを取得
|
|
237
|
+
* @param documentPath ドキュメントID(省略時は自動生成)
|
|
238
|
+
* @returns DocumentReferenceインスタンス
|
|
239
|
+
*/
|
|
240
|
+
doc(documentPath) {
|
|
241
|
+
const docId = documentPath || this._generateId();
|
|
242
|
+
return new DocumentReference(this.client, this.path, docId);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* ドキュメントを追加(IDは自動生成)
|
|
246
|
+
* @param data ドキュメントデータ
|
|
247
|
+
* @returns 作成されたドキュメントのリファレンス
|
|
248
|
+
*/
|
|
249
|
+
async add(data) {
|
|
250
|
+
const result = await this.client.create(this.path, data);
|
|
251
|
+
const docId = result.id;
|
|
252
|
+
return new DocumentReference(this.client, this.path, docId);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* フィルター条件を追加
|
|
256
|
+
* @param fieldPath フィールドパス
|
|
257
|
+
* @param opStr 演算子
|
|
258
|
+
* @param value 値
|
|
259
|
+
* @returns Queryインスタンス
|
|
260
|
+
*/
|
|
261
|
+
where(fieldPath, opStr, value) {
|
|
262
|
+
const query = new Query(this.client, this.path, {
|
|
263
|
+
...this._queryConstraints,
|
|
264
|
+
});
|
|
265
|
+
// 演算子の変換
|
|
266
|
+
let firestoreOp;
|
|
267
|
+
switch (opStr) {
|
|
268
|
+
case "==":
|
|
269
|
+
firestoreOp = "EQUAL";
|
|
270
|
+
break;
|
|
271
|
+
case "!=":
|
|
272
|
+
firestoreOp = "NOT_EQUAL";
|
|
273
|
+
break;
|
|
274
|
+
case "<":
|
|
275
|
+
firestoreOp = "LESS_THAN";
|
|
276
|
+
break;
|
|
277
|
+
case "<=":
|
|
278
|
+
firestoreOp = "LESS_THAN_OR_EQUAL";
|
|
279
|
+
break;
|
|
280
|
+
case ">":
|
|
281
|
+
firestoreOp = "GREATER_THAN";
|
|
282
|
+
break;
|
|
283
|
+
case ">=":
|
|
284
|
+
firestoreOp = "GREATER_THAN_OR_EQUAL";
|
|
285
|
+
break;
|
|
286
|
+
case "array-contains":
|
|
287
|
+
firestoreOp = "ARRAY_CONTAINS";
|
|
288
|
+
break;
|
|
289
|
+
case "in":
|
|
290
|
+
firestoreOp = "IN";
|
|
291
|
+
break;
|
|
292
|
+
case "array-contains-any":
|
|
293
|
+
firestoreOp = "ARRAY_CONTAINS_ANY";
|
|
294
|
+
break;
|
|
295
|
+
case "not-in":
|
|
296
|
+
firestoreOp = "NOT_IN";
|
|
297
|
+
break;
|
|
298
|
+
default:
|
|
299
|
+
firestoreOp = opStr;
|
|
300
|
+
}
|
|
301
|
+
query._queryConstraints.where.push({
|
|
302
|
+
field: fieldPath,
|
|
303
|
+
op: firestoreOp,
|
|
304
|
+
value,
|
|
305
|
+
});
|
|
306
|
+
return query;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* 並べ替え条件を追加
|
|
310
|
+
* @param fieldPath フィールドパス
|
|
311
|
+
* @param directionStr 並べ替え方向('asc'または'desc')
|
|
312
|
+
* @returns Queryインスタンス
|
|
313
|
+
*/
|
|
314
|
+
orderBy(fieldPath, directionStr = "asc") {
|
|
315
|
+
const query = new Query(this.client, this.path, {
|
|
316
|
+
...this._queryConstraints,
|
|
317
|
+
});
|
|
318
|
+
query._queryConstraints.orderBy = fieldPath;
|
|
319
|
+
query._queryConstraints.orderDirection =
|
|
320
|
+
directionStr === "asc" ? "ASCENDING" : "DESCENDING";
|
|
321
|
+
return query;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 取得件数の制限を設定
|
|
325
|
+
* @param limit 最大件数
|
|
326
|
+
* @returns Queryインスタンス
|
|
327
|
+
*/
|
|
328
|
+
limit(limit) {
|
|
329
|
+
const query = new Query(this.client, this.path, {
|
|
330
|
+
...this._queryConstraints,
|
|
331
|
+
});
|
|
332
|
+
query._queryConstraints.limit = limit;
|
|
333
|
+
return query;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* スキップ件数を設定
|
|
337
|
+
* @param offset スキップ件数
|
|
338
|
+
* @returns Queryインスタンス
|
|
339
|
+
*/
|
|
340
|
+
offset(offset) {
|
|
341
|
+
const query = new Query(this.client, this.path, {
|
|
342
|
+
...this._queryConstraints,
|
|
343
|
+
});
|
|
344
|
+
query._queryConstraints.offset = offset;
|
|
345
|
+
return query;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* クエリを実行
|
|
349
|
+
* @returns QuerySnapshotインスタンス
|
|
350
|
+
*/
|
|
351
|
+
async get() {
|
|
352
|
+
const results = await this.client.query(this.path, this._queryConstraints);
|
|
353
|
+
return new QuerySnapshot(results);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* ランダムなIDを生成
|
|
357
|
+
* @returns ランダムなID
|
|
358
|
+
*/
|
|
359
|
+
_generateId() {
|
|
360
|
+
// 20文字のランダムなIDを生成
|
|
361
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
362
|
+
let id = "";
|
|
363
|
+
for (let i = 0; i < 20; i++) {
|
|
364
|
+
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
365
|
+
}
|
|
366
|
+
return id;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
exports.CollectionReference = CollectionReference;
|
|
370
|
+
/**
|
|
371
|
+
* ドキュメントリファレンスクラス
|
|
372
|
+
*/
|
|
373
|
+
class DocumentReference {
|
|
374
|
+
constructor(client, collectionPath, docId) {
|
|
375
|
+
this.client = client;
|
|
376
|
+
this.collectionPath = collectionPath;
|
|
377
|
+
this.docId = docId;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* ドキュメントIDを取得
|
|
381
|
+
*/
|
|
382
|
+
get id() {
|
|
383
|
+
return this.docId;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* ドキュメントのパスを取得
|
|
387
|
+
*/
|
|
388
|
+
get path() {
|
|
389
|
+
return `${this.collectionPath}/${this.docId}`;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* サブコレクションを取得
|
|
393
|
+
* @param collectionPath サブコレクション名
|
|
394
|
+
* @returns CollectionReferenceインスタンス
|
|
395
|
+
*/
|
|
396
|
+
collection(collectionPath) {
|
|
397
|
+
return new CollectionReference(this.client, `${this.path}/${collectionPath}`);
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* ドキュメントを取得
|
|
401
|
+
* @returns DocumentSnapshotインスタンス
|
|
402
|
+
*/
|
|
403
|
+
async get() {
|
|
404
|
+
const data = await this.client.get(this.collectionPath, this.docId);
|
|
405
|
+
return new DocumentSnapshot(this.docId, data);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* ドキュメントを作成または上書き
|
|
409
|
+
* @param data ドキュメントデータ
|
|
410
|
+
* @param options オプション(mergeは現在サポートされていません)
|
|
411
|
+
* @returns WriteResultインスタンス
|
|
412
|
+
*/
|
|
413
|
+
async set(data, options) {
|
|
414
|
+
// 既存のドキュメントを取得
|
|
415
|
+
const existingDoc = await this.client.get(this.collectionPath, this.docId);
|
|
416
|
+
if (existingDoc) {
|
|
417
|
+
// 既存のドキュメントがある場合は更新
|
|
418
|
+
const mergedData = options?.merge ? { ...existingDoc, ...data } : data;
|
|
419
|
+
await this.client.update(this.collectionPath, this.docId, mergedData);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
// 新規作成
|
|
423
|
+
const newData = { ...data, id: this.docId };
|
|
424
|
+
await this.client.create(this.collectionPath, newData);
|
|
425
|
+
}
|
|
426
|
+
return new WriteResult();
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* ドキュメントを更新
|
|
430
|
+
* @param data 更新データ
|
|
431
|
+
* @returns WriteResultインスタンス
|
|
432
|
+
*/
|
|
433
|
+
async update(data) {
|
|
434
|
+
await this.client.update(this.collectionPath, this.docId, data);
|
|
435
|
+
return new WriteResult();
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* ドキュメントを削除
|
|
439
|
+
* @returns WriteResultインスタンス
|
|
440
|
+
*/
|
|
441
|
+
async delete() {
|
|
442
|
+
await this.client.delete(this.collectionPath, this.docId);
|
|
443
|
+
return new WriteResult();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
exports.DocumentReference = DocumentReference;
|
|
447
|
+
/**
|
|
448
|
+
* クエリクラス
|
|
449
|
+
*/
|
|
450
|
+
class Query {
|
|
451
|
+
constructor(client, collectionPath, constraints) {
|
|
452
|
+
this.client = client;
|
|
453
|
+
this.collectionPath = collectionPath;
|
|
454
|
+
this._queryConstraints = constraints;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* フィルター条件を追加
|
|
458
|
+
* @param fieldPath フィールドパス
|
|
459
|
+
* @param opStr 演算子
|
|
460
|
+
* @param value 値
|
|
461
|
+
* @returns Queryインスタンス
|
|
462
|
+
*/
|
|
463
|
+
where(fieldPath, opStr, value) {
|
|
464
|
+
const query = new Query(this.client, this.collectionPath, {
|
|
465
|
+
...this._queryConstraints,
|
|
466
|
+
});
|
|
467
|
+
// 演算子の変換
|
|
468
|
+
let firestoreOp;
|
|
469
|
+
switch (opStr) {
|
|
470
|
+
case "==":
|
|
471
|
+
firestoreOp = "EQUAL";
|
|
472
|
+
break;
|
|
473
|
+
case "!=":
|
|
474
|
+
firestoreOp = "NOT_EQUAL";
|
|
475
|
+
break;
|
|
476
|
+
case "<":
|
|
477
|
+
firestoreOp = "LESS_THAN";
|
|
478
|
+
break;
|
|
479
|
+
case "<=":
|
|
480
|
+
firestoreOp = "LESS_THAN_OR_EQUAL";
|
|
481
|
+
break;
|
|
482
|
+
case ">":
|
|
483
|
+
firestoreOp = "GREATER_THAN";
|
|
484
|
+
break;
|
|
485
|
+
case ">=":
|
|
486
|
+
firestoreOp = "GREATER_THAN_OR_EQUAL";
|
|
487
|
+
break;
|
|
488
|
+
case "array-contains":
|
|
489
|
+
firestoreOp = "ARRAY_CONTAINS";
|
|
490
|
+
break;
|
|
491
|
+
case "in":
|
|
492
|
+
firestoreOp = "IN";
|
|
493
|
+
break;
|
|
494
|
+
case "array-contains-any":
|
|
495
|
+
firestoreOp = "ARRAY_CONTAINS_ANY";
|
|
496
|
+
break;
|
|
497
|
+
case "not-in":
|
|
498
|
+
firestoreOp = "NOT_IN";
|
|
499
|
+
break;
|
|
500
|
+
default:
|
|
501
|
+
firestoreOp = opStr;
|
|
502
|
+
}
|
|
503
|
+
query._queryConstraints.where.push({
|
|
504
|
+
field: fieldPath,
|
|
505
|
+
op: firestoreOp,
|
|
506
|
+
value,
|
|
507
|
+
});
|
|
508
|
+
return query;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* 並べ替え条件を追加
|
|
512
|
+
* @param fieldPath フィールドパス
|
|
513
|
+
* @param directionStr 並べ替え方向('asc'または'desc')
|
|
514
|
+
* @returns Queryインスタンス
|
|
515
|
+
*/
|
|
516
|
+
orderBy(fieldPath, directionStr = "asc") {
|
|
517
|
+
const query = new Query(this.client, this.collectionPath, {
|
|
518
|
+
...this._queryConstraints,
|
|
519
|
+
});
|
|
520
|
+
query._queryConstraints.orderBy = fieldPath;
|
|
521
|
+
query._queryConstraints.orderDirection =
|
|
522
|
+
directionStr === "asc" ? "ASCENDING" : "DESCENDING";
|
|
523
|
+
return query;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* 取得件数の制限を設定
|
|
527
|
+
* @param limit 最大件数
|
|
528
|
+
* @returns Queryインスタンス
|
|
529
|
+
*/
|
|
530
|
+
limit(limit) {
|
|
531
|
+
const query = new Query(this.client, this.collectionPath, {
|
|
532
|
+
...this._queryConstraints,
|
|
533
|
+
});
|
|
534
|
+
query._queryConstraints.limit = limit;
|
|
535
|
+
return query;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* スキップ件数を設定
|
|
539
|
+
* @param offset スキップ件数
|
|
540
|
+
* @returns Queryインスタンス
|
|
541
|
+
*/
|
|
542
|
+
offset(offset) {
|
|
543
|
+
const query = new Query(this.client, this.collectionPath, {
|
|
544
|
+
...this._queryConstraints,
|
|
545
|
+
});
|
|
546
|
+
query._queryConstraints.offset = offset;
|
|
547
|
+
return query;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* クエリを実行
|
|
551
|
+
* @returns QuerySnapshotインスタンス
|
|
552
|
+
*/
|
|
553
|
+
async get() {
|
|
554
|
+
const results = await this.client.query(this.collectionPath, this._queryConstraints);
|
|
555
|
+
return new QuerySnapshot(results);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
exports.Query = Query;
|
|
559
|
+
/**
|
|
560
|
+
* クエリ結果クラス
|
|
561
|
+
*/
|
|
562
|
+
class QuerySnapshot {
|
|
563
|
+
constructor(results) {
|
|
564
|
+
this._docs = results.map(doc => {
|
|
565
|
+
const { id, ...data } = doc;
|
|
566
|
+
return new DocumentSnapshot(id, data);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* 結果のドキュメント配列
|
|
571
|
+
*/
|
|
572
|
+
get docs() {
|
|
573
|
+
return this._docs;
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* 結果が空かどうか
|
|
577
|
+
*/
|
|
578
|
+
get empty() {
|
|
579
|
+
return this._docs.length === 0;
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* 結果の件数
|
|
583
|
+
*/
|
|
584
|
+
get size() {
|
|
585
|
+
return this._docs.length;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* 各ドキュメントに対してコールバックを実行
|
|
589
|
+
* @param callback 各ドキュメントに対して実行するコールバック関数
|
|
590
|
+
*/
|
|
591
|
+
forEach(callback) {
|
|
592
|
+
this._docs.forEach(callback);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
exports.QuerySnapshot = QuerySnapshot;
|
|
596
|
+
/**
|
|
597
|
+
* ドキュメントスナップショットクラス
|
|
598
|
+
*/
|
|
599
|
+
class DocumentSnapshot {
|
|
600
|
+
constructor(id, data) {
|
|
601
|
+
this._id = id;
|
|
602
|
+
this._data = data;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* ドキュメントID
|
|
606
|
+
*/
|
|
607
|
+
get id() {
|
|
608
|
+
return this._id;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* ドキュメントが存在するかどうか
|
|
612
|
+
*/
|
|
613
|
+
get exists() {
|
|
614
|
+
return this._data !== null;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* ドキュメントデータを取得
|
|
618
|
+
* @returns ドキュメントデータ(存在しない場合はundefined)
|
|
619
|
+
*/
|
|
620
|
+
data() {
|
|
621
|
+
return this._data || undefined;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
exports.DocumentSnapshot = DocumentSnapshot;
|
|
625
|
+
/**
|
|
626
|
+
* 書き込み結果クラス
|
|
627
|
+
*/
|
|
628
|
+
class WriteResult {
|
|
629
|
+
constructor() {
|
|
630
|
+
this.writeTime = new Date();
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
exports.WriteResult = WriteResult;
|
|
202
634
|
/**
|
|
203
635
|
* 新しいFirestoreクライアントインスタンスを作成
|
|
204
636
|
* @param config Firestore設定オブジェクト
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./types";
|
|
2
|
-
import { FirestoreClient, createFirestoreClient } from "./client";
|
|
2
|
+
import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, Query, QuerySnapshot, DocumentSnapshot, WriteResult } from "./client";
|
|
3
3
|
export { getFirestoreToken } from "./utils/auth";
|
|
4
4
|
export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
|
|
5
5
|
export { getFirestoreBasePath, getDocumentId } from "./utils/path";
|
|
6
|
-
export { FirestoreClient, createFirestoreClient };
|
|
6
|
+
export { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, Query, QuerySnapshot, DocumentSnapshot, WriteResult, };
|