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.3.0 to 0.3.1

3

CHANGELOG.md

@@ -13,2 +13,5 @@ # Change log

## [0.3.1] - 2019-11-11
#### - :bug: [Bug Fix]
- Allow multi level time #33

@@ -15,0 +18,0 @@ ## [0.3.0] - 2019-11-11

6

dist/import.d.ts
/**
* Get item from deep level string
*
* @param o object
* @param s string
*/
/**
* Restore data to firestore

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

@@ -12,15 +12,12 @@ "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);});}

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;};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(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)
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"));var updateTime=function(dateArray,documentObj){dateArray.forEach(function(c){c.split(".").reduce(function(acc,cur,i,a){if(!a[i+1]&&acc[cur]&&acc[cur]._seconds){acc[cur]=new Date(acc[cur]._seconds*1000);}
else
return acc[cur]||{};},documentObj);});};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(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.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);});});}
_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){updateTime(dateArray,data);}
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 or ignore if you don't need to import the property above.");return false;}};
{
"name": "firestore-export-import",
"version": "0.3.0",
"version": "0.3.1",
"description": "NPM package for backup and restore Firebase Firestore",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -121,2 +121,8 @@ # firestore-export-import

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

@@ -123,0 +129,0 @@ "lastName": "Nguyen",

@@ -1,87 +0,111 @@

import * as admin from 'firebase-admin';
import * as fs from 'fs';
import * as admin from "firebase-admin";
import * as fs from "fs";
/**
* Get item from deep level string
*
* @param o object
* @param s string
* Convert time array in document object
* @param dateArray
* @param documentObj
*/
// 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;
// }
const updateTime = (dateArray, documentObj): void => {
dateArray.forEach(c => {
c.split(".").reduce((acc, cur, i, a) => {
if (!a[i + 1] && acc[cur] && acc[cur]._seconds) {
acc[cur] = new Date(acc[cur]._seconds * 1000);
} else return acc[cur] || {};
}, documentObj);
});
};
/**
* Restore data to firestore
*
* @param {string} fileName
*
* @param {string} fileName
* @param {Array<string>} dateArray
* @param {Array<string>} geoArray
*/
export const restore = (fileName: string, dateArray: Array<string>, geoArray: Array<string>): Promise<any> => {
export const restore = (
fileName: string,
dateArray: Array<string>,
geoArray: Array<string>
): Promise<any> => {
const db = admin.firestore();
return new Promise((resolve, reject) => {
if (typeof fileName === 'object') {
if (typeof fileName === "object") {
let dataArray = fileName;
updateCollection(db, dataArray, dateArray, geoArray).then(() => {
resolve({ status: true, message: 'Collection successfully imported!' });
}).catch(error => {
updateCollection(db, dataArray, dateArray, geoArray)
.then(() => {
resolve({
status: true,
message: "Collection successfully imported!"
});
})
.catch(error => {
reject({ status: false, message: error.message });
});
});
} else {
fs.readFile(fileName, 'utf8', function (err, data) {
fs.readFile(fileName, "utf8", function(err, data) {
if (err) {
console.log(err)
console.log(err);
reject({ status: false, message: err.message });
}
// Turn string from file to an Array
let dataArray = JSON.parse(data);
updateCollection(db, dataArray, dateArray, geoArray).then(() => {
resolve({ status: true, message: 'Collection successfully imported!' });
}).catch(error => {
updateCollection(db, dataArray, dateArray, geoArray)
.then(() => {
resolve({
status: true,
message: "Collection successfully imported!"
});
})
.catch(error => {
reject({ status: false, message: error.message });
});
})
}
})
});
});
}
});
};
}
/**
* Update data to firestore
*
* @param {any} db
* @param {Array<any>} dataArray
* @param {Array<string>} dateArray
* @param {Array<string>} geoArray
*
* @param {any} db
* @param {Array<any>} dataArray
* @param {Array<string>} dateArray
* @param {Array<string>} geoArray
*/
const updateCollection = async (db, dataArray: Array<any>, dateArray: Array<string>, geoArray: Array<string>) => {
const updateCollection = async (
db,
dataArray: Array<any>,
dateArray: Array<string>,
geoArray: Array<string>
) => {
for (var index in dataArray) {
var collectionName = index;
for (var doc in dataArray[index]) {
if (dataArray[index].hasOwnProperty(doc)) {
if (dataArray[index][doc]['subCollection']) {
const subCollections = dataArray[index][doc]['subCollection'];
delete dataArray[index][doc]['subCollection'];
await startUpdating(db, collectionName, doc, dataArray[index][doc], dateArray, geoArray);
if (dataArray[index].hasOwnProperty(doc)) {
if (dataArray[index][doc]["subCollection"]) {
const subCollections = dataArray[index][doc]["subCollection"];
delete dataArray[index][doc]["subCollection"];
await startUpdating(
db,
collectionName,
doc,
dataArray[index][doc],
dateArray,
geoArray
);
await updateCollection(db, subCollections, [], []);
} else {
await startUpdating(db, collectionName, doc, dataArray[index][doc], dateArray, geoArray);
await startUpdating(
db,
collectionName,
doc,
dataArray[index][doc],
dateArray,
geoArray
);
}

@@ -91,67 +115,50 @@ }

}
}
};
/**
* Write data to database
* @param db
* @param collectionName
* @param doc
* @param data
* @param dateArray
* @param geoArray
* @param db
* @param collectionName
* @param doc
* @param data
* @param dateArray
* @param geoArray
*/
const startUpdating = (db, collectionName: string, doc: string, data: object, dateArray: Array<string>, geoArray: Array<string>) => {
let parameterValid = true;
const startUpdating = (
db,
collectionName: string,
doc: string,
data: object,
dateArray: Array<string>,
geoArray: Array<string>
) => {
let parameterValid = true;
if(typeof dateArray === 'object' && dateArray.length > 0) {
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.error(`The package doesn't support more than two levels: ${date}`)
}
});
if (typeof dateArray === "object" && dateArray.length > 0) {
updateTime(dateArray, data);
}
// Enter geo value
if(typeof geoArray !== 'undefined' && geoArray.length > 0) {
if (typeof geoArray !== "undefined" && geoArray.length > 0) {
geoArray.forEach(geo => {
if(data.hasOwnProperty(geo)) {
data[geo] = new admin.firestore.GeoPoint(data[geo]._latitude, data[geo]._longitude);
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);
console.log("Please check your geo parameters!!!", geoArray);
parameterValid = false;
}
})
}
});
}
if (parameterValid) {
return new Promise(resolve => {
db.collection(collectionName).doc(doc)
db.collection(collectionName)
.doc(doc)
.set(data)
.then(() => {
console.log(`${doc} was successfully added to firestore!`);
resolve('Data written!');
resolve("Data written!");
})

@@ -161,7 +168,9 @@ .catch(error => {

});
})
});
} 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.`);
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', 'schedule.time', 'more.than.two'], ['location']);
let status = await firestoreService.restore('test/import-to-firestore.json', ['date', 'schedule.time', 'three.level.time'], ['location']);
expect(status.status).ok;

@@ -44,0 +44,0 @@

@@ -15,2 +15,10 @@ {

},
"three": {
"level": {
"time": {
"_seconds": 1534046400,
"_nanoseconds": 0
}
}
},
"custom": {

@@ -55,2 +63,2 @@ "lastName": "Nguyen",

}
}
}

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