firebase-rest-firestore 1.2.0 → 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.
Files changed (52) hide show
  1. package/README.md +37 -0
  2. package/dist/cjs/client.d.ts +454 -0
  3. package/dist/cjs/client.js +1192 -0
  4. package/dist/cjs/field-value.d.ts +22 -0
  5. package/dist/cjs/field-value.js +31 -0
  6. package/dist/{index.d.ts → cjs/index.d.ts} +2 -1
  7. package/dist/{index.js → cjs/index.js} +4 -2
  8. package/dist/cjs/types.d.ts +146 -0
  9. package/dist/cjs/types.js +75 -0
  10. package/dist/cjs/utils/auth.d.ts +13 -0
  11. package/dist/{utils → cjs/utils}/auth.js +14 -10
  12. package/dist/cjs/utils/config.d.ts +6 -0
  13. package/dist/{utils → cjs/utils}/config.js +0 -12
  14. package/dist/cjs/utils/converter.d.ts +57 -0
  15. package/dist/cjs/utils/converter.js +224 -0
  16. package/dist/cjs/utils/path.d.ts +73 -0
  17. package/dist/cjs/utils/path.js +176 -0
  18. package/dist/esm/client.d.ts +454 -0
  19. package/dist/esm/client.js +1180 -0
  20. package/dist/esm/field-value.d.ts +22 -0
  21. package/dist/esm/field-value.js +27 -0
  22. package/dist/esm/index.d.ts +8 -0
  23. package/dist/esm/index.js +13 -0
  24. package/dist/esm/types.d.ts +146 -0
  25. package/dist/esm/types.js +70 -0
  26. package/dist/esm/utils/auth.d.ts +13 -0
  27. package/dist/esm/utils/auth.js +57 -0
  28. package/dist/esm/utils/config.d.ts +6 -0
  29. package/dist/esm/utils/config.js +11 -0
  30. package/dist/esm/utils/converter.d.ts +57 -0
  31. package/dist/esm/utils/converter.js +216 -0
  32. package/dist/esm/utils/path.d.ts +73 -0
  33. package/dist/esm/utils/path.js +169 -0
  34. package/dist/types/client.d.ts +454 -0
  35. package/dist/types/field-value.d.ts +22 -0
  36. package/dist/types/index.d.ts +8 -0
  37. package/dist/types/types.d.ts +146 -0
  38. package/dist/types/utils/auth.d.ts +13 -0
  39. package/dist/types/utils/config.d.ts +6 -0
  40. package/dist/types/utils/converter.d.ts +57 -0
  41. package/dist/types/utils/path.d.ts +73 -0
  42. package/package.json +19 -5
  43. package/dist/client.d.ts +0 -381
  44. package/dist/client.js +0 -915
  45. package/dist/types.d.ts +0 -65
  46. package/dist/types.js +0 -2
  47. package/dist/utils/auth.d.ts +0 -13
  48. package/dist/utils/config.d.ts +0 -13
  49. package/dist/utils/converter.d.ts +0 -27
  50. package/dist/utils/converter.js +0 -113
  51. package/dist/utils/path.d.ts +0 -14
  52. package/dist/utils/path.js +0 -27
package/README.md CHANGED
@@ -134,6 +134,43 @@ Parameters:
134
134
 
135
135
  Returns: The added document with auto-generated ID.
136
136
 
137
+ ### FieldValue
138
+
139
+ Sentinels for special write behaviors, mirroring the native Firebase SDK.
140
+
141
+ #### FieldValue.serverTimestamp()
142
+
143
+ Sets the field to the server's request timestamp at write time (rather than an
144
+ unreliable client clock). Works with `add`, `update`, `set`, and `createWithId`,
145
+ including nested fields. The returned document contains the resolved `Date`.
146
+
147
+ ```typescript
148
+ import { createFirestoreClient, FieldValue } from "firebase-rest-firestore";
149
+
150
+ const client = createFirestoreClient(config);
151
+
152
+ // On create (auto-generated ID)
153
+ const post = await client.add("posts", {
154
+ title: "Hello",
155
+ createdAt: FieldValue.serverTimestamp(),
156
+ });
157
+ console.log(post.createdAt); // Date, set by the server
158
+
159
+ // On update
160
+ await client.update("posts", post.id, {
161
+ updatedAt: FieldValue.serverTimestamp(),
162
+ });
163
+
164
+ // Nested fields are supported too
165
+ await client.collection("posts").doc(post.id).set({
166
+ meta: { touchedAt: FieldValue.serverTimestamp() },
167
+ });
168
+ ```
169
+
170
+ > Writes containing a `FieldValue` are sent through the Firestore `commit`
171
+ > endpoint so the transform is applied server-side. A `FieldValue` cannot be
172
+ > used inside an array.
173
+
137
174
  ## Error Handling
138
175
 
139
176
  Firebase REST Firestore throws exceptions with appropriate error messages when API requests fail. Here's an example of error handling:
@@ -0,0 +1,454 @@
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
+ * Build the fully-qualified Firestore reference value for a document path,
35
+ * e.g. `projects/{projectId}/databases/{databaseId}/documents/{path}`.
36
+ * Used to encode a DocumentReference without exposing internal path helpers.
37
+ * @param documentPath Document path (ex: "users/uid/posts/postId")
38
+ */
39
+ getReferenceValue(documentPath: string): string;
40
+ /**
41
+ * Apply a single write through the `documents:commit` endpoint. This is the
42
+ * only REST path that supports field transforms (e.g. server timestamps).
43
+ * @param collectionName Collection path
44
+ * @param documentId Document ID
45
+ * @param fields Already-converted Firestore field values
46
+ * @param transforms Field transforms to apply after the update
47
+ * @param currentDocument Optional precondition (e.g. `{ exists: false }`)
48
+ * @private
49
+ */
50
+ private commit;
51
+ /**
52
+ * Commit a write and read the document back, so the resolved transform
53
+ * values (e.g. the server timestamp) are returned to the caller.
54
+ * @private
55
+ */
56
+ private commitAndRead;
57
+ /**
58
+ * Get collection reference
59
+ * @param path Collection path
60
+ * @returns CollectionReference instance
61
+ */
62
+ collection(path: string): CollectionReference;
63
+ /**
64
+ * Get document reference
65
+ * @param path Document path
66
+ * @returns DocumentReference instance
67
+ */
68
+ doc(path: string): DocumentReference;
69
+ /**
70
+ * Get collection group reference
71
+ * @param path Collection group ID
72
+ * @returns CollectionGroup instance
73
+ */
74
+ collectionGroup(path: string): CollectionGroup;
75
+ /**
76
+ * Add document to Firestore
77
+ * @param collectionName Collection name
78
+ * @param data Data to add
79
+ * @returns Added document
80
+ */
81
+ add(collectionName: string, data: Record<string, any>): Promise<Record<string, any> & {
82
+ id: string;
83
+ }>;
84
+ /**
85
+ * Create a document that contains field transforms (e.g. serverTimestamp)
86
+ * with an auto-generated ID. Uses the commit endpoint with an
87
+ * `exists: false` precondition and retries on the (astronomically unlikely)
88
+ * ID collision, mirroring the uniqueness of server-generated IDs.
89
+ * @private
90
+ */
91
+ private addWithTransforms;
92
+ /**
93
+ * Get document
94
+ * @param collectionName Collection name
95
+ * @param documentId Document ID
96
+ * @returns Retrieved document (null if it doesn't exist)
97
+ */
98
+ get(collectionName: string, documentId: string): Promise<(Record<string, any> & {
99
+ id: string;
100
+ }) | null>;
101
+ /**
102
+ * Update document
103
+ * @param collectionName Collection name
104
+ * @param documentId Document ID
105
+ * @param data Data to update
106
+ * @returns Updated document
107
+ */
108
+ update(collectionName: string, documentId: string, data: Record<string, any>): Promise<Record<string, any> & {
109
+ id: string;
110
+ }>;
111
+ /**
112
+ * Delete document
113
+ * @param collectionName Collection name
114
+ * @param documentId Document ID
115
+ * @returns true if deletion successful
116
+ */
117
+ delete(collectionName: string, documentId: string): Promise<boolean>;
118
+ /**
119
+ * Query documents in a collection
120
+ * @param collectionPath Collection path
121
+ * @param options Query options
122
+ * @param allDescendants Whether to include descendant collections
123
+ * @returns Array of documents matching the query
124
+ */
125
+ query(collectionPath: string, options?: QueryOptions, allDescendants?: boolean): Promise<(Record<string, any> & {
126
+ id: string;
127
+ })[]>;
128
+ /**
129
+ * ドキュメントを作成または上書き
130
+ * @param collectionName コレクション名
131
+ * @param documentId ドキュメントID
132
+ * @param data ドキュメントデータ
133
+ * @returns 作成されたドキュメントのリファレンス
134
+ */
135
+ createWithId(collectionName: string, documentId: string, data: Record<string, any>): Promise<Record<string, any> & {
136
+ id: string;
137
+ }>;
138
+ }
139
+ /**
140
+ * Collection reference class
141
+ */
142
+ export declare class CollectionReference {
143
+ private client;
144
+ private _path;
145
+ private _queryConstraints;
146
+ constructor(client: FirestoreClient, path: string);
147
+ /**
148
+ * Get collection path
149
+ */
150
+ get path(): string;
151
+ /**
152
+ * Whether to include all descendant collections
153
+ */
154
+ get allDescendants(): boolean;
155
+ /**
156
+ * Get document reference
157
+ * @param documentPath Document ID (auto-generated if omitted)
158
+ * @returns DocumentReference instance
159
+ */
160
+ doc(documentPath?: string): DocumentReference;
161
+ /**
162
+ * Add document (ID is auto-generated)
163
+ * @param data Document data
164
+ * @returns Reference to the created document
165
+ */
166
+ add(data: Record<string, any>): Promise<DocumentReference>;
167
+ /**
168
+ * Add filter condition
169
+ * @param fieldPath Field path
170
+ * @param opStr Operator
171
+ * @param value Value
172
+ * @returns Query instance
173
+ */
174
+ where(fieldPath: string, opStr: string, value: any): Query;
175
+ /**
176
+ * Add sorting condition
177
+ * @param fieldPath Field path
178
+ * @param directionStr Sort direction ('asc' or 'desc')
179
+ * @returns Query instance
180
+ */
181
+ orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
182
+ /**
183
+ * Set limit on number of results
184
+ * @param limit Maximum number
185
+ * @returns Query instance
186
+ */
187
+ limit(limit: number): Query;
188
+ /**
189
+ * Set number of documents to skip
190
+ * @param offset Number to skip
191
+ * @returns Query instance
192
+ */
193
+ offset(offset: number): Query;
194
+ /**
195
+ * Execute query
196
+ * @returns QuerySnapshot instance
197
+ */
198
+ get(): Promise<QuerySnapshot>;
199
+ /**
200
+ * Generate random ID
201
+ * @returns Random ID
202
+ */
203
+ private _generateId;
204
+ }
205
+ /**
206
+ * Document reference class
207
+ */
208
+ export declare class DocumentReference {
209
+ private client;
210
+ private collectionPath;
211
+ private docId;
212
+ constructor(client: FirestoreClient, collectionPath: string, docId: string);
213
+ /**
214
+ * Get document ID
215
+ */
216
+ get id(): string;
217
+ /**
218
+ * Get document path
219
+ */
220
+ get path(): string;
221
+ /**
222
+ * Fully-qualified Firestore reference value for this document, e.g.
223
+ * `projects/{projectId}/databases/{databaseId}/documents/{path}`.
224
+ */
225
+ get referenceValue(): string;
226
+ /**
227
+ * Get parent collection reference
228
+ */
229
+ get parent(): CollectionReference;
230
+ /**
231
+ * Get subcollection
232
+ * @param collectionPath Subcollection name
233
+ * @returns CollectionReference instance
234
+ */
235
+ collection(collectionPath: string): CollectionReference;
236
+ /**
237
+ * Get document
238
+ * @returns DocumentSnapshot instance
239
+ */
240
+ get(): Promise<DocumentSnapshot>;
241
+ /**
242
+ * Create or overwrite document
243
+ * @param data Document data
244
+ * @param options Options (merge is not currently supported)
245
+ * @returns WriteResult instance
246
+ */
247
+ set(data: Record<string, any>, options?: {
248
+ merge?: boolean;
249
+ }): Promise<WriteResult>;
250
+ /**
251
+ * Update document
252
+ * @param data Update data
253
+ * @returns WriteResult instance
254
+ */
255
+ update(data: Record<string, any>): Promise<WriteResult>;
256
+ /**
257
+ * Delete document
258
+ * @returns WriteResult instance
259
+ */
260
+ delete(): Promise<WriteResult>;
261
+ }
262
+ /**
263
+ * Collection group
264
+ */
265
+ export declare class CollectionGroup {
266
+ private client;
267
+ private path;
268
+ private _queryConstraints;
269
+ constructor(client: FirestoreClient, path: string);
270
+ /**
271
+ * Whether to include all descendant collections
272
+ */
273
+ get allDescendants(): boolean;
274
+ /**
275
+ * Add filter condition
276
+ * @param fieldPath Field path
277
+ * @param opStr Operator
278
+ * @param value Value
279
+ * @returns Query instance
280
+ */
281
+ where(fieldPath: string, opStr: string, value: any): Query;
282
+ /**
283
+ * Add sorting condition
284
+ * @param fieldPath Field path
285
+ * @param directionStr Sort direction ('asc' or 'desc')
286
+ * @returns Query instance
287
+ */
288
+ orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
289
+ /**
290
+ * Set limit on number of results
291
+ * @param limit Maximum number
292
+ * @returns Query instance
293
+ */
294
+ limit(limit: number): Query;
295
+ /**
296
+ * Set number of documents to skip
297
+ * @param offset Number to skip
298
+ * @returns Query instance
299
+ */
300
+ offset(offset: number): Query;
301
+ /**
302
+ * Execute query
303
+ * @returns QuerySnapshot instance
304
+ */
305
+ get(): Promise<QuerySnapshot>;
306
+ }
307
+ /**
308
+ * Query class
309
+ */
310
+ export declare class Query {
311
+ private client;
312
+ private collectionPath;
313
+ private allDescendants;
314
+ _queryConstraints: {
315
+ where: Array<{
316
+ field: string;
317
+ op: string;
318
+ value: any;
319
+ }>;
320
+ orderBy?: string;
321
+ orderDirection?: string;
322
+ limit?: number;
323
+ offset?: number;
324
+ };
325
+ constructor(client: FirestoreClient, collectionPath: string, constraints: {
326
+ where: Array<{
327
+ field: string;
328
+ op: string;
329
+ value: any;
330
+ }>;
331
+ orderBy?: string;
332
+ orderDirection?: string;
333
+ limit?: number;
334
+ offset?: number;
335
+ }, allDescendants: boolean);
336
+ /**
337
+ * Add filter condition
338
+ * @param fieldPath Field path
339
+ * @param opStr Operator
340
+ * @param value Value
341
+ * @returns Query instance
342
+ */
343
+ where(fieldPath: string, opStr: string, value: any): Query;
344
+ /**
345
+ * Add sorting condition
346
+ * @param fieldPath Field path
347
+ * @param directionStr Sort direction ('asc' or 'desc')
348
+ * @returns Query instance
349
+ */
350
+ orderBy(fieldPath: string, directionStr?: "asc" | "desc"): Query;
351
+ /**
352
+ * Set limit on number of results
353
+ * @param limit Maximum number
354
+ * @returns Query instance
355
+ */
356
+ limit(limit: number): Query;
357
+ /**
358
+ * Set number of documents to skip
359
+ * @param offset Number to skip
360
+ * @returns Query instance
361
+ */
362
+ offset(offset: number): Query;
363
+ /**
364
+ * Execute query
365
+ * @returns QuerySnapshot instance
366
+ */
367
+ get(): Promise<QuerySnapshot>;
368
+ }
369
+ /**
370
+ * Query result class
371
+ */
372
+ export declare class QuerySnapshot {
373
+ private _docs;
374
+ constructor(results: Array<Record<string, any>>);
375
+ /**
376
+ * Array of documents in the result
377
+ */
378
+ get docs(): DocumentSnapshot[];
379
+ /**
380
+ * Whether the result is empty
381
+ */
382
+ get empty(): boolean;
383
+ /**
384
+ * Number of results
385
+ */
386
+ get size(): number;
387
+ /**
388
+ * Execute callback for each document
389
+ * @param callback Callback function to execute for each document
390
+ */
391
+ forEach(callback: (result: DocumentSnapshot) => void): void;
392
+ }
393
+ /**
394
+ * Document snapshot class
395
+ */
396
+ export declare class DocumentSnapshot {
397
+ private _id;
398
+ private _data;
399
+ constructor(id: string, data: Record<string, any> | null);
400
+ /**
401
+ * Document ID
402
+ */
403
+ get id(): string;
404
+ /**
405
+ * Whether the document exists
406
+ */
407
+ get exists(): boolean;
408
+ /**
409
+ * Get document data
410
+ * @returns Document data (undefined if it doesn't exist)
411
+ */
412
+ data(): Record<string, any> | undefined;
413
+ }
414
+ /**
415
+ * Write result class
416
+ */
417
+ export declare class WriteResult {
418
+ /**
419
+ * Write timestamp
420
+ */
421
+ readonly writeTime: Date;
422
+ constructor();
423
+ }
424
+ /**
425
+ * Create a new Firestore client instance
426
+ * @param config Firestore configuration object
427
+ * @returns FirestoreClient instance
428
+ *
429
+ * @example
430
+ * // Connect to default database
431
+ * const db = createFirestoreClient({
432
+ * projectId: 'your-project-id',
433
+ * privateKey: 'your-private-key',
434
+ * clientEmail: 'your-client-email'
435
+ * });
436
+ *
437
+ * // Connect to a different named database
438
+ * const customDb = createFirestoreClient({
439
+ * projectId: 'your-project-id',
440
+ * privateKey: 'your-private-key',
441
+ * clientEmail: 'your-client-email',
442
+ * databaseId: 'your-database-id'
443
+ * });
444
+ *
445
+ * // Connect to local emulator (no auth required)
446
+ * const emulatorDb = createFirestoreClient({
447
+ * projectId: 'demo-project',
448
+ * useEmulator: true,
449
+ * emulatorHost: '127.0.',
450
+ * emulatorPort: 8080,
451
+ * debug: true // Optional: enables detailed logging
452
+ * });
453
+ */
454
+ export declare function createFirestoreClient(config: FirestoreConfig): FirestoreClient;