Skip to content

Commit 59687d3

Browse files
Add Compat layer for FieldValue
1 parent 7bf7379 commit 59687d3

File tree

9 files changed

+209
-268
lines changed

9 files changed

+209
-268
lines changed

packages/firestore/exp/test/shim.ts

Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ import * as exp from '../index';
2323

2424
import {
2525
addDoc,
26-
arrayRemove,
27-
arrayUnion,
2826
clearIndexedDbPersistence,
2927
collection,
3028
collectionGroup,
3129
deleteDoc,
32-
deleteField,
3330
disableNetwork,
3431
doc,
3532
DocumentReference as DocumentReferenceExp,
@@ -43,15 +40,13 @@ import {
4340
getDocs,
4441
getDocsFromCache,
4542
getDocsFromServer,
46-
increment,
4743
initializeFirestore,
4844
onSnapshot,
4945
onSnapshotsInSync,
5046
query,
5147
queryEqual,
5248
refEqual,
5349
runTransaction,
54-
serverTimestamp,
5550
setDoc,
5651
snapshotEqual,
5752
terminate,
@@ -71,18 +66,20 @@ import {
7166
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
7267
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
7368
import { isPlainObject } from '../../src/util/input_validation';
69+
import { Compat } from '../../src/compat/compat';
7470

7571
export { GeoPoint, Timestamp } from '../index';
72+
export { FieldValue } from '../../src/compat/field_value';
7673

7774
/* eslint-disable @typescript-eslint/no-explicit-any */
7875

7976
// This module defines a shim layer that implements the legacy API on top
8077
// of the experimental SDK. This shim is used to run integration tests against
8178
// both SDK versions.
8279

83-
export class FirebaseApp implements FirebaseAppLegacy {
84-
constructor(readonly _delegate: FirebaseAppExp) {}
85-
80+
export class FirebaseApp
81+
extends Compat<FirebaseAppExp>
82+
implements FirebaseAppLegacy {
8683
name = this._delegate.name;
8784
options = this._delegate.options;
8885
automaticDataCollectionEnabled = this._delegate
@@ -93,9 +90,9 @@ export class FirebaseApp implements FirebaseAppLegacy {
9390
}
9491
}
9592

96-
export class FirebaseFirestore implements legacy.FirebaseFirestore {
97-
constructor(private readonly _delegate: exp.FirebaseFirestore) {}
98-
93+
export class FirebaseFirestore
94+
extends Compat<exp.FirebaseFirestore>
95+
implements legacy.FirebaseFirestore {
9996
app = new FirebaseApp(this._delegate.app);
10097

10198
settings(settings: legacy.Settings): void {
@@ -170,11 +167,15 @@ export class FirebaseFirestore implements legacy.FirebaseFirestore {
170167
};
171168
}
172169

173-
export class Transaction implements legacy.Transaction {
170+
export class Transaction
171+
extends Compat<exp.Transaction>
172+
implements legacy.Transaction {
174173
constructor(
175174
private readonly _firestore: FirebaseFirestore,
176-
private readonly _delegate: exp.Transaction
177-
) {}
175+
private readonly delegate: exp.Transaction
176+
) {
177+
super(delegate);
178+
}
178179

179180
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
180181
return this._delegate
@@ -231,9 +232,9 @@ export class Transaction implements legacy.Transaction {
231232
}
232233
}
233234

234-
export class WriteBatch implements legacy.WriteBatch {
235-
constructor(private readonly _delegate: exp.WriteBatch) {}
236-
235+
export class WriteBatch
236+
extends Compat<exp.WriteBatch>
237+
implements legacy.WriteBatch {
237238
set<T>(
238239
documentRef: DocumentReference<T>,
239240
data: T,
@@ -288,11 +289,14 @@ export class WriteBatch implements legacy.WriteBatch {
288289
}
289290

290291
export class DocumentReference<T = legacy.DocumentData>
292+
extends Compat<exp.DocumentReference<T>>
291293
implements legacy.DocumentReference<T> {
292294
constructor(
293295
readonly firestore: FirebaseFirestore,
294-
readonly _delegate: exp.DocumentReference<T>
295-
) {}
296+
delegate: exp.DocumentReference<T>
297+
) {
298+
super(delegate);
299+
}
296300

297301
readonly id = this._delegate.id;
298302
readonly path = this._delegate.path;
@@ -407,11 +411,14 @@ export class DocumentReference<T = legacy.DocumentData>
407411
}
408412

409413
export class DocumentSnapshot<T = legacy.DocumentData>
414+
extends Compat<exp.DocumentSnapshot<T>>
410415
implements legacy.DocumentSnapshot<T> {
411416
constructor(
412417
private readonly _firestore: FirebaseFirestore,
413-
readonly _delegate: exp.DocumentSnapshot<T>
414-
) {}
418+
delegate: exp.DocumentSnapshot<T>
419+
) {
420+
super(delegate);
421+
}
415422

416423
readonly ref = new DocumentReference<T>(this._firestore, this._delegate.ref);
417424
readonly id = this._delegate.id;
@@ -449,11 +456,12 @@ export class QueryDocumentSnapshot<T = legacy.DocumentData>
449456
}
450457
}
451458

452-
export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
453-
constructor(
454-
readonly firestore: FirebaseFirestore,
455-
readonly _delegate: exp.Query<T>
456-
) {}
459+
export class Query<T = legacy.DocumentData>
460+
extends Compat<exp.Query<T>>
461+
implements legacy.Query<T> {
462+
constructor(readonly firestore: FirebaseFirestore, delegate: exp.Query<T>) {
463+
super(delegate);
464+
}
457465

458466
where(
459467
fieldPath: string | FieldPath,
@@ -680,34 +688,6 @@ export class CollectionReference<T = legacy.DocumentData>
680688
}
681689
}
682690

683-
export class FieldValue implements legacy.FieldValue {
684-
constructor(readonly _delegate: exp.FieldValue) {}
685-
686-
static serverTimestamp(): FieldValue {
687-
return new FieldValue(serverTimestamp());
688-
}
689-
690-
static delete(): FieldValue {
691-
return new FieldValue(deleteField());
692-
}
693-
694-
static arrayUnion(...elements: any[]): FieldValue {
695-
return new FieldValue(arrayUnion(...unwrap(elements)));
696-
}
697-
698-
static arrayRemove(...elements: any[]): FieldValue {
699-
return new FieldValue(arrayRemove(...unwrap(elements)));
700-
}
701-
702-
static increment(n: number): FieldValue {
703-
return new FieldValue(increment(n));
704-
}
705-
706-
isEqual(other: FieldValue): boolean {
707-
return this._delegate.isEqual(other._delegate);
708-
}
709-
}
710-
711691
export class FieldPath implements legacy.FieldPath {
712692
private readonly fieldNames: string[];
713693

@@ -728,9 +708,7 @@ export class FieldPath implements legacy.FieldPath {
728708
}
729709
}
730710

731-
export class Blob implements legacy.Blob {
732-
constructor(readonly _delegate: BytesExp) {}
733-
711+
export class Blob extends Compat<BytesExp> implements legacy.Blob {
734712
static fromBase64String(base64: string): Blob {
735713
return new Blob(BytesExp.fromBase64String(base64));
736714
}
@@ -790,17 +768,9 @@ function wrap(value: any): any {
790768
function unwrap(value: any): any {
791769
if (Array.isArray(value)) {
792770
return value.map(v => unwrap(v));
793-
} else if (value instanceof FieldPath) {
794-
return value._delegate;
795-
} else if (value instanceof FieldValue) {
771+
} else if (value instanceof Compat) {
796772
return value._delegate;
797-
} else if (value instanceof Blob) {
798-
return value._delegate;
799-
} else if (value instanceof DocumentReference) {
800-
return value._delegate;
801-
} else if (value instanceof DocumentSnapshot) {
802-
return value._delegate;
803-
} else if (value instanceof QueryDocumentSnapshot) {
773+
} else if (value instanceof FieldPath) {
804774
return value._delegate;
805775
} else if (isPlainObject(value)) {
806776
const obj: any = {};

packages/firestore/lite/src/api/field_value.ts

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,71 +24,32 @@ import {
2424
_SerializableFieldValue,
2525
ServerTimestampFieldValueImpl
2626
} from '../../../src/api/field_value';
27-
import { ParseContext } from '../../../src/api/user_data_reader';
28-
import { FieldTransform } from '../../../src/model/mutation';
2927

3028
/** The public FieldValue class of the lite API. */
31-
export abstract class FieldValue extends _SerializableFieldValue {}
32-
33-
/**
34-
* A delegate class that allows the FieldValue implementations returned by
35-
* deleteField(), serverTimestamp(), arrayUnion(), arrayRemove() and
36-
* increment() to be an instance of the lite FieldValue class declared above.
37-
*
38-
* We don't directly subclass `FieldValue` in the various field value
39-
* implementations as the base FieldValue class differs between the lite, full
40-
* and legacy SDK.
41-
*/
42-
class FieldValueDelegate extends FieldValue {
43-
readonly _methodName: string;
44-
45-
constructor(readonly _delegate: _SerializableFieldValue) {
46-
super();
47-
this._methodName = _delegate._methodName;
48-
}
49-
50-
_toFieldTransform(context: ParseContext): FieldTransform | null {
51-
return this._delegate._toFieldTransform(context);
52-
}
53-
54-
isEqual(other: FieldValue): boolean {
55-
if (!(other instanceof FieldValueDelegate)) {
56-
return false;
57-
}
58-
return this._delegate.isEqual(other._delegate);
59-
}
60-
}
29+
export abstract class FieldValue {}
6130

6231
export function deleteField(): FieldValue {
63-
return new FieldValueDelegate(new DeleteFieldValueImpl('deleteField'));
32+
return new DeleteFieldValueImpl('deleteField');
6433
}
6534

6635
export function serverTimestamp(): FieldValue {
67-
return new FieldValueDelegate(
68-
new ServerTimestampFieldValueImpl('serverTimestamp')
69-
);
36+
return new ServerTimestampFieldValueImpl('serverTimestamp');
7037
}
7138

7239
export function arrayUnion(...elements: unknown[]): FieldValue {
7340
validateAtLeastNumberOfArgs('arrayUnion()', arguments, 1);
7441
// NOTE: We don't actually parse the data until it's used in set() or
7542
// update() since we'd need the Firestore instance to do this.
76-
return new FieldValueDelegate(
77-
new ArrayUnionFieldValueImpl('arrayUnion', elements)
78-
);
43+
return new ArrayUnionFieldValueImpl('arrayUnion', elements);
7944
}
8045

8146
export function arrayRemove(...elements: unknown[]): FieldValue {
8247
validateAtLeastNumberOfArgs('arrayRemove()', arguments, 1);
8348
// NOTE: We don't actually parse the data until it's used in set() or
8449
// update() since we'd need the Firestore instance to do this.
85-
return new FieldValueDelegate(
86-
new ArrayRemoveFieldValueImpl('arrayRemove', elements)
87-
);
50+
return new ArrayRemoveFieldValueImpl('arrayRemove', elements);
8851
}
8952

9053
export function increment(n: number): FieldValue {
91-
return new FieldValueDelegate(
92-
new NumericIncrementFieldValueImpl('increment', n)
93-
);
54+
return new NumericIncrementFieldValueImpl('increment', n);
9455
}

0 commit comments

Comments
 (0)