This repository has been archived by the owner on Feb 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for module imports using forFeature method in addition to forRoot. This adds the ability for multiple instances of InMemoryDBService to be registered each with differing configurations. Closes #59
- Loading branch information
1 parent
0a68aa5
commit aae9146
Showing
18 changed files
with
283 additions
and
48 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,2 @@ | ||
export const IN_MEMORY_DB_SERVICE = 'InMemoryDBService'; | ||
export const IN_MEMORY_DB_CONFIG = 'InMemoryDBConfig'; |
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,5 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { getInMemoryDBServiceToken } from './in-memory-db.utils'; | ||
|
||
export const InjectInMemoryDBService = (featureName: string) => | ||
Inject(getInMemoryDBServiceToken(featureName)); |
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,19 @@ | ||
import { getInMemoryDBServiceToken } from './in-memory-db.utils'; | ||
|
||
describe('getInMemoryDBServiceToken', () => { | ||
test.each([ | ||
['oneInMemoryDBService', 'one'], | ||
['InMemoryDBService', ''], | ||
['InMemoryDBService', null], | ||
['InMemoryDBService', undefined], | ||
])( | ||
'should return %p token given input featureName of %p', | ||
(expectedToken: string, featureName: string) => { | ||
// act | ||
const actualToken = getInMemoryDBServiceToken(featureName); | ||
|
||
// assert | ||
expect(actualToken).toEqual(expectedToken); | ||
}, | ||
); | ||
}); |
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,7 @@ | ||
import { IN_MEMORY_DB_SERVICE } from './in-memory-db.constants'; | ||
|
||
export function getInMemoryDBServiceToken(featureName?: string) { | ||
return featureName && featureName !== IN_MEMORY_DB_SERVICE | ||
? `${featureName}${IN_MEMORY_DB_SERVICE}` | ||
: IN_MEMORY_DB_SERVICE; | ||
} |
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,2 @@ | ||
export * from './in-memory-db.decorators'; | ||
export { getInMemoryDBServiceToken } from './in-memory-db.utils'; |
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,15 @@ | ||
import { InMemoryDBConfig, InMemoryDBEntity } from '../interfaces'; | ||
import { InMemoryDBService } from '../services'; | ||
|
||
export function inMemoryDBServiceFactory<T extends InMemoryDBEntity>( | ||
featureConfig: Partial<InMemoryDBConfig> = {}, | ||
featureName?: string, | ||
) { | ||
return () => | ||
new InMemoryDBService<T>({ | ||
featureName: featureName | ||
? featureName | ||
: featureConfig.featureName || 'root', | ||
...featureConfig, | ||
}); | ||
} |
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 @@ | ||
export * from './in-memory-db-service.factory'; |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import 'reflect-metadata'; | ||
import { InjectInMemoryDBService } from './common'; | ||
|
||
export * from './in-memory-db.module'; | ||
export * from './interfaces'; | ||
export * from './services'; | ||
export { InjectInMemoryDBService }; |
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,2 +1,8 @@ | ||
// tslint:disable-next-line: no-empty-interface | ||
export interface InMemoryDBConfig {} | ||
/** | ||
* InMemoryDBConfig defines the config settings for InMemoryDBModule | ||
* | ||
* All properties should remain optional except featureName | ||
*/ | ||
export interface InMemoryDBConfig { | ||
featureName: string; | ||
} |
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,36 @@ | ||
import { FactoryProvider } from '@nestjs/common/interfaces'; | ||
|
||
import { getInMemoryDBServiceToken } from '../common/in-memory-db.utils'; | ||
import { createInMemoryDBForFeatureProviders } from './in-memory-db-for-feature.providers'; | ||
import { InMemoryDBConfig } from '../interfaces'; | ||
import { inMemoryDBServiceFactory } from '../factories'; | ||
|
||
describe('createInMemoryDBForFeatureProviders', () => { | ||
test('returns correct providers array given featureName and featureConfig', () => { | ||
// arrange | ||
const inputFeatureName = 'myFeature'; | ||
const inputFeatureConfig: Partial<InMemoryDBConfig> = {}; | ||
|
||
const expectedProviders: Array<FactoryProvider<any>> = [ | ||
{ | ||
provide: getInMemoryDBServiceToken(inputFeatureName), | ||
useFactory: inMemoryDBServiceFactory( | ||
inputFeatureConfig, | ||
inputFeatureName, | ||
), | ||
}, | ||
]; | ||
|
||
// act | ||
const actualProviders = createInMemoryDBForFeatureProviders( | ||
inputFeatureName, | ||
inputFeatureConfig, | ||
); | ||
|
||
// assert | ||
expect(actualProviders[0].provide).toEqual(expectedProviders[0].provide); | ||
expect(actualProviders[0].useFactory()).toEqual( | ||
expectedProviders[0].useFactory(), | ||
); | ||
}); | ||
}); |
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,17 @@ | ||
import { InMemoryDBConfig } from '../interfaces'; | ||
import { getInMemoryDBServiceToken } from '../common'; | ||
import { inMemoryDBServiceFactory } from '../factories'; | ||
import { FactoryProvider } from '@nestjs/common/interfaces'; | ||
|
||
export function createInMemoryDBForFeatureProviders( | ||
featureName: string, | ||
featureConfig: Partial<InMemoryDBConfig> = {}, | ||
) { | ||
const providers: FactoryProvider[] = [ | ||
{ | ||
provide: getInMemoryDBServiceToken(featureName), | ||
useFactory: inMemoryDBServiceFactory(featureConfig, featureName), | ||
}, | ||
]; | ||
return providers; | ||
} |
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,31 @@ | ||
import { FactoryProvider } from '@nestjs/common/interfaces'; | ||
|
||
import { getInMemoryDBServiceToken } from '../common/in-memory-db.utils'; | ||
import { createInMemoryDBForRootProviders } from './in-memory-db-for-root.providers'; | ||
import { InMemoryDBConfig } from '../interfaces'; | ||
import { inMemoryDBServiceFactory } from '../factories'; | ||
|
||
describe('createInMemoryDBForRootProviders', () => { | ||
test('returns correct providers array given featureName and featureConfig', () => { | ||
// arrange | ||
const inputFeatureConfig: Partial<InMemoryDBConfig> = {}; | ||
|
||
const expectedProviders: Array<FactoryProvider<any>> = [ | ||
{ | ||
provide: getInMemoryDBServiceToken(), | ||
useFactory: inMemoryDBServiceFactory(inputFeatureConfig), | ||
}, | ||
]; | ||
|
||
// act | ||
const actualProviders = createInMemoryDBForRootProviders( | ||
inputFeatureConfig, | ||
); | ||
|
||
// assert | ||
expect(actualProviders[0].provide).toEqual(expectedProviders[0].provide); | ||
expect(actualProviders[0].useFactory()).toEqual( | ||
expectedProviders[0].useFactory(), | ||
); | ||
}); | ||
}); |
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,17 @@ | ||
import { FactoryProvider } from '@nestjs/common/interfaces'; | ||
|
||
import { inMemoryDBServiceFactory } from '../factories'; | ||
import { getInMemoryDBServiceToken } from '../common'; | ||
import { InMemoryDBConfig } from '../interfaces'; | ||
|
||
export function createInMemoryDBForRootProviders( | ||
featureConfig: Partial<InMemoryDBConfig> = {}, | ||
) { | ||
const providers: FactoryProvider[] = [ | ||
{ | ||
provide: getInMemoryDBServiceToken(), | ||
useFactory: inMemoryDBServiceFactory(featureConfig), | ||
}, | ||
]; | ||
return providers; | ||
} |
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,2 @@ | ||
export * from './in-memory-db-for-root.providers'; | ||
export * from './in-memory-db-for-feature.providers'; |
Oops, something went wrong.