Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Add Models using TypeORM #287

Merged
merged 11 commits into from
Dec 15, 2019
Merged

Conversation

ankorGH
Copy link
Contributor

@ankorGH ankorGH commented Dec 8, 2019

  • I have read Chapter's contributing guidelines.
  • My pull request has a descriptive title (not a vague title like Update README.md).
  • My pull request targets the master branch of Chapter.

Add models using typeorm as shown in db diagram.

@Zeko369 Zeko369 mentioned this pull request Dec 8, 2019
3 tasks
@Zeko369
Copy link
Member

Zeko369 commented Dec 8, 2019

Could you order imports by the importance?

  1. npm module
  2. aliased modules
  3. local imports

@@ -21,8 +21,22 @@ export default {
},

async create(req: Request, res: Response) {
const { name, description, category, details } = req.body;
const chapter = new Chapter({ name, description, category, details });
const {
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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_id and creator_id doesn't, so do you think we should change it to location and creator in api docs or make 2 more queries to the db here?

Copy link
Contributor Author

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 location and creator. 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.

Copy link
Member

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

Copy link
Contributor Author

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.

@QuincyLarson
Copy link
Contributor

@ankorGH How is this going? Are you ready for @Zeko369 to take a second look at this? Thanks again for your help.

import { Chapter } from './Chapter';

@Entity({ name: 'user_bans' })
export class UserBan extends BaseModel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe instead of doing this create a joinTable https://typeorm.io/#/many-to-many-relations? Also the same for EventSponsor and UserChapter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we could. I was thinking of a situation where it doesn't extend BaseModel but I didn't test it first.

Copy link
Member

@Zeko369 Zeko369 Dec 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://typeorm.io/#/many-to-many-relations/many-to-many-relations-with-custom-properties 😢 , for them to have createdAt and updatedAt we need custom entities, so maybe we could update BaseModel to only have createdAt and updatedAt and add ID to all models that need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we currently have will also work since UserBan also needs an id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, keep it like this then

@ankorGH
Copy link
Contributor Author

ankorGH commented Dec 15, 2019

@QuincyLarson I am great. You? Hopefully, I and @Zeko369 will get this PR soon.


try {
await chapter.save();
res.status(201).json(chapter);
} catch (e) {
if (e.code === '23503' && e.message.includes('foreign key constraint')) {
Copy link
Member

Choose a reason for hiding this comment

The 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

@Zeko369 Zeko369 merged commit 259cf4f into freeCodeCamp:master Dec 15, 2019
return res.status(404).json({ message: 'creator not found' });
}
}

res.status(500).json({ error: e });
Copy link

@imVinayPandya imVinayPandya Dec 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it good idea to throw full error stack in api response?

});

try {
await chapter.save();
res.status(201).json(chapter);
} catch (e) {
if (e.code === '23503' && e.message.includes('foreign key constraint')) {
if (e.detail.includes('location')) {

Choose a reason for hiding this comment

The 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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants