Skip to content

Commit

Permalink
Merge pull request #44 from dalenguyen/dev
Browse files Browse the repository at this point in the history
Added support for app name #27
  • Loading branch information
Dale Nguyen authored Apr 5, 2020
2 parents 8317482 + b603adb commit 94c696b
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 92 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
---

## [0.5.0] - 2020-04-05

#### - :nail_care: [Polish]

- Updated new pacakges
- Added name parameter when initializing app #27

## [0.4.0] - 2020-03-19

#### - :rocket: [New Feature]
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ You can export collection and sub collection from your data. The sub collection
```javascript
// In your index.js

const firestoreService = require('firestore-export-import')
const serviceAccount = require('./serviceAccountKey.json')
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');

// Initiate Firebase App
firestoreService.initializeApp(serviceAccount, databaseURL)
// appName is optional, you can obmit it.
const appName = '[DEFAULT]';
firestoreService.initializeApp(serviceAccount, databaseURL, appName);

// Start exporting your data
firestoreService
.backup('collection-name')
.then(data => console.log(JSON.stringify(data)))
.then((data) => console.log(JSON.stringify(data)));
```

Sub collections will be added under **'subCollection'** object.
Expand All @@ -60,10 +62,10 @@ The result is an object of collection's data.
```javascript
firestoreService
.backups(['collectionName1', 'collectionName2']) // Array of collection's name is OPTIONAL
.then(collections => {
.then((collections) => {
// You can do whatever you want with collections
console.log(JSON.stringify(collections))
})
console.log(JSON.stringify(collections));
});
```

### Import data to firestore (Predefined Document Id)
Expand All @@ -90,28 +92,30 @@ Usually the date, location & reference is not converted correctly when you backu
const optons = {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location', 'locations'],
refs: ['refKey']
}
refs: ['refKey'],
};
```
After that, the data will be converted based on their types.
```javascript
// In your index.js
const firestoreService = require('firestore-export-import')
const serviceAccount = require('./serviceAccountKey.json')
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
// Initiate Firebase App
firestoreService.initializeApp(serviceAccount, databaseURL)
// appName is optional, you can obmit it.
const appName = '[DEFAULT]';
firestoreService.initializeApp(serviceAccount, databaseURL, appName);
// Start importing your data
// The array of date, location and reference fields are optional
firestoreService.restore('your-file-path.json', {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location', 'locations'],
refs: ['refKey', 'arrayRef']
})
refs: ['refKey', 'arrayRef'],
});
```
#### For HTTP Request
Expand Down
3 changes: 2 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import * as restoreService from './import';
*
* @param {any} serviceAccount
* @param {any} databaseURL
* @param {string} name
*/
export declare const initializeApp: (serviceAccount: string, databaseURL: string) => boolean;
export declare const initializeApp: (serviceAccount: string, databaseURL: string, name?: string) => boolean;
export { admin };
/**
* Backup data from firestore
Expand Down
3 changes: 2 additions & 1 deletion dist/index.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/index.js.map

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firestore-export-import",
"version": "0.4.0",
"version": "0.5.0",
"description": "NPM package for backup and restore Firebase Firestore",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -30,14 +30,14 @@
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "^13.9.1",
"@types/node": "^13.11.0",
"chai": "^4.2.0",
"jsmin": "^1.0.1",
"minimist": "^1.2.5",
"mocha": "^7.1.0",
"mocha": "^7.1.1",
"request": "^2.88.2",
"request-promise": "^4.2.5",
"ts-node": "^8.6.2",
"ts-node": "^8.8.2",
"typescript": "^3.8.3"
},
"dependencies": {
Expand Down
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ import * as backupService from './export';
*
* @param {any} serviceAccount
* @param {any} databaseURL
* @param {string} name
*/
export const initializeApp = (serviceAccount: string, databaseURL: string) => {
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseURL
});
export const initializeApp = (
serviceAccount: string,
databaseURL: string,
name = '[DEFAULT]'
) => {
if (
admin.apps.length === 0 ||
(admin.apps.length > 0 && admin.app().name !== name)
) {
admin.initializeApp(
{
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseURL,
},
name
);
admin.firestore().settings({ timestampsInSnapshots: true });
}
return true;
Expand Down
84 changes: 42 additions & 42 deletions test/firestore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
import { expect } from 'chai'
import request from 'request-promise'
import * as firestoreService from '../dist'
import { serviceAccount } from './serviceAccount'
import { expect } from 'chai';
import request from 'request-promise';
import * as firestoreService from '../dist';
import { serviceAccount } from './serviceAccount';

const app = firestoreService.initializeApp(
serviceAccount,
serviceAccount.databaseUrl
)
);
const backupAPI =
'https://firebasestorage.googleapis.com/v0/b/firbase-function-helper-qa.appspot.com/o/import-to-firestore.json?alt=media&token=a0530902-8983-45a4-90c2-72c345c7a3d5'
'https://firebasestorage.googleapis.com/v0/b/firbase-function-helper-qa.appspot.com/o/import-to-firestore.json?alt=media&token=a0530902-8983-45a4-90c2-72c345c7a3d5';

describe('initializeApp function test', () => {
it('Initialize app', () => {
expect(app).to.equal(true)
})
expect(app).to.equal(true);
});

it('Restore data from API', async () => {
const backupData = await request(backupAPI)
const backupData = await request(backupAPI);
const status = await firestoreService.restore(JSON.parse(backupData), {
dates: ['date'],
geos: ['location']
})
expect(status.status).ok
})
geos: ['location'],
});
expect(status.status).ok;
});

it('Get all collections', async () => {
try {
const all = await firestoreService.backups()
expect(Object.keys(all).length).is.greaterThan(0)
const all = await firestoreService.backups();
expect(Object.keys(all).length).is.greaterThan(0);
} catch (error) {
console.log(error)
console.log(error);
}
})
});

it('Get an array of collections', async () => {
const all = await firestoreService.backups(['test', 'users'])
expect(Object.keys(all).length).is.equal(2)
})
const all = await firestoreService.backups(['test', 'users']);
expect(Object.keys(all).length).is.equal(2);
});

it('Restore data with document id', async () => {
let status = await firestoreService.restore(
'test/import-to-firestore.json',
{
dates: ['date', 'schedule.time', 'three.level.time'],
geos: ['location', 'locations'],
refs: ['secondRef', 'arrayRef']
refs: ['secondRef', 'arrayRef'],
}
)
expect(status.status).ok
);
expect(status.status).ok;

const result = await firestoreService.backup('test')
const result = await firestoreService.backup('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')
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'].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'
].dogName
).is.equal('hello')
})
).is.equal('hello');
});

it('Restore data as an array without document id', async () => {
let status = await firestoreService.restore(
'test/import-array-to-firestore.json'
)
expect(status.status).ok
})
);
expect(status.status).ok;
});

it('Get a colection with sub-collection', async () => {
try {
const data = await firestoreService.backup('test')
const subCol = data['test']['first-key']['subCollection']
const data = await firestoreService.backup('test');
const subCol = data['test']['first-key']['subCollection'];

expect(subCol).is.exist
expect(Object.values(subCol).length).is.greaterThan(0)
expect(subCol).is.exist;
expect(Object.values(subCol).length).is.greaterThan(0);
} catch (error) {
console.log(error)
console.log(error);
}
})
})
});
});
Loading

0 comments on commit 94c696b

Please sign in to comment.