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

Commit

Permalink
feat: implement configuration via forRoot (#21)
Browse files Browse the repository at this point in the history
Adds the ability to provide configuration to the InMemoryDBModule via `forRoot` static method.

Implements #18
  • Loading branch information
wescopeland authored and wesleygrimes committed Aug 18, 2019
1 parent 95cdfac commit 8904c00
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/in-memory-db.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { Module } from '@nestjs/common';
import { DynamicModule, Module } from '@nestjs/common';

import { InMemoryDBConfig } from './interfaces';
import { InMemoryDBService } from './services';

@Module({
providers: [InMemoryDBService],
exports: [InMemoryDBService],
})
export class InMemoryDBModule {}
export class InMemoryDBModule {
static forRoot(config: Partial<InMemoryDBConfig> = {}): DynamicModule {
return {
module: InMemoryDBModule,
providers: [
{
provide: InMemoryDBService,
useValue: new InMemoryDBService(config),
},
],
exports: [InMemoryDBService],
};
}
}
1 change: 1 addition & 0 deletions lib/interfaces/in-memory-db-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface InMemoryDBConfig {}
1 change: 1 addition & 0 deletions lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './in-memory-db-config';
export * from './in-memory-db-entity';
10 changes: 8 additions & 2 deletions lib/services/in-memory-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Injectable } from '@nestjs/common';
import { InMemoryDBEntity } from '../interfaces';
import { Injectable, Optional } from '@nestjs/common';

import { InMemoryDBConfig, InMemoryDBEntity } from '../interfaces';

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

constructor(@Optional() config: Partial<InMemoryDBConfig> = {}) {
this.moduleConfig = config || {};
}

/**
* Given the array of records of type `T`, reduce the array into a dictionary object of
* type `{ [id: number]: T }`. Set the value of the in-memory data store
Expand Down
146 changes: 146 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"repository": "https://github.com/nestjs-addons/in-memory-db",
"scripts": {
"build": "tsc -p tsconfig.json",
"build:dev": "tsc-watch -p tsconfig.json",
"format": "prettier lib/**/*.ts --write",
"test": "jest --config jest.json",
"semantic-release": "semantic-release"
Expand All @@ -25,6 +26,7 @@
"reflect-metadata": "0.1.13",
"semantic-release": "^15.13.19",
"ts-jest": "24.0.2",
"tsc-watch": "^2.4.0",
"typescript": "3.5.3"
},
"peerDependencies": {
Expand Down

0 comments on commit 8904c00

Please sign in to comment.