Tuesday, May 14, 2024
No menu items!
HomeCloud ComputingManage multiple Firestore databases in a project

Manage multiple Firestore databases in a project

We’re excited to announce support for multiple Firestore databases in a Google Cloud project. You can now create multiple databases in a project to isolate customer data, microservices, or dev/test/staging environments. 

The multiple databases feature includes support for the following:

Firestore database CRUD management: Firestore now exposes a new API endpoint, Terraform resource, gcloud CLI command and Firebase CLI command to manage the lifecycle of one or more Firestore databases.

Conditional Identity Access Management control: You can apply different security policies to different Firestore databases with IAM Conditions or Firebase Security Rules.

Support for both Firestore database modes: You can create new databases, in the same project, in either Firestore Native Mode or Datastore Mode.

Support for all Firestore regions: You can create new databases, in the same project, in any supported Firestore region.

Billing: You can now track the cost of your databases separately through Cloud Billing and BigQuery. This makes it easier to both observe consumption and budget for each databases’ usage.

Cloud Monitoring: Firestore’s Cloud Monitoring Metrics and Stats are now aggregated at the database-level. 

Example walkthrough

Sound like a good candidate for your application? Let’s take an end-to-end example to see how it works. 

To create a Firestore database, you need to specify a database identifier, mode, and location. There are multiple ways to create a Firestore database. Here is an example of how to create a Firestore native database named reviews in the us-west1 location using the gcloud CLI:

code_block[StructValue([(u’code’, u’$ gcloud firestore databases create –database=test –location=us-west1 –type=firestore-native’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3ed2c8eb0690>)])]

Once your database is created, you can access it using the Firestore/Datastore console.

When you no longer need a Firestore database, you can delete the Firestore database by running.

code_block[StructValue([(u’code’, u’$ gcloud alpha firestore databases delete –database=test –location=nam5 –type=firestore-native’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3ed2c8eb0b10>)])]

That’s everything you need to do. Once a database is deleted, Firestore performs the data deletion on your behalf.

When using the Firestore client libraries with your newly created databases, specify the database name to get the appropriate Firestore database instance. If you don’t specify a database name, the client library falls back to the (default) database. Here’s an example for writing to a database named “test” with the Firebase Admin SDK for Java:

code_block[StructValue([(u’code’, u’// Initialize Firebase ApprnFirebaseApp app = FirebaseApp.getInstance();rnrn// Initialize Firestore Database with database id `test`rnFirebaseFirestore db = FirebaseFirestore.getInstance(app, u201ctestu201d)rnrn// Document Reference rnDocumentReference docRef = db.collection(u201ccolu201d).document(u201cdocu201d);rnrn// Write DocumentrnwaitFor(docRef.set(Collections.singletonMap(u201cfoou201d, u201cbaru201d)));’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3ed2c8d8fc10>)])]

For examples in additional languages, see Accessing your database.

Next steps

For more information on how to set up and configure multiple databases on Firestore, check out the documentation.

Cloud BlogRead More

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments