I am new to sequelize. I am trying to use row query, but I'm getting an error.
Code:
const sequelize = require('sequelize');
sequelize.query("SELECT * from users").then(results => {
console.log(results);
})
Error while calling this API:
(node:2380) UnhandledPromiseRejectionWarning: TypeError: sequelize.query is not a function
at Promise (C:\NodejsProject\mars\app\schedule\models\schedule.js:247:17)
at new Promise (<anonymous>)
How can I fix this?
Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );
As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can use the sequelize. query method. By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc).
Log SQL Query to the Console Here's how: Sequelize supports the logging option when composing a query object. This configuration overrides the default logging setup in your SQL connector. The logging option expects a log function, like console. log which receives the generates SQL statement.
you can try to call describeTable of QueryInterface (to get QI call getQueryInterface in sequelize) to get information about a table.
You can't use
sequelize
directly fromrequire('sequelize');
, first we need to create instance of that , and instance will be created from DB details , sosequelize
can know on which platform , which DB it has to run the query
Here is the basic snippet , just for a idea :
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql'|'sqlite'|'postgres'|'mssql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
// SQLite only
storage: 'path/to/database.sqlite',
// http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
operatorsAliases: false
});
sequelize.query("SELECT * from users").then(results => {
console.log(results);
});
For more detail : DO READ
If you are running your db config in external file AND want and extra layer to protect from SQL injection ./database/dbs.js
const Sequelize = require('sequelize')
const db = {}
const sequelize = new Sequelize('dbname',
'username',
'password', {
host: 'localhost',
dialect: 'mysql',
logging: console.log,
freezeTableName: true,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
then in the file you want to use:
const db = require('../database/db')
...
const newDbName = 'myNewDb'
db.sequelize.query(`CREATE DATABASE IF NOT EXISTS ${newDbName} `, {
type: db.sequelize.QueryTypes.CREATE
}).then( () => {
res.send('ok done creating ' + req.body.newDbName).end()
}).catch( err => {
res.status(500).send('Err executing command ' + err).end()
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With