UNPKG

12.7 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import { EmulatorMockTokenOptions } from '@firebase/util';
19
20export type DocumentData = { [field: string]: any };
21
22export type UpdateData = { [fieldPath: string]: any };
23
24export const CACHE_SIZE_UNLIMITED: number;
25
26export interface Settings {
27 host?: string;
28 ssl?: boolean;
29 cacheSizeBytes?: number;
30 experimentalForceLongPolling?: boolean;
31 experimentalAutoDetectLongPolling?: boolean;
32 ignoreUndefinedProperties?: boolean;
33 merge?: boolean;
34}
35
36export interface PersistenceSettings {
37 synchronizeTabs?: boolean;
38 experimentalTabSynchronization?: boolean;
39 experimentalForceOwningTab?: boolean;
40}
41
42export type LogLevel =
43 | 'debug'
44 | 'error'
45 | 'silent'
46 | 'warn'
47 | 'info'
48 | 'verbose';
49
50export function setLogLevel(logLevel: LogLevel): void;
51
52export interface FirestoreDataConverter<T> {
53 toFirestore(modelObject: T): DocumentData;
54 toFirestore(modelObject: Partial<T>, options: SetOptions): DocumentData;
55
56 fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): T;
57}
58
59export class FirebaseFirestore {
60 private constructor();
61
62 settings(settings: Settings): void;
63
64 useEmulator(
65 host: string,
66 port: number,
67 options?: {
68 mockUserToken?: EmulatorMockTokenOptions | string;
69 }
70 ): void;
71
72 enablePersistence(settings?: PersistenceSettings): Promise<void>;
73
74 collection(collectionPath: string): CollectionReference<DocumentData>;
75
76 doc(documentPath: string): DocumentReference<DocumentData>;
77
78 collectionGroup(collectionId: string): Query<DocumentData>;
79
80 runTransaction<T>(
81 updateFunction: (transaction: Transaction) => Promise<T>
82 ): Promise<T>;
83
84 batch(): WriteBatch;
85
86 app: any;
87
88 clearPersistence(): Promise<void>;
89
90 enableNetwork(): Promise<void>;
91
92 disableNetwork(): Promise<void>;
93
94 waitForPendingWrites(): Promise<void>;
95
96 onSnapshotsInSync(observer: {
97 next?: (value: void) => void;
98 error?: (error: FirestoreError) => void;
99 complete?: () => void;
100 }): () => void;
101 onSnapshotsInSync(onSync: () => void): () => void;
102
103 terminate(): Promise<void>;
104
105 loadBundle(
106 bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
107 ): LoadBundleTask;
108
109 namedQuery(name: string): Promise<Query<DocumentData> | null>;
110
111 INTERNAL: { delete: () => Promise<void> };
112}
113
114export interface LoadBundleTask extends PromiseLike<LoadBundleTaskProgress> {
115 onProgress(
116 next?: (progress: LoadBundleTaskProgress) => any,
117 error?: (error: Error) => any,
118 complete?: () => void
119 ): void;
120
121 then<T, R>(
122 onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
123 onRejected?: (a: Error) => R | PromiseLike<R>
124 ): Promise<T | R>;
125
126 catch<R>(
127 onRejected: (a: Error) => R | PromiseLike<R>
128 ): Promise<R | LoadBundleTaskProgress>;
129}
130
131export interface LoadBundleTaskProgress {
132 documentsLoaded: number;
133 totalDocuments: number;
134 bytesLoaded: number;
135 totalBytes: number;
136 taskState: TaskState;
137}
138
139export type TaskState = 'Error' | 'Running' | 'Success';
140
141export class GeoPoint {
142 constructor(latitude: number, longitude: number);
143
144 readonly latitude: number;
145 readonly longitude: number;
146
147 isEqual(other: GeoPoint): boolean;
148}
149
150export class Timestamp {
151 constructor(seconds: number, nanoseconds: number);
152
153 static now(): Timestamp;
154
155 static fromDate(date: Date): Timestamp;
156
157 static fromMillis(milliseconds: number): Timestamp;
158
159 readonly seconds: number;
160 readonly nanoseconds: number;
161
162 toDate(): Date;
163
164 toMillis(): number;
165
166 isEqual(other: Timestamp): boolean;
167
168 valueOf(): string;
169}
170
171export class Blob {
172 private constructor();
173
174 static fromBase64String(base64: string): Blob;
175
176 static fromUint8Array(array: Uint8Array): Blob;
177
178 public toBase64(): string;
179
180 public toUint8Array(): Uint8Array;
181
182 isEqual(other: Blob): boolean;
183}
184
185export class Transaction {
186 private constructor();
187
188 get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
189
190 set<T>(
191 documentRef: DocumentReference<T>,
192 data: Partial<T>,
193 options: SetOptions
194 ): Transaction;
195 set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
196
197 update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
198 update(
199 documentRef: DocumentReference<any>,
200 field: string | FieldPath,
201 value: any,
202 ...moreFieldsAndValues: any[]
203 ): Transaction;
204
205 delete(documentRef: DocumentReference<any>): Transaction;
206}
207
208export class WriteBatch {
209 private constructor();
210
211 set<T>(
212 documentRef: DocumentReference<T>,
213 data: Partial<T>,
214 options: SetOptions
215 ): WriteBatch;
216 set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
217
218 update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
219 update(
220 documentRef: DocumentReference<any>,
221 field: string | FieldPath,
222 value: any,
223 ...moreFieldsAndValues: any[]
224 ): WriteBatch;
225
226 delete(documentRef: DocumentReference<any>): WriteBatch;
227
228 commit(): Promise<void>;
229}
230
231export interface SnapshotListenOptions {
232 readonly includeMetadataChanges?: boolean;
233}
234
235export interface SetOptions {
236 readonly merge?: boolean;
237 readonly mergeFields?: (string | FieldPath)[];
238}
239
240export interface GetOptions {
241 readonly source?: 'default' | 'server' | 'cache';
242}
243
244export class DocumentReference<
245 T = DocumentData,
246 T2 extends DocumentData = DocumentData
247> {
248 private constructor();
249
250 readonly id: string;
251 readonly firestore: FirebaseFirestore;
252 readonly parent: CollectionReference<T>;
253 readonly path: string;
254
255 collection(collectionPath: string): CollectionReference<DocumentData>;
256
257 isEqual(other: DocumentReference<T>): boolean;
258
259 set(data: Partial<T>, options: SetOptions): Promise<void>;
260 set(data: T): Promise<void>;
261
262 update(data: UpdateData): Promise<void>;
263 update(
264 field: string | FieldPath,
265 value: any,
266 ...moreFieldsAndValues: any[]
267 ): Promise<void>;
268
269 delete(): Promise<void>;
270
271 get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
272
273 onSnapshot(observer: {
274 next?: (snapshot: DocumentSnapshot<T>) => void;
275 error?: (error: FirestoreError) => void;
276 complete?: () => void;
277 }): () => void;
278 onSnapshot(
279 options: SnapshotListenOptions,
280 observer: {
281 next?: (snapshot: DocumentSnapshot<T>) => void;
282 error?: (error: FirestoreError) => void;
283 complete?: () => void;
284 }
285 ): () => void;
286 onSnapshot(
287 onNext: (snapshot: DocumentSnapshot<T>) => void,
288 onError?: (error: FirestoreError) => void,
289 onCompletion?: () => void
290 ): () => void;
291 onSnapshot(
292 options: SnapshotListenOptions,
293 onNext: (snapshot: DocumentSnapshot<T>) => void,
294 onError?: (error: FirestoreError) => void,
295 onCompletion?: () => void
296 ): () => void;
297
298 withConverter(converter: null): DocumentReference<DocumentData>;
299 withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
300}
301
302export interface SnapshotOptions {
303 readonly serverTimestamps?: 'estimate' | 'previous' | 'none';
304}
305
306export interface SnapshotMetadata {
307 readonly hasPendingWrites: boolean;
308 readonly fromCache: boolean;
309
310 isEqual(other: SnapshotMetadata): boolean;
311}
312
313export class DocumentSnapshot<
314 T = DocumentData,
315 T2 extends DocumentData = DocumentData
316> {
317 protected constructor();
318
319 readonly exists: boolean;
320 readonly ref: DocumentReference<T>;
321 readonly id: string;
322 readonly metadata: SnapshotMetadata;
323
324 data(options?: SnapshotOptions): T | undefined;
325
326 get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
327
328 isEqual(other: DocumentSnapshot<T>): boolean;
329}
330
331export class QueryDocumentSnapshot<
332 T = DocumentData,
333 T2 extends DocumentData = DocumentData
334> extends DocumentSnapshot<T, T2> {
335 private constructor();
336
337 data(options?: SnapshotOptions): T;
338}
339
340export type OrderByDirection = 'desc' | 'asc';
341
342export type WhereFilterOp =
343 | '<'
344 | '<='
345 | '=='
346 | '!='
347 | '>='
348 | '>'
349 | 'array-contains'
350 | 'in'
351 | 'array-contains-any'
352 | 'not-in';
353
354export class Query<T = DocumentData, T2 extends DocumentData = DocumentData> {
355 protected constructor();
356
357 readonly firestore: FirebaseFirestore;
358
359 where(
360 fieldPath: string | FieldPath,
361 opStr: WhereFilterOp,
362 value: any
363 ): Query<T>;
364
365 orderBy(
366 fieldPath: string | FieldPath,
367 directionStr?: OrderByDirection
368 ): Query<T>;
369
370 limit(limit: number): Query<T>;
371
372 limitToLast(limit: number): Query<T>;
373
374 startAt(snapshot: DocumentSnapshot<any>): Query<T>;
375 startAt(...fieldValues: any[]): Query<T>;
376
377 startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
378 startAfter(...fieldValues: any[]): Query<T>;
379
380 endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
381 endBefore(...fieldValues: any[]): Query<T>;
382
383 endAt(snapshot: DocumentSnapshot<any>): Query<T>;
384 endAt(...fieldValues: any[]): Query<T>;
385
386 isEqual(other: Query<T>): boolean;
387
388 get(options?: GetOptions): Promise<QuerySnapshot<T>>;
389
390 onSnapshot(observer: {
391 next?: (snapshot: QuerySnapshot<T>) => void;
392 error?: (error: FirestoreError) => void;
393 complete?: () => void;
394 }): () => void;
395 onSnapshot(
396 options: SnapshotListenOptions,
397 observer: {
398 next?: (snapshot: QuerySnapshot<T>) => void;
399 error?: (error: FirestoreError) => void;
400 complete?: () => void;
401 }
402 ): () => void;
403 onSnapshot(
404 onNext: (snapshot: QuerySnapshot<T>) => void,
405 onError?: (error: FirestoreError) => void,
406 onCompletion?: () => void
407 ): () => void;
408 onSnapshot(
409 options: SnapshotListenOptions,
410 onNext: (snapshot: QuerySnapshot<T>) => void,
411 onError?: (error: FirestoreError) => void,
412 onCompletion?: () => void
413 ): () => void;
414
415 withConverter(converter: null): Query<DocumentData>;
416 withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
417}
418
419export class QuerySnapshot<
420 T = DocumentData,
421 T2 extends DocumentData = DocumentData
422> {
423 private constructor();
424
425 readonly query: Query<T>;
426 readonly metadata: SnapshotMetadata;
427 readonly docs: Array<QueryDocumentSnapshot<T>>;
428 readonly size: number;
429 readonly empty: boolean;
430
431 docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;
432
433 forEach(
434 callback: (result: QueryDocumentSnapshot<T>) => void,
435 thisArg?: any
436 ): void;
437
438 isEqual(other: QuerySnapshot<T>): boolean;
439}
440
441export type DocumentChangeType = 'added' | 'removed' | 'modified';
442
443export interface DocumentChange<
444 T = DocumentData,
445 T2 extends DocumentData = DocumentData
446> {
447 readonly type: DocumentChangeType;
448 readonly doc: QueryDocumentSnapshot<T>;
449 readonly oldIndex: number;
450 readonly newIndex: number;
451}
452
453export class CollectionReference<
454 T = DocumentData,
455 T2 extends DocumentData = DocumentData
456> extends Query<T, T2> {
457 private constructor();
458
459 readonly id: string;
460 readonly parent: DocumentReference<DocumentData> | null;
461 readonly path: string;
462
463 doc(documentPath?: string): DocumentReference<T>;
464
465 add(data: T): Promise<DocumentReference<T>>;
466
467 isEqual(other: CollectionReference<T>): boolean;
468
469 withConverter(converter: null): CollectionReference<DocumentData>;
470 withConverter<U>(
471 converter: FirestoreDataConverter<U>
472 ): CollectionReference<U>;
473}
474
475export class FieldValue {
476 private constructor();
477
478 static serverTimestamp(): FieldValue;
479
480 static delete(): FieldValue;
481
482 static arrayUnion(...elements: any[]): FieldValue;
483
484 static arrayRemove(...elements: any[]): FieldValue;
485
486 static increment(n: number): FieldValue;
487
488 isEqual(other: FieldValue): boolean;
489}
490
491export class FieldPath {
492 constructor(...fieldNames: string[]);
493
494 static documentId(): FieldPath;
495
496 isEqual(other: FieldPath): boolean;
497}
498
499export type FirestoreErrorCode =
500 | 'cancelled'
501 | 'unknown'
502 | 'invalid-argument'
503 | 'deadline-exceeded'
504 | 'not-found'
505 | 'already-exists'
506 | 'permission-denied'
507 | 'resource-exhausted'
508 | 'failed-precondition'
509 | 'aborted'
510 | 'out-of-range'
511 | 'unimplemented'
512 | 'internal'
513 | 'unavailable'
514 | 'data-loss'
515 | 'unauthenticated';
516
517export interface FirestoreError {
518 code: FirestoreErrorCode;
519 message: string;
520 name: string;
521 stack?: string;
522}
523
524declare module '@firebase/component' {
525 interface NameServiceMapping {
526 'firestore-compat': FirebaseFirestore;
527 }
528}