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
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 🙏
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
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 :)
@@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 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!
@@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 :))
I have a question, for now the connection string is having multiple dbs and useDb is being used to switch the dbs. But, what about the case in which the whole database instance is different, for example 5 dbs on 5 different EC2 machines. How to approach this situation?
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
@@Computerix yes exactly, registration, login, interaction between the User and Product table for example to know which user created a particular product
Hi, thanks for the video. I think it would be useful to make it a durable provider and create dependency tree once per tenant. Did you look at NestJS-CLS (Continuation Local Storage, based on Async Hooks)? I think it's a good altrnative approach, which also works with CQRS module (request scope doesn't).
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 ?
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!
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
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
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)
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.
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 : ruclips.net/video/h-68sqxycJY/видео.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)
How to solve If i want to read all products from multiple tenants dbs and show them on main website landing page like udemy or other elearning platform.
@@ChetanSharma-jy8om If you have such a use case, it's better to keep a copy of the newly created products in your main database (the default mongooseModule that you register), that way you fetch them all from the same DB
@@ComputerixOne last thing from your side , As you are creating dynamic dbs in mongo db Is there as such limit for them.Because I am using managed mongodb by digital ocean
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
@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
Thank you very much for this highly valuable content!!! Second approach is awesome.
Thanks for the feedback!
Amazing tutorial, and great content !! looking forward for more 😄
Thank you so much!
this is the most impressive video i have seen on the topic of multitenency with nestjs and mongodb . thank you so much
Thank you so much!!
Very useful video! Thank you for the knowledge
You're welcome !
This is really a great video. Can you try to implement a authentication module as you said in the video
Here you go : ruclips.net/video/7PSboNf4RuQ/видео.htmlsi=3KUn9tRxLTxJ_x9o
Amazing!, wonderful video I really enjoyed it
keep this good work bro
Thank you!
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
I'm gonna make a video covering request scope vs the default scope, hopefully it helps you out!
@@Computerix That sounds interesting.
Well done sir.
Thanks!
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
Thank you. Why didnt you use nest durable providers?
Hello, Please, have you implemented such an architecture that connects several models with mongoose or prisma ?
@@mohsensareminia2988we could have done that! Good point
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 🙏
I'm glad ! The link is in the description : github.com/charbelh3/NestJs-Multi-Tenancy
@@Computerix you are my angel😍
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
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 :)
@@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!
@HamoudyMulti hmm, can you explain your question more?
@@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!
@@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 :))
I have a question, for now the connection string is having multiple dbs and useDb is being used to switch the dbs. But, what about the case in which the whole database instance is different, for example 5 dbs on 5 different EC2 machines. How to approach this situation?
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
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
ruclips.net/video/qR05wWeQ2OI/видео.htmlsi=H40Y7u4bZ12ugdHr
In this case to improve is good to work with redis
great tutorial, I really liked it. please can you implement authentication.
I'm planning on doing so sometime this year!
Just to be clear, authentication handling, specifically in multitenancy, correct?
@@Computerix yes exactly, registration, login, interaction between the User and Product table for example to know which user created a particular product
Hi, thanks for the video. I think it would be useful to make it a durable provider and create dependency tree once per tenant. Did you look at NestJS-CLS (Continuation Local Storage, based on Async Hooks)? I think it's a good altrnative approach, which also works with CQRS module (request scope doesn't).
@@j3bb9z I haven't looked in to it, but the durable provider suggestion is a good one !
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 ?
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!
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?
Thanks for your comment! Yes that's correct.
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
ruclips.net/video/qR05wWeQ2OI/видео.htmlsi=H40Y7u4bZ12ugdHr
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
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)
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.
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 : ruclips.net/video/h-68sqxycJY/видео.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)
@@Computerix Okay, looking forward to your new videos.
How to solve If i want to read all products from multiple tenants dbs and show them on main website landing page like udemy or other elearning platform.
@@ChetanSharma-jy8om If you have such a use case, it's better to keep a copy of the newly created products in your main database (the default mongooseModule that you register), that way you fetch them all from the same DB
@@ComputerixOne last thing from your side , As you are creating dynamic dbs in mongo db Is there as such limit for them.Because I am using managed mongodb by digital ocean
The "mustard" database haha
Amazining tutorial, could u pls share source code. Keep it up.
Thank you for the comment!
Here is the code: github.com/charbelh3/NestJs-Multi-Tenancy
Thank you, engineer. Great work.
Hiw can I communicate with you please ❤
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 :)
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
@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
Most underrated channel on dev youtube. Keep it up!
Thanks 😁
Thank you very much for this excellent video. How can I get in touch with you? by email or discord or linkedin
Thank you! This is my linkedin: www.linkedin.com/in/charbel-el-helou-6523a5200/
Thank you for your work hard. Can you help make more videos of multi-tenant with SQL (Postgres) please
ruclips.net/video/qR05wWeQ2OI/видео.htmlsi=H40Y7u4bZ12ugdHr