Socket
Socket
Sign inDemoInstall

firestore-export-import

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firestore-export-import - npm Package Compare versions

Comparing version 0.2.9 to 0.3.0

.github/FUNDING.yml

13

dist/export.js

@@ -1,4 +0,5 @@

"use strict";var __awaiter=(this&&this.__awaiter)||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value));}catch(e){reject(e);}}
"use strict";var __awaiter=(this&&this.__awaiter)||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value);});}
return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value));}catch(e){reject(e);}}
function rejected(value){try{step(generator["throw"](value));}catch(e){reject(e);}}
function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value);}).then(fulfilled,rejected);}
function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected);}
step((generator=generator.apply(thisArg,_arguments||[])).next());});};var __generator=(this&&this.__generator)||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1];},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),"throw":verb(1),"return":verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this;}),g;function verb(n){return function(v){return step([n,v]);};}

@@ -11,6 +12,8 @@ function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue;}

op=body.call(thisArg,_);}catch(e){op=[6,e];y=0;}finally{f=t=0;}
if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true};}};var __importStar=(this&&this.__importStar)||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result["default"]=mod;return result;};var _this=this;Object.defineProperty(exports,"__esModule",{value:true});var admin=__importStar(require("firebase-admin"));exports.getAllCollections=function(collectionNameArray){var db=admin.firestore();return new Promise(function(resolve){db.listCollections().then(function(snap){var paths=collectionNameArray;if(paths.length===0){snap.forEach(function(collection){return paths.push(collection.path);});}
var promises=[];paths.forEach(function(segment){var result=exports.backup(segment);promises.push(result);});Promise.all(promises).then(function(value){var all=Object.assign.apply(Object,[{}].concat(value));resolve(all);});});});};exports.backup=function(collectionName,subCollection){if(subCollection===void 0){subCollection='';}
if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true};}};var __spreadArrays=(this&&this.__spreadArrays)||function(){for(var s=0,i=0,il=arguments.length;i<il;i++)s+=arguments[i].length;for(var r=Array(s),k=0,i=0;i<il;i++)
for(var a=arguments[i],j=0,jl=a.length;j<jl;j++,k++)
r[k]=a[j];return r;};var __importStar=(this&&this.__importStar)||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result["default"]=mod;return result;};Object.defineProperty(exports,"__esModule",{value:true});var admin=__importStar(require("firebase-admin"));exports.getAllCollections=function(collectionNameArray){var db=admin.firestore();return new Promise(function(resolve){db.listCollections().then(function(snap){var paths=collectionNameArray;if(paths.length===0){snap.forEach(function(collection){return paths.push(collection.path);});}
var promises=[];paths.forEach(function(segment){var result=exports.backup(segment);promises.push(result);});Promise.all(promises).then(function(value){var all=Object.assign.apply(Object,__spreadArrays([{}],value));resolve(all);});});});};exports.backup=function(collectionName,subCollection){if(subCollection===void 0){subCollection='';}
return new Promise(function(resolve,reject){var db=admin.firestore();var data={};data[collectionName]={};var results=db.collection(collectionName).get().then(function(snapshot){snapshot.forEach(function(doc){data[collectionName][doc.id]=doc.data();});return data;}).catch(function(error){console.log(error);});results.then(function(dt){if(subCollection===''){resolve(dt);}
else{console.log('Geting sub collection',subCollection);getSubCollection(db,data,dt,collectionName,subCollection).then(function(){resolve(data);}).catch(function(error){console.log(error);reject(error);});}}).catch(function(error){console.log(error);reject(error);});});};var getSubCollection=function(db,data,dt,collectionName,subCollection){return __awaiter(_this,void 0,void 0,function(){var _i,_a,_b,key,value,subCollectionPath,subCollectionData;return __generator(this,function(_c){switch(_c.label){case 0:_i=0,_a=Object.entries([dt[collectionName]][0]);_c.label=1;case 1:if(!(_i<_a.length))return[3,4];_b=_a[_i],key=_b[0],value=_b[1];subCollectionPath=collectionName+'/'+key+'/'+subCollection;return[4,exports.backup(subCollectionPath)];case 2:subCollectionData=_c.sent();if(Object.keys(subCollectionData[subCollectionPath]).length>0){data[collectionName][key]['subCollection']=subCollectionData;}
else{console.log('Geting sub collection',subCollection);getSubCollection(db,data,dt,collectionName,subCollection).then(function(){resolve(data);}).catch(function(error){console.log(error);reject(error);});}}).catch(function(error){console.log(error);reject(error);});});};var getSubCollection=function(db,data,dt,collectionName,subCollection){return __awaiter(void 0,void 0,void 0,function(){var _i,_a,_b,key,value,subCollectionPath,subCollectionData;return __generator(this,function(_c){switch(_c.label){case 0:_i=0,_a=Object.entries([dt[collectionName]][0]);_c.label=1;case 1:if(!(_i<_a.length))return[3,4];_b=_a[_i],key=_b[0],value=_b[1];subCollectionPath=collectionName+'/'+key+'/'+subCollection;return[4,exports.backup(subCollectionPath)];case 2:subCollectionData=_c.sent();if(Object.keys(subCollectionData[subCollectionPath]).length>0){data[collectionName][key]['subCollection']=subCollectionData;}
_c.label=3;case 3:_i++;return[3,1];case 4:return[2];}});});};
/**
* Get item from deep level string
*
* @param o object
* @param s string
*/
/**
* Restore data to firestore

@@ -3,0 +9,0 @@ *

@@ -1,4 +0,5 @@

"use strict";var __awaiter=(this&&this.__awaiter)||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value));}catch(e){reject(e);}}
"use strict";var __awaiter=(this&&this.__awaiter)||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value);});}
return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value));}catch(e){reject(e);}}
function rejected(value){try{step(generator["throw"](value));}catch(e){reject(e);}}
function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value);}).then(fulfilled,rejected);}
function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected);}
step((generator=generator.apply(thisArg,_arguments||[])).next());});};var __generator=(this&&this.__generator)||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1];},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),"throw":verb(1),"return":verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this;}),g;function verb(n){return function(v){return step([n,v]);};}

@@ -11,11 +12,15 @@ function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue;}

op=body.call(thisArg,_);}catch(e){op=[6,e];y=0;}finally{f=t=0;}
if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true};}};var __importStar=(this&&this.__importStar)||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result["default"]=mod;return result;};var _this=this;Object.defineProperty(exports,"__esModule",{value:true});var admin=__importStar(require("firebase-admin"));var fs=__importStar(require("fs"));exports.restore=function(fileName,dateArray,geoArray){var db=admin.firestore();return new Promise(function(resolve,reject){if(typeof fileName==='object'){var dataArray=fileName;updateCollection(db,dataArray,dateArray,geoArray).then(function(){resolve({status:true,message:'Collection successfully imported!'});}).catch(function(error){reject({status:false,message:error.message});});}
if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true};}};var __importStar=(this&&this.__importStar)||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result["default"]=mod;return result;};Object.defineProperty(exports,"__esModule",{value:true});var admin=__importStar(require("firebase-admin"));var fs=__importStar(require("fs"));exports.restore=function(fileName,dateArray,geoArray){var db=admin.firestore();return new Promise(function(resolve,reject){if(typeof fileName==='object'){var dataArray=fileName;updateCollection(db,dataArray,dateArray,geoArray).then(function(){resolve({status:true,message:'Collection successfully imported!'});}).catch(function(error){reject({status:false,message:error.message});});}
else{fs.readFile(fileName,'utf8',function(err,data){if(err){console.log(err);reject({status:false,message:err.message});}
var dataArray=JSON.parse(data);updateCollection(db,dataArray,dateArray,geoArray).then(function(){resolve({status:true,message:'Collection successfully imported!'});}).catch(function(error){reject({status:false,message:error.message});});});}});};var updateCollection=function(db,dataArray,dateArray,geoArray){return __awaiter(_this,void 0,void 0,function(){var _a,_b,_i,index,collectionName,_c,_d,_e,doc,subCollections;return __generator(this,function(_f){switch(_f.label){case 0:_a=[];for(_b in dataArray)
var dataArray=JSON.parse(data);updateCollection(db,dataArray,dateArray,geoArray).then(function(){resolve({status:true,message:'Collection successfully imported!'});}).catch(function(error){reject({status:false,message:error.message});});});}});};var updateCollection=function(db,dataArray,dateArray,geoArray){return __awaiter(void 0,void 0,void 0,function(){var _a,_b,_i,index,collectionName,_c,_d,_e,doc,subCollections;return __generator(this,function(_f){switch(_f.label){case 0:_a=[];for(_b in dataArray)
_a.push(_b);_i=0;_f.label=1;case 1:if(!(_i<_a.length))return[3,9];index=_a[_i];collectionName=index;_c=[];for(_d in dataArray[index])
_c.push(_d);_e=0;_f.label=2;case 2:if(!(_e<_c.length))return[3,8];doc=_c[_e];if(!dataArray[index].hasOwnProperty(doc))return[3,7];if(!dataArray[index][doc]['subCollection'])return[3,5];subCollections=dataArray[index][doc]['subCollection'];delete dataArray[index][doc]['subCollection'];return[4,startUpdating(db,collectionName,doc,dataArray[index][doc],dateArray,geoArray)];case 3:_f.sent();return[4,updateCollection(db,subCollections,[],[])];case 4:_f.sent();return[3,7];case 5:return[4,startUpdating(db,collectionName,doc,dataArray[index][doc],dateArray,geoArray)];case 6:_f.sent();_f.label=7;case 7:_e++;return[3,2];case 8:_i++;return[3,1];case 9:return[2];}});});};var startUpdating=function(db,collectionName,doc,data,dateArray,geoArray){var parameterValid=true;if(typeof dateArray==='object'&&dateArray.length>0){dateArray.map(function(date){if(data.hasOwnProperty(date)){data[date]=new Date(data[date]._seconds*1000);}
else{console.log('Please check your date parameters!!!',dateArray);parameterValid=false;}});}
if(typeof geoArray!=='undefined'&&geoArray.length>0){geoArray.map(function(geo){if(data.hasOwnProperty(geo)){data[geo]=new admin.firestore.GeoPoint(data[geo]._latitude,data[geo]._longitude);}
_c.push(_d);_e=0;_f.label=2;case 2:if(!(_e<_c.length))return[3,8];doc=_c[_e];if(!dataArray[index].hasOwnProperty(doc))return[3,7];if(!dataArray[index][doc]['subCollection'])return[3,5];subCollections=dataArray[index][doc]['subCollection'];delete dataArray[index][doc]['subCollection'];return[4,startUpdating(db,collectionName,doc,dataArray[index][doc],dateArray,geoArray)];case 3:_f.sent();return[4,updateCollection(db,subCollections,[],[])];case 4:_f.sent();return[3,7];case 5:return[4,startUpdating(db,collectionName,doc,dataArray[index][doc],dateArray,geoArray)];case 6:_f.sent();_f.label=7;case 7:_e++;return[3,2];case 8:_i++;return[3,1];case 9:return[2];}});});};var startUpdating=function(db,collectionName,doc,data,dateArray,geoArray){var parameterValid=true;if(typeof dateArray==='object'&&dateArray.length>0){dateArray.forEach(function(date){var arrDate=date.split('.');if(arrDate.length===1){if(data.hasOwnProperty(date)){data[date]=new Date(data[date]._seconds*1000);}
else{console.log('Please check your date parameters!!!',date);parameterValid=false;}}
else if(arrDate.length===2){if(data.hasOwnProperty(arrDate[0])){var temp=data[arrDate[0]];if(temp.hasOwnProperty([arrDate[1]])){data[arrDate[0]][arrDate[1]]=new Date(data[arrDate[0]][arrDate[1]]._seconds*1000);}
else{console.log('Please check your date parameters!!!',date);parameterValid=false;}}
else{console.log('Please check your date parameters!!!',date);parameterValid=false;}}
else{console.error("The package doesn't support more than two levels: "+date);}});}
if(typeof geoArray!=='undefined'&&geoArray.length>0){geoArray.forEach(function(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(function(resolve){db.collection(collectionName).doc(doc).set(data).then(function(){console.log(doc+" was successfully added to firestore!");resolve('Data written!');}).catch(function(error){console.log(error);});});}
else{console.log(doc+" was not imported to firestore. Please check your parameters!");return false;}};
else{console.log(doc+" was not imported to firestore. Please check your parameters or ignore if you don't need to import the property above.");return false;}};
{
"name": "firestore-export-import",
"version": "0.2.9",
"version": "0.3.0",
"description": "NPM package for backup and restore Firebase Firestore",

@@ -31,15 +31,15 @@ "main": "dist/index.js",

"devDependencies": {
"@types/chai": "^4.2.2",
"@types/chai": "^4.2.4",
"@types/mocha": "^5.2.7",
"@types/node": "^10.14.18",
"@types/node": "^12.12.7",
"chai": "^4.2.0",
"jsmin": "^1.0.1",
"mocha": "^5.2.0",
"mocha": "^6.2.2",
"request": "^2.88.0",
"request-promise": "^4.2.4",
"ts-node": "^8.3.0",
"typescript": "^2.9.2"
"request-promise": "^4.2.5",
"ts-node": "^8.5.0",
"typescript": "^3.7.2"
},
"dependencies": {
"firebase-admin": "^8.5.0"
"firebase-admin": "^8.7.0"
},

@@ -46,0 +46,0 @@ "engines": {

@@ -96,5 +96,7 @@ # firestore-export-import

// The array of date and location fields are optional
firestoreService.restore('your-file-path.json', ['date1', 'date2'], ['location1', 'location2']);
firestoreService.restore('your-file-path.json', ['date1', 'date2.date3'], ['location1', 'location2']);
```
* Note that the date array only support two levels now. If you pass ['date1.date2.date3'], it won't work.
#### For HTTP Request

@@ -101,0 +103,0 @@

@@ -5,2 +5,23 @@ import * as admin from 'firebase-admin';

/**
* Get item from deep level string
*
* @param o object
* @param s string
*/
// const getItemByString = (o, s) => {
// s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
// s = s.replace(/^\./, ''); // strip a leading dot
// let a = s.split('.');
// for (let i = 0, n = a.length; i < n; ++i) {
// const k = a[i];
// if (k in o) {
// o = o[k];
// } else {
// return;
// }
// }
// return o;
// }
/**
* Restore data to firestore

@@ -87,10 +108,29 @@ *

if(typeof dateArray === 'object' && dateArray.length > 0) {
dateArray.map(date => {
if (data.hasOwnProperty(date)) {
// convert date from unixtimestamp
data[date] = new Date(data[date]._seconds * 1000);
dateArray.forEach(date => {
const arrDate = date.split('.');
if (arrDate.length === 1) {
if (data.hasOwnProperty(date)) {
// convert date from unixtimestamp
data[date] = new Date(data[date]._seconds * 1000);
} else {
console.log('Please check your date parameters!!!', date);
parameterValid = false;
}
} else if (arrDate.length === 2) {
if (data.hasOwnProperty(arrDate[0])) {
const temp = data[arrDate[0]];
if (temp.hasOwnProperty([arrDate[1]])) {
// convert date from unixtimestamp
data[arrDate[0]][arrDate[1]] = new Date(data[arrDate[0]][arrDate[1]]._seconds * 1000);
} else {
console.log('Please check your date parameters!!!', date);
parameterValid = false;
}
} else {
console.log('Please check your date parameters!!!', date);
parameterValid = false;
}
} else {
console.log('Please check your date parameters!!!', dateArray);
parameterValid = false;
}
console.error(`The package doesn't support more than two levels: ${date}`)
}
});

@@ -101,3 +141,3 @@ }

if(typeof geoArray !== 'undefined' && geoArray.length > 0) {
geoArray.map(geo => {
geoArray.forEach(geo => {
if(data.hasOwnProperty(geo)) {

@@ -125,5 +165,5 @@ data[geo] = new admin.firestore.GeoPoint(data[geo]._latitude, data[geo]._longitude);

} else {
console.log(`${doc} was not imported to firestore. Please check your parameters!`);
console.log(`${doc} was not imported to firestore. Please check your parameters or ignore if you don't need to import the property above.`);
return false;
}
}

@@ -41,3 +41,3 @@ import { expect } from 'chai';

it ('Restore data', async () => {
let status = await firestoreService.restore('test/import-to-firestore.json', ['date'], ['location']);
let status = await firestoreService.restore('test/import-to-firestore.json', ['date', 'schedule.time', 'more.than.two'], ['location']);
expect(status.status).ok;

@@ -47,2 +47,3 @@

expect(result.test['first-key'].email).is.equal('dungnq@itbox4vn.com');
expect(result.test['first-key'].schedule.time._seconds).equals(1534046400)
});

@@ -60,3 +61,2 @@

});
})

@@ -9,2 +9,8 @@ {

},
"schedule": {
"time": {
"_seconds": 1534046400,
"_nanoseconds": 0
}
},
"custom": {

@@ -11,0 +17,0 @@ "lastName": "Nguyen",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc