Skip to content
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

How can I choose a custom database ID instead of the (default) one? #130

Open
mailaneel opened this issue Apr 16, 2024 · 5 comments
Open
Labels
Bug Something isn't working

Comments

@mailaneel
Copy link

Reopening #126

databaseId is second param to getFirestore, without this it always uses (default) database

return getFirestore(app);

@kossnocorp
Copy link
Owner

Hey! You can configure it using the options schema argument: https://typesaurus.com/api/schema/#app

@kossnocorp kossnocorp closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2024
@mailaneel
Copy link
Author

@kossnocorp apologies for raising this again, I tried using the schema option like you have suggested, but it didn't work as expected.

Below is the logic used in typesaurus to get firestore, when we pass { app: 'some-name' } assuming some-name is the name of the firestore database. we are getting app instance with different name, but firestore is still using default database (getFirestore logic added below). Also I don't see a option to pass to initializeApp

export function firestore(options) {
  const appName = options?.server?.app || options?.app;
  const app = getApp(appName);

  if (options?.server?.preferRest) {
    return initializeFirestore(app, {
      preferRest: options?.server?.preferRest,
    });
  } else {
    return getFirestore(app);
  }
}

getFirestore from firebase.js

export function getFirestore(app: App, databaseId: string): Firestore;

export function getFirestore(
  appOrDatabaseId?: App | string,
  optionalDatabaseId?: string
): Firestore {
  const app: App = typeof appOrDatabaseId === 'object' ? appOrDatabaseId : getApp();
  const databaseId =
    (typeof appOrDatabaseId === 'string' ? appOrDatabaseId : optionalDatabaseId) || DEFAULT_DATABASE_ID;
  const firebaseApp: FirebaseApp = app as FirebaseApp;
  const firestoreService = firebaseApp.getOrInitService(
    'firestore', (app) => new FirestoreService(app));
  return firestoreService.getDatabase(databaseId);
}

@kossnocorp
Copy link
Owner

Hey, sorry that it doesn't work for you. Let me reopen the issue, add tests and fix it.

@kossnocorp kossnocorp reopened this May 13, 2024
@kossnocorp kossnocorp added the Bug Something isn't working label May 13, 2024
@ecaroth
Copy link

ecaroth commented Aug 22, 2024

I had this need myself, just submitted a PR that fixes it @kossnocorp
#139

@aaaaahaaaaa
Copy link

aaaaahaaaaa commented Sep 9, 2024

@kossnocorp Can we get a bit of love on this issue and get this merged and released? Not being able to select a different DB is a hard blocker for many I'm sure. This feature has been GA in Firestore since February.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants