Skip to content

Instantly share code, notes, and snippets.

@gnud
Last active May 1, 2021 22:41
Show Gist options
  • Save gnud/a0e8877e059daae5c19da68d69385d80 to your computer and use it in GitHub Desktop.
Save gnud/a0e8877e059daae5c19da68d69385d80 to your computer and use it in GitHub Desktop.
Instantiate sequelize via REPL instance for interactive db access and model testing
/*
Usage:
start with:
node --experimental-repl-await interactive_sequelize.js
Then we get the REPL shell
>
> await Product.findAll();
out: dataValues and other stuff in an array having one object aka the model instance
*/
const repl = require('repl');
console.log('Welcome to the interactive Sequelize sandbox!');
const r = repl.start({ useGlobal: true });
// Load some sequelize models here
// const { {Product } = require('../models');
// Provided that you exported the models in index
const index = require('../models/index');
// Enumerate all the exports, provided that all exports are models
const modelsNames = Object.keys(index);
console.log('Following Models are available:\n', modelsNames.join(','));
console.log('\nYou can autocomplete with tab.');
console.log('Press enter to continue!');
modelsNames.forEach((model)=>{
r.context[model] = index[model];
})

We like to see only values data from findAll:

First item data (for some reason)

const data = ((await Product.findAll()).shift() || {dataValues: {}}).dataValues;

Just dataValues transformation

const data = (await Product.findAll()).map(item=>item.dataValues || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment