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