Skip to content

Commit 4da7b48

Browse files
Add Firestore Lite API
1 parent babdcfd commit 4da7b48

File tree

1 file changed

+338
-0
lines changed

1 file changed

+338
-0
lines changed

packages/firestore/lite/index.d.ts

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
/**
2+
* @license
3+
* Copyright 2020 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+
18+
import { FirebaseApp } from '@firebase/app-types';
19+
20+
export type DocumentData = { [field: string]: any };
21+
22+
export type UpdateData = { [fieldPath: string]: any };
23+
24+
export interface Settings {
25+
host?: string;
26+
ssl?: boolean;
27+
ignoreUndefinedProperties?: boolean;
28+
}
29+
30+
export type LogLevel = 'debug' | 'error' | 'silent';
31+
32+
export function setLogLevel(logLevel: LogLevel): void;
33+
34+
export interface FirestoreDataConverter<T> {
35+
toFirestore(modelObject: T): DocumentData;
36+
fromFirestore(snapshot: QueryDocumentSnapshot): T; // No SnapshotOptions
37+
}
38+
39+
export class FirebaseFirestore {
40+
private constructor();
41+
}
42+
43+
export function initializeFirestore(
44+
app: FirebaseApp,
45+
settings: Settings
46+
): FirebaseFirestore;
47+
export function getFirestore(app: FirebaseApp): FirebaseFirestore;
48+
export function terminate(firestore: FirebaseFirestore): Promise<void>;
49+
export function writeBatch(firestore: FirebaseFirestore): WriteBatch;
50+
export function runTransaction<T>(
51+
firestore: FirebaseFirestore,
52+
updateFunction: (transaction: Transaction) => Promise<T>
53+
): Promise<T>;
54+
55+
export function collection(
56+
firestore: FirebaseFirestore,
57+
collectionPath: string
58+
): CollectionReference<DocumentData>;
59+
export function collection(
60+
reference: DocumentReference,
61+
collectionPath: string
62+
): CollectionReference<DocumentData>;
63+
export function doc(
64+
firestore: FirebaseFirestore,
65+
documentPath: string
66+
): DocumentReference<DocumentData>;
67+
export function doc<T>(
68+
reference: CollectionReference<T>,
69+
documentPath?: string
70+
): DocumentReference<T>;
71+
export function collectionGroup(
72+
firestore: FirebaseFirestore,
73+
collectionId: string
74+
): Query<DocumentData>;
75+
export function parent(
76+
reference: CollectionReference<unknown>
77+
): DocumentReference<DocumentData> | null;
78+
export function parent<T>(
79+
reference: DocumentReference<T>
80+
): CollectionReference<T>;
81+
82+
export class GeoPoint {
83+
constructor(latitude: number, longitude: number);
84+
85+
readonly latitude: number;
86+
readonly longitude: number;
87+
88+
isEqual(other: GeoPoint): boolean;
89+
}
90+
91+
export class Timestamp {
92+
constructor(seconds: number, nanoseconds: number);
93+
94+
static now(): Timestamp;
95+
96+
static fromDate(date: Date): Timestamp;
97+
98+
static fromMillis(milliseconds: number): Timestamp;
99+
100+
readonly seconds: number;
101+
readonly nanoseconds: number;
102+
103+
toDate(): Date;
104+
105+
toMillis(): number;
106+
107+
isEqual(other: Timestamp): boolean;
108+
109+
valueOf(): string;
110+
}
111+
112+
export class Blob {
113+
private constructor();
114+
115+
static fromBase64String(base64: string): Blob;
116+
117+
static fromUint8Array(array: Uint8Array): Blob;
118+
119+
public toBase64(): string;
120+
121+
public toUint8Array(): Uint8Array;
122+
123+
isEqual(other: Blob): boolean;
124+
}
125+
126+
export class Transaction {
127+
private constructor();
128+
129+
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
130+
131+
set<T>(
132+
documentRef: DocumentReference<T>,
133+
data: T,
134+
options?: SetOptions
135+
): Transaction;
136+
137+
update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
138+
update(
139+
documentRef: DocumentReference<any>,
140+
field: string | FieldPath,
141+
value: any,
142+
...moreFieldsAndValues: any[]
143+
): Transaction;
144+
145+
delete(documentRef: DocumentReference<any>): Transaction;
146+
}
147+
148+
export class WriteBatch {
149+
private constructor();
150+
151+
set<T>(
152+
documentRef: DocumentReference<T>,
153+
data: T,
154+
options?: SetOptions
155+
): WriteBatch;
156+
157+
update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
158+
update(
159+
documentRef: DocumentReference<any>,
160+
field: string | FieldPath,
161+
value: any,
162+
...moreFieldsAndValues: any[]
163+
): WriteBatch;
164+
165+
delete(documentRef: DocumentReference<any>): WriteBatch;
166+
167+
commit(): Promise<void>;
168+
}
169+
170+
export type SetOptions =
171+
| { merge: true }
172+
| { mergeFields: Array<string | FieldPath> };
173+
174+
export class DocumentReference<T = DocumentData> {
175+
private constructor();
176+
readonly id: string;
177+
readonly firestore: FirebaseFirestore;
178+
readonly path: string;
179+
withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
180+
}
181+
182+
export class DocumentSnapshot<T = DocumentData> {
183+
readonly ref: DocumentReference<T>;
184+
readonly id: string;
185+
exists(): this is QueryDocumentSnapshot<T>;
186+
data(): T | undefined;
187+
get(fieldPath: string | FieldPath): any;
188+
}
189+
190+
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
191+
T
192+
> {
193+
data(): T;
194+
}
195+
196+
export type OrderByDirection = 'desc' | 'asc';
197+
198+
export type WhereFilterOp =
199+
| '<'
200+
| '<='
201+
| '=='
202+
| '>='
203+
| '>'
204+
| 'array-contains'
205+
| 'in'
206+
| 'array-contains-any';
207+
208+
export class Query<T = DocumentData> {
209+
protected constructor();
210+
readonly firestore: FirebaseFirestore;
211+
where(
212+
fieldPath: string | FieldPath,
213+
opStr: WhereFilterOp,
214+
value: any
215+
): Query<T>;
216+
orderBy(
217+
fieldPath: string | FieldPath,
218+
directionStr?: OrderByDirection
219+
): Query<T>;
220+
limit(limit: number): Query<T>;
221+
limitToLast(limit: number): Query<T>;
222+
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
223+
startAt(...fieldValues: any[]): Query<T>;
224+
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
225+
startAfter(...fieldValues: any[]): Query<T>;
226+
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
227+
endBefore(...fieldValues: any[]): Query<T>;
228+
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
229+
endAt(...fieldValues: any[]): Query<T>;
230+
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
231+
}
232+
233+
export class QuerySnapshot<T = DocumentData> {
234+
readonly query: Query<T>;
235+
readonly docs: Array<QueryDocumentSnapshot<T>>;
236+
readonly size: number;
237+
readonly empty: boolean;
238+
forEach(
239+
callback: (result: QueryDocumentSnapshot<T>) => void,
240+
thisArg?: any
241+
): void;
242+
}
243+
244+
export class CollectionReference<T = DocumentData> extends Query<T> {
245+
readonly id: string;
246+
readonly path: string;
247+
withConverter<U>(
248+
converter: FirestoreDataConverter<U>
249+
): CollectionReference<U>;
250+
}
251+
252+
export function getDoc<T>(
253+
reference: DocumentReference<T>
254+
): Promise<DocumentSnapshot<T>>;
255+
export function getQuery<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
256+
257+
export function addDoc<T>(
258+
reference: CollectionReference<T>,
259+
data: T
260+
): Promise<DocumentSnapshot<T>>;
261+
export function setDoc<T>(
262+
reference: DocumentReference<T>,
263+
data: T
264+
): Promise<void>;
265+
export function setDoc<T>(
266+
reference: DocumentReference<T>,
267+
data: Partial<T>,
268+
options: SetOptions
269+
): Promise<void>;
270+
export function updateDoc(
271+
reference: DocumentReference,
272+
data: UpdateData
273+
): Promise<void>;
274+
export function updateDoc(
275+
field: string | FieldPath,
276+
value: any,
277+
...moreFieldsAndValues: any[]
278+
): Promise<void>;
279+
export function deleteDoc(reference: DocumentReference): Promise<void>;
280+
281+
export class FieldValue {
282+
private constructor();
283+
isEqual(other: FieldValue): boolean;
284+
}
285+
286+
export function serverTimestamp(): FieldValue;
287+
export function deleteField(): FieldValue;
288+
export function arrayUnion(...elements: any[]): FieldValue;
289+
export function arrayRemove(...elements: any[]): FieldValue;
290+
export function increment(n: number): FieldValue;
291+
292+
export class FieldPath {
293+
constructor(...fieldNames: string[]);
294+
isEqual(other: FieldPath): boolean;
295+
}
296+
297+
export function documentId(): FieldPath;
298+
299+
export function refEqual(
300+
l: DocumentReference | CollectionReference,
301+
r: DocumentReference | CollectionReference
302+
): boolean;
303+
export function queryEqual(l: Query, r: Query): boolean;
304+
export function snapshotEqual(
305+
l: DocumentSnapshot | QuerySnapshot,
306+
r: DocumentSnapshot | QuerySnapshot
307+
): boolean;
308+
309+
export type FirestoreErrorCode =
310+
| 'cancelled'
311+
| 'unknown'
312+
| 'invalid-argument'
313+
| 'deadline-exceeded'
314+
| 'not-found'
315+
| 'already-exists'
316+
| 'permission-denied'
317+
| 'resource-exhausted'
318+
| 'failed-precondition'
319+
| 'aborted'
320+
| 'out-of-range'
321+
| 'unimplemented'
322+
| 'internal'
323+
| 'unavailable'
324+
| 'data-loss'
325+
| 'unauthenticated';
326+
327+
export interface FirestoreError {
328+
code: FirestoreErrorCode;
329+
message: string;
330+
name: string;
331+
stack?: string;
332+
}
333+
334+
declare module '@firebase/component' {
335+
interface NameServiceMapping {
336+
'firestore/lite': FirebaseFirestore;
337+
}
338+
}

0 commit comments

Comments
 (0)