{"id":15741,"date":"2014-10-13T23:48:38","date_gmt":"2014-10-13T18:18:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15741"},"modified":"2016-12-19T15:33:43","modified_gmt":"2016-12-19T10:03:43","slug":"nodejs-with-mysql","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/nodejs-with-mysql\/","title":{"rendered":"NodeJs with MySql using Sequelize"},"content":{"rendered":"<p>This time I am going to explain a very basic <a title=\"CRUD\" href=\"http:\/\/en.wikipedia.org\/wiki\/Create,_read,_update_and_delete\" target=\"_blank\">CRUD<\/a> operation on <a title=\"MySql\" href=\"http:\/\/www.mysql.com\/\" target=\"_blank\">MySql<\/a> database via <a title=\"Nodejs development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">NodeJs<\/a> using Sequelize. Here Sequelize become&#8217;s a bridge between NodeJs and MySql database, it is a <a title=\"Object Relational Mapping\" href=\"http:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping\" target=\"_blank\">Object Relational Mapping Library<\/a>\u00a0written on <a title=\"Javascript Engineering\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">javascript<\/a>.<\/p>\n<p>Let us quickly learn the things with the help of Examples and we will use Item Table to perform all CRUD operation:-<\/p>\n<p><em>&#8212; Install Dependence first<\/em><\/p>\n<p>[js]<\/p>\n<p>npm install mysql<\/p>\n<p>npm install sequelize<\/p>\n<p>[\/js]<\/p>\n<p>&#8212; <em>Setting up the connection with mysql<\/em><\/p>\n<p>[js]<\/p>\n<p>\/\/Including dependency<br \/>\nvar Sequelize = require(&amp;quot;sequelize&amp;quot;);<\/p>\n<p>\/\/Setting up the config<br \/>\nvar sequelize = new Sequelize(&#8216;your-database-name&#8217;, &#8216;db-username&#8217;, &#8216;db-password&#8217;, {<br \/>\n    host: &amp;quot;localhost&amp;quot;,<br \/>\n    port: 3306,<br \/>\n    dialect: &#8216;mysql&#8217;<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p>&#8212;\u00a0Check connection status<\/p>\n<p>[js]<\/p>\n<p>\/\/Checking connection status<br \/>\nsequelize.authenticate().complete(function (err) {<br \/>\n if (err) {<br \/>\n    console.log(&#8216;There is connection in ERROR&#8217;);<br \/>\n } else {<br \/>\n    console.log(&#8216;Connection has been established successfully&#8217;);<br \/>\n }<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p><em><strong>Example-1\u00a0<\/strong><\/em><strong>Create<\/strong> Table: <strong>Item<\/strong> using Node.Js<\/p>\n<p>[js]<\/p>\n<p>\/\/Create Item Table Structure<br \/>\nvar Item = sequelize.define(&#8216;Item&#8217;, {<br \/>\n    id: Sequelize.STRING,<br \/>\n    name:Sequelize.STRING,<br \/>\n    description: Sequelize.STRING,<br \/>\n    qty: Sequelize.INTEGER<br \/>\n});<\/p>\n<p>\/\/Applying Item Table to database<br \/>\nsequelize.sync({force:true}).complete(function (err) {<br \/>\n if(err){<br \/>\n    console.log(&#8216;An error occur while creating table&#8217;);<br \/>\n }else{<br \/>\n    console.log(&#8216;Item table created successfully&#8217;);<br \/>\n }<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p><em><strong>Example-2 Inserting<\/strong> data into\u00a0<\/em>Table: <strong>Item<\/strong> using Node.Js<\/p>\n<p>[js]<\/p>\n<p>\/\/There is two way of inserting data into database<br \/>\n\/\/One way: Forming object from modal<br \/>\nvar item1 = Item.build({<br \/>\n    id: 1,<br \/>\n    name:&#8217;Laptop&#8217;,<br \/>\n    description: &#8216;Acer 2340TL&#8217;,<br \/>\n    qty: 23<br \/>\n});<br \/>\n\/\/Inserting Data into database<br \/>\nitem1.save().complete(function (err) {<br \/>\n if (err) {<br \/>\n    console.log(&#8216;Error in Inserting Record&#8217;);<br \/>\n } else {<br \/>\n    console.log(&#8216;Data successfully inserted&#8217;);<br \/>\n }<br \/>\n});<\/p>\n<p>\/\/Other way: Immediate insertion of data into database<br \/>\nsequelize.sync().success(function () {<br \/>\n  Item.create({<br \/>\n     id: 2,<br \/>\n     name:&#8217;Cell Phone&#8217;,<br \/>\n     description: &#8216;Sony&#8217;,<br \/>\n     qty: 20<br \/>\n  }).success(function (data) {<br \/>\n  console.log(data.values)<br \/>\n })<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p><em><strong>Example-3 Reading<\/strong>\u00a0data from\u00a0<\/em>Table: <strong>Item<\/strong> using Node.Js<\/p>\n<p>[js]<\/p>\n<p>\/\/Reading All Data<br \/>\nItem.find({}).complete(function (err,data) {<br \/>\n    console.log(data);<br \/>\n});<br \/>\n\/\/With where condition<br \/>\n    Item.find({where:{name:&#8217;Laptop&#8217;}}).complete(function (err, data) {<br \/>\n   console.log(data);<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p><em><strong>Example-4 Updating<\/strong>\u00a0data from\u00a0<\/em>Table: <strong>Item<\/strong> using Node.Js<\/p>\n<p>[js]<\/p>\n<p>\/\/Updating Laptop to Computer<br \/>\nItem.find({where:{name:&#8217;Laptop&#8217;}}).complete(function (err, data) {<br \/>\n  if(err){<br \/>\n    console.log(err);<br \/>\n  }<br \/>\n  if(data){<br \/>\n    data.updateAttributes({<br \/>\n    name:&#8217;Computer&#8217;<br \/>\n  }).success(function (data1) {<br \/>\n    console.log(data1);<br \/>\n  })<br \/>\n }<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p><em><strong>Example-5 Delete<\/strong>\u00a0data from\u00a0<\/em>Table: <strong>Item<\/strong> using Node.Js<\/p>\n<p>[js]<\/p>\n<p>\/\/Delete All Computer Records<br \/>\nItem.find({where: {name: &#8216;Computer&#8217;}}).complete(function (err, data) {<br \/>\n   if (err) {<br \/>\n      console.log(err);<br \/>\n   } else {<br \/>\n      data.destroy({}).success(function (err, data) {<br \/>\n   if(err){<br \/>\n      console.log(err);<br \/>\n   }else{<br \/>\n      console.log(data);<br \/>\n   }<br \/>\n  })<br \/>\n }<br \/>\n console.log(data);<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<ul>\n<li>You can find the above code on GITHUB\u00a0<a title=\"Source Code\" href=\"https:\/\/github.com\/kashishgupta1990\/SequelizeCRUDOperation\" target=\"_blank\">click me<\/a> to view\/download code and don&#8217;t forget to update config according to your MySQL database.<\/li>\n<li>There are many other ways to achieve similar CRUD operation. You can refer Sequelize\u00a0docs to explore more.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This time I am going to explain a very basic CRUD operation on MySql database via NodeJs using Sequelize. Here Sequelize become&#8217;s a bridge between NodeJs and MySql database, it is a Object Relational Mapping Library\u00a0written on javascript. Let us quickly learn the things with the help of Examples and we will use Item Table [&hellip;]<\/p>\n","protected":false},"author":135,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":12},"categories":[1185],"tags":[1534,76,1533,1532],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15741"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/135"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15741"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15741\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}