<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raj Jeswal</title>
    <description>The latest articles on DEV Community by Raj Jeswal (@workforweb).</description>
    <link>https://dev.to/workforweb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F765778%2F45366394-30df-4018-b5b6-38c869ab8165.jpg</url>
      <title>DEV Community: Raj Jeswal</title>
      <link>https://dev.to/workforweb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/workforweb"/>
    <language>en</language>
    <item>
      <title>Expressjs: Javascript written in ECMAScript 2015 (ES6)</title>
      <dc:creator>Raj Jeswal</dc:creator>
      <pubDate>Sun, 19 Dec 2021 12:08:33 +0000</pubDate>
      <link>https://dev.to/workforweb/expressjs-javascript-written-in-ecmascript-2015-es6-15ip</link>
      <guid>https://dev.to/workforweb/expressjs-javascript-written-in-ecmascript-2015-es6-15ip</guid>
      <description>&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;p&gt;This tutorial assumes that you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node&lt;/li&gt;
&lt;li&gt;Node installed on your machine&lt;/li&gt;
&lt;li&gt;Any code or text editor of your choice &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initially, create Express application with command in terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx express-generator &amp;lt; your-project-name &amp;gt; --no-view
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, --no-view flag means that we will not use any template engine such as handlebars, ejs or pug, for our skeleton Express application. &lt;/p&gt;

&lt;p&gt;After creating your application, you need to go to your application directory. For Windows Powershell and Linux terminals, use:&lt;/p&gt;

&lt;p&gt;cd your-project-name&lt;/p&gt;

&lt;p&gt;Then open your desired text editor. For me I only use VSCode so I have my terminal and my text editor open at the same time. But you can use any text editor you want.&lt;/p&gt;

&lt;p&gt;Install dependencies with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have the generated project ready, we need to install the dependencies and move some folders. Run this command to install other packages.&lt;/p&gt;

&lt;p&gt;Here we can use three different kinds or methods to do our job with ease. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Method&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Type the following in terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install esm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and also install nodemon for specific project only as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev nodemon  or npm install -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nodemon automatically restarts your application and allows you to test new changes to your Express app faster, or install as a global NPM dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g nodemon  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable esm for packages: Use esm to load the main ES module and export it as CommonJS in the main entry file, index.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require = require("esm")(module);
module.exports = require("./app.js");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create a new file, app.js and add the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
import express from 'express';

var app = express();
var PORT = 3000;

app.listen(PORT, function(err){
    if (err) console.log("Error in server setup")
    console.log("Server listening on Port", PORT);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run the app with:&lt;/p&gt;

&lt;p&gt;npm start&lt;/p&gt;

&lt;p&gt;at this stage if you had any problems use the&lt;br&gt;
&lt;a href="https://codesandbox.io/s/workforweb-5hy3e" rel="noopener noreferrer"&gt;codesendbox link&lt;/a&gt; as a reference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second Method&lt;/strong&gt;: &lt;/p&gt;

&lt;p&gt;add the following in project's package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"type": "module"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and voila, but you need to add extensions with all files, which you want to add as import, like  (filename.js), as example: &lt;/p&gt;

&lt;p&gt;import userRoutes from './routes/userRoutes.js';&lt;/p&gt;

&lt;p&gt;at this stage if you had any problems use the &lt;br&gt;
&lt;a href="https://codesandbox.io/s/workforweb-1ecrh" rel="noopener noreferrer"&gt;codesendbox link&lt;/a&gt; as a reference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third Method&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;using Babel setup&lt;/p&gt;

&lt;p&gt;What is Babel?&lt;/p&gt;

&lt;p&gt;Babel is a JavaScript compiler, that helps you use the ES6 functions of the JavaScript programming language. &lt;/p&gt;

&lt;p&gt;add the following packages, we use the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node babel-plugin-module-resolver rimraf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D @babel/core @babel/cli @babel/preset-env @babel/node babel-plugin-module-resolver rimraf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after installing the packages, add the following in project's package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "build": "npm run clean &amp;amp;&amp;amp; babel ./src --out-dir dist --copy-files",
    "clean": "rimraf dist",
    "prod": "npm run -s build",
    "start": "node dist/app.js",
    "dev": "nodemon --exec babel-node src/app.js"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So once you are done with the installation, create a new file called .babelrc to configure babel. Add the following text in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": ["@babel/preset-env"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "@routes": "./src/routes"
        }
      }
    ]
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  conclusion
&lt;/h3&gt;

&lt;p&gt;We learned how to use ES6 in our Node.js project to write the latest JavaScript functions in the best way.&lt;/p&gt;

&lt;p&gt;Feel free to check the code on &lt;a href="https://github.com/workforweb/express-babel-starter.git" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt; if you had any trouble following the last part of this article, when we included Babel as a JavaScript compiler. &lt;/p&gt;

&lt;p&gt;If you have any questions or comments about this article, please do not hesitate to reach out.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Express.js&lt;/strong&gt;, the minimal and flexible Node.js web application framework: &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;https://expressjs.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Esm&lt;/strong&gt;, Tomorrow's ECMAScript modules today!: &lt;a href="https://github.com/standard-things/esm#readme" rel="noopener noreferrer"&gt;https://github.com/standard-things/esm#readme&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nodemon&lt;/strong&gt;, reload, automatically: &lt;a href="https://nodemon.io/" rel="noopener noreferrer"&gt;https://nodemon.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Babel&lt;/strong&gt;, is a JavaScript compiler: &lt;a href="https://babeljs.io/docs/en/usage" rel="noopener noreferrer"&gt;https://babeljs.io/docs/en/usage&lt;/a&gt;&lt;/p&gt;

</description>
      <category>express</category>
      <category>esm</category>
      <category>babel</category>
      <category>es6</category>
    </item>
    <item>
      <title>How to use MongoDB in Node.js application</title>
      <dc:creator>Raj Jeswal</dc:creator>
      <pubDate>Sun, 19 Dec 2021 10:56:49 +0000</pubDate>
      <link>https://dev.to/workforweb/how-to-use-mongodb-in-nodejs-application-24a5</link>
      <guid>https://dev.to/workforweb/how-to-use-mongodb-in-nodejs-application-24a5</guid>
      <description>&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;This tutorial assumes that you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node&lt;/li&gt;
&lt;li&gt;Node installed on your machine&lt;/li&gt;
&lt;li&gt;Any code or text editor of your choice &lt;/li&gt;
&lt;li&gt;MongoDB installed locally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MongoDB is an open-source non-relational document database and leading NoSQL database that provides support for JSON-like storage.&lt;/p&gt;

&lt;p&gt;Basic terms also change in SQL and NoSQL based databases to map data, since in Sql based database you have tables but nosql database has collections, SQL based database has rows but nosql database has documents, SQL based database has columns but nosql database has fields, Sql based database has relationships, but nosql database has linked and embedded documents. &lt;/p&gt;

&lt;p&gt;Connecting MongoDB with your application using Mongoose, Mongoose is an object document mapper (ODM) used to establish a connection to the MongoDB database in NodeJS. &lt;/p&gt;

&lt;p&gt;install mongoose on your project via this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before launching your server, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect('mongodb://localhost:27017/&amp;lt; DB Name &amp;gt;', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function () {
  console.log('Connected to Mongoose');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;module.exports = db;&lt;/p&gt;

&lt;p&gt;When you restart your Node.js server, you should see in your terminal "Connected to MongoDB database" if your connection is well established. Otherwise, a message containing the error if the connection could not be established. &lt;/p&gt;

&lt;h3&gt;
  
  
  Running Queries With Mongoose
&lt;/h3&gt;

&lt;p&gt;Mongoose requires you to define its schemas before manipulating its objects. Let's start by creating the first schema in a user.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Schema, model } = require('mongoose');

const userSchema = new Schema({
    firstName:  String,
    lastName: String,
    email:   String,
  });

const User = model('User', userSchema);

module.exports = User;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file will contain the User module, which is a mongoose schema. We have the properties that a user can contain. Once this schema is defined, you can now use the mongoose schema methods to perform our create, read, update, or delete operations (CRUD functionality). &lt;/p&gt;

&lt;p&gt;const User = require('./user.js');&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Create a user&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/users', async (req, res) =&amp;gt; {
  try {
    let { firstname, lastname, email } = req.body;

    const user = await new User({ firstname, lastname, email });

    const result = await user.save();

    return res.status(201).json({ status: true, data: result });
  } catch (error) {
    res.status(500).json({ status: false, errors: error });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;List all users&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/users', async (req, res) =&amp;gt; {
  try {
    const user = await User.find();
    return res.status(200).json({ status: true, data: user });
  } catch (error) {
    res.status(500).json({ status: false, errors: error });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Pick a user&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/users/:id', async (req, res) =&amp;gt; {
  try {
    const user = await User.findById(req.params.id);
    return res.status(200).json({ status: true, data: user });
  } catch (error) {
    res.status(500).json({ status: false, errors: error });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Update a user&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.put('/users/:id', async (req, res) =&amp;gt; {
  try {
    let { firstname, lastname, email } = req.body;

    const user = await User.findOne({ _id: req.params.id });

    if (!user) return res.status(404).json({ status: false, error: 'No User' });

    user.firstname = firstname ? firstname : user.firstname;
    user.lastname = lastname ? lastname : user.lastname;
    user.email = email ? email : user.email;

    const updatedUser = await user.save();

    return res.status(200).json({ status: true, data: updatedUser });
  } catch (error) {
    res.status(500).json({ status: false, errors: error });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Delete a user&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.delete('/users/:id', async (req, res) =&amp;gt; {
  try {
    const user = await User.findOne({ _id: req.params.id });

    if (!user) return res.status(404).json({ status: false, error: 'No User' });

    await user.remove();
    return res
      .status(200)
      .json({ status: true, msg: 'User deleted successfully!' });
  } catch (error) {
    console.log(error);
    res.status(500).json({ status: false, errors: error });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  conclusion
&lt;/h3&gt;

&lt;p&gt;We learned how to use Mongoose ODM to connect to the Mongodb database in our Node.js project. &lt;/p&gt;

&lt;p&gt;Feel free to check the code on &lt;a href="https://github.com/workforweb/Express-Mongodb-Starter.git" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt; if you had any trouble following this tutorial. &lt;/p&gt;

&lt;p&gt;If you have any questions or comments about this article, please do not hesitate to reach out.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;MongoDB, Built by developers, for developers: &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;https://www.mongodb.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mongoose, is a MongoDB object modeling tool: &lt;a href="https://mongoosejs.com/" rel="noopener noreferrer"&gt;https://mongoosejs.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>mongodb</category>
      <category>mongoose</category>
    </item>
    <item>
      <title>Setup and Customize Bootstrap styles in Next.js</title>
      <dc:creator>Raj Jeswal</dc:creator>
      <pubDate>Sun, 19 Dec 2021 09:25:25 +0000</pubDate>
      <link>https://dev.to/workforweb/setup-and-customize-bootstrap-styles-in-nextjs-4jfj</link>
      <guid>https://dev.to/workforweb/setup-and-customize-bootstrap-styles-in-nextjs-4jfj</guid>
      <description>&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;This tutorial assumes that you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node.js&lt;/li&gt;
&lt;li&gt;Node 12.22.0 or later installed on your machine, &lt;/li&gt;
&lt;li&gt;Any code or text editor of your choice &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To create a new NextJS project, type and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest &amp;lt; Your APP's NAME &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Steps to follow:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Installing Bootstrap&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's start by installing the required NPM packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install bootstrap react-bootstrap sass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sass Support&lt;/strong&gt;, for more information &lt;a href="https://nextjs.org/docs/basic-features/built-in-css-support" rel="noopener noreferrer"&gt;check this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next.js allows you to import Sass using the extensions .scss and .sass. You can use Sass at the component level through CSS modules and the extension .module.scss or .module.sass.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Creating a Custom SCSS&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let us now create a custom scss file in the styles/scss directory, and name it &amp;lt; ANY-NAME-OF-YOUR-CHOICE &amp;gt;.scss. In this file, we need to import Bootstrap’s bootstrap.scss. For the sake of simplicity, let us override the default colour system provided by Bootstrap.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$primary-color: #64FF00;

@import '/node_modules/bootstrap/scss/bootstrap.scss';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Configuring Next Config&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The best part about the newer versions of Next is that they provide built-in support for SASS / SCSS. All we need to tell Next is where our styles are stored by setting next.config.js and adding the following code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path')

module.exports = {
  sassOptions: {
    includePaths: [path.join(__dirname, 'styles')],
  },
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Importing Bootstrap&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The final step is to import our custom Bootstrap into our project. Based on where we need to use the custom styles, we can import our global.scss. In this example, let us configure it to be used by the entire project.&lt;/p&gt;

&lt;p&gt;In pages/_app.js file, we need to add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'styles/scss/&amp;lt; ANY-NAME-OF-YOUR-CHOICE &amp;gt;.scss'

function MyApp({ Component, pageProps }) {
  return &amp;lt;Component {...pageProps} /&amp;gt;
}

export default MyApp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  conclusion
&lt;/h3&gt;

&lt;p&gt;We learned, how to configured Bootstrap in our next NextJS project.&lt;/p&gt;

&lt;p&gt;Feel free to check the code on &lt;a href="https://github.com/workforweb/NextJS-Bootstrap-Project.git" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;, if you had any trouble following this tutorial.&lt;/p&gt;

&lt;p&gt;If you have any questions or comments about this article, please do not hesitate to reach out.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;Next.js, The React Framework for Production: &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;https://nextjs.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bootstrap, the world’s most popular framework : &lt;a href="https://getbootstrap.com/" rel="noopener noreferrer"&gt;https://getbootstrap.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React-Bootstrap, Bootstrap framework for React.js : &lt;a href="https://react-bootstrap.github.io/" rel="noopener noreferrer"&gt;https://react-bootstrap.github.io/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>bootstrap</category>
      <category>reactbootstrap</category>
    </item>
    <item>
      <title>How to use Sequelize ORM in Node Js App</title>
      <dc:creator>Raj Jeswal</dc:creator>
      <pubDate>Sun, 19 Dec 2021 08:30:52 +0000</pubDate>
      <link>https://dev.to/workforweb/how-to-use-sequelize-orm-in-node-js-app-3amf</link>
      <guid>https://dev.to/workforweb/how-to-use-sequelize-orm-in-node-js-app-3amf</guid>
      <description>&lt;p&gt;Sequelize, the very popular and stable promise-based Node.js ORM that has GitHub 25.36k stars, is a mapping tool, or object relational mapper, for sql databases such as Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server to convert SQL statements to javascript objects. It has strong support for transactions, relationships, lazy and anxious loading, read replication, and more. &lt;/p&gt;

&lt;p&gt;Current version is Sequelize v6 supports Node v10 and above.&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;p&gt;This tutorial assumes that you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node.js&lt;/li&gt;
&lt;li&gt;Node installed on your machine&lt;/li&gt;
&lt;li&gt;Any code or text editor of your choice &lt;/li&gt;
&lt;li&gt;Atleast one database like mysql, mariadb,                postgresql or sqlite3 installed locally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sequelize is available via npm. You have to install sequelize as a dependency in your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save sequelize 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll also have to install One of the following driver manually for your database of choice:&lt;/p&gt;

&lt;p&gt;npm install --save pg pg-hstore (for Postgres)&lt;br&gt;
npm install --save mysql2 (for mysql)&lt;br&gt;
npm install --save mariadb (for mariadb)&lt;br&gt;
npm install --save sqlite3 (for sqlite3)&lt;br&gt;
npm install --save tedious (for Microsoft SQL Server)&lt;/p&gt;
&lt;h3&gt;
  
  
  Connecting to a database
&lt;/h3&gt;

&lt;p&gt;To connect to the database, you must create a Sequelize instance.&lt;/p&gt;

&lt;p&gt;for this, create a new file like sequelize-connection.js.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;sequelize-connection.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Sequelize } = require('sequelize');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Connecting to a database
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Passing parameters separately (other dialects)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mysql' || 'mariadb' || 'postgres' || 'mssql'
});

module.exports = sequelize;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing the connection
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { sequelize } = require('./models');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the .authenticate() function to test if the connection is OK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
    await sequelize.authenticate();
    // await sequelize.sync({ force: true, logging: console.log });
    console.log(`Server started on http://localhost:${port}`);
    console.log('Database connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Closing the connection
&lt;/h3&gt;

&lt;p&gt;Sequelize will keep the connection open by default, and use the same connection for all queries. If you need to close the connection, call sequelize.close() (which is asynchronous and returns a Promise).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create a model&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { DataTypes } = require('sequelize');
const sequelize = require('./index');

const User = sequelize.define('users', {
  firstname: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notNull: { msg: 'User must have a firstname' },
      notEmpty: { msg: 'firstname must not be empty' },
    },
  },
  lastname: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notNull: { msg: 'User must have a lastname' },
      notEmpty: { msg: 'lastname must not be empty' },
    },
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notNull: { msg: 'User must have a email' },
      notEmpty: { msg: 'Email must not be empty' },
      isEmail: { msg: 'Must be a valid email' },
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all set to write a simple Node.js / Express.js application that uses Sequelize to perform CRUD operations and preserve the data in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add new User&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/users', async (req, res) =&amp;gt; {
  try {
    let { firstname, lastname, email } = req.body;

    firstname = firstname.toLowerCase().trim();
    lastname = lastname.toLowerCase().trim();
    email = email.toLowerCase().trim();

    const user = await User.create({ firstname, lastname, email });

    return res.status(201).json({ status: true, data: user });
  } catch (error) {
    res.status(500).json({
      status: false,
      errors: Object.values(error.errors).map((el) =&amp;gt; el.message),
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;List all Users&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/users', async (req, res) =&amp;gt; {
  try {
    const user = await User.findAll();
    return res.status(200).json({ status: true, data: user });
  } catch (error) {
    res.status(500).json({
      status: false,
      errors: Object.values(error.errors).map((el) =&amp;gt; el.message),
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Search a single User&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/users/:id', async (req, res) =&amp;gt; {
  try {
    const user = await User.findAll({ where: { id: req.params.id } });
    return res.status(200).json({ status: true, data: user });
  } catch (error) {
    res.status(500).json({
      status: false,
      errors: Object.values(error.errors).map((el) =&amp;gt; el.message),
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Update an User&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.put('/users/:id', async (req, res) =&amp;gt; {
  try {
    let { firstname, lastname, email } = req.body;

    firstname = firstname.toLowerCase().trim();
    lastname = lastname.toLowerCase().trim();
    email = email.toLowerCase().trim();

    const id = parseInt(req.params.id, 10);
    if (Number.isNaN(id)) return res.status(400).end();

    const isUserExist = await User.findOne({ where: { id } });

    if (!isUserExist)
      return res.status(404).json({ status: false, error: 'No User' });

    const user = await User.findByPk(id);

    user.firstname = firstname ? firstname : user.firstname;
    user.lastname = lastname ? lastname : user.lastname;
    user.email = email ? email : user.email;

    const updatedUser = await user.save();

    return res.status(200).json({ status: true, data: updatedUser });
  } catch (error) {
    res.status(500).json({
      status: false,
      errors: error,
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete an User&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.delete('/users/:id', async (req, res) =&amp;gt; {
  try {
    const id = parseInt(req.params.id, 10);
    if (Number.isNaN(id)) return res.status(400).end();

    const isUserExist = await User.findOne({ where: { id } });

    if (!isUserExist)
      return res.status(404).json({ status: false, error: 'No User' });

    const user = await User.findByPk(id);

    await user.destroy();
    return res
      .status(200)
      .json({ status: true, msg: 'User deleted successfully!' });
  } catch (error) {
    console.log(error);
    res.status(500).json({
      status: false,
      errors: error,
    });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We learned, how to configured Sequelize.js in our next Node.js project.&lt;/p&gt;

&lt;p&gt;Feel free to check the code on &lt;em&gt;&lt;a href="https://github.com/workforweb/Express-Sequelize-Starter.git" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;&lt;/em&gt;, if you had any trouble following this tutorial.&lt;/p&gt;

&lt;p&gt;If you have any questions or comments about this article, please do not hesitate to reach out.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Node.js&lt;/em&gt;, is a JavaScript runtime built on Chrome's V8 JavaScript engine: &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;https://nodejs.org/en/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Express.js&lt;/em&gt;, Fast, unopinionated, minimalist web framework for Node.js: &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;https://expressjs.com/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sequelize&lt;/em&gt;, promise-based Node.js ORM: &lt;a href="https://sequelize.org/" rel="noopener noreferrer"&gt;https://sequelize.org/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>sequelize</category>
    </item>
  </channel>
</rss>
