Blog

Migrating from Firebase to PocketBase Cloud

A comprehensive guide on moving your data, authentication, and logic from Firebase to PocketBase Cloud. Save costs without losing functionality.

David Kim · Jan 31, 2024 · 3 min read migration firebase guide
Migrating from Firebase to PocketBase Cloud

Migrating from Firebase to PocketBase Cloud

Firebase has been the go-to backend-as-a-service for years, but its usage-based pricing and vendor lock-in have led many developers to look for alternatives. PocketBase offers a compelling open-source alternative with a similar feature set but without the unpredictable costs.

In this guide, we’ll cover how to migrate your application from Firebase to PocketBase Cloud.

Feature Comparison

First, let’s map Firebase features to their PocketBase equivalents:

Firebase FeaturePocketBase Equivalent
Firestore/Realtime DBSQLite (with Real-time)
AuthenticationBuilt-in Auth (Email/Pass, OAuth)
StorageFile Storage (Local/S3)
Cloud FunctionsGo/JS Hooks
HostingPocketBase Cloud Hosting
AnalyticsN/A (Use Plausible/Google Analytics)

Migration Strategy

The migration process involves three main steps:

  1. Schema Replication: Recreating your data structure
  2. Data Transfer: Exporting from Firebase and importing to PocketBase
  3. Code Migration: Updating your frontend SDK calls

Step 1: Schema Replication

PocketBase uses strict schemas, unlike Firestore’s NoSQL flexibility. This is actually a good thing for data integrity.

Firestore vs. PocketBase Collections

Firestore:

// users/123
{
  "name": "John Doe",
  "email": "[email protected]",
  "preferences": {
    "theme": "dark"
  }
}

PocketBase:

  1. Create “users” collection (built-in)
  2. Add “preferences” field (JSON type)

You’ll need to define your schema for each collection in the PocketBase dashboard.

Step 2: Data Transfer

We’ve created a simple migration script to help move your data. You can find it in our GitHub repository.

The general process is:

  1. Export Firestore data to JSON
  2. Map JSON fields to PocketBase schema
  3. Use PocketBase API to insert records

Handling Relationships

Firestore often uses subcollections or array of IDs. In PocketBase, you should use Relation fields.

  • 11 Relationships: Use a Relation field (Max Select = 1)
  • 1N Relationships: Use a Relation field on the child record
  • NM Relationships: Use a Relation field (Max Select = Unlimited)

Step 3: Code Migration

The PocketBase SDK is remarkably similar to Firebase’s, making the switch intuitive.

Authentication

Firebase:

import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
await signInWithEmailAndPassword(auth, email, password);

PocketBase:

import PocketBase from "pocketbase";
const pb = new PocketBase("https://your-app.pocketbasecloud.com");
await pb.collection("users").authWithPassword(email, password);

Fetching Data

Firebase:

import { collection, getDocs, query, where } from "firebase/firestore";
const q = query(collection(db, "posts"), where("published", "==", true));
const querySnapshot = await getDocs(q);

PocketBase:

const records = await pb.collection("posts").getList(1, 50, {
  filter: "published = true",
});

Real-time Updates

Firebase:

import { doc, onSnapshot } from "firebase/firestore";
const unsub = onSnapshot(doc(db, "posts", "sf"), (doc) => {
  console.log("Current data: ", doc.data());
});

PocketBase:

pb.collection("posts").subscribe("RECORD_ID", function (e) {
  console.log(e.action);
  console.log(e.record);
});

Special Considerations

Security Rules

Firebase uses firestore.rules. PocketBase uses “API Rules”.

Firebase Rule:

allow read: if request.auth != null && request.auth.uid == resource.data.authorId;

PocketBase Rule:

@request.auth.id != "" && @request.auth.id = author.id

PocketBase rules are generally more concise and easier to read (SQL-like syntax).

Conclusion

Migrating to PocketBase Cloud gives you:

  • Predictable pricing
  • Full data ownership
  • Better performance for relational data
  • A simplified developer experience

Start your migration today and break free from vendor lock-in!