UNPKG

12.3 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 { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
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(host: string, port: number): void;
65
66 enablePersistence(settings?: PersistenceSettings): Promise<void>;
67
68 collection(collectionPath: string): CollectionReference<DocumentData>;
69
70 doc(documentPath: string): DocumentReference<DocumentData>;
71
72 collectionGroup(collectionId: string): Query<DocumentData>;
73
74 runTransaction<T>(
75 updateFunction: (transaction: Transaction) => Promise<T>
76 ): Promise<T>;
77
78 batch(): WriteBatch;
79
80 app: any;
81
82 clearPersistence(): Promise<void>;
83
84 enableNetwork(): Promise<void>;
85
86 disableNetwork(): Promise<void>;
87
88 waitForPendingWrites(): Promise<void>;
89
90 onSnapshotsInSync(observer: {
91 next?: (value: void) => void;
92 error?: (error: FirestoreError) => void;
93 complete?: () => void;
94 }): () => void;
95 onSnapshotsInSync(onSync: () => void): () => void;
96
97 terminate(): Promise<void>;
98
99 loadBundle(
100 bundleData: ArrayBuffer | ReadableStream<Uint8Array> | string
101 ): LoadBundleTask;
102
103 namedQuery(name: string): Promise<Query<DocumentData> | null>;
104
105 INTERNAL: { delete: () => Promise<void> };
106}
107
108export interface LoadBundleTask extends PromiseLike<LoadBundleTaskProgress> {
109 onProgress(
110 next?: (progress: LoadBundleTaskProgress) => any,
111 error?: (error: Error) => any,
112 complete?: () => void
113 ): void;
114
115 then<T, R>(
116 onFulfilled?: (a: LoadBundleTaskProgress) => T | PromiseLike<T>,
117 onRejected?: (a: Error) => R | PromiseLike<R>
118 ): Promise<T | R>;
119
120 catch<R>(
121 onRejected: (a: Error) => R | PromiseLike<R>
122 ): Promise<R | LoadBundleTaskProgress>;
123}
124
125export interface LoadBundleTaskProgress {
126 documentsLoaded: number;
127 totalDocuments: number;
128 bytesLoaded: number;
129 totalBytes: number;
130 taskState: TaskState;
131}
132
133export type TaskState = 'Error' | 'Running' | 'Success';
134
135export class GeoPoint {
136 constructor(latitude: number, longitude: number);
137
138 readonly latitude: number;
139 readonly longitude: number;
140
141 isEqual(other: GeoPoint): boolean;
142}
143
144export class Timestamp {
145 constructor(seconds: number, nanoseconds: number);
146
147 static now(): Timestamp;
148
149 static fromDate(date: Date): Timestamp;
150
151 static fromMillis(milliseconds: number): Timestamp;
152
153 readonly seconds: number;
154 readonly nanoseconds: number;
155
156 toDate(): Date;
157
158 toMillis(): number;
159
160 isEqual(other: Timestamp): boolean;
161
162 valueOf(): string;
163}
164
165export class Blob {
166 private constructor();
167
168 static fromBase64String(base64: string): Blob;
169
170 static fromUint8Array(array: Uint8Array): Blob;
171
172 public toBase64(): string;
173
174 public toUint8Array(): Uint8Array;
175
176 isEqual(other: Blob): boolean;
177}
178
179export class Transaction {
180 private constructor();
181
182 get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
183
184 set<T>(
185 documentRef: DocumentReference<T>,
186 data: Partial<T>,
187 options: SetOptions
188 ): Transaction;
189 set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
190
191 update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
192 update(
193 documentRef: DocumentReference<any>,
194 field: string | FieldPath,
195 value: any,
196 ...moreFieldsAndValues: any[]
197 ): Transaction;
198
199 delete(documentRef: DocumentReference<any>): Transaction;
200}
201
202export class WriteBatch {
203 private constructor();
204
205 set<T>(
206 documentRef: DocumentReference<T>,
207 data: Partial<T>,
208 options: SetOptions
209 ): WriteBatch;
210 set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
211
212 update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
213 update(
214 documentRef: DocumentReference<any>,
215 field: string | FieldPath,
216 value: any,
217 ...moreFieldsAndValues: any[]
218 ): WriteBatch;
219
220 delete(documentRef: DocumentReference<any>): WriteBatch;
221
222 commit(): Promise<void>;
223}
224
225export interface SnapshotListenOptions {
226 readonly includeMetadataChanges?: boolean;
227}
228
229export interface SetOptions {
230 readonly merge?: boolean;
231 readonly mergeFields?: (string | FieldPath)[];
232}
233
234export interface GetOptions {
235 readonly source?: 'default' | 'server' | 'cache';
236}
237
238export class DocumentReference<T = DocumentData> {
239 private constructor();
240
241 readonly id: string;
242 readonly firestore: FirebaseFirestore;
243 readonly parent: CollectionReference<T>;
244 readonly path: string;
245
246 collection(collectionPath: string): CollectionReference<DocumentData>;
247
248 isEqual(other: DocumentReference<T>): boolean;
249
250 set(data: Partial<T>, options: SetOptions): Promise<void>;
251 set(data: T): Promise<void>;
252
253 update(data: UpdateData): Promise<void>;
254 update(
255 field: string | FieldPath,
256 value: any,
257 ...moreFieldsAndValues: any[]
258 ): Promise<void>;
259
260 delete(): Promise<void>;
261
262 get(options?: GetOptions): Promise<DocumentSnapshot<T>>;
263
264 onSnapshot(observer: {
265 next?: (snapshot: DocumentSnapshot<T>) => void;
266 error?: (error: FirestoreError) => void;
267 complete?: () => void;
268 }): () => void;
269 onSnapshot(
270 options: SnapshotListenOptions,
271 observer: {
272 next?: (snapshot: DocumentSnapshot<T>) => void;
273 error?: (error: FirestoreError) => void;
274 complete?: () => void;
275 }
276 ): () => void;
277 onSnapshot(
278 onNext: (snapshot: DocumentSnapshot<T>) => void,
279 onError?: (error: FirestoreError) => void,
280 onCompletion?: () => void
281 ): () => void;
282 onSnapshot(
283 options: SnapshotListenOptions,
284 onNext: (snapshot: DocumentSnapshot<T>) => void,
285 onError?: (error: FirestoreError) => void,
286 onCompletion?: () => void
287 ): () => void;
288
289 withConverter(converter: null): DocumentReference<DocumentData>;
290 withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
291}
292
293export interface SnapshotOptions {
294 readonly serverTimestamps?: 'estimate' | 'previous' | 'none';
295}
296
297export interface SnapshotMetadata {
298 readonly hasPendingWrites: boolean;
299 readonly fromCache: boolean;
300
301 isEqual(other: SnapshotMetadata): boolean;
302}
303
304export class DocumentSnapshot<T = DocumentData> {
305 protected constructor();
306
307 readonly exists: boolean;
308 readonly ref: DocumentReference<T>;
309 readonly id: string;
310 readonly metadata: SnapshotMetadata;
311
312 data(options?: SnapshotOptions): T | undefined;
313
314 get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
315
316 isEqual(other: DocumentSnapshot<T>): boolean;
317}
318
319export class QueryDocumentSnapshot<
320 T = DocumentData
321> extends DocumentSnapshot<T> {
322 private constructor();
323
324 data(options?: SnapshotOptions): T;
325}
326
327export type OrderByDirection = 'desc' | 'asc';
328
329export type WhereFilterOp =
330 | '<'
331 | '<='
332 | '=='
333 | '!='
334 | '>='
335 | '>'
336 | 'array-contains'
337 | 'in'
338 | 'array-contains-any'
339 | 'not-in';
340
341export class Query<T = DocumentData> {
342 protected constructor();
343
344 readonly firestore: FirebaseFirestore;
345
346 where(
347 fieldPath: string | FieldPath,
348 opStr: WhereFilterOp,
349 value: any
350 ): Query<T>;
351
352 orderBy(
353 fieldPath: string | FieldPath,
354 directionStr?: OrderByDirection
355 ): Query<T>;
356
357 limit(limit: number): Query<T>;
358
359 limitToLast(limit: number): Query<T>;
360
361 startAt(snapshot: DocumentSnapshot<any>): Query<T>;
362 startAt(...fieldValues: any[]): Query<T>;
363
364 startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
365 startAfter(...fieldValues: any[]): Query<T>;
366
367 endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
368 endBefore(...fieldValues: any[]): Query<T>;
369
370 endAt(snapshot: DocumentSnapshot<any>): Query<T>;
371 endAt(...fieldValues: any[]): Query<T>;
372
373 isEqual(other: Query<T>): boolean;
374
375 get(options?: GetOptions): Promise<QuerySnapshot<T>>;
376
377 onSnapshot(observer: {
378 next?: (snapshot: QuerySnapshot<T>) => void;
379 error?: (error: FirestoreError) => void;
380 complete?: () => void;
381 }): () => void;
382 onSnapshot(
383 options: SnapshotListenOptions,
384 observer: {
385 next?: (snapshot: QuerySnapshot<T>) => void;
386 error?: (error: FirestoreError) => void;
387 complete?: () => void;
388 }
389 ): () => void;
390 onSnapshot(
391 onNext: (snapshot: QuerySnapshot<T>) => void,
392 onError?: (error: FirestoreError) => void,
393 onCompletion?: () => void
394 ): () => void;
395 onSnapshot(
396 options: SnapshotListenOptions,
397 onNext: (snapshot: QuerySnapshot<T>) => void,
398 onError?: (error: FirestoreError) => void,
399 onCompletion?: () => void
400 ): () => void;
401
402 withConverter(converter: null): Query<DocumentData>;
403 withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
404}
405
406export class QuerySnapshot<T = DocumentData> {
407 private constructor();
408
409 readonly query: Query<T>;
410 readonly metadata: SnapshotMetadata;
411 readonly docs: Array<QueryDocumentSnapshot<T>>;
412 readonly size: number;
413 readonly empty: boolean;
414
415 docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;
416
417 forEach(
418 callback: (result: QueryDocumentSnapshot<T>) => void,
419 thisArg?: any
420 ): void;
421
422 isEqual(other: QuerySnapshot<T>): boolean;
423}
424
425export type DocumentChangeType = 'added' | 'removed' | 'modified';
426
427export interface DocumentChange<T = DocumentData> {
428 readonly type: DocumentChangeType;
429 readonly doc: QueryDocumentSnapshot<T>;
430 readonly oldIndex: number;
431 readonly newIndex: number;
432}
433
434export class CollectionReference<T = DocumentData> extends Query<T> {
435 private constructor();
436
437 readonly id: string;
438 readonly parent: DocumentReference<DocumentData> | null;
439 readonly path: string;
440
441 doc(documentPath?: string): DocumentReference<T>;
442
443 add(data: T): Promise<DocumentReference<T>>;
444
445 isEqual(other: CollectionReference<T>): boolean;
446
447 withConverter(converter: null): CollectionReference<DocumentData>;
448 withConverter<U>(
449 converter: FirestoreDataConverter<U>
450 ): CollectionReference<U>;
451}
452
453export class FieldValue {
454 private constructor();
455
456 static serverTimestamp(): FieldValue;
457
458 static delete(): FieldValue;
459
460 static arrayUnion(...elements: any[]): FieldValue;
461
462 static arrayRemove(...elements: any[]): FieldValue;
463
464 static increment(n: number): FieldValue;
465
466 isEqual(other: FieldValue): boolean;
467}
468
469export class FieldPath {
470 constructor(...fieldNames: string[]);
471
472 static documentId(): FieldPath;
473
474 isEqual(other: FieldPath): boolean;
475}
476
477export type FirestoreErrorCode =
478 | 'cancelled'
479 | 'unknown'
480 | 'invalid-argument'
481 | 'deadline-exceeded'
482 | 'not-found'
483 | 'already-exists'
484 | 'permission-denied'
485 | 'resource-exhausted'
486 | 'failed-precondition'
487 | 'aborted'
488 | 'out-of-range'
489 | 'unimplemented'
490 | 'internal'
491 | 'unavailable'
492 | 'data-loss'
493 | 'unauthenticated';
494
495export interface FirestoreError {
496 code: FirestoreErrorCode;
497 message: string;
498 name: string;
499 stack?: string;
500}
501
502declare module '@firebase/component' {
503 interface NameServiceMapping {
504 'firestore': FirebaseFirestore;
505 }
506}