Skip to content

Commit

Permalink
Merge pull request #43 from dalenguyen/dev
Browse files Browse the repository at this point in the history
added support for array of locations
  • Loading branch information
Dale Nguyen committed Mar 19, 2020
2 parents 59dff08 + 832d4c4 commit 8317482
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
---

## [0.4.0] - 2020-03-19

#### - :rocket: [New Feature]

- Supported import array of locations

## [0.3.4] - 2020-03-17

#### - :nail_care: [Polish]
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Usually the date, location & reference is not converted correctly when you backu
// Import options
const optons = {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location1', 'location2'],
geos: ['location', 'locations'],
refs: ['refKey']
}
```
Expand All @@ -109,7 +109,7 @@ firestoreService.initializeApp(serviceAccount, databaseURL)
// The array of date, location and reference fields are optional
firestoreService.restore('your-file-path.json', {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location1', 'location2'],
geos: ['location', 'locations'],
refs: ['refKey', 'arrayRef']
})
```
Expand Down Expand Up @@ -159,6 +159,16 @@ The JSON is formated as below. The collection name is **test**. **first-key** an
"_latitude": 49.290683,
"_longitude": -123.133956
},
"locations": [
{
"_latitude": 50.290683,
"_longitude": -123.133956
},
{
"_latitude": 51.290683,
"_longitude": -123.133956
}
],
"email": "dungnq@itbox4vn.com",
"secondRef": "test/second-key",
"arrayRef": ["test/second-key", "test/second-key"],
Expand Down
4 changes: 2 additions & 2 deletions dist/import.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/import.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firestore-export-import",
"version": "0.3.4",
"version": "0.4.0",
"description": "NPM package for backup and restore Firebase Firestore",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
20 changes: 14 additions & 6 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,20 @@ const startUpdating = (
if (options.geos && options.geos.length > 0) {
options.geos.forEach(geo => {
if (data.hasOwnProperty(geo)) {
data[geo] = new admin.firestore.GeoPoint(
data[geo]._latitude,
data[geo]._longitude
)
} else {
console.warn('Please check your geo parameters!!!', options.geos)
// array of geo locations
if (Array.isArray(data[geo])) {
data[geo] = data[geo].map(geoValues => {
return new admin.firestore.GeoPoint(
geoValues._latitude,
geoValues._longitude
)
})
} else {
data[geo] = new admin.firestore.GeoPoint(
data[geo]._latitude,
data[geo]._longitude
)
}
}
})
}
Expand Down
6 changes: 5 additions & 1 deletion test/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('initializeApp function test', () => {
'test/import-to-firestore.json',
{
dates: ['date', 'schedule.time', 'three.level.time'],
geos: ['location'],
geos: ['location', 'locations'],
refs: ['secondRef', 'arrayRef']
}
)
Expand All @@ -54,6 +54,10 @@ describe('initializeApp function test', () => {
expect(result.test['first-key'].email).is.equal('dungnq@itbox4vn.com')
expect(result.test['first-key'].schedule.time._seconds).equals(1534046400)
expect(typeof result.test['first-key'].secondRef).is.equal('object')
// locations
expect(result.test['first-key'].location._latitude).equal(49.290683)
expect(result.test['first-key'].locations[0]._latitude).equal(50.290683)
expect(result.test['first-key'].locations[1]._latitude).equal(51.290683)
expect(
result.test['first-key'].subCollection['test/first-key/details'][
'33J2A10u5902CXagoBP6'
Expand Down
10 changes: 10 additions & 0 deletions test/import-to-firestore.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
"_latitude": 49.290683,
"_longitude": -123.133956
},
"locations": [
{
"_latitude": 50.290683,
"_longitude": -123.133956
},
{
"_latitude": 51.290683,
"_longitude": -123.133956
}
],
"email": "dungnq@itbox4vn.com",
"secondRef": "test/second-key",
"arrayRef": ["test/second-key", "test/second-key"],
Expand Down

0 comments on commit 8317482

Please sign in to comment.