Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 351
Add Models using TypeORM#287
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
bc750e6578c900182d9cf5bea2686345ce6f7d6533908319493462616c90850c7cd3b97f9941aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,20 +21,51 @@ export default { | ||
| }, | ||
| async create(req: Request, res: Response) { | ||
| const { name, description, category, details } = req.body; | ||
| const chapter = new Chapter({ name, description, category, details }); | ||
| const { | ||
| name, | ||
| description, | ||
| category, | ||
| details, | ||
| location, | ||
| creator, | ||
| } = req.body; | ||
| const chapter = new Chapter({ | ||
| name, | ||
| description, | ||
| category, | ||
| details, | ||
| location, | ||
| creator, | ||
| }); | ||
| try { | ||
| await chapter.save(); | ||
| res.status(201).json(chapter); | ||
| } catch (e) { | ||
| if (e.code === '23503' && e.message.includes('foreign key constraint')) { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be a bitch to setup so maybe we should extract this for later reuse | ||
| if (e.detail.includes('location')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just an edge case, if e.detail is null or undefined than it will throw an error like ".includes of undefined". To prevent this we can use destructing with default value. | ||
| return res.status(404).json({ message: 'location not found' }); | ||
| } | ||
| if (e.detail.includes('user')) { | ||
| return res.status(404).json({ message: 'creator not found' }); | ||
| } | ||
| } | ||
| res.status(500).json({ error: e }); | ||
| ||
| } | ||
| }, | ||
| async update(req: Request, res: Response) { | ||
| const { id } = req.params; | ||
| const { name, description, category, details } = req.body; | ||
| const { | ||
| name, | ||
| description, | ||
| category, | ||
| details, | ||
| location, | ||
| creator, | ||
| } = req.body; | ||
| const chapter = await Chapter.findOne({ id: parseInt(id) }); | ||
| @@ -43,11 +74,25 @@ export default { | ||
| description && (chapter.description = description); | ||
| category && (chapter.category = category); | ||
| details && (chapter.details = details); | ||
| location && (chapter.location = location); | ||
| creator && (chapter.creator = creator); | ||
| try { | ||
| await chapter.save(); | ||
| res.json(chapter); | ||
| } catch (e) { | ||
| if ( | ||
| e.code === '23503' && | ||
| e.message.includes('foreign key constraint') | ||
| ) { | ||
| if (e.detail.includes('location')) { | ||
| return res.status(404).json({ message: 'location not found' }); | ||
| } | ||
| if (e.detail.includes('user')) { | ||
| return res.status(404).json({ message: 'creator not found' }); | ||
| } | ||
| } | ||
| res.status(500).json({ error: e }); | ||
| } | ||
| } else { | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Sponsor1575816404398 implements MigrationInterface { | ||
| name = 'Sponsor1575816404398'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "sponsors" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "website" character varying NOT NULL, | ||
| "logo_path" character varying NOT NULL, | ||
| "type" character varying NOT NULL, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_6d1114fe7e65855154351b66bfc" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query(`DROP TABLE "sponsors"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Location1575816827867 implements MigrationInterface { | ||
| name = 'Location1575816827867'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "locations" ( | ||
| "id" SERIAL NOT NULL, | ||
| "country_code" character varying NOT NULL, | ||
| "city" character varying NOT NULL, | ||
| "region" character varying NOT NULL, | ||
| "postal_code" character varying NOT NULL, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_7cc1c9e3853b94816c094825e74" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query(`DROP TABLE "locations"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Venue1575816971375 implements MigrationInterface { | ||
| name = 'Venue1575816971375'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "venues" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "location_id" integer, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_cb0f885278d12384eb7a81818be" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "venues" ADD CONSTRAINT "FK_937d4cf54512864b1a842482c13" FOREIGN KEY ("location_id") REFERENCES "locations"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "venues" DROP CONSTRAINT "FK_937d4cf54512864b1a842482c13"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "venues"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class SocialProvider1575817257608 implements MigrationInterface { | ||
| name = 'SocialProvider1575817257608'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "social_providers" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_b436096f9efa0ae93c6dc8d1aec" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query(`DROP TABLE "social_providers"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class User1575817780103 implements MigrationInterface { | ||
| name = 'User1575817780103'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "users" ( | ||
| "id" SERIAL NOT NULL, | ||
| "first_name" character varying NOT NULL, | ||
| "last_name" character varying NOT NULL, | ||
| "email" character varying NOT NULL, | ||
| "password_digest" character varying NOT NULL, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query(`DROP TABLE "users"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Chapter1575817780949 implements MigrationInterface { | ||
| name = 'Chapter1575817780949'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "chapters" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "description" character varying NOT NULL, | ||
| "category" character varying NOT NULL, | ||
| "details" json NOT NULL, | ||
| "location_id" integer, | ||
| "creator_id" integer, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_a2bbdbb4bdc786fe0cb0fcfc4a0" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "chapters" ADD CONSTRAINT "FK_95d978cb93804fa26283d6741cf" FOREIGN KEY ("location_id") REFERENCES "locations"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "chapters" ADD CONSTRAINT "FK_60315cd6f39d467818aa7c60505" FOREIGN KEY ("creator_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "chapters" DROP CONSTRAINT "FK_60315cd6f39d467818aa7c60505"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "chapters" DROP CONSTRAINT "FK_95d978cb93804fa26283d6741cf"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "chapters"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class UserChapter1575817782077 implements MigrationInterface { | ||
| name = 'UserChapter1575817782077'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "user_chapters" ( | ||
| "id" SERIAL NOT NULL, | ||
| "user_id" integer, | ||
| "chapter_id" integer, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_451c90e328a46e8d75f01029c7d" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_chapters" ADD CONSTRAINT "FK_48f715cb79511ccbf947b70d42c" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_chapters" ADD CONSTRAINT "FK_a88e0c594dfd6492084b9b3f13b" FOREIGN KEY ("chapter_id") REFERENCES "chapters"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_chapters" DROP CONSTRAINT "FK_a88e0c594dfd6492084b9b3f13b"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_chapters" DROP CONSTRAINT "FK_48f715cb79511ccbf947b70d42c"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "user_chapters"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Event1575818350511 implements MigrationInterface { | ||
| name = 'Event1575818350511'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "events" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "description" character varying NOT NULL, | ||
| "start_at" TIMESTAMP NOT NULL, "ends_at" TIMESTAMP NOT NULL, | ||
| "canceled" boolean NOT NULL DEFAULT false, | ||
| "capacity" integer NOT NULL, | ||
| "venue_id" integer, | ||
| "chapter_id" integer, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_40731c7151fe4be3116e45ddf73" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "events" ADD CONSTRAINT "FK_26e10dc1ae5cdd5a20279e08b4a" FOREIGN KEY ("venue_id") REFERENCES "venues"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "events" ADD CONSTRAINT "FK_987a4efcef7b92bd79e1071719d" FOREIGN KEY ("chapter_id") REFERENCES "chapters"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "events" DROP CONSTRAINT "FK_987a4efcef7b92bd79e1071719d"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "events" DROP CONSTRAINT "FK_26e10dc1ae5cdd5a20279e08b4a"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "events"`, undefined); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
| export class Tag1575818467092 implements MigrationInterface { | ||
| name = 'Tag1575818467092'; | ||
| public async up(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "tags" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" character varying NOT NULL, | ||
| "event_id" integer, | ||
| "created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| "updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
| CONSTRAINT "PK_e7dc17249a1148a1970748eda99" PRIMARY KEY ("id"))`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "tags" ADD CONSTRAINT "FK_fd793b86b010970181e155eb750" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
| undefined, | ||
| ); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<any> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "tags" DROP CONSTRAINT "FK_fd793b86b010970181e155eb750"`, | ||
| undefined, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "tags"`, undefined); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This unfortunately wont work at property at runtime, beacuse here we implicitly convert location (string) from body into a Location object, change this to location_id and creator_id (for, now later we'll get that from logged in user), and query location and user before inserting a new chapter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zeko369 i think at runtime TypeORM converts an id to an entity object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was wrong, it actually work, but sending
location_idandcreator_iddoesn't, so do you think we should change it tolocationandcreatorinapi docsor make 2 more queries to the db here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zeko369 sorry for the delay. I think we should change it to
locationandcreator. Since, TypeOrm fails if it is not a valid id we can choose to provide helpful error message instead of making 2 more request to the db.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's do that then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright cool, I am on it.