Skip to content

Commit

Permalink
Merge pull request #39 from dalenguyen/dev
Browse files Browse the repository at this point in the history
Added support for auto document id, reference...
  • Loading branch information
Dale Nguyen committed Mar 10, 2020
2 parents 85075e2 + e44ffc6 commit 6c47088
Show file tree
Hide file tree
Showing 16 changed files with 2,436 additions and 3,128 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
---

## [0.3.3] - 2020-01-03

#### - :nail_care: [Polish]

- Update new packages
- Check app before initializing

#### - :boom: [Breaking Change]

- Changed options for date & location & ref

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

- Added support reference type
- Added upload array of document feature (without predefined document id)

## [0.3.2] - 2020-01-03

#### - :rocket: [New Feature]
Expand Down
67 changes: 58 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Install using [**npm**](https://www.npmjs.com/).

```sh
npm install firestore-export-import
OR
yarn add firestore-export-import
```

## Get Google Cloud Account Credentials from Firebase
Expand Down Expand Up @@ -64,7 +66,7 @@ firestoreService
});
```

### Import data to firestore
### Import data to firestore (Predefined Document Id)

This code will help you to import data from a JSON file to firestore. You have two options:

Expand All @@ -81,6 +83,19 @@ firebase firestore:delete [options] <<path>>
#### For local JSON
Usually the date, location & reference is not converted correctly when you backup the Firestore database. In order to import correctly, you have to pass to parameters for the options:
```javascript
// Import options
const optons = {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location1', 'location2'],
refs: ['refKey']
};
```
After that, the data will be converted based on their types.
```javascript
// In your index.js
Expand All @@ -92,22 +107,23 @@ firestoreService.initializeApp(serviceAccount, databaseURL);
// Start importing your data
// The array of date and location fields are optional
firestoreService.restore(
'your-file-path.json',
['date1', 'date2.date3'],
['location1', 'location2']
);
firestoreService.restore('your-file-path.json', {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location1', 'location2'],
refs: ['refKey']
});
```
- Note that the date array only support two levels now. If you pass ['date1.date2.date3'], it won't work.
#### For HTTP Request
```javascript
import request from 'request-promise';
...
const backupData = await request('JSON-URL');
const status = await firestoreService.restore(JSON.parse(backupData), ['date'], ['location']);
const status = await firestoreService.restore(JSON.parse(backupData), {
dates: ['date'],
geos: ['location']
});
```
The JSON is formated as below. The collection name is **test**. **first-key** and **second-key** are document ids.
Expand All @@ -127,6 +143,14 @@ The JSON is formated as below. The collection name is **test**. **first-key** an
"_nanoseconds": 0
}
},
"three": {
"level": {
"time": {
"_seconds": 1534046400,
"_nanoseconds": 0
}
}
},
"custom": {
"lastName": "Nguyen",
"firstName": "Dale"
Expand All @@ -136,6 +160,7 @@ The JSON is formated as below. The collection name is **test**. **first-key** an
"_longitude": -123.133956
},
"email": "dungnq@itbox4vn.com",
"secondRef": "test/second-key",
"subCollection": {
"test/first-key/details": {
"33J2A10u5902CXagoBP6": {
Expand Down Expand Up @@ -169,6 +194,30 @@ The JSON is formated as below. The collection name is **test**. **first-key** an
}
```
### Import data to firestore (auto generate document id)
It works the same way as above. However the structure of JSON file is different. It's an array of documents.
```json
// import-array-to-firestore.json
{
"test": [
{
"name": "Dale Nguyen",
"email": "dale@dalenguyen.me"
},
{
"name": "Yen Nguyen",
"email": "yenchan@gmail.com"
},
{
"name": "Harry Potter",
"email": "harry@potter.me"
}
]
}
```
## Contributions
This project is based on [firestore-import-export](https://github.com/dalenguyen/firestore-import-export), feel free to report bugs and make feature requests in the [Issue Tracker](https://github.com/dalenguyen/firestore-backup-restore/issues), fork and create pull requests!
10 changes: 7 additions & 3 deletions dist/import.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export interface IImportOptions {
dates?: string[];
geos?: string[];
refs?: string[];
}
/**
* Restore data to firestore
*
* @param {string} fileName
* @param {Array<string>} dateArray
* @param {Array<string>} geoArray
* @param {IImportOptions} options
*/
export declare const restore: (fileName: string, dateArray: string[], geoArray: string[]) => Promise<any>;
export declare const restore: (fileName: string, options: IImportOptions) => Promise<any>;
19 changes: 10 additions & 9 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.

Loading

0 comments on commit 6c47088

Please sign in to comment.