In this quickstart, you will learn how to build Firebase Data Connect in your application with a production SQL instance. You will:
- Add Firebase Data Connect to your Firebase project.
- Provision a Cloud SQL instance for your app.
- 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 a movie app
- Define queries and mutations that will be used in your app
- Test your queries and mutations with sample data
- Generate strongly typed SDKs and use them in your app
- Deploy your final schema, queries and data to the cloud.
Create a Firebase project and Cloud SQL database
- If you haven't already, create a Firebase project.
- In the Firebase console, click Add project, then follow the on-screen instructions.
- Navigate to the Data Connect section of the Firebase console and follow the product setup workflow.
Upgrade your project to the Blaze plan. This lets you create a Cloud SQL for PostgreSQL instance.
Select a location for your CloudSQL for PostgreSQL database.
Note the project, service and database names and IDs for confirmation later.
Follow the remaining setup flow then click Done.
Choose a development flow
Data Connect offers you two ways to install development tools.
Prerequisites
To use this quickstart, you'll need the following.
- Visual Studio Code.
- A Node.js installation, using nvm-windows for Windows or nvm for macOS or Linux.
- A Firebase project. If you haven't already created one, do so in the Firebase console.
Set up the development environment
- Create a new directory for your local project.
- Open VS Code in the new directory.
- Install the Firebase Data Connect extension from the Visual Studio Code Marketplace.
Set up your project directory
To set up your local project, initialize your project directory. In the IDE window, in the left-hand panel, click the Firebase icon to open the Data Connect VS Code extension UI:
- Click the Sign in with Google button.
- Click the Connect a Firebase project button and select the project you created earlier in the console.
- Click the Run firebase init button and complete the flow.
Click the Start emulators button.
Create a schema
In your Firebase project directory, in the /dataconnect/schema/schema.gql
file, start defining a GraphQL schema that includes movies.
Movie
In Data Connect, GraphQL fields are mapped to columns. The Movie
type has id
, title
, imageUrl
and genre
. Data Connect
recognizes the primitive data types String
and UUID
.
Copy the following snippet or uncomment the corresponding lines in the file.
# File `/dataconnect/schema/schema.gql`
# By default, a UUID id key will be created by default as primary key.
type Movie @table {
id: UUID! @default(expr: "uuidV4()")
title: String!
imageUrl: String!
genre: String
}
MovieMetadata
Now that you have movies, you can model movie metadata.
Copy the following snippet or uncomment the corresponding lines in the file.
# Movie - MovieMetadata is a one-to-one relationship
type MovieMetadata @table {
# This time, we omit adding a primary key because
# you can rely on Data Connect to manage it.
# @unique indicates a 1-1 relationship
movie: Movie! @unique
# movieId: UUID <- this is created by the above reference
rating: Float
releaseYear: Int
description: String
}
Notice that the movie
field is mapped to a type of Movie
.
Data Connect understands that this is a relationship between Movie
and MovieMetadata
and will manage this relationship for you.
Learn more about Data Connect schemas in the documentation
Deploy your schema to production
You need to deploy your schema before continuing.
In the extension UI, under the Firebase Data Connect panel, click Deploy to production.
After deploying your schema to your production database, you should be able to view the schema in the Firebase console.
Add data to your tables
In the IDE editor panel, you can see CodeLens buttons appear over the
GraphQL types in /dataconnect/schema/schema.gql
. Since you've deployed
your schema to production, you can use the Add data and
Run (Production) buttons add data to your database on the backend.
To add records to the Movie
table:
- In
schema.gql
, click the Add data button above theMovie
type declaration.
- In the
Movie_insert.gql
file that is generated, hard code data for the four fields. - Click the Run (Production) button.
- Repeat the previous steps to add a record to the
MovieMetadata
table, supplying theid
of your Movie in themovieId
field, as prompted in the generatedMovieMetadata_insert
mutation.
To quickly verify data was added:
- Back in
schema.gql
, click the Read data button above theMovie
type declaration. - In the resulting
Movie_read.gql
file, click the Run (Production) button to execute the query.
Learn more about Data Connect mutations in the documentation
Define your query
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/connector/queries.gql
file. If you want to get all movies, use a query like this.
# File `/dataconnect/connector/queries.gql`
# @auth() directives control who can call each operation.
# Anyone should be able to list all movies, so the auth level
# is set to PUBLIC
query ListMovies @auth(level: PUBLIC) {
movies {
id
title
imageUrl
genre
}
}
Execute the query using the nearby CodeLens button.
Learn more about Data Connect queries in the documentation
Generate SDKs and use them in your app
- Click the Add SDK to app button.
In the dialog that appears, select a directory containing code for your app. Data Connect SDK code will be generated and saved there.
Select your app platform, then note that SDK code is immediately generated in your selected directory.
Learn how to use the generated SDK to call queries and mutations from client apps (web, Android, iOS, Flutter).
Deploy your schema and query 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.
In the IDE window, in the VS Code Extension UI, click the Deploy to production button.
Once deployed, go to the Firebase console to verify the schema, operations and data has been uploaded to the cloud. You should be able to view the schema, and run your operations on the console as well. The Cloud SQL for PostgreSQL instance will be updated with its final deployed generated schema and data.
Learn more about using the Data Connect emulator in the documentation
Next steps
Review your deployed project and discover more tools:
Add data to your database, inspect and modify your schemas, and monitor your Data Connect service in the Firebase console. Access more information in the documentation. For example, since you've completed the quickstart:
Learn more about schema, query and mutation development
Learn about generating client SDKs and calling queries and mutations from client code for web, Android, iOS, and Flutter.