How do I model data in GCP Cloud Firestore using Pulumi?
In this guide, we will demonstrate how to model data in Google Cloud Firestore using Pulumi. We will create a Firestore database, define collections, and add documents to those collections. This example will show you how to use Pulumi to manage your Firestore data schema programmatically.
Key Points
- Create a Firestore database.
- Define collections and documents in Firestore.
- Use Pulumi to manage Firestore resources.
Below is the complete Pulumi program in TypeScript to achieve this:
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
// Create a Firestore database
const firestoreDatabase = new gcp.firestore.Database("myFirestoreDatabase", {
name: "my-database",
locationId: "us-central",
type: "FIRESTORE_NATIVE",
concurrencyMode: "OPTIMISTIC",
});
// Define a collection and a document within the Firestore database
const collectionName = "myCollection";
const documentId = "myDocument";
const documentFields = {
name: "John Doe",
age: 30,
email: "john.doe@example.com",
};
const firestoreDocument = new gcp.firestore.Document("myFirestoreDocument", {
collection: collectionName,
documentId: documentId,
fields: JSON.stringify(documentFields),
project: firestoreDatabase.project,
database: firestoreDatabase.name,
});
// Export the Firestore database URL
export const firestoreDatabaseUrl = pulumi.interpolate`https://console.cloud.google.com/firestore/databases/${firestoreDatabase.name}?project=${firestoreDatabase.project}`;
Summary
In this program, we created a Firestore database and defined a collection with a document in it using Pulumi. We also exported the Firestore database URL for easy access. This setup allows you to manage your Firestore data schema efficiently using infrastructure as code principles with Pulumi.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.