Multitenancy in NestJS + Mongoose

Sdílet
Vložit
  • čas přidán 5. 07. 2024
  • Learn how to handle multitenancy in NestJS with Mongoose in this easy-to-follow tutorial.
    Where we learn how to connect to the correct dedicated tenant database based on the tenant making the request.
    Github Repo: github.com/charbelh3/NestJs-M...
    Buy me a coffee at : buymeacoffee.com/computerix
    Timestamps:
    ---------------------
    0:00- Single-Tenant vs Multi-Tenant
    2:13- Our Multi-Tenant Application
    4:45- Explaining the boilerplate code
    7:11- Which database is NestJs using?
    10:18- Identify the correct Tenant
    11:33- Creating the Tenants Middleware
    14:15- Accessing the correct database (Approach 1)
    19:05- The issue with this approach
    19:53- Creating the TENANT_CONNECTION Provider
    26:25- Understanding the Request Scope
    31:00- Creating the Product Model Provider
    32:45 - Accessing the correct database (Approach 2)
    38:45- Summary

Komentáře • 56

  • @user-rv8by4qc8l
    @user-rv8by4qc8l Před 2 měsíci +4

    This is really a great video. Can you try to implement a authentication module as you said in the video

    • @Computerix
      @Computerix  Před 16 dny

      Here you go : czcams.com/video/7PSboNf4RuQ/video.htmlsi=3KUn9tRxLTxJ_x9o

  • @sergeyplotnikov4303
    @sergeyplotnikov4303 Před 6 měsíci +2

    Thank you very much for this highly valuable content!!! Second approach is awesome.

  • @Viraj-jh6pk
    @Viraj-jh6pk Před 3 měsíci

    this is the most impressive video i have seen on the topic of multitenency with nestjs and mongodb . thank you so much

  • @RishiDeorukhkar
    @RishiDeorukhkar Před 3 měsíci +2

    Amazing tutorial, and great content !! looking forward for more 😄

  • @ycoda
    @ycoda Před 5 měsíci +1

    Very useful video! Thank you for the knowledge

  • @hassanmhassan5500
    @hassanmhassan5500 Před 5 měsíci

    Amazing!, wonderful video I really enjoyed it
    keep this good work bro

  • @mariusdanyelbuzatu4075
    @mariusdanyelbuzatu4075 Před 2 měsíci

    In this case to improve is good to work with redis

  • @jean8135
    @jean8135 Před měsícem

    The "mustard" database haha

  • @emirsalihovic6616
    @emirsalihovic6616 Před 3 měsíci

    Well done sir.

    • @Computerix
      @Computerix  Před 3 měsíci

      Thanks!

    • @emirsalihovic6616
      @emirsalihovic6616 Před 3 měsíci

      Can you please cover the topic of dockerizing a nest app and the cloud usage suggestions etc, that would be a great video, thank you!@@Computerix

  • @mohsensareminia2988
    @mohsensareminia2988 Před 2 měsíci +1

    Thank you. Why didnt you use nest durable providers?

    • @recitdafrique
      @recitdafrique Před 2 měsíci

      Hello, Please, have you implemented such an architecture that connects several models with mongoose or prisma ?

  • @juhandvan
    @juhandvan Před měsícem

    I find this lesson a bit beyond my understanding (from 26:30) . I hope there will be another lesson in the future that is a little easier to understand.
    Thank so much

    • @Computerix
      @Computerix  Před měsícem +1

      I'm gonna make a video covering request scope vs the default scope, hopefully it helps you out!

    • @juhandvan
      @juhandvan Před měsícem

      @@Computerix That sounds interesting.

  • @empowerurjourney
    @empowerurjourney Před 3 měsíci

    Thank you so much for this great content. I have one question: what if we have more then one model, should we create a provider for each model?

    • @Computerix
      @Computerix  Před 3 měsíci +1

      Thanks for your comment! Yes that's correct.

  • @abdulrahmanarmashi6954
    @abdulrahmanarmashi6954 Před 2 měsíci

    Thank you so much for this great video. I have
    one question: what if we have more then one
    model, should we create a provider for each
    model or is there any way to make the provider generic and accepte any model ?

    • @Computerix
      @Computerix  Před 2 měsíci

      Tokens that are injected that represent a provider are unique, so as a first thought I'd say yes, you need to create a provider for each model. I haven't really thought of workarounds or of ways to make something generic work. If you have done that in the past day, let us know!

  • @pramodpatil2860
    @pramodpatil2860 Před 5 měsíci +1

    Excellent tutorials!! Could you please help us to have lookup query on multiple databases? I need to join tenant DB with master and get the result

    • @Computerix
      @Computerix  Před 5 měsíci

      Sadly, as per the mongoDb documentation (www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/), lookup only works on collections within the same database.
      However, you can create multiple queries (each query could be on a seperate database) and merge the results yourself.
      To fetch results from the master database :
      1- Simply register the model like we normally do using MongooseSchema.forFeature. (This works, since in app module our default connection is pointing to the master DB)
      2- Inject the model using the normal @InjectModel(). This model will point to a collection in the master DB.
      To fetch results from the tenant specific database, inject the custom tenant model provider like in the video :)
      ..and then manually merge the results

  • @HamoudyMulti
    @HamoudyMulti Před 2 měsíci +1

    thank you for the video! Question, u said that the tenantId comes from the header, but shouldnt it be stored at the users table DB level? does it depend on requirements or it always comes from header of the front end? Thanks in advance

    • @Computerix
      @Computerix  Před 2 měsíci +1

      It depends. If you are trying to fetch some data specific to a tenant (so it exists inside the tenant specific Database) for example a list of products being sold by a tenant, you need the frontend to specify the tenantId in the headers (Also it's a best practise to sign each user token with a tenant-specific secret for security purposes) that way, you know where to connect and what data to fetch. However, if a user is trying to login, they send their credentials only. To tell which tenant they belong to, you can store the tenantId inside the users table.
      So in summary, it depends on the requirements, but for most cases, the frontend would need to send the tenandId in the headers.
      Hope this helps :)

    • @HamoudyMulti
      @HamoudyMulti Před 2 měsíci

      @@Computerix Thank you so much for the reply! I just had a meeting with my company and we decided to go with the tenantId in the headers approach. u helped me alot and this video will help me through implementing it in nestjs, making my task easier :D (please dont remove this video lol)
      1 thing we were not sure about is how we can have 1 front end server deal with 2 different domain urls, my company just started implementing custom solutions so we are trying our best, do u know how its done? Thanks alot!

    • @Computerix
      @Computerix  Před 2 měsíci

      @HamoudyMulti hmm, can you explain your question more?

    • @HamoudyMulti
      @HamoudyMulti Před 2 měsíci

      @@Computerix I have another question after going through ur video. im currently using a RDS with MS SQL and sequelize ORM. I couldnt apply most of the things in the video, as sequelize doesn't allow u to change database within same connection like in mongoose (in ur videos its the useDB method).
      do u think its a good idea to create all the connections at once when initiliazing the application for all tenants? or initialize the connection on each request for a given tenant? the number of tenants is not that high (3 or 4 max), although im not sure what is the best practice. thanks again for ur great help!

    • @Computerix
      @Computerix  Před 2 měsíci

      ​@@HamoudyMulti I haven't used sequelize before, so i'm not very familiar with it. But what you can do, is check if you have an existing connection to a certain tenant DB, if that's the case, use it. If not, create it and use it for the current and future requests for that tenant.
      The key, is managing the connections in a clean way. So make sure to reuse existing connections :))

  • @omarps6622
    @omarps6622 Před 4 měsíci

    Thank you bro , Your content and method of explanation are more than perfect❤
    I also wish if you could share this project with us it would be a great gift for me 🙏

    • @Computerix
      @Computerix  Před 4 měsíci

      I'm glad ! The link is in the description : github.com/charbelh3/NestJs-Multi-Tenancy

    • @omarps6622
      @omarps6622 Před 4 měsíci

      @@Computerix you are my angel😍

  • @recitdafrique
    @recitdafrique Před 2 měsíci

    Thank you for this tutorial, I have a project where I have to use prismaORM to implement multitenancy, can you make another video using prismaORM or provide me with a possible solution. Thanks

  • @JoyceWen-py1pb
    @JoyceWen-py1pb Před měsícem

    Hello,
    Your videos have been incredibly helpful to me. I was wondering if we could eventually have the instructional video you mentioned on unified identity verification. I really need it. Thank you, but unfortunately, I can't sponsor you from my region. I apologize.

    • @Computerix
      @Computerix  Před měsícem

      I'm glad my videos are helpful!
      I am going to create an authentication video for multitenancy soon, meanwhile feel free to watch this authentication video that I uploaded a week ago : czcams.com/video/h-68sqxycJY/video.htmlsi=VeiWno8dL0UA2ike
      It's going to be similar, except we're gonna be using different secret keys for different tenants (these keys would be stored in the database)

    • @JoyceWen-py1pb
      @JoyceWen-py1pb Před měsícem

      @@Computerix Okay, looking forward to your new videos.

  • @thehaughton8936
    @thehaughton8936 Před 3 měsíci

    Amazing tutorial, it helped me a lot, but I have a question, you did it multi-tenant in multiple databases, can it also be done but for different collections? Like products_tenant_id in the same data base, or do you know where I can find information about it?
    btw new sub

    • @Computerix
      @Computerix  Před 3 měsíci

      Thanks for the feedback! I think it's best if you avoid creating a collection per tenant. Instead go for 1 single database and 1 products collection for all tenants, and create a field named "tenantId" inside the same collection to know which document belongs to which tenant, if you don't want to use the database per tenant approach. (refer to stackoverflow.com/questions/2748825/what-is-the-recommended-approach-towards-multi-tenant-databases-in-mongodb and web.archive.org/web/20140812091703/support.mongohq.com/use-cases/multi-tenant.html)

  • @shadcnuisnipset
    @shadcnuisnipset Před 2 měsíci

    great tutorial, I really liked it. please can you implement authentication.

    • @Computerix
      @Computerix  Před 2 měsíci +1

      I'm planning on doing so sometime this year!
      Just to be clear, authentication handling, specifically in multitenancy, correct?

    • @shadcnuisnipset
      @shadcnuisnipset Před 2 měsíci +1

      @@Computerix yes exactly, registration, login, interaction between the User and Product table for example to know which user created a particular product

  • @linoypappachan4272
    @linoypappachan4272 Před 5 měsíci

    Amazining tutorial, could u pls share source code. Keep it up.

    • @Computerix
      @Computerix  Před 5 měsíci +1

      Thank you for the comment!
      Here is the code: github.com/charbelh3/NestJs-Multi-Tenancy

  • @amrhussien4334
    @amrhussien4334 Před 6 měsíci

    Thank you, engineer. Great work.
    Hiw can I communicate with you please ❤

    • @Computerix
      @Computerix  Před 6 měsíci

      Hello! I was thinking of making a discord community server.. but at a later time. In the meantime, feel free to tell me your concerns here :)

    • @amrhussien4334
      @amrhussien4334 Před 6 měsíci

      I have one project I want to discuss it with you via another channel. Appreciate if you can tell me how to reach you. I added my contact here many times but it's deleted
      Thank you

    • @amrhussien4334
      @amrhussien4334 Před 6 měsíci

      ​@Computerix I have one project I want to discuss it with you, So please tell how to contact you. I am trying to put my contact here but it always deleted

  • @maintenance_informatique
    @maintenance_informatique Před 4 měsíci +1

    Thank you very much for this excellent video. How can I get in touch with you? by email or discord or linkedin

    • @Computerix
      @Computerix  Před 4 měsíci

      Thank you! This is my linkedin: www.linkedin.com/in/charbel-el-helou-6523a5200/

  • @KhodeCamp
    @KhodeCamp Před 2 měsíci

    Thank you for your work hard. Can you help make more videos of multi-tenant with SQL (Postgres) please