Netscapes backend repo 🎉
Before running the api, there are a few things we need to download:
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
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.
- 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 🎊
$ npm install -g @nestjs/cli
$ npm i -g typeorm
$ npm install -g ts-node
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
Using npm install the packages required:
$ npm i
Run api in watch mode:
$ npm run start:dev
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
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