Getting Started

Install MSR Firebase, connect it to Firebase Realtime Database, and run your first migration.

Table of contents

  1. Before you begin
  2. Install MSR Firebase
  3. Create a migration script
  4. Configure Firebase credentials
  5. Run migrations
  6. Run migrations from your application
  7. Next steps

Before you begin

  • Node.js 22.13.0 or later and npm.
  • A Firebase project with Realtime Database enabled.
  • A Firebase service-account JSON key file and the Realtime Database URL.

You can find the database URL in your Firebase project settings. Keep service-account keys out of source control; add any local key file to .gitignore.


Install MSR Firebase

npm install @migration-script-runner/firebase

Create a migration script

Create the migrations folder and add a versioned file. See Writing Migration Scripts for naming rules and more patterns.

mkdir migrations

For example, create migrations/V202611251200_create_settings.ts:

import type { IMigrationInfo, IRunnableScript } from '@migration-script-runner/core';
import type { FirebaseHandler, IFirebaseDB } from '@migration-script-runner/firebase';

export default class CreateSettings implements IRunnableScript<IFirebaseDB> {
  async up(db: IFirebaseDB, info: IMigrationInfo, handler: FirebaseHandler): Promise<string> {
    await db.database.ref(handler.cfg.buildPath('settings')).set({ maintenance: false });
    return `Created settings in ${info.name}`;
  }
}

Add a down() method when you need the migration to support rollback. Firebase transactions are atomic at a single database node; see Firebase Transactions for this limitation and safe patterns.


Configure Firebase credentials

Set the database URL and the path to your service-account JSON key file:

export DATABASE_URL="<your-Realtime-Database-URL>"
export GOOGLE_APPLICATION_CREDENTIALS="./serviceAccountKey.json"

The current Firebase adapter requires this credential file; it does not use Application Default Credentials when the path is omitted. See CLI Configuration for config files, environment variables, and command-line options.


Run migrations

First check that the CLI can connect, then apply pending migrations:

npx msr-firebase firebase:test-connection
npx msr-firebase migrate --folder ./migrations

For the available commands and options, see CLI Commands.


Run migrations from your application

Use FirebaseRunner when migrations should run from Node.js application code:

import { FirebaseConfig, FirebaseRunner } from '@migration-script-runner/firebase';

const config = new FirebaseConfig();
config.folder = './migrations';
const runner = await FirebaseRunner.getInstance({ config });
await runner.migrate();

FirebaseConfig reads DATABASE_URL and GOOGLE_APPLICATION_CREDENTIALS from the process environment. See Library Usage for configuration, results, and error handling.


Next steps

  • CLI Usage — configure the CLI and review its commands.
  • Library Usage — integrate migrations into a Node.js application.
  • Writing Migrations — build reversible migrations and follow Firebase-specific practices.
  • Testing — test against the Firebase Realtime Database Emulator.
  • Backup and Restore — choose a backup and recovery approach.
  • API Reference — explore Firebase-specific classes, interfaces, and types.