Features

  • copy Replicate document data from some node to another node. With Wildcard option, Replicate all document data from some collection node to another collection node.
    • In addition to this, replicate document data from some projectId's Firestore to another projectId's Firestore.
  • dump Backup document data from some node to local JSON file.
  • restore Restore document data from local JSON file. Because the data can be restored to the firestore emulator as well, it can be used to create test data.

Table Of Contents:

DEMO

copy
copy
restore dump
restore dump

SETUP

homebrew

you can use homebrew for macOS

# add informal formula
brew tap matsu0228/homebrew-fsrpl

brew install fsrpl

go get

go get github.com/matsu0228/fsrpl

Download

download here to get binary. https://github.com/matsu0228/fsrpl/releases

Firestore private key

  • you should set firestore's private key(JSON file).
  • You have two options.
    • set environment variable: FSRPL_CREDENTIALS
    • use --cred option

USAGE

copy: copy some documents

  • copy some documents with copy sub command.
fsrpl copy [input document path] --dest [output document path]

e.g.

fsrpl copy "inputData/user" --dest "new/user"
fsrpl copy "inputData/*" --dest "outputData/*"

dump: export data from some documents

  • export data from some documents with dump sub command.
fsrpl dump [input document path] -f [json file directory path]

e.g.

fsrpl dump "inputData/user" --path ./
cat user.json
{
  "_created_at": "2019-08-26T05:00:00Z",
  "coin": 0,
  "favorites": [
    "1",
    "2"
  ],
  "isDeleted": true,
  "mapData": {
    "isMan": true,
    "name": "subName"
  },
  "name": "user"
}



fsrpl dump "inputData/*" --path ./

cat cat.json | jq
{
  "_created_at": "2019-08-26T05:00:00Z",
  "coin": 0,
  "favorites": [
    "1",
    "2"
  ],
  "isDeleted": true,
  "mapData": {
    "isBrownhair": true,
    "name": "calico"
  },
  "name": "cat"
}

cat dog.json | jq
{
  "name": "dog"
  ...
}
...

restore: import data from some JSON files

  • import data from JSON files with restore sub command.
fsrpl restore [import document path] --path [import JSON file directory path]

e.g.

fsrpl restore "importData/*" --path "./"

save to importData/ dog. data: map[string]interface {}{"_created_at":time.Time{wall:0x0, ext:63702392400, loc:(*time.Location)(nil)}, "coin":0, "favorites":[]interface {}{"1", "2"}, "isDeleted":true, "mapData":map[string]interface {}{"b":true, "name":"mName"}, "name":"pig"}
...

restore: import Data to firestore emulator

  • With setting the FIRESTORE_EMULATOR_HOST environment variable, the restore command can be used to restore the emulator.
    • The --emulators-project-id option allow to specify the projectId with to avoid data conflicts by specifying it.
    • see this examples
FIRESTORE_EMULATOR_HOST=**your_firestore_emulator** fsrpl restore [import document path] --path [import JSON file directory path] --emulators-project-id [test unique Id]

e.g.

FIRESTORE_EMULATOR_HOST=localhost:8080 fsrpl restore "importData/*" --path "./" --emulators-project-id emulator-integration-test

(for gopher) show Go struct from some document

  • generate Go struct from some document with -show-go-struct option
e.g.

fsrpl dump "inputData/user" --show-go-struct

package main

type JsonStruct struct {
    CreatedAt string   `json:"_created_at"`
    Coin      int64    `json:"coin"`
    Favorites []string `json:"favorites"`
    IsDeleted bool     `json:"isDeleted"`
    MapData   struct {
        B    bool   `json:"b"`
        Name string `json:"name"`
    } `json:"mapData"`
    Name string `json:"name"`
}