{"id":355,"date":"2023-10-19T10:00:52","date_gmt":"2023-10-19T09:00:52","guid":{"rendered":"https:\/\/clasenceneba.com\/?p=355"},"modified":"2023-11-06T19:06:22","modified_gmt":"2023-11-06T18:06:22","slug":"node-typescript-mongodb","status":"publish","type":"post","link":"https:\/\/clasenceneba.com\/fr\/node-typescript-mongodb\/","title":{"rendered":"Node (TypeScript+MongoDB) Starter Project for Startups."},"content":{"rendered":"<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/github.com\/scneba\/node_typescript_base\"><img decoding=\"async\" loading=\"lazy\" width=\"1008\" height=\"1024\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-2.png\" alt=\"\" class=\"wp-image-507\" style=\"aspect-ratio:0.984375;width:517px;height:auto\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-2.png 1008w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-2-295x300.png 295w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-2-768x780.png 768w\" sizes=\"(max-width: 1008px) 100vw, 1008px\" \/><\/a><figcaption class=\"wp-element-caption\">See Project on <a href=\"https:\/\/github.com\/scneba\/node_typescript_base\" data-type=\"link\" data-id=\"https:\/\/github.com\/scneba\/node_typescript_base\">GitHub<\/a><\/figcaption><\/figure>\n\n\n\n<p><span style=\"font-family: Georgia, Arial, serif; white-space-collapse: collapse;\">This is a free project to help startups quickly get up and running with a Node\/TypeScript\/MongoDB project. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on.<\/span><\/p>\n\n\n\n<p>The philosophy of this base project is to separate the code into three core folders: <strong>controllers<\/strong> which have the main logic, <strong>models<\/strong> folder which has all the database schema, and a <strong>data<\/strong> folder which has all the functions to access the database.  The data folder model can be replaced with any database and this will not affect the functionality of the controllers and the tests. <\/p>\n\n\n\n<p>Each controller has smaller independent service folders, each folder carrying similar logic to maximize reuse. Each controller folder name must end with an &#8220;ing&#8221; like &#8220;registering, listing, deleting &#8230;&#8221;. Each folder must contain at least three files. <\/p>\n\n\n\n<ul>\n<li><strong>service.ts<\/strong>: This contains all the functions with the logic in this folder.<\/li>\n\n\n\n<li><strong>sevice.test.ts:<\/strong> This contains the test of all the functions written in service.ts.<\/li>\n\n\n\n<li><strong>errors.ts:<\/strong> This contains all the errors which are used in the service.ts logic. <\/li>\n\n\n\n<li><strong>validate.ts<\/strong>(optional): This folder has all the logic to validate the data received over the REST API. <\/li>\n<\/ul>\n\n\n\n<p>Find below the folder structure of the project with explanations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project structure<\/h2>\n\n\n\n<ul>\n<li><strong>postman<\/strong>: all postman files should be added here.\n<ul>\n<li><strong>nodebase.postman_collection.json<\/strong>&nbsp;postman collection for integration testing.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>src<\/strong>&nbsp;all source code should be added in here.\n\n\n\n\n\n\n<ul>\n<li><strong>controllers<\/strong>&nbsp; &#8211; all core logic for endpoint control is added here.\n<ul>\n<li>registering: Sub-controller handling all registration logic.\n<ul>\n<li><strong>errors.ts<\/strong>&nbsp;All errors used within the subcontroller. <\/li>\n\n\n\n<li><strong>service.ts&nbsp;<\/strong>All the logic for the sub-controller should be added here.<\/li>\n\n\n\n<li><strong>service.test.js<\/strong>&nbsp;Tests for all sub-controller functions should be added here. See the documentation for jest testing&nbsp;<a href=\"https:\/\/jestjs.io\/docs\/getting-started\">here<\/a>.<\/li>\n\n\n\n<li><strong>validate.ts<\/strong>: all validation logic. <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>data<\/strong>&nbsp;all database access methods are added here.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>models<\/strong>&nbsp;All database schemas for MongoDB&nbsp;<\/li>\n\n\n\n<li><strong>routes<\/strong>&nbsp;All routes contained in the repo.&nbsp;<\/li>\n\n\n\n<li><strong>seeders<\/strong>&nbsp;All seeds for the database<\/li>\n\n\n\n<li><strong>utils<\/strong>&nbsp;Any reusable typescript code, constants and errors are added here. <\/li>\n\n\n\n<li><strong>index.ts<\/strong>&nbsp;entry point of application.<\/li>\n\n\n\n<li><strong>connectmon.ts.<\/strong>&nbsp;service which connects to mongodb <\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>.env.example:<\/strong> Contains all the env variables used in the project. Copy and rename to .env to use locally.<\/li>\n\n\n\n<li><strong>.gitlab-ci.yml<\/strong>: Gitlab CI\/CD configuration file.<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Technologies and Resources<\/h2>\n\n\n\n<ul>\n<li>Node.js<\/li>\n\n\n\n<li>Express.js<\/li>\n\n\n\n<li>TypeScript<\/li>\n\n\n\n<li>MongoDB<\/li>\n\n\n\n<li>Postman<\/li>\n\n\n\n<li>Jest<\/li>\n\n\n\n<li>Docker\/Docker Compose<\/li>\n\n\n\n<li>Git<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Setup<\/h2>\n\n\n\n<ol>\n<li>Download and Install your variant of MongoDB from&nbsp;<a href=\"https:\/\/www.mongodb.com\/try\/download\/compass\">here<\/a>.<\/li>\n\n\n\n<li>Open the app and create a database called&nbsp;<em>nodebase<\/em>.<\/li>\n\n\n\n<li>Install Node.js. Download your variant from&nbsp;<a href=\"https:\/\/nodejs.org\/en\/download\/\">here<\/a><\/li>\n<\/ol>\n\n\n\n<ul>\n<li>After install, open git bash or a terminal and type the command below to see if you get the help page<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npm -h<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li>Clone this repository<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/scneba\/node_typescript_base.git<\/code><\/pre>\n\n\n\n<ol start=\"5\">\n<li>open git bash or terminal on the root folder and run<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install<\/code><\/pre>\n\n\n\n<ol start=\"6\">\n<li>Make a copy of the .env.example and rename it to .env<\/li>\n\n\n\n<li>Update the connection string on line 2 to include your mongodb connection string<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>MONGO_URL=\"mongodb:\/\/localhost:27017\/nodebase\"\n<\/code><\/pre>\n\n\n\n<ol start=\"8\">\n<li>Run&nbsp;<code>npm run dev<\/code>&nbsp;on the root folder to start the project.<\/li>\n\n\n\n<li>Install postman and create an account. Download from&nbsp;<a href=\"https:\/\/www.postman.com\/downloads\/\">here<\/a>.<\/li>\n\n\n\n<li><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Run Seeds<\/h2>\n\n\n\n<ul>\n<li>Update the connection string on line 19, src\/seeder\/seeder.ts to your mongodb connection string.<\/li>\n\n\n\n<li>Run this command in the root folder to run the seeds on the database<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npx tsnd src\\seeder\\seeder.ts\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing<\/h2>\n\n\n\n<p>Ideally, all core logic in controllers should be tested. To test a particular sub-controller file, use the testPathPattern eg.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm test -- --testPathPattern \"registering\/\"\n<\/code><\/pre>\n\n\n\n<p>This will run all tests in the registering\/ path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Postman<\/h2>\n\n\n\n<p>Import the collection at Postman\/nodebase.postman_collection.json into postman.<\/p>\n\n\n\n<ul>\n<li> Update the domain variable in postman&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/scneba\/node_typescript_base\/blob\/main\/images\/postman.jpg\" target=\"_blank\"><\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"370\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2-1024x370.png\" alt=\"\" class=\"wp-image-363\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2-1024x370.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2-300x109.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2-768x278.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2-1536x556.png 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-2.png 1963w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul>\n<li>Start project with ts-node<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run dev\n<\/code><\/pre>\n\n\n\n<ul>\n<li>Run postman collection and make sure all tests pass&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/scneba\/node_typescript_base\/blob\/main\/images\/postman_all_tests.jpg\" target=\"_blank\"><\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"423\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1-1024x423.png\" alt=\"\" class=\"wp-image-360\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1-1024x423.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1-300x124.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1-768x317.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1-1536x634.png 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1.png 1937w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Congratulations, you are ready to start development with this base project.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a free project to help startups quickly get up and running with a Node\/TypeScript\/MongoDB project. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on. The philosophy of this base project is to separate the code into three core folders: controllers which have [&hellip;]<\/p>","protected":false},"author":1,"featured_media":506,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[36,39,31,38,40],"blocksy_meta":"","featured_image_urls":{"full":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7.png",2015,2048,false],"thumbnail":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7-150x150.png",150,150,true],"medium":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7-295x300.png",295,300,true],"medium_large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7-768x781.png",768,781,true],"large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7-1008x1024.png",1008,1024,true],"1536x1536":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7-1511x1536.png",1511,1536,true],"2048x2048":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7.png",2015,2048,false],"trp-custom-language-flag":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/MongoDB_logo_PNG7.png",12,12,false]},"post_excerpt_stackable":"<p>See Project on GitHub This is a free project to help startups quickly get up and running with a Node\/TypeScript\/MongoDB project. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on. The philosophy of this base project is to separate the code into three core folders: controllers which have the main logic, models folder which has all the database schema, and a data folder which has all the functions to access the database. The data folder model can be replaced with any database and this will not affect the functionality\u2026<\/p>\n","category_list":"<a href=\"https:\/\/clasenceneba.com\/fr\/category\/projects\/\" rel=\"category tag\">Projects<\/a>","author_info":{"name":"clasenceneba.com","url":"https:\/\/clasenceneba.com\/fr\/author\/clasenceneba-com\/"},"comments_num":"0 commentaire","_links":{"self":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/355"}],"collection":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":6,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/355\/revisions\/509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media\/506"}],"wp:attachment":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}