-
-
Save vinbarnes/e5930cbeb86b571b2623341da59233ec to your computer and use it in GitHub Desktop.
Sequelize REPL
This file contains hidden or 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
// Require the REPL module | |
// and models | |
let repl = require('repl').start({}); | |
const models = require('./models'); | |
// Make the `models` object | |
// a global variable in the | |
// REPL | |
repl.context.models = models; | |
// Make each model a global | |
// object in the REPL | |
Object.keys(models).forEach((modelName) => { | |
repl.context[modelName] = models[modelName]; | |
}); | |
// Provide a convenience function `lg` | |
// to pass to `then()` and `catch()` | |
// to output less verbose values for | |
// sequelize model query results | |
repl.context.lg = data => { | |
if (Array.isArray(data)) { | |
if (data.length && data[0].dataValues) { | |
data = data.map(item => item.dataValues); | |
} | |
} else { | |
if (data.dataValues) { | |
data = data.dataValues; | |
} | |
} | |
console.log(data); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment