-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added two other varieties of module registration
- Loading branch information
1 parent
31441c5
commit eb71a1e
Showing
11 changed files
with
192 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {DynamicModule} from "@nestjs/common"; | ||
import {CART_REPO, ORDER_REPO, PRODUCT_REPO} from "./index"; | ||
import {MemoryCartRepository} from "./cart.repo"; | ||
import {InMemoryOrderRepository, InMemoryProductRepository} from "./fake"; | ||
import {ProductTemplate} from "../types"; | ||
|
||
|
||
export class MemoryModule { | ||
static forTests(products: ProductTemplate[]): DynamicModule { | ||
return { | ||
module: MemoryModule, | ||
providers: [ | ||
|
||
{ | ||
provide: PRODUCT_REPO, | ||
useValue: new InMemoryProductRepository(products) | ||
}, | ||
{ | ||
provide: ORDER_REPO, | ||
useClass: InMemoryOrderRepository, | ||
}, | ||
{ | ||
provide: CART_REPO, | ||
useClass: MemoryCartRepository, | ||
} | ||
], | ||
exports: [PRODUCT_REPO, ORDER_REPO, CART_REPO] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {MongoClient} from "mongodb"; | ||
import {DynamicModule} from "@nestjs/common"; | ||
import {CART_REPO, ORDER_REPO, PRODUCT_REPO} from "./index"; | ||
import {MongoDBProductRepository} from "./product.repo"; | ||
import {MongoDBOrderRepository} from "./order.repo"; | ||
import {MemoryCartRepository} from "./cart.repo"; | ||
|
||
export class MongoDBModule { | ||
static forRoot(uri: string): DynamicModule { | ||
|
||
return { | ||
module: MongoDBModule, | ||
providers: [ | ||
{ | ||
provide: "storeDB", | ||
useFactory: async () => { | ||
const mongo = await new MongoClient(uri, { | ||
connectTimeoutMS: 100, | ||
serverSelectionTimeoutMS: 100, | ||
socketTimeoutMS: 100 | ||
}).connect(); | ||
|
||
return mongo.db("store"); | ||
}, | ||
}, | ||
{ | ||
provide: PRODUCT_REPO, | ||
useClass: MongoDBProductRepository | ||
}, | ||
{ | ||
provide: ORDER_REPO, | ||
useClass: MongoDBOrderRepository, | ||
}, | ||
{ | ||
provide: CART_REPO, | ||
useClass: MemoryCartRepository, | ||
} | ||
], | ||
exports: [PRODUCT_REPO, ORDER_REPO, CART_REPO] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module} from "@nestjs/common"; | ||
import {CartController, CheckoutController, OrderController, ProductController} from "./controllers"; | ||
import {MongoDBModule} from "./adapters/mongodb.module"; | ||
|
||
@Module({ | ||
imports: [MongoDBModule.forRoot(`mongodb://root:[email protected]`)], | ||
controllers: [CartController, ProductController, OrderController, CheckoutController] | ||
}) | ||
export class AppModuleOverrides { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {DynamicModule, Module} from "@nestjs/common"; | ||
import {CartController, CheckoutController, OrderController, ProductController} from "./controllers"; | ||
|
||
|
||
@Module({}) | ||
export class AppModuleWithRegister { | ||
static register(adapters: DynamicModule) { | ||
return { | ||
imports: [adapters], | ||
controllers: [CartController, ProductController, OrderController, CheckoutController], | ||
module: AppModuleWithRegister | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,13 @@ import { MongoClient } from "mongodb"; | |
import { MongoDBOrderRepository } from "./adapters/order.repo"; | ||
import { MongoDBProductRepository } from "./adapters/product.repo"; | ||
import {NestFactory} from "@nestjs/core"; | ||
import {AppModule} from "./app.module"; | ||
import {AppModuleInversionOfControl} from "./app.module.ioc"; | ||
import {AppModuleOverrides} from "./app.module.overrides"; | ||
import {AppModuleWithRegister} from "./app.module.register"; | ||
import {MongoDBModule} from "./adapters/mongodb.module"; | ||
|
||
async function startServer() { | ||
// @ts-ignore | ||
async function startServerIoC() { | ||
const mongo = await new MongoClient( | ||
`mongodb://root:[email protected]?retryWrites=true&writeConcern=majority` | ||
).connect(); | ||
|
@@ -13,11 +17,26 @@ async function startServer() { | |
const productRepo = new MongoDBProductRepository(db); | ||
const orderRepo = new MongoDBOrderRepository(db); | ||
|
||
const app = await NestFactory.create(AppModule.register(productRepo, orderRepo)) | ||
const app = await NestFactory.create(AppModuleInversionOfControl.register(productRepo, orderRepo)) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
void startServer(); | ||
// @ts-ignore | ||
async function startServerOverrides() { | ||
const app = await NestFactory.create(AppModuleOverrides) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
async function startServerRegister() { | ||
const app = await NestFactory.create(AppModuleWithRegister.register( | ||
MongoDBModule.forRoot(`mongodb://root:[email protected]`) | ||
)); | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
void startServerRegister(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,68 @@ | ||
import {ProductTemplate} from "./types"; | ||
import {Test} from "@nestjs/testing"; | ||
import {InMemoryOrderRepository, InMemoryProductRepository} from "./adapters/fake"; | ||
import {AppModule} from "./app.module"; | ||
export async function createTestingModule(products: ProductTemplate[] = []) { | ||
import {AppModuleInversionOfControl} from "./app.module.ioc"; | ||
import {AppModuleOverrides} from "./app.module.overrides"; | ||
import {ORDER_REPO, PRODUCT_REPO} from "./adapters"; | ||
import {MongoDBModule} from "./adapters/mongodb.module"; | ||
import {Module} from "@nestjs/common"; | ||
import {AppModuleWithRegister} from "./app.module.register"; | ||
import {MemoryModule} from "./adapters/memory.module"; | ||
export async function createTestingModuleWithIoC(products: ProductTemplate[] = []) { | ||
const productRepo = new InMemoryProductRepository(products); | ||
const orderRepo = new InMemoryOrderRepository(); | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModule.register(productRepo, orderRepo)], | ||
imports: [AppModuleInversionOfControl.register(productRepo, orderRepo)], | ||
}) | ||
.compile(); | ||
|
||
const nest = testingModule.createNestApplication(); | ||
nest.enableCors({origin: "*"}); | ||
await nest.init(); | ||
return {nest, orderRepo, productRepo}; | ||
} | ||
} | ||
|
||
export async function createTestingModuleWithOverrides(products: ProductTemplate[] = []) { | ||
const productRepo = new InMemoryProductRepository(products); | ||
const orderRepo = new InMemoryOrderRepository(); | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModuleOverrides], | ||
}) | ||
.overrideModule(MongoDBModule).useModule(NopModule) // this doesn't actually do anything, MongoDB needs to be available for connection even though we don't use it | ||
.overrideProvider(PRODUCT_REPO).useValue(productRepo) | ||
.overrideProvider(ORDER_REPO).useValue(orderRepo) | ||
.compile(); | ||
|
||
const nest = testingModule.createNestApplication(); | ||
nest.enableCors({origin: "*"}); | ||
await nest.init(); | ||
return {nest, orderRepo, productRepo}; | ||
} | ||
|
||
export async function createTestingModuleWithRegister(products: ProductTemplate[] = []) { | ||
|
||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModuleWithRegister.register(MemoryModule.forTests(products))], | ||
}) | ||
.compile(); | ||
|
||
const productRepo: InMemoryProductRepository = testingModule.get(PRODUCT_REPO); | ||
const orderRepo: InMemoryOrderRepository = testingModule.get(ORDER_REPO); | ||
|
||
const nest = testingModule.createNestApplication(); | ||
nest.enableCors({origin: "*"}); | ||
await nest.init(); | ||
return {nest, orderRepo, productRepo}; | ||
} | ||
|
||
// export const createTestingModule = createTestingModuleWithOverrides; | ||
// export const createTestingModule = createTestingModuleWithIoC; | ||
export const createTestingModule = createTestingModuleWithRegister; | ||
|
||
@Module({ | ||
providers: [{ | ||
provide: "storeDB", | ||
useValue: null | ||
}], | ||
}) | ||
class NopModule{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters