Last active
February 27, 2019 10:11
-
-
Save sp90/27467ab252e1843513d609f506746426 to your computer and use it in GitHub Desktop.
A script to help you move your data from your dynamodb to mongodb, if your using dynogels and mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird') | |
const _ = require('lodash') | |
// Include your dynogels models | |
let models = require('./routes/helpers/models') | |
// Include your mongoose models | |
let mongooseModels = require('./routes/helpers/mongooseModels') | |
module.exports = (settings, itemsMapping) => { | |
if (!(settings && settings.name && settings.oldModel && settings.newModel)) { | |
throw Error('Missing settings name, oldModel & newModel is required') | |
} | |
let _self = {} | |
_self.notTesting = settings.notTesting | |
_self.name = settings.name | |
_self.limit = settings.limit | |
_self.OLDMODEL = models[settings.oldModel] | |
_self.NEWMODEL = mongooseModels[settings.newModel] | |
let getOldItems = () => { | |
return new Promise((resolve, reject) => { | |
let SCAN = _self.OLDMODEL | |
.scan() | |
if (_self.limit) { | |
SCAN = SCAN.limit(_self.limit) | |
} else { | |
SCAN = SCAN.loadAll() | |
} | |
SCAN | |
.execAsync() | |
.then(data => { | |
let newData = _.map(data.Items, 'attrs') | |
resolve({ | |
Count: data.Count, | |
data: newData | |
}) | |
}) | |
.catch(err => { | |
console.log("get err: ", err) | |
throw Error('Failed to get ' + _self.name) | |
}) | |
}) | |
} | |
let insertNewItems = (newItems) => { | |
return new Promise((resolve, reject) => { | |
_self.NEWMODEL | |
.create(newItems) | |
.then(items => { | |
resolve(items) | |
}) | |
.catch(err => { | |
console.log("insert err: ", err) | |
throw Error('Failed to insert new ' + _self.name) | |
}) | |
}) | |
} | |
if (_self.notTesting) { | |
return getOldItems() | |
.then(result => { | |
let oldItems = result.data | |
_self.oldCount = result.Count | |
return itemsMapping(oldItems) | |
}) | |
.then(newItems => { | |
return insertNewItems(newItems) | |
}) | |
.then(result => { | |
console.log("result: ", result) | |
if (result.length === _self.oldCount) { | |
console.log('Success count matches') | |
} else { | |
var string = 'Error, count dosn\'t match - result: ' + result.length + ' & oldCount: ' + oldCount | |
console.log(string) | |
} | |
}) | |
.catch(err => { | |
console.log("catch err: ", err) | |
}) | |
} else { | |
return getOldItems() | |
.then(result => { | |
let oldItems = result.data | |
_self.oldCount = result.Count | |
return itemsMapping(oldItems) | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird') | |
const _ = require('lodash') | |
// Require the remapping script | |
const remapData2M = require('./dynogels-to-mongoose') | |
remapData2M({ | |
name: 'User', // Just for logging messages could be anything | |
notTesting: false, // Set to true to write the data to the new table | |
//limit: 50, // Limit how many entries, good for testing | |
oldModel: 'User', // Your dynogels model object name | |
newModel: 'UserV2' // Your mongoose model object name | |
}, (oldItems) => { | |
return new Promise((resolve, reject) => { | |
// Here you can do some data modeling | |
resolve(oldItems) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment