Skip to content

Commit c71eee8

Browse files
Use App Compat
1 parent b709c3d commit c71eee8

File tree

11 files changed

+127
-52
lines changed

11 files changed

+127
-52
lines changed

packages/firestore/exp/test/shim.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp as FirebaseAppLegacy } from '@firebase/app-types';
19-
import { FirebaseApp as FirebaseAppExp } from '@firebase/app-types-exp';
20-
import { deleteApp } from '@firebase/app-exp';
2118
import * as legacy from '@firebase/firestore-types';
2219
import * as exp from '../index';
2320

@@ -68,19 +65,6 @@ export { GeoPoint, Timestamp } from '../index';
6865
// of the experimental SDK. This shim is used to run integration tests against
6966
// both SDK versions.
7067

71-
export class FirebaseApp
72-
extends Compat<FirebaseAppExp>
73-
implements FirebaseAppLegacy {
74-
name = this._delegate.name;
75-
options = this._delegate.options;
76-
automaticDataCollectionEnabled = this._delegate
77-
.automaticDataCollectionEnabled;
78-
79-
delete(): Promise<void> {
80-
return deleteApp(this._delegate);
81-
}
82-
}
83-
8468
export class Transaction
8569
extends Compat<exp.Transaction>
8670
implements legacy.Transaction {

packages/firestore/index.console.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
export { Firestore } from './src/api/database';
18+
import { FirebaseFirestore as FirestoreExp } from './exp/src/api/database';
19+
import {
20+
Firestore as FirestoreCompat,
21+
MemoryPersistenceProvider
22+
} from './src/api/database';
23+
import { FirestoreDatabase } from './lite/src/api/database';
24+
import { Provider } from '@firebase/component';
25+
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
1926
export {
2027
CollectionReference,
2128
DocumentReference,
@@ -27,3 +34,16 @@ export { GeoPoint } from './src/api/geo_point';
2734
export { FieldPath } from './src/api/field_path';
2835
export { FieldValue } from './src/compat/field_value';
2936
export { Timestamp } from './src/api/timestamp';
37+
38+
export class Firestore extends FirestoreCompat {
39+
constructor(
40+
databaseId: FirestoreDatabase,
41+
authProvider: Provider<FirebaseAuthInternalName>
42+
) {
43+
super(
44+
databaseId,
45+
new FirestoreExp(databaseId, authProvider),
46+
new MemoryPersistenceProvider()
47+
);
48+
}
49+
}

packages/firestore/index.memory.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

2121
import { Firestore, MemoryPersistenceProvider } from './src/api/database';
22+
import { FirebaseFirestore } from './exp/src/api/database';
2223
import { configureForFirebase } from './src/config';
2324

2425
import './register-module';
@@ -31,7 +32,12 @@ import { name, version } from './package.json';
3132
export function registerFirestore(instance: FirebaseNamespace): void {
3233
configureForFirebase(
3334
instance,
34-
(app, auth) => new Firestore(app, auth, new MemoryPersistenceProvider())
35+
(app, auth) =>
36+
new Firestore(
37+
app,
38+
new FirebaseFirestore(app, auth),
39+
new MemoryPersistenceProvider()
40+
)
3541
);
3642
instance.registerVersion(name, version);
3743
}

packages/firestore/index.node.memory.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

2121
import { Firestore, MemoryPersistenceProvider } from './src/api/database';
22+
import { FirebaseFirestore } from './exp/src/api/database';
2223
import { configureForFirebase } from './src/config';
2324
import './register-module';
2425

@@ -31,7 +32,12 @@ import { name, version } from './package.json';
3132
export function registerFirestore(instance: FirebaseNamespace): void {
3233
configureForFirebase(
3334
instance,
34-
(app, auth) => new Firestore(app, auth, new MemoryPersistenceProvider())
35+
(app, auth) =>
36+
new Firestore(
37+
app,
38+
new FirebaseFirestore(app, auth),
39+
new MemoryPersistenceProvider()
40+
)
3541
);
3642
instance.registerVersion(name, version, 'node');
3743
}

packages/firestore/index.node.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ import { FirebaseNamespace } from '@firebase/app-types';
1919

2020
import { Firestore, IndexedDbPersistenceProvider } from './src/api/database';
2121
import { configureForFirebase } from './src/config';
22+
import { FirebaseFirestore } from './exp/src/api/database';
2223

2324
import './register-module';
2425

2526
import { name, version } from './package.json';
26-
2727
/**
2828
* Registers the main Firestore Node build with the components framework.
2929
* Persistence can be enabled via `firebase.firestore().enablePersistence()`.
3030
*/
3131
export function registerFirestore(instance: FirebaseNamespace): void {
32-
configureForFirebase(instance, (app, auth) => {
33-
return new Firestore(app, auth, new IndexedDbPersistenceProvider());
34-
});
32+
configureForFirebase(
33+
instance,
34+
(app, auth) =>
35+
new Firestore(
36+
app,
37+
new FirebaseFirestore(app, auth),
38+
new IndexedDbPersistenceProvider()
39+
)
40+
);
3541
instance.registerVersion(name, version, 'node');
3642
}
3743

packages/firestore/index.rn.memory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@
1818
import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

21+
import { FirebaseFirestore } from './exp/src/api/database';
2122
import { Firestore, MemoryPersistenceProvider } from './src/api/database';
2223
import { configureForFirebase } from './src/config';
2324

2425
import './register-module';
2526

2627
import { name, version } from './package.json';
27-
2828
/**
2929
* Registers the memory-only Firestore build for ReactNative with the components
3030
* framework.
3131
*/
3232
export function registerFirestore(instance: FirebaseNamespace): void {
3333
configureForFirebase(
3434
instance,
35-
(app, auth) => new Firestore(app, auth, new MemoryPersistenceProvider())
35+
(app, auth) =>
36+
new Firestore(
37+
app,
38+
new FirebaseFirestore(app, auth),
39+
new MemoryPersistenceProvider()
40+
)
3641
);
3742
instance.registerVersion(name, version, 'rn');
3843
}

packages/firestore/index.rn.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { FirebaseNamespace } from '@firebase/app-types';
1919

2020
import { Firestore, IndexedDbPersistenceProvider } from './src/api/database';
2121
import { configureForFirebase } from './src/config';
22+
import { FirebaseFirestore } from './exp/src/api/database';
2223

2324
import './register-module';
2425
import { name, version } from './package.json';
@@ -28,9 +29,15 @@ import { name, version } from './package.json';
2829
* Persistence can be enabled via `firebase.firestore().enablePersistence()`.
2930
*/
3031
export function registerFirestore(instance: FirebaseNamespace): void {
31-
configureForFirebase(instance, (app, auth) => {
32-
return new Firestore(app, auth, new IndexedDbPersistenceProvider());
33-
});
32+
configureForFirebase(
33+
instance,
34+
(app, auth) =>
35+
new Firestore(
36+
app,
37+
new FirebaseFirestore(app, auth),
38+
new IndexedDbPersistenceProvider()
39+
)
40+
);
3441
instance.registerVersion(name, version, 'rn');
3542
}
3643

packages/firestore/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

2121
import { Firestore, IndexedDbPersistenceProvider } from './src/api/database';
22+
import { FirebaseFirestore } from './exp/src/api/database';
2223
import { configureForFirebase } from './src/config';
2324
import { name, version } from './package.json';
2425

@@ -29,9 +30,15 @@ import './register-module';
2930
* Persistence can be enabled via `firebase.firestore().enablePersistence()`.
3031
*/
3132
export function registerFirestore(instance: FirebaseNamespace): void {
32-
configureForFirebase(instance, (app, auth) => {
33-
return new Firestore(app, auth, new IndexedDbPersistenceProvider());
34-
});
33+
configureForFirebase(
34+
instance,
35+
(app, auth) =>
36+
new Firestore(
37+
app,
38+
new FirebaseFirestore(app, auth),
39+
new IndexedDbPersistenceProvider()
40+
)
41+
);
3542
instance.registerVersion(name, version);
3643
}
3744

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ export class FirebaseFirestore implements _FirebaseService {
225225
throw new FirestoreError(
226226
Code.FAILED_PRECONDITION,
227227
'Firestore has already been started and its settings can no longer ' +
228-
'be changed. initializeFirestore() cannot be called after calling ' +
229-
'getFirestore().'
228+
'be changed. You can only modify settings before calling any other ' +
229+
'methods on a Firestore object.'
230230
);
231231
}
232232
this._settings = new FirestoreSettings(settings);

packages/firestore/src/api/database.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,20 @@ export class IndexedDbPersistenceProvider implements PersistenceProvider {
219219
export class Firestore
220220
extends Compat<exp.FirebaseFirestore>
221221
implements PublicFirestore, FirebaseService {
222+
_appCompat?: FirebaseApp;
222223
// Note: We are using `MemoryComponentProvider` as a default
223224
// ComponentProvider to ensure backwards compatibility with the format
224225
// expected by the console build.
225226
constructor(
226227
databaseIdOrApp: FirestoreDatabase | FirebaseApp,
227-
authProvider: Provider<FirebaseAuthInternalName>,
228-
private _persistenceProvider: PersistenceProvider = new MemoryPersistenceProvider()
228+
delegate: exp.FirebaseFirestore,
229+
private _persistenceProvider: PersistenceProvider
229230
) {
230-
super(new exp.FirebaseFirestore(databaseIdOrApp, authProvider));
231+
super(delegate);
232+
233+
if (typeof (databaseIdOrApp as FirebaseApp).options === 'object') {
234+
this._appCompat = databaseIdOrApp as FirebaseApp;
235+
}
231236
}
232237

233238
get _databaseId(): DatabaseId {
@@ -313,7 +318,14 @@ export class Firestore
313318
}
314319

315320
get app(): FirebaseApp {
316-
return this._delegate.app as FirebaseApp;
321+
if (!this._appCompat) {
322+
throw new FirestoreError(
323+
Code.FAILED_PRECONDITION,
324+
"Firestore was not initialized using the Firebase SDK. 'app' is " +
325+
'not available'
326+
);
327+
}
328+
return this._appCompat as FirebaseApp;
317329
}
318330

319331
INTERNAL = {

packages/firestore/test/integration/util/firebase_export.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@
2828

2929
import * as firestore from '@firebase/firestore-types';
3030

31+
import firebaseAppCompat from '@firebase/app-compat';
32+
import '../../../exp/index';
33+
3134
import firebase from '@firebase/app';
3235
import * as exp from '../../../exp/test/shim';
33-
import { FirebaseApp as FirebaseAppShim } from '../../../exp/test/shim';
3436
import { FieldValue } from '../../../src/compat/field_value';
3537
import { FirebaseApp } from '@firebase/app-types';
3638
import {
3739
Firestore,
3840
IndexedDbPersistenceProvider
3941
} from '../../../src/api/database';
40-
import { Provider, ComponentContainer } from '@firebase/component';
42+
import { getFirestore } from '../../../exp/src/api/database';
4143

4244
/**
4345
* Detects whether we are running against the functionial (tree-shakeable)
@@ -60,26 +62,46 @@ let appCount = 0;
6062
*/
6163
export function newTestFirestore(
6264
projectId: string,
63-
nameOrApp?: string | FirebaseApp | FirebaseAppShim,
65+
nameOrApp?: string | FirebaseApp,
6466
settings?: firestore.Settings
6567
): firestore.FirebaseFirestore {
6668
if (nameOrApp === undefined) {
6769
nameOrApp = 'test-app-' + appCount++;
6870
}
6971

70-
const app =
71-
typeof nameOrApp === 'string'
72-
? firebase.initializeApp({ apiKey: 'fake-api-key', projectId }, nameOrApp)
73-
: nameOrApp;
72+
let firestore: firestore.FirebaseFirestore;
73+
if (usesFunctionalApi()) {
74+
const app =
75+
typeof nameOrApp === 'string'
76+
? firebaseAppCompat.initializeApp(
77+
{
78+
apiKey: 'fake-api-key',
79+
projectId
80+
},
81+
nameOrApp
82+
)
83+
: nameOrApp;
84+
85+
firestore = new Firestore(
86+
app,
87+
getFirestore(app),
88+
new IndexedDbPersistenceProvider()
89+
);
90+
} else {
91+
const app =
92+
typeof nameOrApp === 'string'
93+
? firebase.initializeApp(
94+
{
95+
apiKey: 'fake-api-key',
96+
projectId
97+
},
98+
nameOrApp
99+
)
100+
: nameOrApp;
74101

75-
const firestore = usesFunctionalApi()
76-
? new Firestore(
77-
app,
78-
new Provider('auth-internal', new ComponentContainer('default')),
79-
new IndexedDbPersistenceProvider()
80-
)
81-
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
82-
(firebase as any).firestore(app);
102+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103+
firestore = (firebase as any).firestore(app);
104+
}
83105

84106
if (settings) {
85107
firestore.settings(settings);

0 commit comments

Comments
 (0)