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

Commit bf68214

Browse files
authored
fix: update readme and docs (#186)
1 parent 8303e26 commit bf68214

File tree

3 files changed

+226
-35
lines changed

3 files changed

+226
-35
lines changed

API.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ This is the service that provides the in-memory database. All methods interact w
66

77
### Public Methods
88

9-
**`public create(record: Partial<T>): T`**
9+
**`public create(record: Partial<T>, getNextId?: () => string): T`**
1010

11-
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 record with the newly generated `id`.
11+
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 record with the newly generated `id`. An optional parameter of `getNextId` can be used to pass a function that returns a `string` and will be used by the service to get the next id. By default this uses the `uuid` npm package.
1212

1313
Example Usage:
1414

@@ -30,9 +30,9 @@ console.log({ newUser });
3030
// }
3131
```
3232

33-
**`public createMany(records: Array<Partial<T>>): T[]`**
33+
**`public createMany(records: Array<Partial<T>>, getNextId?: () => string): T[]`**
3434

35-
This method takes in an array of `Partial<T>` as we do not always know the `id` for records when we are creating. If we leave off the `id` properties the service will automatically generate `id`s for us. Upon successful creation, the method returns the an array of records with the newly generated `id`s.
35+
This method takes in an array of `Partial<T>` as we do not always know the `id` for records when we are creating. If we leave off the `id` properties the service will automatically generate `id`s for us. Upon successful creation, the method returns the an array of records with the newly generated `id`s. An optional parameter of `getNextId` can be used to pass a function that returns a `string` and will be used by the service to get the next id. By default this uses the `uuid` npm package.
3636

3737
Example Usage:
3838

@@ -81,31 +81,31 @@ this.userService.update({
8181
});
8282
```
8383

84-
**`public delete(id: number): void`**
84+
**`public delete(id: string): void`**
8585

86-
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.
86+
This method takes in a `id: string` and deletes the record from the `records` array based on the `id` in the object. This method does not return a value.
8787

8888
Example Usage:
8989

9090
```typescript
91-
this.userService.delete(1);
91+
this.userService.delete('1');
9292
```
9393

94-
**`public get(id: number): T`**
94+
**`public get(id: string): T`**
9595

96-
This method takes in a `id: number` and returns the record from the `records` array based on the `id` in the object.
96+
This method takes in a `id: string` and returns the record from the `records` array based on the `id` in the object.
9797

9898
Example Usage:
9999

100100
```typescript
101-
const foundUser = this.userService.get(1);
101+
const foundUser = this.userService.get('1');
102102

103103
console.log({ foundUser });
104104

105105
// logs out
106106
// {
107107
// foundUser: {
108-
// id:1,
108+
// id: '1',
109109
// firstName: 'Some',
110110
// lastName: 'Person'
111111
// }
@@ -127,12 +127,12 @@ console.log({ allUsers });
127127
// {
128128
// allUsers: [
129129
// {
130-
// id: 1,
130+
// id: '1',
131131
// firstName: 'Some',
132132
// lastName: 'Person'
133133
// },
134134
// {
135-
// id: 2,
135+
// id: '2',
136136
// firstName: 'Other',
137137
// lastName: 'Person'
138138
// }
@@ -148,7 +148,7 @@ Example Usage:
148148

149149
```typescript
150150
const foundUsers = this.userService.query(
151-
record => record.lastName === 'Person',
151+
(record) => record.lastName === 'Person',
152152
);
153153

154154
console.log({ foundUsers });
@@ -157,12 +157,12 @@ console.log({ foundUsers });
157157
// {
158158
// allUsers: [
159159
// {
160-
// id: 1,
160+
// id: '1',
161161
// firstName: 'Some',
162162
// lastName: 'Person'
163163
// },
164164
// {
165-
// id: 2,
165+
// id: '2',
166166
// firstName: 'Other',
167167
// lastName: 'Person'
168168
// }
@@ -174,12 +174,12 @@ console.log({ foundUsers });
174174

175175
- `records: T[]` - This is the in-memory array used in all crud and read operations for the service. Please access with care.
176176

177-
## `InMemoryEntity`
177+
## `InMemoryDBEntity`
178178

179179
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.
180180

181181
```typescript
182182
export interface InMemoryDBEntity {
183-
id: number;
183+
id: string;
184184
}
185185
```

API_V1.md

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# API Documentation - V1
2+
3+
## `InMemoryDBV1Service<T extends InMemoryDBV1Entity>`
4+
5+
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 InMemoryDBV1Entity` passed in.
6+
7+
### Public Methods
8+
9+
**`public create(record: Partial<T>): T`**
10+
11+
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 record with the newly generated `id`.
12+
13+
Example Usage:
14+
15+
```typescript
16+
const newUser = this.userService.create({
17+
firstName: 'Some',
18+
lastName: 'Person',
19+
});
20+
21+
console.log({ newUser });
22+
23+
// logs out
24+
// {
25+
// newUser: {
26+
// id: 1,
27+
// firstName: 'Some',
28+
// lastName: 'Person,
29+
// }
30+
// }
31+
```
32+
33+
**`public createMany(records: Array<Partial<T>>): T[]`**
34+
35+
This method takes in an array of `Partial<T>` as we do not always know the `id` for records when we are creating. If we leave off the `id` properties the service will automatically generate `id`s for us. Upon successful creation, the method returns the an array of records with the newly generated `id`s.
36+
37+
Example Usage:
38+
39+
```typescript
40+
const recordsToCreate = [
41+
{
42+
firstName: 'Some',
43+
lastName: 'Person',
44+
},
45+
{
46+
firstName: 'Other',
47+
lastName: 'Person',
48+
},
49+
];
50+
51+
const newUsers = this.userService.createMany(recordsToCreate);
52+
53+
console.log({ newUsers });
54+
55+
// logs out
56+
// {
57+
// newUsers: [{
58+
// id: 1,
59+
// firstName: 'Some',
60+
// lastName: 'Person,
61+
// },
62+
// {
63+
// id: 2,
64+
// firstName: 'Other',
65+
// lastName: 'Person,
66+
// }]
67+
// }
68+
```
69+
70+
**`public update(record: T): void`**
71+
72+
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.
73+
74+
Example Usage:
75+
76+
```typescript
77+
this.userService.update({
78+
id: 1,
79+
firstName: 'Other',
80+
lastName: 'Person',
81+
});
82+
```
83+
84+
**`public delete(id: number): void`**
85+
86+
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.
87+
88+
Example Usage:
89+
90+
```typescript
91+
this.userService.delete(1);
92+
```
93+
94+
**`public get(id: number): T`**
95+
96+
This method takes in a `id: number` and returns the record from the `records` array based on the `id` in the object.
97+
98+
Example Usage:
99+
100+
```typescript
101+
const foundUser = this.userService.get(1);
102+
103+
console.log({ foundUser });
104+
105+
// logs out
106+
// {
107+
// foundUser: {
108+
// id:1,
109+
// firstName: 'Some',
110+
// lastName: 'Person'
111+
// }
112+
// }
113+
```
114+
115+
**`public getAll(): T[]`**
116+
117+
This method has no parameters and returns the all from the `records` array.
118+
119+
Example Usage:
120+
121+
```typescript
122+
const allUsers = this.userService.getAll();
123+
124+
console.log({ allUsers });
125+
126+
// logs out
127+
// {
128+
// allUsers: [
129+
// {
130+
// id: 1,
131+
// firstName: 'Some',
132+
// lastName: 'Person'
133+
// },
134+
// {
135+
// id: 2,
136+
// firstName: 'Other',
137+
// lastName: 'Person'
138+
// }
139+
// ];
140+
// }
141+
```
142+
143+
**`public query(predicate: (record: T) => boolean): T[]`**
144+
145+
This method has takes in a `record: T` predicate and returns all from the `records` array that meet that predicate's requirements.
146+
147+
Example Usage:
148+
149+
```typescript
150+
const foundUsers = this.userService.query(
151+
(record) => record.lastName === 'Person',
152+
);
153+
154+
console.log({ foundUsers });
155+
156+
// logs out
157+
// {
158+
// allUsers: [
159+
// {
160+
// id: 1,
161+
// firstName: 'Some',
162+
// lastName: 'Person'
163+
// },
164+
// {
165+
// id: 2,
166+
// firstName: 'Other',
167+
// lastName: 'Person'
168+
// }
169+
// ];
170+
// }
171+
```
172+
173+
### Public Properties
174+
175+
- `records: T[]` - This is the in-memory array used in all crud and read operations for the service. Please access with care.
176+
177+
## `InMemoryDBV1Entity`
178+
179+
This is an interface used by the `InMemoryDBV1Service` for intellisense and type-safety. Do not use this interface directly. Rather, implement your own `interface` that `extends` this.
180+
181+
```typescript
182+
export interface InMemoryDBV1Entity {
183+
id: number;
184+
}
185+
```

0 commit comments

Comments
 (0)