Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbconnection #32

Merged
merged 8 commits into from
Aug 26, 2021
Merged

Dbconnection #32

merged 8 commits into from
Aug 26, 2021

Conversation

waltervan00
Copy link
Collaborator

Personal observations

I would like to bring attention to api-router.ts where the database models are meant to be generated.

Models generated within this file are bound to individual api Handlers through the bind method. Hence for each ApiRequestHandler type, the model can be accessed through this.<model property in api-router.ts>.

Is this an intended outcome? For each new model required it would have to update this class. So far we have a User model and nothing else. But in terms of extending the DB model for further collections, how do you think this will interact?

@waltervan00 waltervan00 linked an issue Aug 25, 2021 that may be closed by this pull request
3 tasks
@chomosuke
Copy link
Owner

I think the answer is Yes. Looking at server.ts, all routes starting with /api will be routed to ApiRouter. After that ApiRouter will direct each route to their corresponding handler, and presumably pass all the models to each handler as well.

this.<model property in api-router.ts> wouldn't work for now I think because userModel at least in ApiRouter is private.

However it's probably a good idea to get the author of this code which is @shangzhel i'm assuming to clarify and confirm on this issue.

Copy link
Owner

@chomosuke chomosuke left a comment

Choose a reason for hiding this comment

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

After some test, I've successfully made the collection appear on my Compass by inserting an document into the collection. I was expecting the collection to appear straight away after db.model but I guess this works too.

Everything else looks good to me too.

@shangzhel
Copy link
Collaborator

shangzhel commented Aug 25, 2021

But in terms of extending the DB model for further collections, how do you think this will interact?

Indeed it is not perfect in terms of extensibility. The Mongoose models could be exposed through property getters, and the getters themselves located in an interface so that the ApiRequestHandlers do not depend directly on the ApiRouter class. For example:

In db-provider.ts

import { Model } from 'mongoose';

interface IDbProvider {
  get User(): Model<IUser>;
}

In api-router.ts

import { IDbProvider } from './db-provider';

class ApiRouter implements IDbProvider {
  // ...

  get User() {
    return this.userModel;
  }
}

I was going to make this change when I set up testing because the routes need to be testable without actually making a database connection in test cases.

@waltervan00
Copy link
Collaborator Author

But in terms of extending the DB model for further collections, how do you think this will interact?

Indeed it is not perfect in terms of extensibility. The Mongoose models could be exposed through property getters, and the getters themselves located in an interface so that the ApiRequestHandlers do not depend directly on the ApiRouter class. For example:

In db-provider.ts

import { Model } from 'mongoose';

interface IDbProvider {
  get User(): Model<IUser>;
}

In api-router.ts

import { IDbProvider } from './db-provider';

class ApiRouter implements IDbProvider {
  // ...

  get User() {
    return this.userModel;
  }
}

I was going to make this change when I set up testing because the routes need to be testable without actually making a database connection in test cases.

I could make these changes and make a new commit if you'd like.

@waltervan00
Copy link
Collaborator Author

@shangzhel Actually, considering that you'll implement that feature in CI. I will refrain from making these changes else we run into conflicts.

@shangzhel shangzhel mentioned this pull request Aug 25, 2021
@chomosuke chomosuke added this to the Sprint 1 milestone Aug 25, 2021
@shangzhel
Copy link
Collaborator

Let's leave this PR open until after the CI PR gets merged, then we can write unit tests before merging.

@shangzhel shangzhel added the enhancement New feature or request label Aug 25, 2021
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

Needs a few fixes.

company: string;
fields: ICardField[];
tags: ObjectId[];
image?: Buffer;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move this property up to before the arrays.

name: { type: String, require: true },
phone: { type: String, require: true },
email: { type: String, require: true },
image: Buffer,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move this element down to before the fields value.

}

export const tagSchema = new Schema<ITag>({
tagName: String,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename property to match ITag interface definition.


export const tagSchema = new Schema<ITag>({
tagName: String,
colour: String,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above, rename property to match interface definition.

@shangzhel shangzhel self-requested a review August 26, 2021 07:54
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

Just one change, otherwise it looks good.

company: { type: String, require: true },
image: Buffer,
fields: {
type: [new Schema<ICardField>({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move the initialization of the ICardField schema into another constant above like const cardFieldSchema = .... It does not need to be exported.

@shangzhel
Copy link
Collaborator

Don't know how your code got past the linter pre-commit hook, but I can fix it for you.

@shangzhel shangzhel mentioned this pull request Aug 26, 2021
3 tasks
@shangzhel shangzhel self-requested a review August 26, 2021 08:12
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@shangzhel shangzhel merged commit 3b82cc1 into master Aug 26, 2021
@chomosuke chomosuke deleted the dbconnection branch August 26, 2021 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hook Mongo Cluster onto Server backend
4 participants