Skip to content
This repository was archived by the owner on Feb 7, 2021. It is now read-only.

Commit 8904c00

Browse files
wescopelandwesleygrimes
authored andcommitted
feat: implement configuration via forRoot (#21)
Adds the ability to provide configuration to the InMemoryDBModule via `forRoot` static method. Implements #18
1 parent 95cdfac commit 8904c00

File tree

6 files changed

+175
-4
lines changed

6 files changed

+175
-4
lines changed

lib/in-memory-db.module.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule, Module } from '@nestjs/common';
2+
3+
import { InMemoryDBConfig } from './interfaces';
24
import { InMemoryDBService } from './services';
35

46
@Module({
57
providers: [InMemoryDBService],
68
exports: [InMemoryDBService],
79
})
8-
export class InMemoryDBModule {}
10+
export class InMemoryDBModule {
11+
static forRoot(config: Partial<InMemoryDBConfig> = {}): DynamicModule {
12+
return {
13+
module: InMemoryDBModule,
14+
providers: [
15+
{
16+
provide: InMemoryDBService,
17+
useValue: new InMemoryDBService(config),
18+
},
19+
],
20+
exports: [InMemoryDBService],
21+
};
22+
}
23+
}

lib/interfaces/in-memory-db-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface InMemoryDBConfig {}

lib/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './in-memory-db-config';
12
export * from './in-memory-db-entity';

lib/services/in-memory-db.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { Injectable } from '@nestjs/common';
2-
import { InMemoryDBEntity } from '../interfaces';
1+
import { Injectable, Optional } from '@nestjs/common';
2+
3+
import { InMemoryDBConfig, InMemoryDBEntity } from '../interfaces';
34

45
@Injectable()
56
export class InMemoryDBService<T extends InMemoryDBEntity> {
7+
private readonly moduleConfig: Partial<InMemoryDBConfig>;
68
private recordMap: { [id: number]: T } = {};
79

10+
constructor(@Optional() config: Partial<InMemoryDBConfig> = {}) {
11+
this.moduleConfig = config || {};
12+
}
13+
814
/**
915
* Given the array of records of type `T`, reduce the array into a dictionary object of
1016
* type `{ [id: number]: T }`. Set the value of the in-memory data store

package-lock.json

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"repository": "https://github.com/nestjs-addons/in-memory-db",
88
"scripts": {
99
"build": "tsc -p tsconfig.json",
10+
"build:dev": "tsc-watch -p tsconfig.json",
1011
"format": "prettier lib/**/*.ts --write",
1112
"test": "jest --config jest.json",
1213
"semantic-release": "semantic-release"
@@ -25,6 +26,7 @@
2526
"reflect-metadata": "0.1.13",
2627
"semantic-release": "^15.13.19",
2728
"ts-jest": "24.0.2",
29+
"tsc-watch": "^2.4.0",
2830
"typescript": "3.5.3"
2931
},
3032
"peerDependencies": {

0 commit comments

Comments
 (0)