new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Supabase · all subjects

migration

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Firestore to Supabase migration tools location

Supabase provides migration tools at https://github.com/supabase-community/firebase-to-supabase/tree/main/firestore to convert data from a Firebase Firestore database to a Supabase Postgres database.

Firestore collection conversion to Postgres table

The migration process copies the entire contents of a single Firestore collection to a single Postgres table. The Firestore collection is flattened and converted to a table with basic columns of types: text, numeric, boolean, or jsonb.

Clone firebase-to-supabase repository

To set up the migration tool, clone the repository using: git clone https://github.com/supabase-community/firebase-to-supabase.git

supabase-service.json configuration for Firestore migration

In the /firestore directory, create a file named supabase-service.json with the following structure: {"host": "database.server.com", "password": "secretpassword", "user": "postgres", "database": "postgres", "port": 5432}. The host and user values should be obtained from the Connect page in the project dashboard under Session pooler connection parameters. The password should be the one used when creating the Supabase project.

List all Firestore collections command

To list all Firestore collections during migration, run: node collections.js

Dump Firestore collection to JSON command

To dump a Firestore collection to a JSON file, run: node firestore2json.js <collectionName> [<batchSize>] [<limit>]. The batchSize parameter is optional and defaults to 1000. The output filename is <collectionName>.json. The limit parameter is optional and defaults to 0 (no limit).

Import JSON file to Supabase Postgres command

To import a JSON file to Supabase Postgres, run: node json2supabase.js <path_to_json_file> [<primary_key_strategy>] [<primary_key_name>]. The path_to_json_file is required (e.g., ./my_collection.json). The primary_key_strategy is optional and defaults to 'none'.

Firestore to Supabase primary key strategies

When importing JSON to Supabase, the following primary key strategies are available: none (default, no primary key added), smallserial (id SMALLSERIAL PRIMARY KEY, autoincrementing 2-byte integer), serial (id SERIAL PRIMARY KEY, autoincrementing 4-byte integer), bigserial (id BIGSERIAL PRIMARY KEY, autoincrementing 8-byte integer), uuid (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), randomly generated UUID), firestore_id (id TEXT PRIMARY KEY, uses existing firestore_id random text as key).

Custom hook parameter for Firestore JSON export

The primary key name parameter for json2supabase.js is optional and defaults to 'id'.

Custom hooks for Firestore migration

Custom hooks allow customization of the Firestore to JSON export process. They can be used for modifying keys, calculating data, and flattening nested documents into related SQL tables. For a Firestore collection named 'users', create a file called 'users.js' in the current folder.

Custom hook file structure for Firestore migration

A custom hook file should export a function with the signature: module.exports = (collectionName, doc, recordCounters, writeRecord) => { return doc }

Custom hook parameters for Firestore migration

Custom hook parameters are: collectionName (the name of the collection being processed), doc (the current document as a JSON object), recordCounters (internal object tracking processed records per collection), writeRecord (function for writing data to other JSON files with parameters: name - JSON file name, doc - document to write, recordCounters - same object passed to hook).

Flatten JSON for Firestore migration example

A custom hook can flatten nested arrays into separate files. For example, a 'weapons' array in a users collection can be split: module.exports = (collectionName, doc, recordCounters, writeRecord) => { for (let i = 0; i < doc.weapons.length; i++) { const weapon = { uid: doc.uid, weapon: doc.weapons[i] }; writeRecord('weapons', weapon, recordCounters) } delete doc.weapons; return doc }

Add numeric key to Firestore collection migration example

A custom hook can add a unique numeric key: module.exports = (collectionName, doc, recordCounters, writeRecord) => { doc.unique_key = recordCounters[collectionName] + 1; return doc }

Add timestamp to Firestore migration example

A custom hook can add a timestamp of when a record was dumped from Firestore: module.exports = (collectionName, doc, recordCounters, writeRecord) => { doc.dump_time = new Date().toISOString(); return doc }

Complex Firestore structure handling

If a Firestore structure is more complex than basic flattening, you can write a program to split the newly-created JSON file into multiple related tables before importing the JSON files to Supabase.

Give your agent this brain