Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to help you move your data from your dynamodb to mongodb, if your using dynogels and mongoose
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)
})
}
}
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
You can’t perform that action at this time.