Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calls listDocuments to also get refs to empty documents #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,28 @@ var promiseSerial = function promiseSerial(funcs) {
};

var backupDocument = function backupDocument(document, backupPath, logPath) {
console.log("Backing up Document '" + logPath + document.id + "'" + (plainJSONBackup === true ? ' with -J --plainJSONBackup' : ' with type information'));
console.log("Backing up" + (document.exists ? " " : " (empty) ") + "Document '" + logPath + document.id + "'" + (plainJSONBackup === true ? ' with -J --plainJSONBackup' : ' with type information'));

try {
_mkdirp2.default.sync(backupPath);
var fileContents = void 0;
var documentBackup = plainJSONBackup === true ? document.data() : (0, _FirestoreDocument.constructDocumentObjectToBackup)(document.data());
if (prettyPrint === true) {
if (stable === true) {
fileContents = (0, _jsonStableStringify2.default)(documentBackup, { space: 2 });
} else {
fileContents = JSON.stringify(documentBackup, null, 2);
}
} else {
if (stable === true) {
fileContents = (0, _jsonStableStringify2.default)(documentBackup);
if (document.exists) {
var fileContents = void 0;
var documentBackup = plainJSONBackup === true ? document.data() : (0, _FirestoreDocument.constructDocumentObjectToBackup)(document.data());
if (prettyPrint === true) {
if (stable === true) {
fileContents = (0, _jsonStableStringify2.default)(documentBackup, { space: 2 });
} else {
fileContents = JSON.stringify(documentBackup, null, 2);
}
} else {
fileContents = JSON.stringify(documentBackup);
if (stable === true) {
fileContents = (0, _jsonStableStringify2.default)(documentBackup);
} else {
fileContents = JSON.stringify(documentBackup);
}
}
_fs2.default.writeFileSync(backupPath + '/' + document.id + '.json', fileContents);
}
_fs2.default.writeFileSync(backupPath + '/' + document.id + '.json', fileContents);

return document.ref.getCollections().then(function (collections) {
return promiseSerial(collections.map(function (collection) {
Expand All @@ -170,16 +172,20 @@ var backupCollection = function backupCollection(collection, backupPath, logPath
try {
_mkdirp2.default.sync(backupPath);

return collection.get().then(function (snapshots) {
var backupFunctions = [];
snapshots.forEach(function (document) {
backupFunctions.push(function () {
var backupDocumentPromise = backupDocument(document, backupPath + '/' + document.id, logPath + collection.id + '/');
restoreDocument(logPath + collection.id, document);
return backupDocumentPromise;
return collection.listDocuments().then(function (documentRefs) {
return Promise.all(documentRefs.map(function (documentRef) {
return documentRef.get();
})).then(function (snapshots) {
var backupFunctions = [];
snapshots.forEach(function (document) {
backupFunctions.push(function () {
var backupDocumentPromise = backupDocument(document, backupPath + '/' + document.id, logPath + collection.id + '/');
restoreDocument(logPath + collection.id, document);
return backupDocumentPromise;
});
});
return promiseSerial(backupFunctions);
});
return promiseSerial(backupFunctions);
});
} catch (error) {
console.log(_colors2.default.bold(_colors2.default.red("Unable to create backup path, skipping backup of Collection '" + collection.id + "': ")) + _colors2.default.bold(backupPath) + ' - ' + error);
Expand All @@ -192,6 +198,9 @@ var accountDb = accountCredentialsPath ? accountApp.firestore() : null;
var restoreAccountDb = exports.restoreAccountDb = restoreAccountCredentialsPath ? restoreAccountApp.firestore() : null;

var restoreDocument = function restoreDocument(collectionName, document) {
if (!document.exists) {
return Promise.resolve(null);
}
var restoreMsg = 'Restoring to collection ' + collectionName + ' document ' + document.id;
console.log(restoreMsg + '...');
return Promise.resolve(
Expand Down
67 changes: 37 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const backupDocument = (
logPath: string
): Promise<any> => {
console.log(
"Backing up Document '" +
"Backing up" + (document.exists ? " " : " (empty) ") + "Document '" +
logPath +
document.id +
"'" +
Expand All @@ -164,25 +164,27 @@ const backupDocument = (

try {
mkdirp.sync(backupPath);
let fileContents: string;
const documentBackup =
plainJSONBackup === true
? document.data()
: constructDocumentObjectToBackup(document.data());
if (prettyPrint === true) {
if (stable === true) {
fileContents = stringify(documentBackup, { space: 2 });
if (document.exists) {
let fileContents: string;
const documentBackup =
plainJSONBackup === true
? document.data()
: constructDocumentObjectToBackup(document.data());
if (prettyPrint === true) {
if (stable === true) {
fileContents = stringify(documentBackup, { space: 2 });
} else {
fileContents = JSON.stringify(documentBackup, null, 2);
}
} else {
fileContents = JSON.stringify(documentBackup, null, 2);
}
} else {
if (stable === true) {
fileContents = stringify(documentBackup);
} else {
fileContents = JSON.stringify(documentBackup);
if (stable === true) {
fileContents = stringify(documentBackup);
} else {
fileContents = JSON.stringify(documentBackup);
}
}
fs.writeFileSync(backupPath + '/' + document.id + '.json', fileContents);
}
fs.writeFileSync(backupPath + '/' + document.id + '.json', fileContents);

return document.ref.getCollections().then(collections => {
return promiseSerial(
Expand Down Expand Up @@ -230,20 +232,22 @@ const backupCollection = (
try {
mkdirp.sync(backupPath);

return collection.get().then(snapshots => {
const backupFunctions = [];
snapshots.forEach(document => {
backupFunctions.push(() => {
const backupDocumentPromise = backupDocument(
document,
backupPath + '/' + document.id,
logPath + collection.id + '/'
);
restoreDocument(logPath + collection.id, document);
return backupDocumentPromise;
return collection.listDocuments().then(documentRefs => {
return Promise.all(documentRefs.map(documentRef => documentRef.get())).then(snapshots => {
const backupFunctions = [];
snapshots.forEach(document => {
backupFunctions.push(() => {
const backupDocumentPromise = backupDocument(
document,
backupPath + '/' + document.id,
logPath + collection.id + '/'
);
restoreDocument(logPath + collection.id, document);
return backupDocumentPromise;
});
});
});
return promiseSerial(backupFunctions);
return promiseSerial(backupFunctions);
})
});
} catch (error) {
console.log(
Expand All @@ -269,6 +273,9 @@ export const restoreAccountDb = restoreAccountCredentialsPath
: null;

const restoreDocument = (collectionName: string, document: Object) => {
if (!document.exists) {
return Promise.resolve(null)
}
const restoreMsg = `Restoring to collection ${collectionName} document ${
document.id
}`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"babel-runtime": "^6.26.0",
"colors": "^1.1.2",
"commander": "^2.11.0",
"firebase-admin": "^5.4.3",
"firebase-admin": "^6.2.0",
"json-stable-stringify": "^1.0.1",
"mkdirp": "^0.5.1"
},
Expand Down
Loading