0:00 All right, so now we wanna go ahead and choose which 0:04 database we're using by an environment variable. 0:07 So in our root.env, let's set this to a 0:11 new value called database, where we can set this to 0:14 Postgres or MongoDB. In our case, let's say we 0:18 wanna choose MongoDB here. And so what we're now gonna 0:22 do is go back into our product module, where 0:26 right now we're hard coding the implementation to the 0:30 Drizzle product repository. But instead, what we can do is actually 0:34 refactor this to use useFactory. 0:38 And if you recall, useFactory allows us to inject 0:41 dependencies 0:42 inside of this callback function. 0:43 So we can inject any dependency we have available in NestJS, 0:48 and that includes our config service, 0:51 which allows us to read environment variables. 0:55 And so in this useFactory, we also need to add the inject 0:59 property and add the config service provider. 1:03 But now here in useFactory, we can actually determine which 1:07 provider we want to return 1:10 based off of our configuration value. 1:13 So we can check to see return 1:17 this.config service, in this case, just config service, 1:21 .get. If database is set and it is 1:25 equal to MongoDB, then we will 1:28 choose our Mongo product repository 1:33 as the provider here. Otherwise, we'll go ahead and use 1:36 our existing Drizzle product repository. 1:39 And so with this in place, we now have the correct provider 1:43 that is specified. The way we've configured this won't 1:46 actually work 1:47 right now because we're returning still the raw 1:51 class value here, which only works when we use the use 1:55 class implementation. In useFactory, we expect to return 1:59 the actual instance itself. So in order to support this, 2:03 we need to declare both the Drizzle product 2:06 repository and the Mongo product repository as their own 2:10 providers in the providers array.
The rest of the transcript is for members.
Sign in