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

Commit cf6ce61

Browse files
committed
Updated to version 0.0.2
1 parent 12283cb commit cf6ce61

File tree

10 files changed

+277
-46
lines changed

10 files changed

+277
-46
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"trailingComma": "all",
3+
"singleQuote": true
4+
}

README.md

Lines changed: 217 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# NestJS Addons - In-Memory DB Service
1+
# NestJS Addons: In-Memory DB Service
22

33
## Description
44

5-
Coming soon...
5+
`@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 InMemoryEntity`, 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.
6+
7+
This provides a great way to quickly get up and running with prototypes and mock backends.
68

79
## Installation
810

@@ -12,7 +14,219 @@ $ npm i --save @nestjs-addons/in-memory-db
1214

1315
## Quick Start
1416

15-
Coming soon...
17+
### Import into Module(s)
18+
19+
To get started, let's first update our `app.module.ts` to include the necessary pieces.
20+
21+
> While we are importing to the AppModule in this example, InMemoryDBModule could be imported in Feature modules just as well.
22+
23+
```typescript
24+
// app.module.ts
25+
26+
import { Module } from '@nestjs/common';
27+
import { InMemoryDBModule } from '@nestjs-addons/in-memory-db';
28+
...
29+
30+
@Module({
31+
...
32+
imports: [InMemoryDBModule],
33+
...
34+
})
35+
export class AppModule {}
36+
```
37+
38+
As you can see we did the following:
39+
40+
- Import `InMemoryDBModule` from `@nestjs-addons/in-memory-db`
41+
- Add `InMemoryDBModule` to the `imports` array in the `@Module` of your choice
42+
43+
### Define an interface for each InMemoryEntity
44+
45+
An instance of `InMemoryDBService<T>` will be created for each `InMemoryEntity` entity `interface` defined. The `InMemoryEntity` adds an `id: number` property as the only required field. Additional fields can be defined by extending the `interface`.
46+
47+
To define a new `InMemoryEntity` extension create an `interface` similar to the following example:
48+
49+
```typescript
50+
interface UserEntity extends InMemoryEntity {
51+
firstName: string;
52+
lastName: string;
53+
emailAddress: string;
54+
admin: boolean;
55+
}
56+
```
57+
58+
Now we can make use of our new `interface` when injecting the `InMemoryDBService<T>` into our controllers or other services.
59+
60+
### Inject into Controller(s) and/or Services(s)
61+
62+
In order to use the `InMemoryDBService<T>` we need to do the following:
63+
64+
- Add `private readonly inMemoryDb: InMemoryDBService<T>` to the `constructor` of each controller and/or service that you would like to use it in.
65+
- Begin using `InMemoryDBService` as expected.
66+
67+
An example of injecting `InMemoryDBService` into a `UserController` for the `UserEntity` we defined earlier would look something like this:
68+
69+
```typescript
70+
@Controller()
71+
export class UserController {
72+
constructor(private readonly userService: InMemoryDBService<UserEntity>) {}
73+
74+
@Get('users/:id')
75+
getUser(@Param() id: number): User {
76+
return this.userService.get(id);
77+
}
78+
79+
@Post('users')
80+
createUser(@Body() user: User): User {
81+
return this.service.create(user);
82+
}
83+
}
84+
```
85+
86+
## API Documentation
87+
88+
### `InMemoryDBService<T extends InMemoryEntity>`
89+
90+
This is the service that provides the in-memory database. All methods interact with a `records` array and implement `generics` to provide type-safety and intellisense based on the `T extends InMemoryEntity` passed in.
91+
92+
#### Public Methods
93+
94+
**`public create(record: Partial<T>): number`**
95+
96+
This method takes in a `Partial<T>` as we do not always know the `id` for a record when we are creating. If we leave off the `id` property the service will automatically generate an `id` for us. Upon successful creation, the method returns the generated `id`.
97+
98+
Example Usage:
99+
100+
```typescript
101+
const newUserId = this.userService.create({
102+
firstName: 'Some',
103+
lastName: 'Person',
104+
});
105+
106+
console.log({ newUserId });
107+
108+
// logs out
109+
// {
110+
// newUserId: 1
111+
// }
112+
```
113+
114+
**`public update(record: T): void`**
115+
116+
This method takes in a `T` record object and updates the record in the `records` array based on the `id` in the object. This method does not return a value.
117+
118+
Example Usage:
119+
120+
```typescript
121+
this.userService.update({
122+
id: 1,
123+
firstName: 'Other',
124+
lastName: 'Person',
125+
});
126+
```
127+
128+
**`public delete(id: number): void`**
129+
130+
This method takes in a `id: number` and deletes the record from the `records` array based on the `id` in the object. This method does not return a value.
131+
132+
Example Usage:
133+
134+
```typescript
135+
this.userService.delete(1);
136+
```
137+
138+
**`public get(id: number): T`**
139+
140+
This method takes in a `id: number` and returns the record from the `records` array based on the `id` in the object.
141+
142+
Example Usage:
143+
144+
```typescript
145+
const foundUser = this.userService.get(1);
146+
147+
console.log({ foundUser });
148+
149+
// logs out
150+
// {
151+
// foundUser: {
152+
// id:1,
153+
// firstName: 'Some',
154+
// lastName: 'Person'
155+
// }
156+
// }
157+
```
158+
159+
**`public getAll(): T[]`**
160+
161+
This method has no parameters and returns the all from the `records` array.
162+
163+
Example Usage:
164+
165+
```typescript
166+
const allUsers = this.userService.getAll();
167+
168+
console.log({ allUsers });
169+
170+
// logs out
171+
// {
172+
// allUsers: [
173+
// {
174+
// id: 1,
175+
// firstName: 'Some',
176+
// lastName: 'Person'
177+
// },
178+
// {
179+
// id: 2,
180+
// firstName: 'Other',
181+
// lastName: 'Person'
182+
// }
183+
// ];
184+
// }
185+
```
186+
187+
**`public query(predicate: (record: T) => boolean): T[]`**
188+
189+
This method has takes in a `record: T` predicate and returns all from the `records` array that meet that predicate's requirements.
190+
191+
Example Usage:
192+
193+
```typescript
194+
const foundUsers = this.userService.query(
195+
record => record.lastName === 'Person',
196+
);
197+
198+
console.log({ foundUsers });
199+
200+
// logs out
201+
// {
202+
// allUsers: [
203+
// {
204+
// id: 1,
205+
// firstName: 'Some',
206+
// lastName: 'Person'
207+
// },
208+
// {
209+
// id: 2,
210+
// firstName: 'Other',
211+
// lastName: 'Person'
212+
// }
213+
// ];
214+
// }
215+
```
216+
217+
#### Public Properties
218+
219+
- `records: T[]` - This is the in-memory array used in all crud and read operations for the service. Please access with care.
220+
221+
### `InMemoryEntity`
222+
223+
This is an interface used by the `InMemoryDBService` for intellisense and type-safety. Do not use this interface directly. Rather, implement your own `interface` that `extends` this.
224+
225+
```typescript
226+
export interface InMemoryDBEntity {
227+
id: number;
228+
}
229+
```
16230

17231
## Stay in touch
18232

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./dist";
1+
export * from './dist';

lib/in-memory-db.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Module } from "@nestjs/common";
2-
import { InMemoryDBService } from "./in-memory-db.service";
1+
import { Module } from '@nestjs/common';
2+
import { InMemoryDBService } from './in-memory-db.service';
33

44
@Module({
55
providers: [InMemoryDBService],
6-
exports: [InMemoryDBService]
6+
exports: [InMemoryDBService],
77
})
88
export class InMemoryDBModule {}

0 commit comments

Comments
 (0)