Skip to content

ArthurVerrept/ecoscan-backend

Repository files navigation

Description

Netscapes backend repo 🎉



Setup

Before running the api, there are a few things we need to download:

  1. Node v12+
  2. Homebrew
  3. pgAdmin

1. Install & Run Postgresql:

Check you don't already have postgres installed:

$ postgres -V

(If you have postgres installed I assume you know how to link it using the env file)
If you do not have postgres, use homebrew to download it:

$ brew install postgresql

Once it is downloaded, go ahead and run it:

$ brew services start postgresql

*If you are having issues running the th command above use: rm /usr/local/var/postgres/postmaster.pid
DO NOT DO THIS IF YOU HAVE IMPORTANT DATA RUNNING IN ANY POSTGRES DB AS YOU COULD LOSE IS

2. Find Your User:

Firstly enter the postgres db command line by running:

$ psql postgres

Find your user by running:

$ postgres=# \du

This should say return a table with your role name on the left.

3. Connect to Server

  • Open pgAdmin (if this is tour first time it will prompt you to create a master password, make this whatever you want)
  • Create a new server by right clicking on servers
  • Set the name as whatever you like
  • Under "Connection" set the Hostname as localhost
  • Set the Username as The role name you found in step 2
  • Leave/ set the port as 5432

You should now see a server with a database called postgres in it. This means you're ready to move on to step 4 🎊

4. Tooling Install

Nestjs cli:

$ npm install -g @nestjs/cli

Typeorm cli:

$ npm i -g typeorm

Ts-node:

$ npm install -g ts-node

5. Setup Environment Variables

Finally we can open the actual project!

  • Find the ".env-example" file in the root of the project, and rename it to ".env".
  • Open it and replace username to be your user from step 2

6. Install packages

Using npm install the packages required:

$ npm i

Running the API 🏃

Run api in watch mode:

$ npm run start:dev


Development

Creating new Controllers, Modules, and Services

The easist way to create new controllers, modules and services is to use the nestjs cli. Simply write the command below replacing the necessary parts for your desired file to be generated.

nest g <schematic> <module-name>

This will also hook up all the modules together without you having to do anything. Docs for this command

Updating the DB

The database tables are created based on what we define in our entities folder. every file that ends with .entity.ts is a db table. Since locally we are using typeorm's synchronize feature it will ✨automagically✨ update the postgres database.

To change a table, simply alter/ create an .entity.ts file and rebuild the dist folder if you are not running in watch mode:

npm run build

However if you wish to work with migrations you must first go to - src/database/database.module.ts and set synchronise to false:
synchronise: false

then you can run:

npm run migration:generate <name>

Once this is finished run to your new migration and change your local database:

npm run migration:run

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published