From 5446509b96d5d5bcd0aa9855ce5b9568fe706eb7 Mon Sep 17 00:00:00 2001 From: Joseph Ditton Date: Fri, 3 Dec 2021 18:31:24 -0700 Subject: [PATCH] update readme add git scripts --- README.md | 58 +++++++++++++++---- bin/create_db.sh | 12 ++++ bin/setup_new_project.sh | 18 ++++++ package.json | 2 + server/controllers/users.controller.ts | 3 +- .../migrations/1637028716848-AddUser.ts | 7 ++- server/database/seeds.ts | 3 +- server/dto/create_user.dto.ts | 3 +- server/entities/user.entity.ts | 5 +- 9 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 bin/create_db.sh create mode 100644 bin/setup_new_project.sh diff --git a/README.md b/README.md index 51e5e25..0b287f3 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,26 @@ -# Nest Starter App +# USU CS4610 Nest Starter App ## Description A starter app with Postgres, NestJS, and React +## Cloning the project +This app is designed to used as a starting point for another application so you will want to clone the project into a folder that matches your app. Run +```bash +$ git clone git@github.com:dittonjs/OnTheSpot.git +``` + +Replace your app name with the name of your app, for example +```bash +$ git clone git@github.com:dittonjs/OnTheSpot.git SpyChat +``` + +Next, go create a remote repository in github (or gitlab, or bitbucket, it doesn't matter) for your new application. + +Finally, run +```bash +$ bash ./bin/setup_new_project.sh +``` +and follow the prompts. This script will link the repo to your new repo while maintaining a reference to the starter app. This way, if we make changes to the starter app, you can still get those changes. ## Prerequisites ### asdf-vm Tool versions are managed using `asdf-vm`. You will need to have `asdf-vm` installed first. @@ -15,13 +33,15 @@ $ asdf install ``` ### Install yarn -We will use `yarn` instead of `npm` for package managment +We will use `yarn` instead of `npm` for package managment. To install yarn run ```bash $ npm install -g yarn ``` ### .env -Create a file in the root called `.env` and copy the contents of `.env.example` +Create a file in the root called `.env` and copy the contents of `.env.example`. + +Make sure you create a new file instead of renaming the `.env.example` file. In your new `.env` file update the values for each key as you would like @@ -37,27 +57,43 @@ $ cd client && yarn && cd .. ``` ### Database -Create the database +To setup the database run ```bash -$ pg_ctl start # starts postgres -$ createdb neststarterappdevelopment # creates a postgres database +$ yarn db:setup ``` +This will create the database, run the migrations, and run the seed for you. -Run the migrations +### Migrations +Any time you want make changes to your database schema you will need to generate a migration file +```bash +yarn db:migration:generate AddContextToRoles # replace this name with a name that describes your migration +``` +Open that migration file and make the changes. Then, when you are ready ```bash $ yarn db:migrate ``` +will run any pending migrations. -Migrations need to be run again everytime a new migration is created +If a team member adds a migrations you will need to run the migrate command to make the changes to your local database as well. -Run the seeds +### Seeds +Seeds allow you prepopulate your database with data. By default this application prepopulates the `Roles` and the Admin `User` into your database. + +If you make changes to the seeds file at `server/database/seeds.ts` the make sure that, in the event seeds are run multiple times, you don't end up with duplicate data. + +To run the seeds ```bash $ yarn db:seed ``` -This should create roles and your admin level user in your database. - ### SSL +**Only do this step if you intend on developing your app in an environment where you need SSL (like canvas or other embedded platforms).** + +In your `.env` set +``` +USE_SSL=true +``` + Create a ssl key and certificate and place them in the root directory ```bash diff --git a/bin/create_db.sh b/bin/create_db.sh new file mode 100644 index 0000000..0144e64 --- /dev/null +++ b/bin/create_db.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +source .env +echo "Creating db '$DATABASE_URL'" + +pg_ctl status || pg_ctl start + +{ + createdb $DATABASE_URL && echo "Database '$DATABASE_URL' created successfully" +} || { + echo "Database '$DATABASE_URL' already exists, skipping creation." +} diff --git a/bin/setup_new_project.sh b/bin/setup_new_project.sh new file mode 100644 index 0000000..bcffc7d --- /dev/null +++ b/bin/setup_new_project.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +source .env +echo "What is the name of your application?" +read appname + +echo "Where is your git repo? (eg git@github.com:dittonjs/NestStarterApp.git)" + +read reponame + +sed -i "s/USU CS4610 Nest Starter App/$appname/" README.md + +git remote rename origin upstream +git remote add origin $reponame +git branch -M main +git push -u origin main + + diff --git a/package.json b/package.json index 4713982..c4f2aa3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "db:migrate": "yarn db:start && yarn typeorm migration:run", "db:migrate:undo": "yarn db:start && yarn typeorm migration:revert", "db:seed": "yarn db:start && ts-node ./node_modules/typeorm-seeding/dist/cli.js seed -n cli_config.ts -r $(pwd)/server/database", + "db:reset": "yarn db:start && yarn typeorm schema:drop && yarn db:migrate && yarn db:seed", + "db:setup": "bash bin/create_db.sh && yarn db:migrate && yarn db:seed", "prebuild": "rimraf dist", "build": "nest build && yarn client:build", "format": "prettier --write \"server/**/*.ts\" \"test/**/*.ts\"", diff --git a/server/controllers/users.controller.ts b/server/controllers/users.controller.ts index fda71b3..b06a8fd 100644 --- a/server/controllers/users.controller.ts +++ b/server/controllers/users.controller.ts @@ -43,7 +43,8 @@ export class UsersController { async create(@Body() userPayload: CreateUserDto, @Res({ passthrough: true }) res: Response) { const newUser = new User(); newUser.email = userPayload.email; - newUser.name = userPayload.name; + newUser.firstName = userPayload.firstName; + newUser.lastName = userPayload.lastName; newUser.passwordHash = await bcrypt.hash(userPayload.password, 10); const [role] = await this.rolesService.findByKey(RoleKey.USER); const userRole = new UserRole(); diff --git a/server/database/migrations/1637028716848-AddUser.ts b/server/database/migrations/1637028716848-AddUser.ts index 5cc3b7c..13cbbe0 100644 --- a/server/database/migrations/1637028716848-AddUser.ts +++ b/server/database/migrations/1637028716848-AddUser.ts @@ -13,7 +13,12 @@ export class AddUser1637028716848 implements MigrationInterface { isGenerated: true, }, { - name: 'name', + name: 'firstName', + type: 'text', + isNullable: false, + }, + { + name: 'lastName', type: 'text', isNullable: false, }, diff --git a/server/database/seeds.ts b/server/database/seeds.ts index 4e9acc4..3b6cdf9 100644 --- a/server/database/seeds.ts +++ b/server/database/seeds.ts @@ -36,7 +36,8 @@ export default class Seeds implements Seeder { adminUser = new User(); adminUser.email = process.env.ADMIN_EMAIL; adminUser.passwordHash = passwordHash; - adminUser.name = 'Site Admin'; + adminUser.firstName = 'Admin'; + adminUser.lastName = 'Site'; const adminUserRole = new UserRole(); adminUserRole.role = adminRole; adminUser.userRoles = [adminUserRole]; diff --git a/server/dto/create_user.dto.ts b/server/dto/create_user.dto.ts index cf87fed..3c84f2c 100644 --- a/server/dto/create_user.dto.ts +++ b/server/dto/create_user.dto.ts @@ -1,5 +1,6 @@ export class CreateUserDto { - name: string; + firstName: string; + lastName: string; email: string; password: string; } diff --git a/server/entities/user.entity.ts b/server/entities/user.entity.ts index 7d0537c..aeef107 100644 --- a/server/entities/user.entity.ts +++ b/server/entities/user.entity.ts @@ -11,7 +11,10 @@ export class User { email: string; @Column({ nullable: false }) - name: string; + firstName: string; + + @Column({ nullable: false }) + lastName: string; @Column({ nullable: false }) passwordHash: string;