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.
Related
I am migrating from sequelizeORM to typeORM. In sequelize-cli there are nice commands to drop database and create a new one for example:
node_modules/.bin/sequelize db:drop
node_modules/.bin/sequelize db:create
node_modules/.bin/sequelize db:migrate
Ok, for typeORM I know how to run migration but I can't find anywhere how to automatically create or drop a database. Tnx in advance.
you can use the CLI tool typeorm schema:drop or if you want to drop and recreate the database on each startup, you can use
{
...
dropSchema: true,
...
"migrations": [
"dist/migrations/**/*.js"
]
}
where you do your connection options and set migrations path, and so forth.
You can also use typeorm-extension package, to create or drop the database specified for the connection, for nearly all database driver.
The package also parses the extra parameter charset and characterSet of the extra parameter (ENV: TYPEORM_DRIVER_EXTRA)
import {createDatabase} from "typeorm-extension";
(async () => {
await createDatabase({ifNotExist: true});
await dropDatabase({ifExist: true});
process.exit(0);
})();
You can also provide the charset and characterSet as properties for the first parameter to the createDatabase() function.
F.e
postgres
createDatabase({ifNotExist: true, characterSet: "UTF8"});
mysql
createDatabase({ifNotExist: true, charset: "utf8mb4_general_ci", characterSet: "utf8mb4"});
If you have any questions or concerns feel free to contact me or contribute to the codebase on Github
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'm currently working in a project where our Node.js server will perform a lot of interactions against an existing MySQL database. Thus I'm wondering if Sequelize is a good library to interface the database. From what I've read about it, it is most often used as a master of the database. But in my case it will only have select,insert,delete access and not access to modify and create tables and so on. Does Sequelize support this method of interaction with a database?
If Sequelize does indeed work good for this, what settings do i need to disable to not run into much trouble? After reading their documentation i could not find any global settings to turn it into a simple interface tool. Timestamps and such could be disabled on table definition but not globally what I saw. Any input is greatly appreciated.
There are a lot of questions in this post, I'll try to answer them all:
Disable timestamps globally:
new Sequelize(... ,{
define: {
timestamps: false
}
});
You can pass any define options to the sequelize constructor and they will be applied to all calls to sequelize.define
Mapping to an existing database
I'll try to describe some common cases here:
I want my model to have a different name to my database table:
sequelize.define('name of model', attributes, {
tableName: 'name of table'
});
My database columns are called something different than the attributes in my model:
sequelize.define('name of model', {
name_of_attribute_in_model: {
type: ...
field: 'name of field in table'
}
});
My primary key is not called id:
sequelize.define('name of model', {
a_field_totally_not_called_id: {
primaryKey: true // also allows for composite primary keys, even though the support for composite keys accross associations is spotty
autoIncrement: true
}
});
My foreign keys are called something different
X.belongsTo(Y, { foreignKey: 'something_bla' });
disclaimer: I am a sequelize maintainer :). Overall I think we have pretty good support for working with legacy DBs. Feel free to ask more questions here or on irc://irc.freenode.net#sequelizejs
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
}
});
Is there any way to set database schema with sails-postgresql waterline adapter?
By default postgres adapter allways choose default public schema in database but I want to connect it to another schema.
For example I have database dev, schema test in database dev and table users in schema test.
Now I want to select all data from table users, in sql syntax I can simply write:
SELECT * FROM test.users
How to make it work in sails ?
When I write a model that uses postgres adapter, method Users.find() will look for the table users in default public schema. I want to change it to look in schema test without interactions with my postgres database.
Is it possible?
There is support for this, although it is as-yet undocumented. You can set the schema name for a model using the meta.schemaName property, eg:
module.exports = {
tableName: 'users',
meta: {
schemaName: 'test'
},
attributes: {
...
}
};
Update
It turns out this functionality was essentially broken for several versions, but it has been revamped and released in Sails-Postgresql v0.11.0. The syntax is the same as above. The main caveat is that it will not work with multiple Waterline models sharing the same tableName.
It appears that this is a bit buggy on the latest 1.0.3 version, but I found a way to accomplish it, by doing :
postgresql: {
adapter: require('sails-postgresql'),
url: 'postgresql://postgres:blablabla#localhost:5432/simplerp',
schemaName: 'radius',
}
On your config/datastores.js file.
Peace, out!