Meet Firebase Data Connect: PostgreSQL Backend-as-a-Service

Overview

Firebase Data Connect is a new backend-as-a-service powered by Cloud SQL Postgres, designed to offer high performance, scalability, and security for applications that require a relational data model. With Data Connect, developers can define their data models through a GraphQL-based schema and queries, enabling secure endpoints and typesafe SDKs for data access. This new service is ideal for building rich queries, performing relational joins, handling complex conditions, and even conducting semantic vector searches.

Key Features

1. Schema Definition

Data Connect allows you to define your data models and their relationships using GraphQL schemas. This approach eliminates the need to manually keep your SQL database, app server, and data access code in sync.

Example schema definition:

type Movie @table(key: "id") {
  id: String!
  title: String!
  releaseYear: Int!
  genre: String
  rating: Int!
}

2. Queries and Mutations

Data Connect supports creating predefined queries and mutations based on your schema, allowing client applications to interact with the database securely. You can define complex queries and update operations easily.

Example query:

query ListMovies @auth(level: PUBLIC) {
  movies {
    id
    title
    releaseYear
    genre
    rating
  }
}

Example mutation:

mutation createMovie($title: String!, $description: String!, $genre: String!) {
  movie_insert(data: { title: $title, genre: $genre, description: $description, descriptionEmbedding_embed: {model: "textembedding-gecko@003", text: $description} })
}

mutation updateDescription($id: String!, $description: String!) {
  movie_update(id: $id, data: { description: $description, descriptionEmbedding_embed: {model: "textembedding-gecko@003", text: $description} })
}

3. Relationships

You can define relationships between types in your schema, such as many-to-many relationships, allowing for complex data structures and queries.

Example relationship schema:

type Actor @table {
  id: String!
  name: String!
}

type MovieActor @table(key: ["movie", "actor"]) {
  movie: Movie!
  actor: Actor!
  role: String!
}

Example query with relationships:

query ActorsByLastName($lastName: String!) {
  actors(where: {name: {endsWith: $lastName}}) {
    id
    name
    movies: movies_via_MovieActor(limit: 10, orderBy: [{releaseYear: DESC}]) {
      id
      title
      releaseYear
      genre
      rating
    }
  }
}

4. Generated Typesafe Client SDKs

Data Connect generates typesafe client SDKs based on your schema and queries, allowing you to call server-side operations directly from your application code.

Example JavaScript SDK usage:

import { actorsByLastName } from "@movies/app";

const result = await actorsByLastName({lastName: "Roberts"});
for (const actor of result.data.actors) {
  // actor is a strongly-typed object with all the properties defined in the query
}

5. Semantic Search with Vector Embeddings

Data Connect integrates with Vertex AI to generate vector embeddings, enabling advanced search capabilities based on semantic similarity.

Example schema with vector embeddings:

type Movie @table {
  ...
  description: String
  descriptionEmbedding: Vector! @col(size: 768)
}

Example query for semantic search:

query SearchMovies($query: String!, $minYear: Int) @auth(level: PUBLIC) {
  movies: movies_descriptionEmbedding_similarity(
    compare_embed: {model: "textembedding-gecko@001", text: $query},
    where: {releaseYear: {gte: $minYear}},
    limit: 5
  ) {
    id
    title
    description
    releaseYear
  }
}

Getting Started

Firebase Data Connect is currently in a gated preview phase. To get started, you can:

Conclusion

Firebase Data Connect simplifies the development process by allowing you to focus on application logic rather than database management. With its robust GraphQL-based schema definition, secure queries and mutations, typesafe client SDKs, and advanced features like semantic search, Data Connect is set to transform how you build and scale your applications. Join the preview today and explore the future of backend-as-a-service with Firebase.

Next Post Previous Post
No Comment
Add Comment
comment url