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
{{ message }}
This repository was archived by the owner on Feb 7, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+217-3Lines changed: 217 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,10 @@
1
-
# NestJS Addons - In-Memory DB Service
1
+
# NestJS Addons: In-Memory DB Service
2
2
3
3
## Description
4
4
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.
6
8
7
9
## Installation
8
10
@@ -12,7 +14,219 @@ $ npm i --save @nestjs-addons/in-memory-db
12
14
13
15
## Quick Start
14
16
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.
- 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
+
interfaceUserEntityextendsInMemoryEntity {
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:
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.
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.
0 commit comments