<?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: sujon554</title>
    <description>The latest articles on DEV Community by sujon554 (@sujon554).</description>
    <link>https://dev.to/sujon554</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%2F811892%2Fca4a6e8c-7b49-4c32-8b6a-f4741050f446.jpeg</url>
      <title>DEV Community: sujon554</title>
      <link>https://dev.to/sujon554</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sujon554"/>
    <language>en</language>
    <item>
      <title>Setup MongoDB in Node.js with Mongoose</title>
      <dc:creator>sujon554</dc:creator>
      <pubDate>Fri, 15 Apr 2022 06:42:38 +0000</pubDate>
      <link>https://dev.to/sujon554/setup-mongodb-in-nodejs-with-mongoose-4229</link>
      <guid>https://dev.to/sujon554/setup-mongodb-in-nodejs-with-mongoose-4229</guid>
      <description>&lt;h2&gt;
  
  
  Setting up database connection
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;First let import the dependency;&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 mongoose = require("mongoose");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let’s store the path of the database in a variable. The path should look like the following, with and being replaced with a user you have created for the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dbPath = "mongodb://&amp;lt;dbuser&amp;gt;:&amp;lt;dbpassword&amp;gt;@ds250607.mlab.com:38485/test-db";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;connect to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect(dbPath, {
    useNewUrlParser: true,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = mongoose;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the application is started, it would be better if there was an indicator showing whether the application successfully connected to the database or not. So let’s add some more code to fix that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = mongoose.connection;
db.on("error", () =&amp;gt; {
    console.log("&amp;gt; error occurred from the database");
});
db.once("open", () =&amp;gt; {
    console.log("&amp;gt; successfully opened the database");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the end the database.js should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// database.js
const mongoose = require("mongoose");
const dbPath = "mongodb://&amp;lt;dbuser&amp;gt;:&amp;lt;dbpassword&amp;gt;@ds250607.mlab.com:38485/test-db";
mongoose.connect(dbPath, {
    useNewUrlParser: true,
});
const db = mongoose.connection;
db.on("error", () =&amp;gt; {
    console.log("&amp;gt; error occurred from the database");
});
db.once("open", () =&amp;gt; {
    console.log("&amp;gt; successfully opened the database");
});
module.exports = mongoose;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting up models/schema
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// models/userModel.js
const mongoose = require("../database");
const schema = {
    name: { type: mongoose.SchemaTypes.String, required: true },
    email: { type: mongoose.SchemaTypes.String, required: true },
    password: { 
        type: mongoose.SchemaTypes.String, 
        required: true, 
        select: false
    }
};
const collectionName = "user"; // Name of the collection of documents
const userSchema = mongoose.Schema(schema);
const User = mongoose.model(collectionName, userSchema);
module.exports = User;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create user
User.create({
    name: name,
    email: email,
    password: password
});
// Find user by email
User.findOne({
    email: email
});
// Find user by email with the password field included
User.findOne({
    email: email
}).select("+password");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>CURD Operations MongoDB with Node</title>
      <dc:creator>sujon554</dc:creator>
      <pubDate>Wed, 09 Feb 2022 06:36:00 +0000</pubDate>
      <link>https://dev.to/sujon554/curd-operations-mongodb-with-node-523f</link>
      <guid>https://dev.to/sujon554/curd-operations-mongodb-with-node-523f</guid>
      <description>&lt;h2&gt;
  
  
  What is the CURD operations?
&lt;/h2&gt;

&lt;p&gt;When we create a project with React as a client site and with NodeJs as a server site, we have to process some operations on the server site with NodeJs. CURD is an acronym that stands for Create, Update, Read, and Delete. According to our needs, We use to get, post, put, delete methods.&lt;/p&gt;

&lt;p&gt;_Important Note:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;NodeJs is a separate or another runtime of JavaScript.&lt;/li&gt;
&lt;li&gt;NASA, Uber, IBM, Paypal use NodeJs.&lt;/li&gt;
&lt;li&gt;The V8 engine makes JavaScript fast Just in time (JIT) compile.&lt;/li&gt;
&lt;li&gt;npm stands for Node Package Manager.&lt;/li&gt;
&lt;li&gt;MongoDB keeps the data in JSON structure.
_&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-blocking:&lt;/strong&gt; Single-threaded accepts the request and then sends the request to others. The current thread won’t remain blocked with the request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous/ Call back:&lt;/strong&gt; NodeJs does not work synchronously or one by one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  get any data from mongodb with node.js
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Use a database collection by
const productcollection = database.collection('product');
Then, use app.get() function by below system
app.get('/product', async (req, res) =&amp;gt; {
const getdata = productcollection.find({});
const showdata = await getdata.toArray();
res.send(showdata);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  get any single data from mongodb with node.js
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Use a database collection by
const productcollection = database.collection('product');
Then, use app.get() function by below system,
app.get('/product/:id', async (req, res) =&amp;gt; {
const id = req.params.id;
const getId = { _id: ObjectId(id) };
const showId = await productcollection.findOne(getId);
res.json(showId);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Post any data to mongodb with node.js
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Use a database collection by
const productcollection = database.collection('product');
Then, use app.post() function by below system

app.post('/product', async (req, res) =&amp;gt; {
const add = req.body;
const result = await productcollection.insertOne(add);
console.log(result);
res.json(result);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  delete any data from mongodb with node.js
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Use a database collection by
const productcollection = database.collection('product');
Then, use app.delete() function by below system

app.delete('/product/:id', async(req, res)=&amp;gt;{
const id = req.params.id;
const getId = {_id: ObjectId(id)};
const deleteId = await productcollection.deleteOne(getId);
res.json(deleteId);
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>mongodb</category>
      <category>mongoe</category>
      <category>curd</category>
    </item>
    <item>
      <title>Prop Types</title>
      <dc:creator>sujon554</dc:creator>
      <pubDate>Wed, 09 Feb 2022 05:23:54 +0000</pubDate>
      <link>https://dev.to/sujon554/prop-types-388m</link>
      <guid>https://dev.to/sujon554/prop-types-388m</guid>
      <description>&lt;p&gt;Before the release of React 15.5.0 version prop-Types is available in the react package but in later versions of React have to add a dependency in your project. You can add the dependency in your project by using the command given below:&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 prop-types --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import Elements&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import PropTypes from 'prop-types'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SO we get a something Different type of Prop-Types below: &lt;/p&gt;

&lt;h2&gt;
  
  
  Basic types
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;PropTypes.any: The prop can be of any data type&lt;br&gt;
PropTypes.bool: The prop should be a Boolean&lt;br&gt;
PropTypes.number: The prop should be a number&lt;br&gt;
PropTypes.string: The prop should be a string&lt;br&gt;
PropTypes.func: The prop should be a function&lt;br&gt;
PropTypes.array: The prop should be an array&lt;br&gt;
PropTypes.object: The prop should be an object&lt;br&gt;
PropTypes.symbol: The prop should be a symbol&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and Here we can example&lt;br&gt;
&lt;code&gt;Component.propTypes = {&lt;br&gt;
  anyProp: PropTypes.any,&lt;br&gt;
  booleanProp: PropTypes.bool,&lt;br&gt;
  numberProp: PropTypes.number,&lt;br&gt;
  stringProp: PropTypes.string,&lt;br&gt;
  functionProp: PropTypes.func&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Renderable types
&lt;/h2&gt;

&lt;p&gt;PropTypes also exports the following validators for ensuring that the value passed to a prop can be rendered by React.&lt;br&gt;
&lt;code&gt;Component.propTypes = {&lt;br&gt;
  nodeProp: PropTypes.node,&lt;br&gt;
  elementProp: PropTypes.element&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Instance types
&lt;/h2&gt;

&lt;p&gt;In cases where you require a prop to be an instance of a particular JavaScript class, you can use the PropTypes.instanceOf validator. This leverages the underlying JavaScript instanceof operator.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Component.propTypes = {&lt;br&gt;
  personProp: PropTypes.instanceOf(Person)&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple types
&lt;/h2&gt;

&lt;p&gt;PropTypes also exports validators that can allow a limited set of values or multiple sets of data types for a prop. Here they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PropTypes.oneOf: The prop is limited to a specified set of values, treating it like an enum&lt;/li&gt;
&lt;li&gt;PropTypes.oneOfType: The prop should be one of a specified set of types, behaving like a union of types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Component.propTypes = {&lt;br&gt;
  enumProp: PropTypes.oneOf([true, false, 0, 'Unknown']),&lt;br&gt;
  unionProp: PropTypes.oneOfType([&lt;br&gt;
    PropType.bool,&lt;br&gt;
    PropType.number,&lt;br&gt;
    PropType.string,&lt;br&gt;
    PropType.instanceOf(Person)&lt;br&gt;
  ]) &lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;`*&lt;em&gt;and here also *&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Collection types
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Required types
&lt;/h2&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>reactnative</category>
      <category>redux</category>
    </item>
  </channel>
</rss>
