{"id":526,"date":"2023-12-11T11:52:01","date_gmt":"2023-12-11T10:52:01","guid":{"rendered":"https:\/\/clasenceneba.com\/?p=526"},"modified":"2023-12-11T11:54:45","modified_gmt":"2023-12-11T10:54:45","slug":"node-typescript-prisma","status":"publish","type":"post","link":"https:\/\/clasenceneba.com\/fr\/node-typescript-prisma\/","title":{"rendered":"Node (TypeScript+ Postgres+Prisma) Starter Project ."},"content":{"rendered":"<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/github.com\/scneba\/node_typescript_prisma\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"683\" src=\"http:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp\" alt=\"\" class=\"wp-image-527\" style=\"aspect-ratio:0.984375;width:517px;height:auto\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-300x200.webp 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-768x512.webp 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-18x12.webp 18w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">See Project on <a href=\"https:\/\/github.com\/scneba\/node_typescript_prisma\" data-type=\"link\" data-id=\"https:\/\/github.com\/scneba\/node_typescript_prisma\">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 or individuals&nbsp; get up and running with a Node TypeScript+Postgres+Prisma 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>data<\/strong> folder which has al the database access logic, and a <strong>routes<\/strong> folder which has all project routes.  The <strong>data<\/strong> folder model can be easily 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\n\n\n\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>routes<\/strong>&nbsp;All routes contained in the repo.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul>\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<\/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<\/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>PostgreSQL<\/li>\n\n\n\n<li>Prisma ORM<\/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 git from&nbsp;<a href=\"https:\/\/git-scm.com\/downloads\">here<\/a>. This should also install git bash which can be opened from the windows start menu.<\/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_prisma.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 &#8216;DATABASE_URL&#8217; to  include your postgres connection string<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\nDATABASE_URL=\"postgresql:\/\/postgres:PASSWORD@localhost:5432\/nodeprisma?schema=public\"\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>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 prisma db seed<\/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 starter project.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a free project to help startups or individuals&nbsp; get up and running with a Node TypeScript+Postgres+Prisma 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 [&hellip;]<\/p>","protected":false},"author":1,"featured_media":527,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[36,31,44,45,38,40],"blocksy_meta":"","featured_image_urls":{"full":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp",1024,683,false],"thumbnail":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-150x150.webp",150,150,true],"medium":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-300x200.webp",300,200,true],"medium_large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-768x512.webp",768,512,true],"large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp",1024,683,false],"1536x1536":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp",1024,683,false],"2048x2048":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma.webp",1024,683,false],"trp-custom-language-flag":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/12\/prisma-18x12.webp",18,12,true]},"post_excerpt_stackable":"<p>See Project on GitHub This is a free project to help startups or individuals&nbsp; get up and running with a Node TypeScript+Postgres+Prisma 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, data folder which has al the database access logic, and a routes folder which has all project routes. The data folder model can be easily 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\/526"}],"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=526"}],"version-history":[{"count":3,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/526\/revisions\/531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media\/527"}],"wp:attachment":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}