How to fetch all fields of table using sequelize in nodejs? - mysql

I want to fetch all fields without to write all fields name of table. How to use * to fetch all the fields from the table ?
This is UserController function to fetch all the rows with all fields of table.
user.findAll({
attributes: ['name','email','mobile','dob','address','image','is_active'],
where:{
is_active:'1',
name:{$like:'%%'}
},
limit:10
}).then(function(users,fields){
console.log(users);
res.send({error:false,message:'users list',data:users});
}).catch(function(err){
console.log('Oops! something went wrong, : ', err);
});
This is model code and define some property of model.
var sequalize = require('../../config/db_config');
const User = sequalize.define('user',{},{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: 'user',
createdAt:'created_on',
updatedAt:'updated_on'
});
User.sync({force:false}).then(() => {
console.log('Table is created!');
}).catch(err => {
console.log('An error occur when table is created!');
});
module.exports = User;
Please help me that how to fetch all fields with write to attribute in controller.

Simply ommit the attributes key in your query and all the fields will return.
user.findAll({
where:{
is_active:'1',
name:{$like:'%%'}
},
limit:10
}).then(function(users){
console.log(users);
res.send({error:false,message:'users list',data:users});
}).catch(function(err){
console.log('Oops! something went wrong, : ', err);
});
EDIT: In your model definition is best if you specify the fields your table has. Like so: http://docs.sequelizejs.com/manual/tutorial/models-definition.html
Otherwise you will have to specify the attributes key in every find operation.

#yBrodsky is right . Also you should make model like this.so that it can know the field to select when no attribute.
const User = sequalize.define('user',{},{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: 'user',
createdAt:'created_on',
updatedAt:'updated_on'
});
to this
var sequalize = require('../../config/db_config');
const User = sequalize.define('user',{
name: {type: Sequelize.STRING},
email: {type: Sequelize.STRING},
mobile: {type: Sequelize.INTEGER},
dob: {type: Sequelize.DATE}
address: {type: Sequelize.STRING},
image: {type: Sequelize.INTEGER},
is_active: {type: Sequelize.BOOLEAN }
},{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: 'user',
createdAt:'created_on',
updatedAt:'updated_on'
});
for dtatype see here
http://docs.sequelizejs.com/manual/tutorial/models-definition.html#data-types

Simply remove attributes from model , you will be able fetch all columns

Related

Sequelize associations not generating foreign key

Sequelize is not creating the foreign key automatically, and is throwing a "no column "userId" in "fieldset"" error. I try to provide all the information down below. Im completely stuck on where to go from here as my code is 100% correct. (Read below)
So i have a Product and User model. both before were working fine. I added some code to set up the relationship:
Product.belongsTo(User, { constraints: true, onUpdate: "CASCADE" });
User.hasMany(Product);
I also, when syncing the db, have used {force: true} and removed it after tables were refreshed. Ive tried restarting pc after these steps, restarting workbench, creating a new database and changing connection to connect to fresh one, still it doesnt put a "userId" column in my product schema.
Ive had this code checked by two people so far and they confirm my syntax is fine, and are equally baffled. Im also confident myself that its not incorrect because im following a reputable course and i've now had to copy and paste his code in replacement to mine just incase, which didnt work.
Product model:
const Sequelize = require("sequelize");
const sequelize = require("../util/database");
const Product = sequelize.define("product", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
title: Sequelize.STRING,
price: {
type: Sequelize.DOUBLE,
allowNull: false,
},
image_url: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
});
module.exports = Product;
User model:
const Sequelize = require("sequelize");
const sequelize = require("../util/database");
const User = sequelize.define("user", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
name: Sequelize.STRING,
email: Sequelize.STRING,
});
module.exports = User;
Syncing code (I create a test user as the course is at a stage where we are testing we can make one):
// db.sync({ force: true })
db.sync()
.then((result) => {
return User.findByPk(1);
// console.log(result);
})
.then((user) => {
if (!user) {
return User.create({ name: "Max", email: "test#test.com" });
}
return user;
})
.then((user) => {
app.listen(5000);
})
.catch((err) => {
console.log(err);
});
The connection is 100% connected as things happen to the tables in my database, just the "userId" column which sequelize should auto-generate from my associations doesnt come up.
Also have tried putting in a foreignKey: "userId" in my Product.belongsTo() line of code to try to implicitly set it. That didnt even work.
Therefore im stuck and cannot continue with my sql code.
Github repo if need further code:
https://github.com/NinjaInShade/online-shop
I tried your code with some modifications about associations and foreign keys and you have two ways to create a column userId and a foreign key:
Add a userId field definition to Product model with references option like this:
userId: {
allowNull: true,
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id'
}
}
Synchronize models individually using their sync method:
User.sync({ force: true })
.then(() => {
Product.sync({ force: true }).then(() => {
app.listen(5000);
})
})
Unfortunately the official documentation does not clarify why sync method in Sequelize acts differently in comparison with sync of separate models.
Usually I use migrations and that's why I don't have this issue.

Sequelize in Nodejs creating duplicate tables upon app start

Ok. Landscape: Node, MySql, Sequelize
Issue: After creating a new data model & migration (node migrate.js which creates just fine), upon app start Sequelize creates a duplicate Table (and also forwards form data to the new table).
Ex: db.virtual_class is the main table, and upon start, db.virtual_classes is also created.
My model:
const Sequelize = require('sequelize');
const sequelize = require('../sequelize');
const model = sequelize.define('virtual_class', {
id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
style: Sequelize.STRING, // e.g. Style of class
description: Sequelize.STRING(1024), // e.g. class Details
jwt_secret: Sequelize.STRING, // e.g. rando string to be used to gen unique keys for every room
});
module.exports = model;
I've isolated what I think is the issue - I'm including the model in a variable on my index controller for my functions.
const Virtual_class = require('./model');
const classQuery = require('./classQuery');
async function addClass({ style, description, secret }) {
const vClass = await Virtual_class.create({
style,
description,
jwt_secret: secret,
}, { raw: true });
return classQuery(vClass);
}
module.exports = {
addClass,
};
Class Query function to return the data in a usable object:
function classQuery(queryResult) {
if (!queryResult) {
return null;
}
return {
id: queryResult.id,
style: queryResult.style,
description: queryResult.description,
secret: queryResult.jwt_secret,
};
}
module.exports = classQuery;
and the migration:
module.exports = {
up: (sequelize, Sequelize) => sequelize.getQueryInterface().createTable('virtual_class', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
style: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.STRING,
},
jwt_secret: {
type: Sequelize.STRING,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('now'),
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('now'),
},
}),
down: sequelize => sequelize.getQueryInterface().dropTable('virtual_class'),
};
Net result is fine before I run app - DB shows new table, After running app - DB shows dup table.
I'm a relative noob, and been wracking my brain (and trying to find solutions here) to the problem. I've done this before with other migrations with no issue.
Any advice is appreciated! Thanks!
DOH! For those who are new like me - Sequelize automatically creates plural tables by default, You can force the override tp singular table names.

How to stop sequelize from sending default value to MYSQL table?

The console log:
Executing (default): INSERT INTO `testtables` (`id`,`forgot_code`,`customer_id`) VALUES (DEFAULT,610,199)
How can I stop sequelize from sending DEFAULT value into my column id ?
How can I stop sequelize from inserting into my Primary Key since it is already on auto increment ?
My code:
var TestTable= sequelize.define('testtables', {
id:{
type:Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
forgot_code:Sequelize.INTEGER,
customer_id:Sequelize.INTEGER
},{
timestamps: false,
});
A bit late reply, but I had similar issue with Percona. So the solution for us was adding a hook:
new Sequelize(database, username, password, {
dialect: 'mysql',
// FIXME: This is a temporary solution to avoid issues on Percona when Sequelize transform insert query into
// INSERT INTO [TABLE_NAME] (`id`, ...) VALUES (DEFAULT, ...)
hooks: {
beforeCreate: ((attributes) => {
if (attributes
&& attributes.dataValues
&& attributes.dataValues.hasOwnProperty('id')
) {
delete attributes.dataValues.id
}
})
},
})
Update: found this solution on DB level: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_auto_value_on_zero
1.delete all node modules
2.Re install node modules( npm install)
Now, issue will be fixed. This worked for me.
You have to remove autoIncrement: true from your model definition. Now, inserting without providing the id value will fail. For example, the below code will fail
const User = sequelize.define('user', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
// autoIncrement: true
},
username: Sequelize.STRING,
});
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test123'
}).then((user) => {
console.log(user);
}));
However if you uncomment autoIncrement: true, insert will go through

SQL JOIN with condition on second table with sequelizejs

I have the following models:
const User = sequelize.define('user', {
name: Sequelize.TEXT,
avatar: Sequelize.TEXT,
});
const Event = sequelize.define('event', {
title: Sequelize.STRING,
description: Sequelize.TEXT,
timestamp: Sequelize.DATE,
});
const EventUser = sequelize.define('event_user', {});
Event.hasMany(EventUser);
EventUser.belongsTo(User);
So EventUser holds my list of participants for a specific event.
I'm trying to query for all events where user X is participating:
let events = await Event.findAll({
include: [
{model: EventUser, include: [{model: User, where: {id: 1}}]}
],
});
But it won't fetch the list of other participants for that event. Not sure how to achieve that, thanks!
Right way is to define many-to-many relation between Users and Events
User.belongsToMany(Event, {through: 'EventUser'});
Event.belongsToMany(User, {through: 'EventUser'});
Then, you can get all events of some user like this:
User.findById(userId).then(function(user){
return user.getEvents();
}).then(function(events){
//here's events
});
Or through one query:
Event.findAll({
include: [{
model: User,
where: {id: userId}
}]
}).then(function(events){
//here's events
});

Sequelize Many To Many Relationship : Cannot read property 'split' of undefined

I'm trying to create many to many relationship using Sequelize + nodeJs using existing MySQL Database :
Below is my tables :
- "usr" Table : usr_id (PK)
- Intermediate "usr_role" : usr_id, role_id
- "role" table : role_id
This is my models
"User" Models:
"use strict";
var bcrypt = require('bcryptjs');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
usrid : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
field:'usr_id'
},
name :{
type: DataTypes.STRING,
allowNull: false,
field:'usr_name'
},
}, {
timestamps: false,
freezeTableName: true,
tableName: 'usr',
name:'User',
underscored:'true',
classMethods: {
associate:function(models){
User.belongsToMany(models.Role, { through: 'UserRole', foreignKey:'usr_id', as:'UserRoles'});
}
}
}
);
return User;
};
"Role" models
module.exports = function(sequelize, DataTypes) {
var Role = sequelize.define('Role', {
id : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
field:'role_id'
},
name :{
type: DataTypes.STRING,
allowNull: false,
field:'role_name'
},
},{
timestamps: false,
freezeTableName: true,
tableName: 'role_ref',
underscored:'true',
classMethods: {
associate:function(models){
Role.belongsToMany(models.User, {
through: 'UserRole',
foreignKey:'role_id'});
}
}
}
)
return Role;
};
E:\nodejsWS\learn\node_modules\inflection\lib\inflection.js:795
var str_path = str.split( '::' );
^
TypeError: Cannot read property 'split' of undefined
at Object.inflector.underscore
(E:\nodejsWS\learn\node_modules\inflection\lib\inflection.js:795:25)
at Object.module.exports.underscoredIf
(E:\nodejsWS\learn\node_modules\sequelize\lib\utils.js:28:27)
at new BelongsToMany (E:\nodejsWS\learn\node_modules\sequelize
\lib\associations\belongs-to-many.js:134:15)
at Mixin.belongsToMany
(E:\nodejsWS\learn\node_modules\sequelize\lib\associations
\mixin.js:264:21)
at sequelize.define.classMethods.associate
(E:\nodejsWS\learn\models\Role.js:29:12)
at E:\nodejsWS\learn\models\index.js:50:21
this is my index.js
// Import models
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
//associate models
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
console.log(db);
db[modelName].associate(db);
}
});
Line 50 refer to db[modelName].associate(db), that cause the error.
Can somebody please guide me what's i'm doing wrong. Do i need to manually create intermediate Models 'UserRole'. Your help is very much appreciated. Thanks.
You need to specify the actual intermediate table's name in the belongsToMany config, or define the model UserRole. Also, unless your intermediate table has the fields, you'll probably also want to specify timestamps: false as well. So, for example:
Role.belongsToMany(models.User, {
through: 'usr_role',
foreignKey:'role_id',
timestamps: false
});
This will cause sequelize to use that table in the queries it builds without needing a model defined, and will prevent it from requesting the timestamp fields from either the User table or the intermediate.