Get started with Firebase Data Connect

In this quickstart, you will learn how to:

  • Add Firebase Data Connect to your Firebase project.
  • Set up a development environment including a Visual Studio Code extension to work with a production instance.
  • Then we will show you how to:
    • Create a schema for an email app and deploy to production.
    • Define queries and mutations for your schema.
    • Deploy your final prototype to production.

Prerequisites

To use this quickstart, you'll need the following.

Add Data Connect to your project and create a data source

  1. If you haven't already, create a Firebase project.
    1. In the Firebase console, click Add project, then follow the on-screen instructions.
  2. Upgrade your project to the Blaze plan. This lets you create a Cloud SQL for PostgreSQL instance.

  3. Navigate to the Data Connect section of the Firebase console and follow the product setup workflow.

  4. Select a location for your CloudSQL for PostgreSQL database.

  5. Note the project, service and database names and IDs for confirmation later.

  6. Follow the remaining setup flow then click Done.

Choose and set up a development environment

You'll start with Data Connect by prototyping an app in Visual Studio Code.

Optionally, you can install a local PostgreSQL database for local development with the Data Connect emulator. This setup is covered at the end of this quickstart guide.

Data Connect supports two development experiences for prototyping:

  • If you're a web or Kotlin Android developer, you can prototype schemas and operations locally while connecting to your Cloud SQL for PostgreSQL instance, or (optionally) run PostgreSQL locally.
  • If you're a web developer, you can use IDX to prototype in an IDX workspace using a pre-configured IDX template with PostgreSQL, VS Code extension with Data Connect emulator, and quickstart code set up for you.

VS Code development

If you like to develop locally instead of using IDX, set up the Firebase VS Code extension to help you iterate your development quickly.

  1. Create a new directory for your local project.
  2. Open VS Code in the new directory.
  3. Download the extension, bundled as a VSIX package, from Firebase Storage.
  4. In VS Code, from the View menu, select Extensions.
  5. On the Extensions panel title bar, click the menu icon more_horiz, then follow Install from VSIX....

IDX development

IDX is an environment optimized for web app development. If you're a Kotlin Android developer, follow the steps on the VS Code development tab.

To set up a Data Connect IDX template:

  1. Access the template at the Project IDX site.
  2. Follow the setup flow.

Set up your local project

Install the CLI, following the normal instructions.

Then, enable the Firebase Data Connect experiment.

firebase experiments:enable dataconnect

To set up your local project, initialize your project directory.

Set up your project directory

Initialize your project directory.

In the VS Code left-hand panel, click the Firebase icon to open the Firebase VS Code extension UI.

In the Firebase extension UI:

  1. Make sure you've signed in.
  2. Confirm the Data Connect setup flow, including database provisioning, is complete using the Google Cloud console.
  3. Click the Run firebase init button.
  4. Check the Terminal tab in the VS Code lower Panel for prompts.
  5. Select Data Connect as a feature to use in this directory.
  6. When prompted, supply the project, service and database IDs of the Data Connect project you created earlier in the console.

This flow will create a firebase.json and .firebaserc files, and dataconnect subdirectories including important dataconnect.yaml and connector.yaml files in your local working directory.

Create a Data Connect schema and query

Your setup is done. Now we can start developing with Data Connect.

Start using GraphQL to model users and emails. You'll update sources in:

  • /dataconnect/schema/schema.gql
  • /dataconnect/default-connector/queries.gql

Start developing a schema

In your Firebase project directory, note the dataconnect folder. This is where you define your data model for a Cloud SQL database using GraphQL.

In the /dataconnect/schema/schema.gql file, start defining a schema that includes users and emails.

User

In Data Connect, GraphQL fields are mapped to columns. Users have a uid, name, and email address. Data Connect recognizes several primitive data types: String and Date.

Copy the following snippet or uncomment the corresponding lines in the file.

# File `/dataconnect/schema/schema.gql`

type User @table(key: "uid") {
   uid: String!
   name: String!
   address: String!
}

By default Firebase Data Connect will add a UUID id key if none is provided. However, in this case you want my uid to be the primary key, which you can do through the @table(key: "uid") directive.

Email

Now that you have users, you can model emails. Here you can add the typical fields (or columns) for email data. This time, we omit adding a primary key because you can rely on Data Connect to manage it.

# File `/dataconnect/schema/schema.gql`

type Email @table {
   subject: String!
   sent: Date!
   text: String!
   from: User!
}

Notice that the from field is mapped to a type of User. Data Connect understands that this is a relationship between Email and User and will manage this relationship for you.

Deploy your schema to production

Because you are using the Firebase VS Code extension to work with your production database, you need to deploy your schema before continuing.

  1. You can use the Firebase VS Code extension to deploy.
    • In the extension UI, under the Firebase Data Connect panel, click Deploy.
  2. Alternatively, you can use the Firebase CLI.

    firebase deploy
    
  3. In either the extension or CLI flow, you may need to review schema changes and approve potentially destructive modifications. You'll be prompted to:

    • Review schema changes using firebase dataconnect:sql:diff
    • When you are satisfied with changes, apply them using the flow started by firebase dataconnect:sql:migrate.

Execute mutations to add data to your tables

In the VS Code editor panel, you can see CodeLens buttons appear over the GraphQL types in /dataconnect/schema/schema.gql.

Development time queries and mutations

The unnamed operations associated with these CodeLens buttons are quick and useful actions, in this case, adding data to the table. Data Connect uses GraphQL mutations to describe how and who can operate against the database. Using this button creates a development time operation for quick data seeding.

Since you've deployed your schema to production, you can use the Add data CodeLens buttons in schema.gql and Run (Production) buttons in the resulting generated files to perform these actions on your backend.

To add records to the User and Email tables:

  1. In schema.gql, click the Add data button above the User type declaration.
  2. In the User_insert.gql file that is generated, hard code data for the three fields.
  3. Click the Run (Production) button.
    Code Lens Run button for Firebase Data Connect
  4. Repeat the previous steps to add a record to the Email table, supplying the uid of your user in the fromUid field, as prompted in the generated Email_insert mutation.

Write a query to list emails

Now the fun part, queries. As a developer, you're accustomed to writing SQL queries rather than GraphQL queries, so this can feel a bit different at first. However, GraphQL is far more terse and type-safe than raw SQL. And our VS Code extension eases the development experience.

Start editing the /dataconnect/default-connector/queries.gql file. If you want to get all emails, use a query like this.

# File `/dataconnect/default-connector/queries.gql`

query ListEmails @auth(level: NO_ACCESS) {
  emails {
    id, subject, text, sent
    from {
      name
    }
  }
}

Execute the query using the nearby CodeLens button.

A really exciting feature here is the ability to treat the database's relationships like a graph. Since an Email has a from field that references a User, you can nest into the field and get information about the user back.

Deploy your completed prototype to production

You have worked through a development iteration. Now you can deploy your schema, data, and queries to the server with the Firebase extension UI or the Firebase CLI, just as you did with your schema.

Once deployed, your Data Connect service will be ready to process operations from clients. The Cloud SQL for PostgreSQL instance will be updated with its final deployed generated schema and data.

(Optional) Install PostgreSQL locally

Installing PostgreSQL locally and integrating it with the emulator lets you prototype in a fully local development environment.

You can install a new instance of PostgreSQL or use an existing instance.

Install PostgreSQL

Install PostgreSQL version 15.x following instructions for your platform.

Note hostname, port, username and password and related parameters output during the installation sequence.

To connect to your PostgreSQL instance, the emulator needs:

  • These setup configuration parameters
  • The database name from your dataconnect.yaml and a correspondingly-named database initialized in your local instance.

Update your .firebaserc with the connection string

Use your local PostgreSQL configuration details, including your local PostgreSQL username and password, for a connection string to add to the following key in your .firebaserc file.

{
  "projects": {},
  ...,
  ...,
  "dataconnectEmulatorConfig": {
    "postgres": {
      "localConnectionString": "postgresql://postgresusername:postgrespassword@localhost:5432?sslmode=disable"
    }}
}

Connect to your local PostgreSQL instance

With this configuration done, to connect to your local database:

  1. In VS Code, in the left-hand panel, click the Firebase icon to open the Firebase VS Code extension UI.
  2. Click the Connect to Local PostgreSQL button.

Next steps