Jump to content

Manage multiple Firestore databases in a project


Recommended Posts

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.

firestore.png

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 App\r\nFirebaseApp app = FirebaseApp.getInstance();\r\n\r\n// Initialize Firestore Database with database id `test`\r\nFirebaseFirestore db = FirebaseFirestore.getInstance(app, \u201ctest\u201d)\r\n\r\n// Document Reference \r\nDocumentReference docRef = db.collection(\u201ccol\u201d).document(\u201cdoc\u201d);\r\n\r\n// Write Document\r\nwaitFor(docRef.set(Collections.singletonMap(\u201cfoo\u201d, \u201cbar\u201d)));'), (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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...