This question already has answers here:
How can I see the SQL generated by Sequelize.js?
(4 answers)
Closed 6 years ago.
Good afternoon everyone. I am developing a node.js/express system using sequelize.js (postgresql).
My problem is: I need to store the raw queries generated by sequelize in a log history, but I can't find a function that returns the generated query. Does anyone know if sequelize provides a function that returns the generated SQL query, or if there's any other way to achieve this?
Your best bet is to use Sequelize's built-in logging functionality.
var sequelize = new Sequelize('db', 'username', 'pwd', {
// you can either write to console
logging: console.log
// or write your own custom logging function
logging: function (str) {
// do stuff with the sql str
}
});
Related
This question already has answers here:
Mongoose: findOneAndUpdate doesn't return updated document
(16 answers)
Closed 3 years ago.
I am working on node js app using express.js and mongodb with mongoose module to handle the database.
in one case I need to update a user property languages using findOneAndUpdate method. this property is an array of objects should looks like [{"language":"Ar","level":4}]
when I update it using the following nodejs code :
User.findOneAndUpdate({_id:mongoose.Types.ObjectId(req.body.id)},
{$set:{[req.body.key]:req.body[req.body.key]}},(err,doc)=>{
console.log(req.body) // {id:5d1619fa7c11fa102210ef86,"languages":[{"language":"Ar","level":4}],key:"languages"}
if (!err) {
res.json(200,doc);
}else{
res.json(500,{error:err});
}
})
I get the following results
but when I try same thing from the mongo shell
db.users.findOneAndUpdate({"_id" : ObjectId("5d1619fa7c11fa102210ef86")},{ '$set': {"languages":[{"language":"Ar","level":4}]} })
I get the correct result
which is the correct expected results.
any idea why the nodejs snippet not working properly.
thanks in advance.
I think you are not getting the updated result back after update has successfully run. you need to pass new : true in your update query as the third argument.
By default mongodb returns the old document after the update, but you can specify to return the updated document in the query.
Try this :
User.findOneAndUpdate({_id:mongoose.Types.ObjectId(req.body.id)},
{$set:{[req.body.key]:req.body[req.body.key]},{new:true},(err,doc)=>{
...
//doc will be updated doc here
})
Is it possible to omit a single query log while using Sequelize?
For obvious reason I want to know every query shot by my NodeJs server, but I have one that is tied to a recursive function that works like a printing spooler and it's spamming my server log.
Cheers
Yes, it's possible.
If you want to omit logging from a plain query, use this code:
sequelize
.query('SELECT ...', null, {
logging: false
});
If you want to disable logging when finding an object ORM-style, use this syntax:
Model.findAll({
where: {
id: 123
},
logging: false
});
The official documentation where this information is from can be found at http://docs.sequelizejs.com/en/latest/api/model/#findall.
I am creating an expressjs api and using mongodb. I have a decent understanding of indexes and I understand that they are expensive to create when there is data in the database.
In MS Sql Server you would create indexes when creating your database tables. My question is do I handle this creation of indexes in a post call in my express app or do I achieve this using scripts when deploymening my application?
For example I need Geospatial indexing.
Would index creation be handled in the express app like this?
//express post call
let col = db.collection( 'collection' );
col.createIndex( // someIndex );
col.insertOne( //Some document );
I am looking for the best method to creating the 'initial' state of my mongodb and specifically creating indexes I will need for certain collections before these collections contain any documents.
So, It may happen, You have a lot of data in your database while deployment and you do not want your Indexing terrible. Here's what MongoDB can Help. You can do indexing in Background which will not prevent all read and write operations to the database while the index builds.A simple Command:
db.collection.createIndex( { a: 1 }, { background: true } )
Check the Manual For details.
https://docs.mongodb.org/manual/tutorial/build-indexes-in-the-background/
This question already has answers here:
Node.js - Mongoose - Check if a collection exists
(4 answers)
Closed 8 years ago.
I am new to mongo Db and need to see if schema is already been created or not. I am using mongoose and node.js. If its not created i need to somehow run the creation script for once or else proceed with other stuff.
TIA
Assuming that by schema you mean that you want a check if the collection is already created in mongoDB, you should check this question here that explains the solution.
Quoting it:
Assuming you have a Mongoose Connection object named conn that's been opened using mongoose.createConnection, you can access the native mongo Db object via conn.db. From there you can call collectionNames which should provide what you're looking for:
conn.db.collectionNames(function (err, names) {
// names contains an array of objects that contain the collection names
});
You can also pass a collection name as a parameter to
collectionNames to filter the results to just what you're looking for.
I want to see the SQL commands that are sent to the PostgreSQL server because I need to check if they are correct. In particular, I am interested in the table creation commands.
For instance, ActiveRecord (Ruby) prints its SQL statements to standard output. Is this possible with Node.js/ActionHero.js and Sequelize.js as well?
You can pass a logging option when initializing sequelize, which can either be a function or console.log
var sequelize = new Sequelize('database', 'username', 'password', {
logging: console.log
logging: function (str) {
// do your own logging
}
});
You can also pass a logging option to .sync if you only want to view the table creation queries
sequelize.sync({ logging: console.log })
As stated in the log Error: Please note that find* was refactored and uses only one options object from now on.. For the latest sequelize version (4) if you want to have the result for only one command:
User.findAll({where: {...}, logging: console.log})
If you want to look at the sequelize for one command you can listen to it and attach a function to the print the sql.
Check out this example:
User.find(1).on('sql', console.log).then(function(user) {
// do whatever you want with the user here
You can also take advantage of Sequelize's use of the Debug module, by setting your environment, thus:
DEBUG=sequelize:sql*
before starting your app.