@cougargrades/firebase-rest-firestore 1.6.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.ja.md +294 -0
- package/README.md +393 -0
- package/dist/cjs/client.d.ts +417 -0
- package/dist/cjs/client.js +1078 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.js +43 -0
- package/dist/cjs/types.d.ts +127 -0
- package/dist/cjs/types.js +86 -0
- package/dist/cjs/utils/auth.d.ts +13 -0
- package/dist/cjs/utils/auth.js +94 -0
- package/dist/cjs/utils/config.d.ts +6 -0
- package/dist/cjs/utils/config.js +14 -0
- package/dist/cjs/utils/converter.d.ts +27 -0
- package/dist/cjs/utils/converter.js +132 -0
- package/dist/cjs/utils/path.d.ts +73 -0
- package/dist/cjs/utils/path.js +176 -0
- package/dist/esm/client.d.ts +417 -0
- package/dist/esm/client.js +1066 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/types.d.ts +127 -0
- package/dist/esm/types.js +81 -0
- package/dist/esm/utils/auth.d.ts +13 -0
- package/dist/esm/utils/auth.js +57 -0
- package/dist/esm/utils/config.d.ts +6 -0
- package/dist/esm/utils/config.js +11 -0
- package/dist/esm/utils/converter.d.ts +27 -0
- package/dist/esm/utils/converter.js +126 -0
- package/dist/esm/utils/path.d.ts +73 -0
- package/dist/esm/utils/path.js +169 -0
- package/dist/types/client.d.ts +417 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/types.d.ts +127 -0
- package/dist/types/utils/auth.d.ts +13 -0
- package/dist/types/utils/config.d.ts +6 -0
- package/dist/types/utils/converter.d.ts +27 -0
- package/dist/types/utils/path.d.ts +73 -0
- package/package.json +65 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { FirestoreConfig, QueryOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Firestore client class
|
|
4
|
+
*/
|
|
5
|
+
export declare class FirestoreClient {
|
|
6
|
+
private token;
|
|
7
|
+
private tokenExpiry;
|
|
8
|
+
private config;
|
|
9
|
+
private configChecked;
|
|
10
|
+
private debug;
|
|
11
|
+
private pathUtil;
|
|
12
|
+
/**
|
|
13
|
+
* Constructor
|
|
14
|
+
* @param config Firestore configuration object
|
|
15
|
+
*/
|
|
16
|
+
constructor(config: FirestoreConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Check configuration parameters
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
private checkConfig;
|
|
22
|
+
/**
|
|
23
|
+
* Get authentication token (with caching)
|
|
24
|
+
*/
|
|
25
|
+
private getToken;
|
|
26
|
+
/**
|
|
27
|
+
* Prepare request headers
|
|
28
|
+
* @param additionalHeaders Additional headers
|
|
29
|
+
* @returns Prepared headers object
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
private prepareHeaders;
|
|
33
|
+
/**
|
|
34
|
+
* Get collection reference
|
|
35
|
+
* @param path Collection path
|
|
36
|
+
* @returns CollectionReference instance
|
|
37
|
+
*/
|
|
38
|
+
collection(path: string): CollectionReference;
|
|
39
|
+
/**
|
|
40
|
+
* Get document reference
|
|
41
|
+
* @param path Document path
|
|
42
|
+
* @returns DocumentReference instance
|
|
43
|
+
*/
|
|
44
|
+
doc(path: string): DocumentReference;
|
|
45
|
+
/**
|
|
46
|
+
* Get collection group reference
|
|
47
|
+
* @param path Collection group ID
|
|
48
|
+
* @returns CollectionGroup instance
|
|
49
|
+
*/
|
|
50
|
+
collectionGroup(path: string): CollectionGroup;
|
|
51
|
+
/**
|
|
52
|
+
* Add document to Firestore
|
|
53
|
+
* @param collectionName Collection name
|
|
54
|
+
* @param data Data to add
|
|
55
|
+
* @returns Added document
|
|
56
|
+
*/
|
|
57
|
+
add(collectionName: string, data: Record<string, any>): Promise<Record<string, any> & {
|
|
58
|
+
id: string;
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* Get document
|
|
62
|
+
* @param collectionName Collection name
|
|
63
|
+
* @param documentId Document ID
|
|
64
|
+
* @returns Retrieved document (null if it doesn't exist)
|
|
65
|
+
*/
|
|
66
|
+
get(collectionName: string, documentId: string): Promise<(Record<string, any> & {
|
|
67
|
+
id: string;
|
|
68
|
+
}) | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Update document
|
|
71
|
+
* @param collectionName Collection name
|
|
72
|
+
* @param documentId Document ID
|
|
73
|
+
* @param data Data to update
|
|
74
|
+
* @returns Updated document
|
|
75
|
+
*/
|
|
76
|
+
update(collectionName: string, documentId: string, data: Record<string, any>): Promise<Record<string, any> & {
|
|
77
|
+
id: string;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Delete document
|
|
81
|
+
* @param collectionName Collection name
|
|
82
|
+
* @param documentId Document ID
|
|
83
|
+
* @returns true if deletion successful
|
|
84
|
+
*/
|
|
85
|
+
delete(collectionName: string, documentId: string): Promise<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* Query documents in a collection
|
|
88
|
+
* @param collectionPath Collection path
|
|
89
|
+
* @param options Query options
|
|
90
|
+
* @param allDescendants Whether to include descendant collections
|
|
91
|
+
* @returns Array of documents matching the query
|
|
92
|
+
*/
|
|
93
|
+
query(collectionPath: string, options?: QueryOptions, allDescendants?: boolean): Promise<(Record<string, any> & {
|
|
94
|
+
id: string;
|
|
95
|
+
})[]>;
|
|
96
|
+
/**
|
|
97
|
+
* ドキュメントを作成または上書き
|
|
98
|
+
* @param collectionName コレクション名
|
|
99
|
+
* @param documentId ドキュメントID
|
|
100
|
+
* @param data ドキュメントデータ
|
|
101
|
+
* @returns 作成されたドキュメントのリファレンス
|
|
102
|
+
*/
|
|
103
|
+
createWithId(collectionName: string, documentId: string, data: Record<string, any>): Promise<Record<string, any> & {
|
|
104
|
+
id: string;
|
|
105
|
+
}>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Collection reference class
|
|
109
|
+
*/
|
|
110
|
+
export declare class CollectionReference {
|
|
111
|
+
private client;
|
|
112
|
+
private _path;
|
|
113
|
+
private _queryConstraints;
|
|
114
|
+
constructor(client: FirestoreClient, path: string);
|
|
115
|
+
/**
|
|
116
|
+
* Get collection path
|
|
117
|
+
*/
|
|
118
|
+
get path(): string;
|
|
119
|
+
/**
|
|
120
|
+
* Whether to include all descendant collections
|
|
121
|
+
*/
|
|
122
|
+
get allDescendants(): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Get document reference
|
|
125
|
+
* @param documentPath Document ID (auto-generated if omitted)
|
|
126
|
+
* @returns DocumentReference instance
|
|
127
|
+
*/
|
|
128
|
+
doc(documentPath?: string): DocumentReference;
|
|
129
|
+
/**
|
|
130
|
+
* Add document (ID is auto-generated)
|
|
131
|
+
* @param data Document data
|
|
132
|
+
* @returns Reference to the created document
|
|
133
|
+
*/
|
|
134
|
+
add(data: Record<string, any>): Promise<DocumentReference>;
|
|
135
|
+
/**
|
|
136
|
+
* Add filter condition
|
|
137
|
+
* @param fieldPath Field path
|
|
138
|
+
* @param opStr Operator
|
|
139
|
+
* @param value Value
|
|
140
|
+
* @returns Query instance
|
|
141
|
+
*/
|
|
142
|
+
where(fieldPath: string, opStr: string, value: any): Query;
|
|
143
|
+
/**
|
|
144
|
+
* Add sorting condition
|
|
145
|
+
* @param fieldPath Field path
|
|
146
|
+
* @param directionStr Sort direction ('asc' or 'desc')
|
|
147
|
+
* @returns Query instance
|
|
148
|
+
*/
|
|
149
|
+
orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
|
|
150
|
+
/**
|
|
151
|
+
* Set limit on number of results
|
|
152
|
+
* @param limit Maximum number
|
|
153
|
+
* @returns Query instance
|
|
154
|
+
*/
|
|
155
|
+
limit(limit: number): Query;
|
|
156
|
+
/**
|
|
157
|
+
* Set number of documents to skip
|
|
158
|
+
* @param offset Number to skip
|
|
159
|
+
* @returns Query instance
|
|
160
|
+
*/
|
|
161
|
+
offset(offset: number): Query;
|
|
162
|
+
/**
|
|
163
|
+
* Execute query
|
|
164
|
+
* @returns QuerySnapshot instance
|
|
165
|
+
*/
|
|
166
|
+
get(): Promise<QuerySnapshot>;
|
|
167
|
+
/**
|
|
168
|
+
* Generate random ID
|
|
169
|
+
* @returns Random ID
|
|
170
|
+
*/
|
|
171
|
+
private _generateId;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Document reference class
|
|
175
|
+
*/
|
|
176
|
+
export declare class DocumentReference {
|
|
177
|
+
private client;
|
|
178
|
+
private collectionPath;
|
|
179
|
+
private docId;
|
|
180
|
+
constructor(client: FirestoreClient, collectionPath: string, docId: string);
|
|
181
|
+
/**
|
|
182
|
+
* Get document ID
|
|
183
|
+
*/
|
|
184
|
+
get id(): string;
|
|
185
|
+
/**
|
|
186
|
+
* Get document path
|
|
187
|
+
*/
|
|
188
|
+
get path(): string;
|
|
189
|
+
/**
|
|
190
|
+
* Get parent collection reference
|
|
191
|
+
*/
|
|
192
|
+
get parent(): CollectionReference;
|
|
193
|
+
/**
|
|
194
|
+
* Get subcollection
|
|
195
|
+
* @param collectionPath Subcollection name
|
|
196
|
+
* @returns CollectionReference instance
|
|
197
|
+
*/
|
|
198
|
+
collection(collectionPath: string): CollectionReference;
|
|
199
|
+
/**
|
|
200
|
+
* Get document
|
|
201
|
+
* @returns DocumentSnapshot instance
|
|
202
|
+
*/
|
|
203
|
+
get(): Promise<DocumentSnapshot>;
|
|
204
|
+
/**
|
|
205
|
+
* Create or overwrite document
|
|
206
|
+
* @param data Document data
|
|
207
|
+
* @param options Options (merge is not currently supported)
|
|
208
|
+
* @returns WriteResult instance
|
|
209
|
+
*/
|
|
210
|
+
set(data: Record<string, any>, options?: {
|
|
211
|
+
merge?: boolean;
|
|
212
|
+
}): Promise<WriteResult>;
|
|
213
|
+
/**
|
|
214
|
+
* Update document
|
|
215
|
+
* @param data Update data
|
|
216
|
+
* @returns WriteResult instance
|
|
217
|
+
*/
|
|
218
|
+
update(data: Record<string, any>): Promise<WriteResult>;
|
|
219
|
+
/**
|
|
220
|
+
* Delete document
|
|
221
|
+
* @returns WriteResult instance
|
|
222
|
+
*/
|
|
223
|
+
delete(): Promise<WriteResult>;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Collection group
|
|
227
|
+
*/
|
|
228
|
+
export declare class CollectionGroup {
|
|
229
|
+
private client;
|
|
230
|
+
private path;
|
|
231
|
+
private _queryConstraints;
|
|
232
|
+
constructor(client: FirestoreClient, path: string);
|
|
233
|
+
/**
|
|
234
|
+
* Whether to include all descendant collections
|
|
235
|
+
*/
|
|
236
|
+
get allDescendants(): boolean;
|
|
237
|
+
/**
|
|
238
|
+
* Add filter condition
|
|
239
|
+
* @param fieldPath Field path
|
|
240
|
+
* @param opStr Operator
|
|
241
|
+
* @param value Value
|
|
242
|
+
* @returns Query instance
|
|
243
|
+
*/
|
|
244
|
+
where(fieldPath: string, opStr: string, value: any): Query;
|
|
245
|
+
/**
|
|
246
|
+
* Add sorting condition
|
|
247
|
+
* @param fieldPath Field path
|
|
248
|
+
* @param directionStr Sort direction ('asc' or 'desc')
|
|
249
|
+
* @returns Query instance
|
|
250
|
+
*/
|
|
251
|
+
orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
|
|
252
|
+
/**
|
|
253
|
+
* Set limit on number of results
|
|
254
|
+
* @param limit Maximum number
|
|
255
|
+
* @returns Query instance
|
|
256
|
+
*/
|
|
257
|
+
limit(limit: number): Query;
|
|
258
|
+
/**
|
|
259
|
+
* Set number of documents to skip
|
|
260
|
+
* @param offset Number to skip
|
|
261
|
+
* @returns Query instance
|
|
262
|
+
*/
|
|
263
|
+
offset(offset: number): Query;
|
|
264
|
+
/**
|
|
265
|
+
* Execute query
|
|
266
|
+
* @returns QuerySnapshot instance
|
|
267
|
+
*/
|
|
268
|
+
get(): Promise<QuerySnapshot>;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Query class
|
|
272
|
+
*/
|
|
273
|
+
export declare class Query {
|
|
274
|
+
private client;
|
|
275
|
+
private collectionPath;
|
|
276
|
+
private allDescendants;
|
|
277
|
+
_queryConstraints: {
|
|
278
|
+
where: Array<{
|
|
279
|
+
field: string;
|
|
280
|
+
op: string;
|
|
281
|
+
value: any;
|
|
282
|
+
}>;
|
|
283
|
+
orderBy?: string;
|
|
284
|
+
orderDirection?: string;
|
|
285
|
+
limit?: number;
|
|
286
|
+
offset?: number;
|
|
287
|
+
};
|
|
288
|
+
constructor(client: FirestoreClient, collectionPath: string, constraints: {
|
|
289
|
+
where: Array<{
|
|
290
|
+
field: string;
|
|
291
|
+
op: string;
|
|
292
|
+
value: any;
|
|
293
|
+
}>;
|
|
294
|
+
orderBy?: string;
|
|
295
|
+
orderDirection?: string;
|
|
296
|
+
limit?: number;
|
|
297
|
+
offset?: number;
|
|
298
|
+
}, allDescendants: boolean);
|
|
299
|
+
/**
|
|
300
|
+
* Add filter condition
|
|
301
|
+
* @param fieldPath Field path
|
|
302
|
+
* @param opStr Operator
|
|
303
|
+
* @param value Value
|
|
304
|
+
* @returns Query instance
|
|
305
|
+
*/
|
|
306
|
+
where(fieldPath: string, opStr: string, value: any): Query;
|
|
307
|
+
/**
|
|
308
|
+
* Add sorting condition
|
|
309
|
+
* @param fieldPath Field path
|
|
310
|
+
* @param directionStr Sort direction ('asc' or 'desc')
|
|
311
|
+
* @returns Query instance
|
|
312
|
+
*/
|
|
313
|
+
orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
|
|
314
|
+
/**
|
|
315
|
+
* Set limit on number of results
|
|
316
|
+
* @param limit Maximum number
|
|
317
|
+
* @returns Query instance
|
|
318
|
+
*/
|
|
319
|
+
limit(limit: number): Query;
|
|
320
|
+
/**
|
|
321
|
+
* Set number of documents to skip
|
|
322
|
+
* @param offset Number to skip
|
|
323
|
+
* @returns Query instance
|
|
324
|
+
*/
|
|
325
|
+
offset(offset: number): Query;
|
|
326
|
+
/**
|
|
327
|
+
* Execute query
|
|
328
|
+
* @returns QuerySnapshot instance
|
|
329
|
+
*/
|
|
330
|
+
get(): Promise<QuerySnapshot>;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Query result class
|
|
334
|
+
*/
|
|
335
|
+
export declare class QuerySnapshot {
|
|
336
|
+
private _docs;
|
|
337
|
+
constructor(results: Array<Record<string, any>>);
|
|
338
|
+
/**
|
|
339
|
+
* Array of documents in the result
|
|
340
|
+
*/
|
|
341
|
+
get docs(): DocumentSnapshot[];
|
|
342
|
+
/**
|
|
343
|
+
* Whether the result is empty
|
|
344
|
+
*/
|
|
345
|
+
get empty(): boolean;
|
|
346
|
+
/**
|
|
347
|
+
* Number of results
|
|
348
|
+
*/
|
|
349
|
+
get size(): number;
|
|
350
|
+
/**
|
|
351
|
+
* Execute callback for each document
|
|
352
|
+
* @param callback Callback function to execute for each document
|
|
353
|
+
*/
|
|
354
|
+
forEach(callback: (result: DocumentSnapshot) => void): void;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Document snapshot class
|
|
358
|
+
*/
|
|
359
|
+
export declare class DocumentSnapshot {
|
|
360
|
+
private _id;
|
|
361
|
+
private _data;
|
|
362
|
+
constructor(id: string, data: Record<string, any> | null);
|
|
363
|
+
/**
|
|
364
|
+
* Document ID
|
|
365
|
+
*/
|
|
366
|
+
get id(): string;
|
|
367
|
+
/**
|
|
368
|
+
* Whether the document exists
|
|
369
|
+
*/
|
|
370
|
+
get exists(): boolean;
|
|
371
|
+
/**
|
|
372
|
+
* Get document data
|
|
373
|
+
* @returns Document data (undefined if it doesn't exist)
|
|
374
|
+
*/
|
|
375
|
+
data(): Record<string, any> | undefined;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Write result class
|
|
379
|
+
*/
|
|
380
|
+
export declare class WriteResult {
|
|
381
|
+
/**
|
|
382
|
+
* Write timestamp
|
|
383
|
+
*/
|
|
384
|
+
readonly writeTime: Date;
|
|
385
|
+
constructor();
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Create a new Firestore client instance
|
|
389
|
+
* @param config Firestore configuration object
|
|
390
|
+
* @returns FirestoreClient instance
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* // Connect to default database
|
|
394
|
+
* const db = createFirestoreClient({
|
|
395
|
+
* projectId: 'your-project-id',
|
|
396
|
+
* privateKey: 'your-private-key',
|
|
397
|
+
* clientEmail: 'your-client-email'
|
|
398
|
+
* });
|
|
399
|
+
*
|
|
400
|
+
* // Connect to a different named database
|
|
401
|
+
* const customDb = createFirestoreClient({
|
|
402
|
+
* projectId: 'your-project-id',
|
|
403
|
+
* privateKey: 'your-private-key',
|
|
404
|
+
* clientEmail: 'your-client-email',
|
|
405
|
+
* databaseId: 'your-database-id'
|
|
406
|
+
* });
|
|
407
|
+
*
|
|
408
|
+
* // Connect to local emulator (no auth required)
|
|
409
|
+
* const emulatorDb = createFirestoreClient({
|
|
410
|
+
* projectId: 'demo-project',
|
|
411
|
+
* useEmulator: true,
|
|
412
|
+
* emulatorHost: '127.0.',
|
|
413
|
+
* emulatorPort: 8080,
|
|
414
|
+
* debug: true // Optional: enables detailed logging
|
|
415
|
+
* });
|
|
416
|
+
*/
|
|
417
|
+
export declare function createFirestoreClient(config: FirestoreConfig): FirestoreClient;
|