<?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: Emeka Okwor</title>
    <description>The latest articles on DEV Community by Emeka Okwor (@chubbystrings).</description>
    <link>https://dev.to/chubbystrings</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%2F532054%2Fa9bbd17f-073a-4976-b7e5-ac8faf80939f.jpg</url>
      <title>DEV Community: Emeka Okwor</title>
      <link>https://dev.to/chubbystrings</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chubbystrings"/>
    <language>en</language>
    <item>
      <title>Dynamic Model Referencing with Mongoose </title>
      <dc:creator>Emeka Okwor</dc:creator>
      <pubDate>Wed, 16 Jun 2021 23:35:53 +0000</pubDate>
      <link>https://dev.to/chubbystrings/dynamic-model-referencing-with-mongoose-41jf</link>
      <guid>https://dev.to/chubbystrings/dynamic-model-referencing-with-mongoose-41jf</guid>
      <description>&lt;p&gt;So, I had a mongoose schema that referenced multiple collections. I wanted a situation where I could populate the schema from these collections depending on the property value in the document. Did I confuse you? Oh, my bad, I'd give you a logical example.&lt;br&gt;
Say we have a comment schema, and a user can comment on a blog post and a product. How can we populate the schema with the blog post comments or product comments programmatically? Well, now you see what I mean, right?.&lt;br&gt;
It appears there is something called refPath. The refPath option is a more sophisticated alternative to ref. If ref is just a string, mongoose will always query the same model to find the populated sub documents. With refPath, you can configure what model Mongoose uses for each doc. It automatically instructs mongoose to look into an 'onModel' property to find the correct model to populate. For example, let's say we have a comment schema like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const commentSchema = new Schema({
  body: { type: String, required: true },
  on: {
    type: Schema.Types.ObjectId,
    required: true,
    refPath: 'onModel'
  },
  onModel: {
    type: String,
    required: true,
    enum: ['BlogPost', 'Product']
  }
});

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

&lt;/div&gt;



&lt;p&gt;Notice the property 'onModel'?. It defines the name of our models "BlogPost" and "Product" referenced in the schema like so&lt;code&gt;refpath:'onModel'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, Let's create our models and add some data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// create models
const Product = mongoose.model('Product', new Schema({ name: String }));

const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));

const Comment = mongoose.model('Comment', commentSchema);

// add data to collections
const book = await Product.create({ name: 'The Count of Monte Cristo' });

const post = await BlogPost.create({ title: 'Top 10 French Novels' });

const commentOnBook = await Comment.create({
  body: 'Great read',
  on: book._id,
  onModel: 'Product'
});

const commentOnPost = await Comment.create({
  body: 'Very informative',
  on: post._id,
  onModel: 'BlogPost'
});


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

&lt;/div&gt;



&lt;p&gt;We can query the comment schema, populate it with the data of a particular model we specified like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const comments = await Comment.find().populate({ path: "BlogPost"})

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

&lt;/div&gt;



&lt;p&gt;when you log comments, you should see something 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;// [ { _id: ....., on: { title: 'Top 10 French Novels' } }]

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

&lt;/div&gt;



&lt;p&gt;Cool, init? I hope it helps someone out there. Happy coding.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Convert Express-Generator App To Typescript</title>
      <dc:creator>Emeka Okwor</dc:creator>
      <pubDate>Mon, 14 Jun 2021 18:44:00 +0000</pubDate>
      <link>https://dev.to/chubbystrings/convert-express-generator-app-to-typescript-29md</link>
      <guid>https://dev.to/chubbystrings/convert-express-generator-app-to-typescript-29md</guid>
      <description>&lt;p&gt;Hi guys,&lt;/p&gt;

&lt;p&gt;In this article, I'll be showing you how to convert an express-generator app to Typescript. It's pretty straightforward and we should be done in a flash. &lt;/p&gt;

&lt;p&gt;This is my first ever write-up 😩, I do hope you find a place in your heart to pardon any errors.😁 😁   &lt;/p&gt;


&lt;p&gt;I started learning Typescript recently, and honestly, my life has been a lot easier ever since. I’ll be sharing my knowledge with you all once in a while. &lt;/p&gt;

&lt;p&gt;Enough said, let's jump right in and start coding, shall we? 😎 &lt;/p&gt;

&lt;h3&gt;
  
  
  From an Express-Generator App to Typescript
&lt;/h3&gt;

&lt;p&gt;Let’s head to the terminal. &lt;/p&gt;

&lt;p&gt;First, you need to navigate to the directory where you'd like to create your express app, type in &lt;code&gt;npx express-generator&lt;/code&gt;, and hit the enter button. &lt;/p&gt;

&lt;p&gt;You should see a structure like this automatically generated for you&lt;/p&gt;

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

├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── Javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug


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

&lt;/div&gt;

&lt;p&gt;Next, run &lt;code&gt;yarn&lt;/code&gt; in your terminal to install all dependencies for this project. If everything goes fine, run &lt;code&gt;yarn start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your express server should be up and running, cool. 😎 &lt;/p&gt;


&lt;p&gt; Now, it's time for typescript, but before you start writing typescript codes, you’ll need to install typescript and all the type definitions for the project dependencies. &lt;/p&gt;

&lt;p&gt;So, just head to your terminal and type the command below;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add --dev typescript @types/cookie-parser @types/http-errors @types/morgan @types/node ts-node&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Next, you’ll be creating a configuration file for typescript. Thus, run &lt;code&gt;yarn tsc --init&lt;/code&gt;; this will create a tsconfig file. &lt;/p&gt;

&lt;p&gt;You can then navigate to your root directory and create an src folder. Move the routes folder and app.js into the newly created src folder. &lt;/p&gt;

&lt;p&gt;Your new folder structure should look like this;&lt;/p&gt;

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

├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── Javascripts
│   └── stylesheets
│       └── style.css
├── src
│   ├── app.js
│   ├── routes
│     ├── index.js
│     └── users.js
├── tsconfig.json
├── views
    ├── error.pug
    ├── index.pug
    └── layout.pug


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

&lt;/div&gt;

&lt;p&gt;Let's do some configuration in your "tsconfig.json" file. &lt;/p&gt;

&lt;p&gt;Here, we want to give typescript some instructions on what to do with the typescript files. So, open the tsconfig file and change the target value to "ES2020"; uncomment the outDir field, change its value to "dist". Do that for rootDir as well and change its value to "src". &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8qsd1ofxan9lr0jhhop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8qsd1ofxan9lr0jhhop.png" alt="tsconfig.ts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lest I forget, you also need to add an include field after "compilerOptions", its value should be an array with a string in it like this;&lt;code&gt;"include": ["src/"]&lt;/code&gt;. &lt;/p&gt;

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

 {
    "compilerOptions": {
       ....
    },
   "include": ["src/"]
}


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

&lt;/div&gt;

&lt;p&gt;You can also check out this article to help with that: &lt;a href="https://javascript.plainenglish.io/typescript-configuration-options-tsconfig-json-561d4a2ad4b" rel="noopener noreferrer"&gt;tsconfig.json&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, I believe we are done with the configurations, so, let's start coding. &lt;/p&gt;

&lt;p&gt;Step 1: Head to the src folder and rename all .js files to .ts and open the "app.ts" file. Change all the commonjs "require" imports to ES modules "import" like so &lt;code&gt;import express from 'express'&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Once done, you’ll need to add the types to the parameters on the error handler function, since you have installed the type definitions. You can import them from their libraries.&lt;/p&gt;

&lt;p&gt;You'll need to import Request, Response, and NextFunction from express and HttpError from http-errors as named imports. &lt;/p&gt;

&lt;p&gt;Make sure you change the export statement at the bottom to ES modules &lt;code&gt;export default&lt;/code&gt;. Hmm, yeah one more thing, don’t forget to change all "vars" to "const".  &lt;/p&gt;

&lt;p&gt;Your app.ts should look like so;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkab1q597enp0zrlfyun0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkab1q597enp0zrlfyun0.png" alt="app.ts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re all done with the "app.ts", let's now convert the files in the routes directory. &lt;/p&gt;

&lt;p&gt;Step 2: Open the "index.ts" file in the routes directory. You will be doing the same thing you did in the "app.ts". &lt;/p&gt;

&lt;p&gt;So, import the types for the request, response, and next parameter and change the export statement at the bottom to ES modules "export default".&lt;/p&gt;

&lt;p&gt;Do the same for the "users.ts" file. It should look like this;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ploaksvhqy50988fb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ploaksvhqy50988fb1.png" alt="index.ts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice work, you are almost done. &lt;/p&gt;

&lt;p&gt;If you head to the bin folder and open the www file, you will notice you are requiring "app.js" which has now been changed to a typescript file. The server will crash if you try to run it this way.&lt;/p&gt;

&lt;p&gt;You need to require the compiled app.js file in the dist folder. Before you do that, run &lt;code&gt;tsc&lt;/code&gt; in your terminal to transpile our typescript code into javascript. This will create the "dist" folder. Remember where you set it in the tsconfig? Cool!&lt;/p&gt;

&lt;p&gt;Now, you have the dist folder. You can change the directory to "../dist/app" and chain a &lt;code&gt;.default&lt;/code&gt; method to it like so;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dq8txheh7soeh1oxgv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dq8txheh7soeh1oxgv1.png" alt="www"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, you might encounter an error about the views engine when you run your localhost:3000 on the browser, you can easily fix that by adding a '../" to your view engine setup in app.ts like this;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2j1dglw66p5ha5opyn7c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2j1dglw66p5ha5opyn7c.png" alt="views"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’ve carefully followed all the steps, run &lt;code&gt;yarn start&lt;/code&gt; in the terminal. Yep, your server should be up and running. &lt;/p&gt;

&lt;p&gt;Cheers, you have just converted your app to typescript. Easy-peasy, right? 🔥 &lt;/p&gt;

&lt;p&gt;Do leave a comment if you found this useful.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>express</category>
      <category>node</category>
    </item>
  </channel>
</rss>
