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

Constructs explanation #64

Open
ssow opened this issue Mar 26, 2020 · 2 comments
Open

Constructs explanation #64

ssow opened this issue Mar 26, 2020 · 2 comments

Comments

@ssow
Copy link

ssow commented Mar 26, 2020

Greetings,

Would you mind explain/comment the following construct, which I'm not familiar with :

public async SignUp(userInputDTO: IUserInputDTO): Promise<{ user: IUser; token: string }>
export default ({ mongoConnection, models }: { mongoConnection; models: { name: string; model: any }[] })
export default mongoose.model<IUser & mongoose.Document>('User', User);

Do they require a special package ? Where can I find a documentation ?
Thank you

@santiq
Copy link
Owner

santiq commented Mar 26, 2020

Sure.
This project is written in typescript, so

public async SignUp(userInputDTO: IUserInputDTO): Promise<{ user: IUser; token: string }> {
...
}

There we are using typescript to type the response of the function, which will be Promise where it holds a value that is an object with the shape of { user, token }

In plain javascript would be something similar to this

async SignUp(userInputDTO) {
 ...
};

======

Here we are using Mongoose, which is a node.js package to connect to Mongodb and an ODM

export default mongoose.model<IUser & mongoose.Document>('User', User);

The part where we are using <> is called Generics it is used to give a Type to the result of the database's results.

Then the IUser & mongoose.Document is telling that the results of the databases will be an object which type is the Intersection of the interface IUser and the interface mongoose.Document

In practice, the mongoose.Document means that the result will have the property _id and all of the properties of the interface IUser

======

This is a little trickier

 export default ({ mongoConnection, models }: { mongoConnection; models: { name: string; model: any }[] })

We are exporting an object, which has this shape: { mongoConnection, models }
but also defining the type on the same line with the : operator.
since models is an array of objects, I'm saying that each object of the array will have the shape { name: string, model: any}

@ssow
Copy link
Author

ssow commented Mar 27, 2020

Very kind of you and cristal clear.
May i ask you a last explanation regarding the following type declaration and the purpose of the Models namespace please ?

namespace Models {
export type UserModel = Model<IUser & Document>;
}

Thank you.

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

No branches or pull requests

2 participants