Skip to content

Commit

Permalink
Merge pull request #1 from steadyequipment/s-shiva1995-patch-18
Browse files Browse the repository at this point in the history
s-shiva1995 patch 18 branch from steadyequipment/node-firestore-backup
  • Loading branch information
s-shiva1995 authored Jan 25, 2018
2 parents be12104 + c08ab20 commit 16887df
Show file tree
Hide file tree
Showing 12 changed files with 726 additions and 658 deletions.
142 changes: 142 additions & 0 deletions dist/firestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.backupRootCollections = exports.backupCollection = exports.backupDocument = exports.constructDocumentValue = exports.constructReferenceUrl = undefined;

var _types = require('./types');

var _utility = require('./utility');

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var constructReferenceUrl = exports.constructReferenceUrl = function constructReferenceUrl(reference) {
var referencePath = '';
Object.keys(reference).forEach(function (key) {
Object.keys(reference[key]).forEach(function (subKey) {
if (subKey === 'segments') {
var pathArray = reference[key][subKey];
pathArray.forEach(function (pathKey) {
referencePath = referencePath ? referencePath + '/' + pathKey : pathKey;
});
}
});
});
return referencePath ? { value: referencePath, typeof: 'reference' } : referencePath;
};

var constructDocumentValue = exports.constructDocumentValue = function constructDocumentValue() {
var documentDataToStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var keys = arguments[1];
var documentData = arguments[2];

keys.forEach(function (key) {
// Boolean - boolean
// Reference - reference
// Integer - number
// Array - array
// Object - object
// Float - number
// Geographical Point - todo
// Map = todo
// Null - null
// String - string
if ((0, _types.isBoolean)(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, (0, _types.isBoolean)(documentData[key])));
} else if ((0, _types.isDate)(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, (0, _types.isDate)(documentData[key])));
} else if ((0, _types.isNumber)(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, (0, _types.isNumber)(documentData[key])));
} else if ((0, _types.isArray)(documentData[key])) {
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], { typeof: 'array' });
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], constructDocumentValue({}, Object.keys(documentData[key]), documentData[key]));
} else if ((0, _types.isObject)(documentData[key])) {
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], { typeof: 'object' });
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], constructDocumentValue({}, Object.keys(documentData[key]), documentData[key]));
} else if ((0, _types.isNull)(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, (0, _types.isNull)(documentData[key])));
} else if ((0, _types.isString)(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, (0, _types.isString)(documentData[key])));
} else {
documentDataToStore = Object.assign({}, documentDataToStore, _defineProperty({}, key, constructReferenceUrl(documentData[key])));
}
});
return documentDataToStore;
};

var backupDocument = exports.backupDocument = function backupDocument(document, backupPath, logPath, prettyPrintJSON) {
console.log('Backing up Document \'' + logPath + document.id + '\'');
try {
_mkdirp2.default.sync(backupPath);
} catch (error) {
throw new Error('Unable to create backup path for Document \'' + document.id + '\': ' + error);
}

var fileContents = void 0;
try {
var documentData = document.data();
var keys = Object.keys(documentData);
var documentDataToStore = {};
documentDataToStore = Object.assign({}, constructDocumentValue(documentDataToStore, keys, documentData));
if (prettyPrintJSON === true) {
fileContents = JSON.stringify(documentDataToStore, null, 2);
} else {
fileContents = JSON.stringify(documentDataToStore);
}
} catch (error) {
throw new Error('Unable to serialize Document \'' + document.id + '\': ' + error);
}
try {
_fs2.default.writeFileSync(backupPath + '/' + document.id + '.json', fileContents);
} catch (error) {
throw new Error('Unable to write Document \'' + document.id + '\': ' + error);
}

return document.ref.getCollections().then(function (collections) {
return (0, _utility.promiseSerial)(collections.map(function (collection) {
return function () {
return backupCollection(collection, backupPath + '/' + collection.id, logPath + document.id + '/', prettyPrintJSON);
};
}));
});
};

var backupCollection = exports.backupCollection = function backupCollection(collection, backupPath, logPath, prettyPrintJSON) {
console.log('Backing up Collection \'' + logPath + collection.id + '\'');
try {
_mkdirp2.default.sync(backupPath);
} catch (error) {
throw new Error('Unable to create backup path for Collection \'' + collection.id + '\': ' + error);
}

return collection.get().then(function (documentSnapshots) {
var backupFunctions = [];
documentSnapshots.forEach(function (document) {
backupFunctions.push(function () {
return backupDocument(document, backupPath + '/' + document.id, logPath + collection.id + '/', prettyPrintJSON);
});
});
return (0, _utility.promiseSerial)(backupFunctions);
});
};

var backupRootCollections = exports.backupRootCollections = function backupRootCollections(database, backupPath, prettyPrintJSON) {
return database.getCollections().then(function (collections) {
return (0, _utility.promiseSerial)(collections.map(function (collection) {
return function () {
return backupCollection(collection, backupPath + '/' + collection.id, '/', prettyPrintJSON);
};
}));
});
};
124 changes: 124 additions & 0 deletions dist/firestore.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* @flow */

import { isString, isNull, isObject, isArray, isNumber, isDate, isBoolean } from './types'
import { promiseSerial } from './utility'

import fs from 'fs'
import mkdirp from 'mkdirp'

export const constructReferenceUrl = (reference: Object) => {
var referencePath = ''
Object.keys(reference).forEach(key => {
Object.keys(reference[key]).forEach(subKey => {
if (subKey === 'segments') {
const pathArray = reference[key][subKey]
pathArray.forEach(pathKey => { referencePath = referencePath ? `${referencePath}/${pathKey}` : pathKey })
}
})
})
return referencePath ? { value: referencePath, typeof: 'reference' } : referencePath
}

export const constructDocumentValue = (documentDataToStore: Object = {}, keys: Array<string>, documentData: Object) => {
keys.forEach(key => {
// Boolean - boolean
// Reference - reference
// Integer - number
// Array - array
// Object - object
// Float - number
// Geographical Point - todo
// Map = todo
// Null - null
// String - string
if (isBoolean(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: isBoolean(documentData[key]) })
} else if (isDate(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: isDate(documentData[key]) })
} else if (isNumber(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: isNumber(documentData[key]) })
} else if (isArray(documentData[key])) {
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], { typeof: 'array' })
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], constructDocumentValue({}, Object.keys(documentData[key]), documentData[key]))
} else if (isObject(documentData[key])) {
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], { typeof: 'object' })
documentDataToStore[key] = Object.assign({}, documentDataToStore[key], constructDocumentValue({}, Object.keys(documentData[key]), documentData[key]))
} else if (isNull(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: isNull(documentData[key]) })
} else if (isString(documentData[key])) {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: isString(documentData[key]) })
} else {
documentDataToStore = Object.assign({}, documentDataToStore, { [key]: constructReferenceUrl(documentData[key]) })
}
})
return documentDataToStore
}

export const backupDocument = (document: Object, backupPath: string, logPath: string, prettyPrintJSON: boolean) => {
console.log('Backing up Document \'' + logPath + document.id + '\'')
try {
mkdirp.sync(backupPath)
} catch (error) {
throw new Error('Unable to create backup path for Document \'' + document.id + '\': ' + error)
}

let fileContents: string
try {
const documentData = document.data()
const keys = Object.keys(documentData)
var documentDataToStore = {}
documentDataToStore = Object.assign({}, constructDocumentValue(documentDataToStore, keys, documentData))
if (prettyPrintJSON === true) {
fileContents = JSON.stringify(documentDataToStore, null, 2)
} else {
fileContents = JSON.stringify(documentDataToStore)
}
} catch (error) {
throw new Error('Unable to serialize Document \'' + document.id + '\': ' + error)
}
try {
fs.writeFileSync(backupPath + '/' + document.id + '.json', fileContents)
} catch (error) {
throw new Error('Unable to write Document \'' + document.id + '\': ' + error)
}

return document.ref.getCollections()
.then((collections) => {
return promiseSerial(collections.map((collection) => {
return () => {
return backupCollection(collection, backupPath + '/' + collection.id, logPath + document.id + '/', prettyPrintJSON)
}
}))
})
}

export const backupCollection = (collection: Object, backupPath: string, logPath: string, prettyPrintJSON: boolean) => {
console.log('Backing up Collection \'' + logPath + collection.id + '\'')
try {
mkdirp.sync(backupPath)
} catch (error) {
throw new Error('Unable to create backup path for Collection \'' + collection.id + '\': ' + error)
}

return collection.get()
.then((documentSnapshots) => {
const backupFunctions = []
documentSnapshots.forEach((document) => {
backupFunctions.push(() => {
return backupDocument(document, backupPath + '/' + document.id, logPath + collection.id + '/', prettyPrintJSON)
})
})
return promiseSerial(backupFunctions)
})
}

export const backupRootCollections = (database: Object, backupPath: string, prettyPrintJSON: boolean) => {
return database.getCollections()
.then((collections) => {
return promiseSerial(collections.map((collection) => {
return () => {
return backupCollection(collection, backupPath + '/' + collection.id, '/', prettyPrintJSON)
}
}))
})
}
Loading

0 comments on commit 16887df

Please sign in to comment.