How to run a sequelize query in the console?

Viewed 1112

When I run a sequelize query in the console during a debug session, the return is an unresolved promise. But how can I get the outcome of that query/promise instantly?

The way I do it so far:

Author.findOne({})
      .then(function(error, result){
               debugger;
               //now I can work with the outcome in the console
            })

This approach is extremely time-consuming, simple changes in the query would require a rerun of the whole debug session to see the new outcome.

1 Answers

Even though Node offloads operations to the OS when possible and there might be multiple threads running under the hood, there is only one execution context for JavaScript / one event loop. Once you hit a breakpoint, event loop will not be processed until you yield control. Therefore, none of the asynchronous API operations will be processed. To get the result of your sequelize query, you must resume execution.

Alternatively to using a debugger, you could consider a REPL approach. Run node --inspect-brk and inspect the waiting node runtime in Chrome dev tools (listed under chrome://inspect). Now initialize sequelize, connect to the db, load the Author model (or do this in a setup script which you would run with --inspect) and then evaluate the following:

await Author.findOne({})

You can inspect the resulting object in the console, store it in a global variable (right click on the evaluated object -> Store as global variable), stringify / copy the globally stored variable to clipboard (copy(JSON.stringify(temp1))) etc. You can easily tweak and re-run your query.

Related