Skip to content

Instantly share code, notes, and snippets.

@blackwright
Last active April 7, 2020 10:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blackwright/4b3ff2cdd60ab5fcf2512b2ea670dfa1 to your computer and use it in GitHub Desktop.
Save blackwright/4b3ff2cdd60ab5fcf2512b2ea670dfa1 to your computer and use it in GitHub Desktop.
Sequelize REPL
// 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