Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore export schema structure to json

I've been looking on the net and I'm not sure, it is possible to export the Firestore schema structure to a json?

Something similar to create a DDL in which are the instructions to create the table of a DB.

I just need the schema not the data.

like image 966
Dr3ko Avatar asked Dec 05 '25 15:12

Dr3ko


1 Answers

As the comments mention, Firestore doesn't really follow a schema.

But we can still try to understand the structure of the data.

Here's what I did:

Step 1: export your data.

On the Firebase console, go to -> project settings -> Service account -> Generate new private key -> save it as exportedDB.json Run npx -p node-firestore-import-export firestore-export -a exportedDB.json -b original.json

Step 2: remove unnecessary information to make the file much smaller.

Here's a script I used to cut mine from 7MB to 10kB while still maintaining the structure. Essentially it just drops sibling documents and truncates long strings:

import json

def truncate_strings(json_obj):
    if isinstance(json_obj, dict):
        # check if object has more than one key that is alphanumeric and 20 characters long
        long_keys = [k for k in json_obj.keys() if isinstance(k, str) and k.isalnum() and len(k) == 20]
        if len(long_keys) > 1:
            # drop all keys except the first one
            first_key = long_keys[0]
            return {first_key: truncate_strings(json_obj[first_key])}
        else:
            # truncate all keys and values recursively
            return {k[:30]: truncate_strings(v) for k, v in json_obj.items()}
    elif isinstance(json_obj, list):
        if len(json_obj) > 5:
            truncated_list = [truncate_strings(elem) for elem in json_obj[:5]]
        else:
            truncated_list = [truncate_strings(elem) for elem in json_obj]
        return truncated_list
    elif isinstance(json_obj, str) and len(json_obj) > 30:
        return json_obj[:30]
    else:
        return json_obj

# Read in the original JSON file
with open('original.json', 'r') as f:
    original_json = json.load(f)

# Truncate any strings longer than 30 characters, drop array elements beyond the first 5,
# and drop object keys beyond the first one if there are multiple long keys
truncated_json = truncate_strings(original_json)

# Write the truncated JSON to a new file
with open('truncated.json', 'w') as f:
    json.dump(truncated_json, f)

Step 3: Ask your favorite LLM, such as Chat-GPT, to infer the schema for you:

Please infer the database schema from this json: [paste the contents of the truncated file]

like image 180
ppt Avatar answered Dec 09 '25 08:12

ppt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!