Skip to content

Revert "Tomandersen/multi db" #6552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .changeset/heavy-spiders-fry.md

This file was deleted.

13 changes: 2 additions & 11 deletions common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,13 @@ export function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnap
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;

// @public
export function getFirestore(): Firestore;

// @public
export function getFirestore(app: FirebaseApp): Firestore;

// @public
export function getFirestore(databaseId: string): Firestore;

// @public
export function getFirestore(app: FirebaseApp, databaseId: string): Firestore;
export function getFirestore(app?: FirebaseApp): Firestore;

// @public
export function increment(n: number): FieldValue;

// @public
export function initializeFirestore(app: FirebaseApp, settings: Settings, databaseId?: string): Firestore;
export function initializeFirestore(app: FirebaseApp, settings: Settings): Firestore;

// @public
export function limit(limit: number): QueryConstraint;
Expand Down
13 changes: 2 additions & 11 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,13 @@ export function getDocsFromCache<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
export function getDocsFromServer<T>(query: Query<T>): Promise<QuerySnapshot<T>>;

// @public
export function getFirestore(): Firestore;

// @public
export function getFirestore(app: FirebaseApp): Firestore;

// @public
export function getFirestore(databaseId: string): Firestore;

// @public
export function getFirestore(app: FirebaseApp, databaseId: string): Firestore;
export function getFirestore(app?: FirebaseApp): Firestore;

// @public
export function increment(n: number): FieldValue;

// @public
export function initializeFirestore(app: FirebaseApp, settings: FirestoreSettings, databaseId?: string): Firestore;
export function initializeFirestore(app: FirebaseApp, settings: FirestoreSettings): Firestore;

// @public
export function limit(limit: number): QueryConstraint;
Expand Down
28 changes: 11 additions & 17 deletions integration/firestore/firebase_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,19 @@ import {

let appCount = 0;

export function newTestApp(projectId: string, appName?: string): FirebaseApp {
if (appName === undefined) {
appName = 'test-app-' + appCount++;
}
return initializeApp(
{
apiKey: 'fake-api-key',
projectId
},
appName
);
}

export function newTestFirestore(
app: FirebaseApp,
settings?: FirestoreSettings,
dbName?: string
projectId: string,
nameOrApp?: string | FirebaseApp,
settings?: FirestoreSettings
): Firestore {
return initializeFirestore(app, settings || {}, dbName);
if (nameOrApp === undefined) {
nameOrApp = 'test-app-' + appCount++;
}
const app =
typeof nameOrApp === 'string'
? initializeApp({ apiKey: 'fake-api-key', projectId }, nameOrApp)
: nameOrApp;
return initializeFirestore(app, settings || {});
}

export * from '@firebase/firestore';
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore-compat/src/index.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ export class Firestore extends FirestoreCompat {
super(
databaseIdFromFirestoreDatabase(firestoreDatabase),
new FirestoreExp(
databaseIdFromFirestoreDatabase(firestoreDatabase),
new _EmptyAuthCredentialsProvider(),
new _EmptyAppCheckTokenProvider(),
databaseIdFromFirestoreDatabase(firestoreDatabase)
new _EmptyAppCheckTokenProvider()
),
new MemoryPersistenceProvider()
);
Expand Down
11 changes: 5 additions & 6 deletions packages/firestore/lite/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {
LiteAppCheckTokenProvider,
LiteAuthCredentialsProvider
} from '../src/api/credentials';
import { databaseIdFromApp } from '../src/core/database_info';
import { setSDKVersion } from '../src/core/version';
import { Firestore } from '../src/lite-api/database';
import { FirestoreSettings } from '../src/lite-api/settings';

declare module '@firebase/component' {
interface NameServiceMapping {
Expand All @@ -42,25 +42,24 @@ export function registerFirestore(): void {
_registerComponent(
new Component(
'firestore/lite',
(container, { instanceIdentifier: databaseId, options: settings }) => {
(container, { options: settings }: { options?: FirestoreSettings }) => {
const app = container.getProvider('app').getImmediate()!;
const firestoreInstance = new Firestore(
app,
new LiteAuthCredentialsProvider(
container.getProvider('auth-internal')
),
new LiteAppCheckTokenProvider(
container.getProvider('app-check-internal')
),
databaseIdFromApp(app, databaseId),
app
)
);
if (settings) {
firestoreInstance._setSettings(settings);
}
return firestoreInstance;
},
'PUBLIC' as ComponentType.PUBLIC
).setMultipleInstances(true)
)
);
// RUNTIME_ENV and BUILD_TARGET are replaced by real values during the compilation
registerVersion('firestore-lite', version, '__RUNTIME_ENV__');
Expand Down
87 changes: 15 additions & 72 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
OfflineComponentProvider,
OnlineComponentProvider
} from '../core/component_provider';
import { DatabaseId, DEFAULT_DATABASE_NAME } from '../core/database_info';
import { DatabaseId } from '../core/database_info';
import {
FirestoreClient,
firestoreClientDisableNetwork,
Expand Down Expand Up @@ -103,18 +103,17 @@ export class Firestore extends LiteFirestore {

/** @hideconstructor */
constructor(
databaseIdOrApp: DatabaseId | FirebaseApp,
authCredentialsProvider: CredentialsProvider<User>,
appCheckCredentialsProvider: CredentialsProvider<string>,
databaseId: DatabaseId,
app?: FirebaseApp
appCheckCredentialsProvider: CredentialsProvider<string>
) {
super(
databaseIdOrApp,
authCredentialsProvider,
appCheckCredentialsProvider,
databaseId,
app
appCheckCredentialsProvider
);
this._persistenceKey = app?.name || '[DEFAULT]';
this._persistenceKey =
'name' in databaseIdOrApp ? databaseIdOrApp.name : '[DEFAULT]';
}

_terminate(): Promise<void> {
Expand All @@ -136,26 +135,17 @@ export class Firestore extends LiteFirestore {
* @param app - The {@link @firebase/app#FirebaseApp} with which the {@link Firestore} instance will
* be associated.
* @param settings - A settings object to configure the {@link Firestore} instance.
* @param databaseId - The name of database.
* @returns A newly initialized {@link Firestore} instance.
*/
export function initializeFirestore(
app: FirebaseApp,
settings: FirestoreSettings,
databaseId?: string
settings: FirestoreSettings
): Firestore {
if (!databaseId) {
databaseId = DEFAULT_DATABASE_NAME;
}
const provider = _getProvider(app, 'firestore');

if (provider.isInitialized(databaseId)) {
const existingInstance = provider.getImmediate({
identifier: databaseId
});
const initialSettings = provider.getOptions(
databaseId
) as FirestoreSettings;
if (provider.isInitialized()) {
const existingInstance = provider.getImmediate();
const initialSettings = provider.getOptions() as FirestoreSettings;
if (deepEqual(initialSettings, settings)) {
return existingInstance;
} else {
Expand All @@ -180,63 +170,20 @@ export function initializeFirestore(
);
}

return provider.initialize({
options: settings,
instanceIdentifier: databaseId
});
return provider.initialize({ options: settings });
}

/**
* Returns the existing default {@link Firestore} instance that is associated with the
* default {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new
* instance with default settings.
*
* @returns The {@link Firestore} instance of the provided app.
*/
export function getFirestore(): Firestore;
/**
* Returns the existing default {@link Firestore} instance that is associated with the
* provided {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new
* instance with default settings.
*
* @param app - The {@link @firebase/app#FirebaseApp} instance that the returned {@link Firestore}
* instance is associated with.
* @returns The {@link Firestore} instance of the provided app.
*/
export function getFirestore(app: FirebaseApp): Firestore;
/**
* Returns the existing {@link Firestore} instance that is associated with the
* default {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new
* instance with default settings.
*
* @param databaseId - The name of database.
* @returns The {@link Firestore} instance of the provided app.
*/
export function getFirestore(databaseId: string): Firestore;
/**
* Returns the existing {@link Firestore} instance that is associated with the
* provided {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new
* instance with default settings.
*
* @param app - The {@link @firebase/app#FirebaseApp} instance that the returned {@link Firestore}
* instance is associated with.
* @param databaseId - The name of database.
* @returns The {@link Firestore} instance of the provided app.
*/
export function getFirestore(app: FirebaseApp, databaseId: string): Firestore;
export function getFirestore(
appOrDatabaseId?: FirebaseApp | string,
optionalDatabaseId?: string
): Firestore {
const app: FirebaseApp =
typeof appOrDatabaseId === 'object' ? appOrDatabaseId : getApp();
const databaseId =
typeof appOrDatabaseId === 'string'
? appOrDatabaseId
: optionalDatabaseId || DEFAULT_DATABASE_NAME;
return _getProvider(app, 'firestore').getImmediate({
identifier: databaseId
}) as Firestore;
export function getFirestore(app: FirebaseApp = getApp()): Firestore {
return _getProvider(app, 'firestore').getImmediate() as Firestore;
}

/**
Expand Down Expand Up @@ -551,11 +498,7 @@ export function disableNetwork(firestore: Firestore): Promise<void> {
* terminated.
*/
export function terminate(firestore: Firestore): Promise<void> {
_removeServiceInstance(
firestore.app,
'firestore',
firestore._databaseId.database
);
_removeServiceInstance(firestore.app, 'firestore');
return firestore._delete();
}

Expand Down
20 changes: 1 addition & 19 deletions packages/firestore/src/core/database_info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { FirebaseApp } from '@firebase/app';

import { Code, FirestoreError } from '../util/error';

/**
* @license
* Copyright 2017 Google LLC
Expand Down Expand Up @@ -50,7 +46,7 @@ export class DatabaseInfo {
}

/** The default database name for a project. */
export const DEFAULT_DATABASE_NAME = '(default)';
const DEFAULT_DATABASE_NAME = '(default)';

/**
* Represents the database ID a Firestore client is associated with.
Expand Down Expand Up @@ -78,17 +74,3 @@ export class DatabaseId {
);
}
}

export function databaseIdFromApp(
app: FirebaseApp,
database?: string
): DatabaseId {
if (!Object.prototype.hasOwnProperty.apply(app.options, ['projectId'])) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'"projectId" not provided in firebase.initializeApp.'
);
}

return new DatabaseId(app.options.projectId!, database);
}
Loading