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

Multi DB Batch writes #2198

Open
garrett-wombat opened this issue Sep 23, 2024 · 5 comments
Open

Multi DB Batch writes #2198

garrett-wombat opened this issue Sep 23, 2024 · 5 comments
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@garrett-wombat
Copy link

garrett-wombat commented Sep 23, 2024

Hi all,

Im currently implementing regional db's in my organization and Im wondering if batchWrites will use the internal firebase ref to target the correct database or if all operations (set, update, delete, etc.) will use be written to the database the batch was created from.

e.g.

...
dbp1 = admin.app('us').firestore().doc('/path/to/doc/us');
dbp2 = admin.app('ca').firestore().doc('diff/path');

const batch = admin.app('ca').firestore().batch();
batch.set(dbp1, { updatedAt: now });
batch.set(dbp2, { updatedAt: now });
batch.commit()
...

^^^ Will this write to both 'us' and 'ca' databases or just the 'ca' one.

@garrett-wombat garrett-wombat added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Sep 23, 2024
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Sep 23, 2024
@cherylEnkidu cherylEnkidu self-assigned this Sep 23, 2024
@cherylEnkidu
Copy link
Contributor

Hi @garrett-wombat

The example posted here is not calling multiple databases instead calling multiple apps. If the app is under same project, then they will share the same default database. Please refer to the link here introducing how to set up multiple databases: https://firebase.google.com/docs/firestore/manage-databases

If the databases are different, the data won't be shared.

@garrett-wombat
Copy link
Author

garrett-wombat commented Sep 23, 2024

Well, I must be messing something then, In order to write to two different firestore instances I have to initialize two apps, even though they are for the same project

...
Object.entries(regions).forEach(([region, config]) => {
    const app = admin.initializeApp(
      {
        credential: admin.credential.applicationDefault(),
        projectId,
      },
      // name of app is region key
      region,
    );
    app.firestore().settings({
      ignoreUndefinedProperties: true,
      // for region key "us" set to default firestore db
      databaseId: region === "us" ? undefined : config.dbRegion,
    });
  });
...

If theres a way to initialize once but configure the firestore so it can write to either instance please point me to those docs. Regardless of apps / firestore instances or sudo code above, you haven't really answered my question. Will/can the batchWrite .batch() work across firestore instances. If it can is there an example of how to do that somewhere?

@cherylEnkidu
Copy link
Contributor

Hi @garrett-wombat ,

You can refer to this section in the doc to create another database under the same project: https://firebase.google.com/docs/firestore/manage-databases#create_a_database

Here is the sample code for using different databases. The only thing needs to be changed is databaseId inside Firestore Setting. The sample code uses @google-cloud/firestore with is a part of admin SDK. You can achieve the same result using admin SDK.

const { Firestore, setLogFunction } = require('@google-cloud/firestore');

const settingForAnotherDatabase = {
    databaseId: "us-database"
}

const firestore = new Firestore();
const firestoreAnother = new Firestore(settingForAnotherDatabase);

try {
        const docRef = firestore.doc('foo/bar')
        const docRefAnother = firestoreAnother.doc('foo/bar')
        const batch = firestore.batch()
        const batchAnother = firestoreAnother.batch()
        batch.set(docRef, { value: "default" });
        batch.commit()
        batchAnother.set(docRefAnother, { value: "another database" });
        batchAnother.commit()
    } catch (error) {
        console.log(error);

For your question, in order to write to different database, you need to create different firestore instance.

Please feel free to reopen the issue if you have more questions.

@garrett-wombat
Copy link
Author

@cherylEnkidu Thanks for your response and this example, I hadn't thought about moving to use the @google-cloud/firestore. I don't see a Re-open button so i'm stuck with it closed for now. In you example, if you were to only have one batch for one instance, but pass in a ref created by the firestoreAnother instance. would that error, or write to the instance the batch was created agaist, or do that batch does normally but write to both instances in the expected way.

const { Firestore, setLogFunction } = require('@google-cloud/firestore');

const settingForAnotherDatabase = {
    databaseId: "us-database"
}

const firestoreA = new Firestore();
const firestoreB = new Firestore(settingForAnotherDatabase);

try {
        const docRefA = firestoreA.doc('foo/bar');
        const docRefB = firestoreB.doc('foo/baz');
     
        const batch = firestoreA.batch();
        batch.set(docRefA, { value: "default" });
        batch.set(docRefB, { value: "another database" });
        batch.commit();
    } catch (error) {
        console.log(error);

i.e., will foo/baz be written to to firestoreA, firestoreB, or just error

@cherylEnkidu cherylEnkidu reopened this Sep 25, 2024
@cherylEnkidu
Copy link
Contributor

Hi @garrett-wombat , the example you write here will lead to error since docRef is tight to firestore instance. If you are interested to learn more usage case, you can run the code and try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants