{"id":318,"date":"2023-08-16T13:28:00","date_gmt":"2023-08-16T12:28:00","guid":{"rendered":"https:\/\/clasenceneba.com\/?p=318"},"modified":"2023-12-11T11:32:26","modified_gmt":"2023-12-11T10:32:26","slug":"node-js-postgres","status":"publish","type":"post","link":"https:\/\/clasenceneba.com\/en_gb\/node-js-postgres\/","title":{"rendered":"Node.js\/Javascript\/Postgres starter project for startups."},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/github.com\/scneba\/nodebase\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"553\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1-1024x553.jpg\" alt=\"\" class=\"wp-image-320\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1-1024x553.jpg 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1-300x162.jpg 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1-768x415.jpg 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1-1536x830.jpg 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1.jpg 1895w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Access project on <a href=\"https:\/\/github.com\/scneba\/nodebase\" data-type=\"link\" data-id=\"https:\/\/github.com\/scneba\/nodebase\">Github<\/a><\/figcaption><\/figure>\n\n\n\n<p>For any startup thinking about developing a prototype, architecting and building from scratch can be exhausting. A myriad of questions come up like &#8212; what framework, database, or ORM should we use? How do we structure our code? What naming conventions should we use? How do we write code that can accommodate comprehensive testing? How should we do integration testing?  <\/p>\n\n\n\n<p>This freely available project can help you answer a number of these questions. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on.  <\/p>\n\n\n\n<p>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 schemas, and a data folder which has all the functions to access the database. he 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.js<\/strong>: This contains all the functions with the logic in this folder.<\/li>\n\n\n\n<li><strong>sevice.test.js:<\/strong> This contains the test of all the functions written in service.js.<\/li>\n\n\n\n<li><strong>errors.js:<\/strong> This contains all the errors which are used in the service.js logic. <\/li>\n<\/ul>\n\n\n\n<p>Find below the folder structure of the project with explanations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Project structure<\/h3>\n\n\n\n<ul>\n<li><strong>postman<\/strong>: all postman files should be added here.\n<ul>\n<li><strong>role_base.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<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.js<\/strong>&nbsp;All error used within the subcontroller. Any errors used in this sub-controller should be here<strong>.<\/strong><\/li>\n\n\n\n<li><strong>service.js&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<\/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\n\n\n<li><strong>migrations<\/strong>&nbsp;All Sequelize migrations are added here. See the docs  <a href=\"https:\/\/sequelize.org\/docs\/v6\/other-topics\/migrations\/\" data-type=\"link\" data-id=\"https:\/\/sequelize.org\/docs\/v6\/other-topics\/migrations\/\">here<\/a>.<\/li>\n\n\n\n<li><strong>models<\/strong>&nbsp;All database schemas for Sequelize&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 Sequelize seeds. Seed documentation <a href=\"https:\/\/sequelize.org\/master\/manual\/migrations.html#running-seeds\" data-type=\"link\" data-id=\"https:\/\/sequelize.org\/master\/manual\/migrations.html#running-seeds\">here<\/a>.<\/li>\n\n\n\n<li><strong>utils<\/strong>&nbsp;Any reusable javascript code is added here. <\/li>\n\n\n\n<li><strong>app.js<\/strong>&nbsp;entry point of application.<\/li>\n\n\n\n<li><strong>migrate.js<\/strong>&nbsp;service to run migrations when the app is run. <\/li>\n\n\n\n<li><strong>storage<\/strong>&nbsp;storage for sessions<\/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>sqlstructure.sql: The structure of the SQL exported from<a href=\"https:\/\/dbdiagram.io\/\" data-type=\"link\" data-id=\"https:\/\/dbdiagram.io\/\"> dbdiagrams.io<\/a><\/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>Jest<\/li>\n\n\n\n<li>Git<\/li>\n\n\n\n<li>PostgreSQL<\/li>\n\n\n\n<li>Sequelize ORM<\/li>\n\n\n\n<li>Postman<\/li>\n\n\n\n<li>GitLab CI\/CD<\/li>\n\n\n\n<li>Docker\/Docker compose<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"setup\">Setup<\/h2>\n\n\n\n<p>1.  Download and install git from&nbsp;<a href=\"https:\/\/git-scm.com\/downloads\">here<\/a>. This should also install git bash. <\/p>\n\n\n\n<p>2. Download and Install PgAdmin 4 from&nbsp;<a href=\"https:\/\/www.pgadmin.org\/download\/pgadmin-4-windows\/\">here<\/a>. This comes with PostgreSQL and should automatically run after installation on port 5432(unless changed during installation). Make sure to take note of your passwords. There should be a PgAdmin password and the default user password. After installation, open git bash and run the following commands to check if Postgres is running on the same port.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql --version<\/code><\/pre>\n\n\n\n<p>If the version does not show up, add psql path to your system paths. On windows, this is installed by default at<em>&nbsp;<code>C:\\Program Files\\PostgreSQL\\14\\bin<\/code><\/em> for version 14. <\/p>\n\n\n\n<ul>\n<li>Connect user (use the default username, which should be <em>postgres<\/em> if not changed). Enter the default user password when requested. <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres<\/code><\/pre>\n\n\n\n<ul>\n<li>Check connection status<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\\conninfo<\/code><\/pre>\n\n\n\n<p>You should see output like this image here. This contains the informations you will need to connect to the database. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"97\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1024x97.png\" alt=\"\" class=\"wp-image-321\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1024x97.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-300x28.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-768x73.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1536x146.png 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres.png 1611w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul>\n<li>Start PgAdmin app( Can be found on the start menu on windows)<\/li>\n\n\n\n<li>Create a database called&nbsp;<code><em>nodebase<\/em><\/code>&nbsp;with the user <em>postgres<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>3. Install Node.js. Download your variant from&nbsp;<a href=\"https:\/\/nodejs.org\/en\/download\/\">here<\/a><\/p>\n\n\n\n<ul>\n<li>After installation, open git bash and type the command below to see if you get help information. <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npm -h<\/code><\/pre>\n\n\n\n<p>4. Install sequelize-cli globally<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g sequelize-cli<\/code><\/pre>\n\n\n\n<p>After installation, the below command should display the help page<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx sequelize-cli --help<\/code><\/pre>\n\n\n\n<p>5.  Clone this repository<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/scneba\/nodebase.git\n<\/code><\/pre>\n\n\n\n<p>6. Install dependencies: open git bash on the root folder and run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install<\/code><\/pre>\n\n\n\n<p>7. Make a copy of the .env.example and rename it to .env. Update the connection string on line 1 to include your postgres user password (update the string&nbsp;<em>password<\/em>&nbsp;below)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONN_ST=postgres:\/\/postgres:YOUR_PASSWORD@localhost:5432\/nodebase<\/code><\/pre>\n\n\n\n<p>9. Run the command below to start the project with refreshing managed by <a href=\"https:\/\/www.npmjs.com\/package\/nodemon\" data-type=\"link\" data-id=\"https:\/\/www.npmjs.com\/package\/nodemon\">nodemon<\/a>. This should also run all the migrations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run dev<\/code><\/pre>\n\n\n\n<p>10.  Install postman and create an account. Download from&nbsp;<a href=\"https:\/\/www.postman.com\/downloads\/\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Development<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Run Seeds and Migrations with Sequelize<\/h2>\n\n\n\n<ul>\n<li>cd into src\/ folder and run the following command to create a sequelize config file<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code> npx sequelize init<\/code><\/pre>\n\n\n\n<p>This should  create config\/config.yml file and fail to create model and migrations folders which already exist.<\/p>\n\n\n\n<p>Update the config\/config.json file to reflect the database configurations<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"development\": {\n    \"username\": \"postgres\",\n    \"password\": \"YOURPASSWORD\",\n    \"database\": \"nodebase\",\n    \"host\": \"127.0.0.1\",\n    \"dialect\": \"postgres\"\n},\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Seeding<\/h3>\n\n\n\n<p>Undo all seeds<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx sequelize-cli db:seed:undo:all<\/code><\/pre>\n\n\n\n<p>Seed Database<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx sequelize-cli db:seed:all<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing<\/h3>\n\n\n\n<p>Ideally, all core logic in controllers should be tested. To run tests for a particular sub controller file, use the testPathPattern eg.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm test -- --testPathPattern \"registering\/\"<\/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 <em>postman\/role_base.postman_collection.json<\/em> into postman. Update the <em><strong>domain<\/strong><\/em> variable in postman to <em><strong><span style=\"color: #141212;\" class=\"stk-highlight\">localhost:{PORT}<\/span><\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"463\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1-1024x463.jpg\" alt=\"\" class=\"wp-image-322\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1-1024x463.jpg 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1-300x136.jpg 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1-768x348.jpg 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1-1536x695.jpg 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman1.jpg 2022w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul>\n<li>Run the  project with nodemon from the root directory<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run dev<\/code><\/pre>\n\n\n\n<ul>\n<li>Run the Postman collection and make sure all the tests pass<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"453\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-1024x453.jpg\" alt=\"\" class=\"wp-image-323\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-1024x453.jpg 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-300x133.jpg 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-768x340.jpg 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-1536x680.jpg 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman2-2048x906.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Congratulations, you are ready to start developing with the base project. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>For any startup thinking about developing a prototype, architecting and building from scratch can be exhausting. A myriad of questions come up like &#8212; what framework, database, or ORM should we use? How do we structure our code? What naming conventions should we use? How do we write code that can accommodate comprehensive testing? How [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[36,31,37,38],"blocksy_meta":"","featured_image_urls":{"full":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image.jpg",1895,1024,false],"thumbnail":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-150x150.jpg",150,150,true],"medium":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-300x162.jpg",300,162,true],"medium_large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-768x415.jpg",768,415,true],"large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1024x553.jpg",1024,553,true],"1536x1536":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image-1536x830.jpg",1536,830,true],"2048x2048":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image.jpg",1895,1024,false],"trp-custom-language-flag":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/feature_image.jpg",18,10,false]},"post_excerpt_stackable":"<p>Access project on Github For any startup thinking about developing a prototype, architecting and building from scratch can be exhausting. A myriad of questions come up like &#8212; what framework, database, or ORM should we use? How do we structure our code? What naming conventions should we use? How do we write code that can accommodate comprehensive testing? How should we do integration testing? This freely available project can help you answer a number of these questions. The concepts and methods used have been tested and validated in successful startups and industrial projects I have worked on. The philosophy of&hellip;<\/p>\n","category_list":"<a href=\"https:\/\/clasenceneba.com\/en_gb\/category\/projects\/\" rel=\"category tag\">Projects<\/a>","author_info":{"name":"clasenceneba.com","url":"https:\/\/clasenceneba.com\/en_gb\/author\/clasenceneba-com\/"},"comments_num":"0 comments","_links":{"self":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/318"}],"collection":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":10,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/318\/revisions\/525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/media\/319"}],"wp:attachment":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}