-
Notifications
You must be signed in to change notification settings - Fork 150
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
Comments
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. |
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 |
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 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. |
@cherylEnkidu Thanks for your response and this example, I hadn't thought about moving to use the 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 |
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. |
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.
^^^ Will this write to both 'us' and 'ca' databases or just the 'ca' one.
The text was updated successfully, but these errors were encountered: