Setup MongoDB With Nest.js | Simple + Scaleable
Вставка
- Опубліковано 25 лют 2021
- Github: github.com/mguay22/nestjs-mongo
Docs: docs.nestjs.com/techniques/mo...
Get my highly-rated Udemy courses at a discount here: michaelguay.dev/udemy/ - Наука та технологія
Get my highly-rated Udemy courses at a discount here: michaelguay.dev/udemy/
Amazing Michael! Good job!
Hi Michael, A very straight forward tutorial!
Thank you very much!
Amazing and so clean and helpful video! Thank you so much!🙏
Thanks, just what I needed, subscribed!
Thanks for good example of extensible repo
You helped me a lot! Thanks)
Thank you!
Whats the vscode theme you are using ? Thanks (L
I was following along but after you wrote the code for module and repository suddenly the code for service and controller is already there. Would have been better if we could follow along through that as well.
hey bro do you know how to type the projection? this is as far as i got
async find(
entityFilterQuery: FilterQuery,
projection?: Partial<
Record
>,
): Promise {}
the extends part isn't working :/
Hi! New subscriber here! I'm new to Node and Typescript and NestJS and MongoDB. Loving your NestJS related vids!
Amazing to find very good tutorial like this with application of some best practices such as creating a Repository Layer.
Hope you could provide or help on how to make a generic/abstract class for Repository?
Also, if you could provide tutorial on Atomic Transaction Management in MongoDB/Mongoose in NestJS way.
Thank you for the kind works Jerome. Sure, happy to make some videos on those topics and I think that is a great idea. Keep an eye out for that later this month.
HI Michael, thanks for the video. There is one question i have: In the route handlers in the controller file, the return type is a Promise. Which part of the code waits for the Promise to get resolved? You can't really return a Promise to a client like postman.. Just trying to understand how this really works.
Great question, if we look at the Nest.js source code I would imagine it's calling await on our route (or maybe it's Express under the hood?) - this is a good exercise
From where did you learn NestJs?
Great video. Thx
Thank you for the greate video. Could you please complete the delete user endpoint?
Can we use this user schema in other modules or do we have to describe schemas in shared modules?
You can certainly use it in other modules. You really only need to initialize it one module. If you use it in multiple places, it may be cleaner to instantiate it in a shared module like you suggest.
Hi Michael, Thanks for the great content. Really helping me with rapidly learning some NestJS.
Can I just ask though, is there any difference between what you are calling a repository and what NestJS calls a service?
I get that the repository is named after the pattern but is it simply a case of (more sensible on your part) naming conventions?
The reason I ask is that one of the main selling points of Nest for me is to be able to standardise some of the coding practices amongst my team.
As such I'm hoping to take advantage of the generators provided by Nest. However, when you generate a resource it automatically creates a service rather than a repository.
Also, I seem to be getting a typescript error using `FilterQuery` within my repository:
`Argument of type 'FilterQuery' is not assignable to parameter of type 'FilterQuery'.`
Have you ever seen that before?
Hey, Daniel thanks for watching. For me, the difference between a repository and a service is their responsibilities.
The repository is the only class that has a handle to the underlying DB implementation so it can perform CRUD operations on it. Importantly, it abstracts away the underlying implementation. If we swapped MongoDB out for MySQL in the repository, the users of the repository wouldn't know the difference because they simply call the public methods on it.
A service is a more generic term that can encompass many business operations.
And I believe you need to pass the UserDocument to the FilterQuery as Mongo expects the type to be a schema.
@@mguay Did you remove my response or am I going crazy?
@@DazDotOne I got a notifiation for it, but when I looked I didn't see it. I didn't remove it, promise!
@@mguay Your approach is very similar of what I used to do in symfony. Especially when you talk (in the video) about create a generic crud and extends it to every others repositories like "userRepository extends abstractRepository". I'm just learning nestJs but I guess it's what you meant. Great video btw :)
Someone also have an error on controller class? the userModle its define to contain a Model but the method return a userFilterQuery with user instance.
same I get error
You need to pass the UserDocument instead of User like FilterQuery
@@olok95ify thanks !
This tutorial is really good.
Can you please make some videos for Nest.js and web sockets?
Thank you.
Great idea. That will be my next video. Thanks!
@@mguay Thank you.
Que Guay Miguel
when I do a post request with the json data, its returning
{
"statusCode": 404,
"message": "Cannot POST /",
"error": "Not Found"
}
Is your server running? Do you see any errors?
@@mguay server running, yeah. Error above (postman)
It sounds like an issue with the controller. Did you register it in the controllers array in the module? And do the decorators match the request you are sending? You should be able to see it in the Nest logs when you start the app.. something like this:
Mapped {/users/search, GET} route
@@mguay uhm I only installed it and tried to run it, so i guess no
Wire UserModule in app.module.ts in import's array and import it at the top in the same file from users.module
A bit similar to how things are done in ASP Dotnet Core
I really don't get the purpose of adding forFeature and defining a *{ name: **User.name**, schema: UserSchema }* why not just *{ schema: UserSchema }* ?
Hi, the MongooseModule requires that you provide a name for the Model being defined:
export declare type ModelDefinition = {
name: string;
schema: any;
collection?: string;
discriminators?: DiscriminatorOptions[];
};
because Nest.js needs to identify each model by some kind of token (and model name is part of the token I guess). You can later inject model with this identifier in other providers.
Sir, Please make a video on gRPC with Nest.js.
Great idea!
Hi Michael, I have a code error in usersFilterQuery: No overload matches this call.
Overload 1 of 3, '(callback?: Callback): Query', gave the following error.
Argument of type 'FilterQuery' is not assignable to parameter of type 'Callback'.
Type 'FilterQuery' provides no match for the signature '(error: NativeError, result: (User & Document & { _id: any; })[]): void'.
Overload 2 of 3, '(filter: FilterQuery, callback?: Callback): Query', gave the following error.
Argument of type 'FilterQuery' is not assignable to parameter of type 'FilterQuery'.
Overload 3 of 3, '(filter: FilterQuery, projection?: any, options?: QueryOptions, callback?: Callback): Query', gave the following error.
Argument of type 'FilterQuery' is not assignable to parameter of type 'FilterQuery'.
Looks more complicated here than in express
true nestjs is pretty tough to learn
very difuse the visual content
Hello, Can you do a video setup mysql db? But can you use Nestjs without TypeOrm ? I really want to know how to setup a program using sql statement instead of type orm ? I've seen so many videos with TypeOrm .
Thanks