Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Added support for Geo variable in firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Nguyen committed Feb 18, 2019
1 parent 3d6f701 commit 98883c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ node export.js <your-collection-name> <sub-collection-name-(optional)>

# Import database to Firestore

This will import one collection to Firestore and it will overwrite your current collection if there is a collection with that name in your Firestore. If you have date type in your JSON, please add the field to the command line. **The date arguments is optional**.
This will import one collection to Firestore and it will overwrite your current collection if there is a collection with that name in your Firestore. If you have date type in your JSON, please add the field to the command line. **The date and geo arguments is optional**.

```
node import.js import-to-firestore.json "date1,date2,date3"
node import.js import-to-firestore.json date=date geo=Location
```

If you have date type in your JSON, please add to your command line
Expand All @@ -58,6 +58,10 @@ Sample from __import-to-firestore.json__. "test" will be the collection name. Th
"date": {
"_seconds":1534046400,
"_nanoseconds":0
},
"Location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
},
"second-key" : {
Expand All @@ -70,6 +74,10 @@ Sample from __import-to-firestore.json__. "test" will be the collection name. Th
"date": {
"_seconds":1534262435,
"_nanoseconds":0
},
"Location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions import-to-firestore.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"date": {
"_seconds":1534046400,
"_nanoseconds":0
},
"Location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
},
"second-key" : {
Expand All @@ -22,6 +26,10 @@
"date": {
"_seconds":1534262435,
"_nanoseconds":0
},
"Location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions import.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const admin = require("firebase-admin");
const fs = require('fs');
const YAML = require('js-yaml');
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");

const fileName = process.argv[2];

let dateArray;
const reDate = new RegExp(/^date/);
const reGeo = new RegExp(/^geo/);

let dateArray = process.argv.filter(item => item.match(reDate))[0];
let geoArray = process.argv.filter(item => item.match(reGeo))[0];

if(process.argv[3]) {
dateArray = process.argv[3].split(',');
if (dateArray) {
dateArray = dateArray.split('=')[1].split(',');
}

// You should replae databaseURL with your own
if (geoArray) {
geoArray = geoArray.split('=')[1].split(',');
}

// You should replace databaseURL with your own
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://ionic-firestore-dn.firebaseio.com"
Expand Down Expand Up @@ -51,6 +59,7 @@ function startUpdating(collectionName, doc, data){
// convert date from unixtimestamp
let parameterValid = true;

// Enter date value
if(typeof dateArray !== 'undefined') {
dateArray.map(date => {
if (data.hasOwnProperty(date)) {
Expand All @@ -61,6 +70,18 @@ function startUpdating(collectionName, doc, data){
}
});
}

// Enter geo value
if(typeof geoArray !== 'undefined') {
geoArray.map(geo => {
if(data.hasOwnProperty(geo)) {
data[geo] = new admin.firestore.GeoPoint(data[geo]._latitude, data[geo]._longitude);
} else {
console.log('Please check your geo parameters!!!', geoArray);
parameterValid = false;
}
})
}

if(parameterValid) {
return new Promise(resolve => {
Expand All @@ -77,5 +98,4 @@ function startUpdating(collectionName, doc, data){
} else {
console.log(`${doc} is not imported to firestore. Please check your parameters!`);
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"author": "Dale Nguyen",
"license": "ISC",
"dependencies": {
"dependencies": {
"firebase-admin": "^5.13.1",
"js-yaml": "^3.12.1"
}
Expand Down

0 comments on commit 98883c0

Please sign in to comment.