You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note that starting with V2.0.0, the InMemoryDB\* has been deprecated and moved to V1 instances. For more information on this API, checkout docs here: [V1 Docs](API_V1.md).
13
-
14
-
Detailed Changes:
15
-
16
-
- All V1 assets have been renamed to `InMemoryDBv1***`
17
-
- New V2+ assets are the same name as before the V1 deprecation. You will just need to make modifications to support the `id` being a `string` as opposed to `number`
18
-
-`id` has been changed from `number` to `string` to support GUIDs.
19
-
- You can now _optionally_ provide a `getNextId` function that the service will use to generate new string IDs. By default, it will use the `uuid` npm package and `v4` implementation.
20
-
21
-
## Description
22
-
23
-
`@nestjs-addons/in-memory-db` provides a ridiculously simple, no configuration needed, way to create a simple in-memory database for use in your `nestjs` applications. You simply define an `interface` that extends the `interface InMemoryDBEntity`, inject the `InMemoryDBService<T>` into your controllers and/or services, and immediately profit. The records are stored in-memory, as a singleton, for each interface, for the life of the service.
24
-
25
-
This provides a great way to quickly get up and running with prototypes and mock backends.
- Import `InMemoryDBModule` from `@nestjs-addons/in-memory-db`
90
-
- Add `InMemoryDBModule` to the `imports` array in the `@Module` of your choice
91
-
92
-
### Define an interface for each InMemoryEntity
93
-
94
-
An instance of `InMemoryDBService<T>` will be created for each `InMemoryDBEntity` entity `interface` defined. The `InMemoryDBEntity` adds an `id: string` property as the only required field. Additional fields can be defined by extending the `interface`.
95
-
96
-
To define a new `InMemoryDBEntity` extension create an `interface` similar to the following example:
97
-
98
-
```typescript
99
-
interfaceUserEntityextendsInMemoryDBEntity {
100
-
firstName:string;
101
-
lastName:string;
102
-
emailAddress:string;
103
-
admin:boolean;
104
-
}
105
-
```
106
-
107
-
Now we can make use of our new `interface` when injecting the `InMemoryDBService<T>` into our controllers or other services.
108
-
109
-
### Inject into Controller(s) and/or Services(s)
110
-
111
-
In order to use the `InMemoryDBService<T>` we need to do the following:
112
-
113
-
- Add `private readonly inMemoryDB: InMemoryDBService<T>` to the `constructor` of each controller and/or service that you would like to use it in.
114
-
- Begin using `InMemoryDBService` as expected.
115
-
116
-
An example of injecting `InMemoryDBService` into a `UserController` for the `UserEntity` we defined earlier would look something like this:
## Feature Modules - Registering Multiple Instances using `forFeature`
136
-
137
-
Registering multiple instances for specific feature modules is super simple. Each feature module is guaranteed isolated to that feature. In order to get up and running you need to do the following:
Using this decorator ensures that the correct instance is injected.
181
-
182
-
## Entity Controller
183
-
184
-
In order to prevent code duplication and boilerplate for each controller, we have created two base entity controllers `InMemoryDBEntityController` and `InMemoryDBEntityAsyncController`. This allows you to quickly provide endpoints to make requests without having to manually implement each action.
185
-
186
-
To use the controllers, simply create a new controller and extend it with one of the provided base controllers.
In order to have an Entity Controller use a feature-specific instance of the service, use the decorator `InjectInMemoryDBService` in the controller's provided by this library as shown below:
0 commit comments