sequelize example with findAll and update

Jan 12, 05:35 AM

CREATE TABLE `sequelizeTable` ( `sequelizeName` varchar(50) DEFAULT NULL, `sequelizeAddress` varchar(50) DEFAULT NULL, `id` mediumint(9) NOT NULL AUTO_INCREMENT, `createdAt` date DEFAULT NULL, `updatedAt` date DEFAULT NULL, PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;


— Dumping data for table `sequelizeTable`

INSERT INTO `sequelizeTable` VALUES ( ‘lori edwards’ , ’948 south third street’ ,1 , ’2017-01-01’ , NULL )
, ( ‘mark edwards’ , ’123 swallow lane’ ,2 , ’2017-01-01’ , NULL )
, ( ‘marjorie edwards’ , ’7366 east ironwood court’ ,3 , ’2017-01-01’ , NULL )
;

const Sequelize = require(‘sequelize’);
const util = require(‘util’);

// mysql —host=localhost —user=jsonTestUser —password=jabberwolky exampleDb ;

var sequelize = new Sequelize(‘exampleDb’, ‘jsonTestUser’, ‘jabberwolky’, { host: ‘localhost’, dialect: ‘mariadb’,

pool: { max: 5, min: 0, idle: 10000 }

});

// create table sequelizeTable ( sequelizeName VARCHAR, sequelizeAddress VARCHAR, id mediumint not null auto_increment, primary key (id) ) ;

var sequelizeTable = sequelize.define(‘sequelizeTable’, { sequelizeName: { type: Sequelize.STRING, field: ‘sequelizeName’ // Will result in an attribute that is firstName when user facing but first_name in the database }, sequelizeAddress: { type: Sequelize.STRING }, id: { type: Sequelize.INTEGER, primaryKey: true }
}, { freezeTableName: true // Model tableName will be the same as the model name
});

sequelizeTable.findAll({ where: { id: 1, sequelizeName: ‘lori edwards’ } ,order: [ ‘id’ ] ,attributes: [‘sequelizeName’,‘sequelizeAddress’,‘id’] }).then( (returnedValue) => {

//console.log(JSON.stringify(returnedValue )); returnedValue.forEach ( © => { var seconds = new Date() / 1000; //console.dir(c.toJSON() ); console.dir(c.sequelizeName ); console.log(c.sequelizeAddress ); sequelizeTable.update( { sequelizeAddress : c.sequelizeAddress + ‘ ‘ + seconds } , { where : { id: c.id } } ) .then( (result) => { console.log(result) ; }) .catch( (err) => { console.log(‘err: ‘ + err) ; }) ; }) });
Mark Edwards

,

---

Commenting is closed for this article.

---