Skip to content

Commit

Permalink
fix async/await functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wooklym committed Jan 7, 2017
1 parent 16889da commit aaabb0a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
108 changes: 67 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import admin from 'firebase-admin';
import mongo from 'mongo-async';
import MongoClient from 'mongodb';
import Promise from 'promise';

const MONGO_URL = process.env.MONGO_URL || '<YOUR_MONGO_URL>';
const FIREBASE_DATABASE_NAME = process.env.FIREBASE_DATABASE_NAME || '<YOUR_FIREBASE_DATABASE_NAME>';
Expand All @@ -12,52 +13,77 @@ admin.initializeApp({
databaseURL: FIREBASE_DATABASE_URL
});

const ref = admin.database().ref('/');
function getMongoConnection(url) {
return new Promise((resolve, reject) => {
MongoClient.connect(url, (err, db) => {
if (err) {
return reject(err);
}
resolve(db);
});
});
}

async function getCollectionsFromMongo (callback) {
try {
const db = await mongo.connect(MONGO_URL);
const collections = await db.collections();

const collectionNames = collections.map(c => c.s.name);
console.log(collectionNames);
collectionNames.forEach((name, count) => {
db.collection(name).find({}).toArray((err, docs) => {
if (err) {
return callback(err);
}

if (docs.length === 0) {
return callback(`${name} is empty.`);
}

const docObjects = docs.reduce((o, v) => {
o[v._id] = v;
return o;
}, {});

if (name !== 'system.indexes') {
callback(null, {name: name, docObjects});
}
});
function getMongoCollections(db) {
return new Promise((resolve, reject) => {
db.collections((err, collections) => {
if (err) {
return reject(err);
}
resolve(collections);
});
} catch (error) {
callback (error);
}
});
}

function getDocsOfCollection(db, collectionName) {
return new Promise((resolve, reject) => {
db.collection(collectionName).find({}).toArray((err, docs) => {
if (err) {
return reject(err);
}
console.log(`size of ${collectionName}: ${docs.length}`);
resolve(docs);
});
});
}

function mongoDocsToObject(docs) {
return docs.reduce((o, v) => {
o[v._id] = v;
return o;
}, {});
}

getCollectionsFromMongo((err, result) => {
if (err) {
return console.log(err);
async function saveMongoCollectionToFirebase(db, ref, collectionName) {
try {
const docs = await getDocsOfCollection(db, collectionName);
const docObj = mongoDocsToObject(docs);
console.log(`start migration : collection ${collectionName}`);
return ref.child(collectionName).set(docObj);
} catch (e) {
console.log(e);
}
}

ref.child(result.name).set(result.docObjects, (err) => {
if (err) {
return console.log(err);
}
async function migrateFromMongoToFirebase() {
try {
const db = await getMongoConnection(MONGO_URL);
const collections = await getMongoCollections(db);

console.log(`${result.name} Collection has been inserted to Firebase.`);
});
});
const ref = admin.database().ref('/');

const promises = collections.map(c => c.s.name)
.filter(collectionName => collectionName !== 'system.indexes')
.map(async (collectionName) => await saveMongoCollectionToFirebase(db, ref, collectionName));

return Promise.all(promises);
} catch(e) {
console.log(e);
}
}

(async function() {
await migrateFromMongoToFirebase();
console.log('complete');
}());

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"homepage": "https://github.com/wooklym/firemongo#readme",
"dependencies": {
"firebase-admin": "^4.0.4",
"mongo-async": "^0.2.1"
"mongodb": "^2.2.19",
"promise": "^7.1.1"
},
"devDependencies": {
"babel-cli": "^6.18.0",
Expand Down

0 comments on commit aaabb0a

Please sign in to comment.