Create or Update Sequelize

From the docs, you don’t need to query where to perform the update once you have the object. Also, the use of promise should simplify callbacks: Implementation function upsert(values, condition) { return Model .findOne({ where: condition }) .then(function(obj) { // update if(obj) return obj.update(values); // insert return Model.create(values); }) } Usage upsert({ first_name: ‘Taku’ }, … Read more

How to Add, Delete new Columns in Sequelize CLI

If you are using sequelize-cli you need to create the migration first. This is just a file that tells the engine how to update the database and how to roll back the changes in case something goes wrong. You should always commit this file to your repository $ sequelize migration:create –name name_of_your_migration The migration file … Read more

Node.js 7 how to use sequelize transaction with async / await?

let transaction; try { // get transaction transaction = await sequelize.transaction(); // step 1 await Model.destroy({ where: {id}, transaction }); // step 2 await Model.create({}, { transaction }); // step 3 await Model.update({}, { where: { id }, transaction }); // commit await transaction.commit(); } catch (err) { // Rollback transaction only if the transaction … Read more

Specifying specific fields with Sequelize (NodeJS) instead of *

You have to specify the attributes as a property in the object that you pass to findAll(): Project.findAll({attributes: [‘name’, ‘age’]}).on(‘success’, function (projects) { console.log(projects); }); How I found this: The query is first called here: https://github.com/sdepold/sequelize/blob/master/lib/model-definition.js#L131 Then gets constructed here: https://github.com/sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js#L56-59

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)