UNPKG

12.4 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<T = DocumentData> {
245 private constructor();
246
247 readonly id: string;
248 readonly firestore: FirebaseFirestore;
249 readonly parent: CollectionReference<T>;
250 readonly path: string;
251
252 collection(collectionPath: string): CollectionReference<DocumentData>;
253
254 isEqual(other: DocumentReference<T>): boolean;
255
256 set(data: Partial<T>, options: SetOptions): Promise<void>;
257 set(data: T): Promise<void>;
258
259 update(data: UpdateData): Promise<void>;
260 update(
261 field: string | FieldPath,
262 value: any,
263 ...moreFieldsAndValues: any[]
264 ): Promise<void>;
265
266 delete(): Promise<void>;
267
268 get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
269
270 onSnapshot(observer: {
271 next?: (snapshot: DocumentSnapshot<T>) => void;
272 error?: (error: FirestoreError) => void;
273 complete?: () => void;
274 }): () => void;
275 onSnapshot(
276 options: SnapshotListenOptions,
277 observer: {
278 next?: (snapshot: DocumentSnapshot<T>) => void;
279 error?: (error: FirestoreError) => void;
280 complete?: () => void;
281 }
282 ): () => void;
283 onSnapshot(
284 onNext: (snapshot: DocumentSnapshot<T>) => void,
285 onError?: (error: FirestoreError) => void,
286 onCompletion?: () => void
287 ): () => void;
288 onSnapshot(
289 options: SnapshotListenOptions,
290 onNext: (snapshot: DocumentSnapshot<T>) => void,
291 onError?: (error: FirestoreError) => void,
292 onCompletion?: () => void
293 ): () => void;
294
295 withConverter(converter: null): DocumentReference<DocumentData>;
296 withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
297}
298
299export interface SnapshotOptions {
300 readonly serverTimestamps?: 'estimate' | 'previous' | 'none';
301}
302
303export interface SnapshotMetadata {
304 readonly hasPendingWrites: boolean;
305 readonly fromCache: boolean;
306
307 isEqual(other: SnapshotMetadata): boolean;
308}
309
310export class DocumentSnapshot<T = DocumentData> {
311 protected constructor();
312
313 readonly exists: boolean;
314 readonly ref: DocumentReference<T>;
315 readonly id: string;
316 readonly metadata: SnapshotMetadata;
317
318 data(options?: SnapshotOptions): T | undefined;
319
320 get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
321
322 isEqual(other: DocumentSnapshot<T>): boolean;
323}
324
325export class QueryDocumentSnapshot<
326 T = DocumentData
327> extends DocumentSnapshot<T> {
328 private constructor();
329
330 data(options?: SnapshotOptions): T;
331}
332
333export type OrderByDirection = 'desc' | 'asc';
334
335export type WhereFilterOp =
336 | '<'
337 | '<='
338 | '=='
339 | '!='
340 | '>='
341 | '>'
342 | 'array-contains'
343 | 'in'
344 | 'array-contains-any'
345 | 'not-in';
346
347export class Query<T = DocumentData> {
348 protected constructor();
349
350 readonly firestore: FirebaseFirestore;
351
352 where(
353 fieldPath: string | FieldPath,
354 opStr: WhereFilterOp,
355 value: any
356 ): Query<T>;
357
358 orderBy(
359 fieldPath: string | FieldPath,
360 directionStr?: OrderByDirection
361 ): Query<T>;
362
363 limit(limit: number): Query<T>;
364
365 limitToLast(limit: number): Query<T>;
366
367 startAt(snapshot: DocumentSnapshot<any>): Query<T>;
368 startAt(...fieldValues: any[]): Query<T>;
369
370 startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
371 startAfter(...fieldValues: any[]): Query<T>;
372
373 endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
374 endBefore(...fieldValues: any[]): Query<T>;
375
376 endAt(snapshot: DocumentSnapshot<any>): Query<T>;
377 endAt(...fieldValues: any[]): Query<T>;
378
379 isEqual(other: Query<T>): boolean;
380
381 get(options?: GetOptions): Promise<QuerySnapshot<T>>;
382
383 onSnapshot(observer: {
384 next?: (snapshot: QuerySnapshot<T>) => void;
385 error?: (error: FirestoreError) => void;
386 complete?: () => void;
387 }): () => void;
388 onSnapshot(
389 options: SnapshotListenOptions,
390 observer: {
391 next?: (snapshot: QuerySnapshot<T>) => void;
392 error?: (error: FirestoreError) => void;
393 complete?: () => void;
394 }
395 ): () => void;
396 onSnapshot(
397 onNext: (snapshot: QuerySnapshot<T>) => void,
398 onError?: (error: FirestoreError) => void,
399 onCompletion?: () => void
400 ): () => void;
401 onSnapshot(
402 options: SnapshotListenOptions,
403 onNext: (snapshot: QuerySnapshot<T>) => void,
404 onError?: (error: FirestoreError) => void,
405 onCompletion?: () => void
406 ): () => void;
407
408 withConverter(converter: null): Query<DocumentData>;
409 withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
410}
411
412export class QuerySnapshot<T = DocumentData> {
413 private constructor();
414
415 readonly query: Query<T>;
416 readonly metadata: SnapshotMetadata;
417 readonly docs: Array<QueryDocumentSnapshot<T>>;
418 readonly size: number;
419 readonly empty: boolean;
420
421 docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;
422
423 forEach(
424 callback: (result: QueryDocumentSnapshot<T>) => void,
425 thisArg?: any
426 ): void;
427
428 isEqual(other: QuerySnapshot<T>): boolean;
429}
430
431export type DocumentChangeType = 'added' | 'removed' | 'modified';
432
433export interface DocumentChange<T = DocumentData> {
434 readonly type: DocumentChangeType;
435 readonly doc: QueryDocumentSnapshot<T>;
436 readonly oldIndex: number;
437 readonly newIndex: number;
438}
439
440export class CollectionReference<T = DocumentData> extends Query<T> {
441 private constructor();
442
443 readonly id: string;
444 readonly parent: DocumentReference<DocumentData> | null;
445 readonly path: string;
446
447 doc(documentPath?: string): DocumentReference<T>;
448
449 add(data: T): Promise<DocumentReference<T>>;
450
451 isEqual(other: CollectionReference<T>): boolean;
452
453 withConverter(converter: null): CollectionReference<DocumentData>;
454 withConverter<U>(
455 converter: FirestoreDataConverter<U>
456 ): CollectionReference<U>;
457}
458
459export class FieldValue {
460 private constructor();
461
462 static serverTimestamp(): FieldValue;
463
464 static delete(): FieldValue;
465
466 static arrayUnion(...elements: any[]): FieldValue;
467
468 static arrayRemove(...elements: any[]): FieldValue;
469
470 static increment(n: number): FieldValue;
471
472 isEqual(other: FieldValue): boolean;
473}
474
475export class FieldPath {
476 constructor(...fieldNames: string[]);
477
478 static documentId(): FieldPath;
479
480 isEqual(other: FieldPath): boolean;
481}
482
483export type FirestoreErrorCode =
484 | 'cancelled'
485 | 'unknown'
486 | 'invalid-argument'
487 | 'deadline-exceeded'
488 | 'not-found'
489 | 'already-exists'
490 | 'permission-denied'
491 | 'resource-exhausted'
492 | 'failed-precondition'
493 | 'aborted'
494 | 'out-of-range'
495 | 'unimplemented'
496 | 'internal'
497 | 'unavailable'
498 | 'data-loss'
499 | 'unauthenticated';
500
501export interface FirestoreError {
502 code: FirestoreErrorCode;
503 message: string;
504 name: string;
505 stack?: string;
506}
507
508declare module '@firebase/component' {
509 interface NameServiceMapping {
510 'firestore-compat': FirebaseFirestore;
511 }
512}