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

Commit

Permalink
fix: update readme and docs (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleygrimes authored Sep 9, 2020
1 parent 8303e26 commit bf68214
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 35 deletions.
36 changes: 18 additions & 18 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ This is the service that provides the in-memory database. All methods interact w

### Public Methods

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

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`.
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.

Example Usage:

Expand All @@ -30,9 +30,9 @@ console.log({ newUser });
// }
```

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

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

Example Usage:

Expand Down Expand Up @@ -81,31 +81,31 @@ this.userService.update({
});
```

**`public delete(id: number): void`**
**`public delete(id: string): void`**

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

Example Usage:

```typescript
this.userService.delete(1);
this.userService.delete('1');
```

**`public get(id: number): T`**
**`public get(id: string): T`**

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

Example Usage:

```typescript
const foundUser = this.userService.get(1);
const foundUser = this.userService.get('1');

console.log({ foundUser });

// logs out
// {
// foundUser: {
// id:1,
// id: '1',
// firstName: 'Some',
// lastName: 'Person'
// }
Expand All @@ -127,12 +127,12 @@ console.log({ allUsers });
// {
// allUsers: [
// {
// id: 1,
// id: '1',
// firstName: 'Some',
// lastName: 'Person'
// },
// {
// id: 2,
// id: '2',
// firstName: 'Other',
// lastName: 'Person'
// }
Expand All @@ -148,7 +148,7 @@ Example Usage:

```typescript
const foundUsers = this.userService.query(
record => record.lastName === 'Person',
(record) => record.lastName === 'Person',
);

console.log({ foundUsers });
Expand All @@ -157,12 +157,12 @@ console.log({ foundUsers });
// {
// allUsers: [
// {
// id: 1,
// id: '1',
// firstName: 'Some',
// lastName: 'Person'
// },
// {
// id: 2,
// id: '2',
// firstName: 'Other',
// lastName: 'Person'
// }
Expand All @@ -174,12 +174,12 @@ console.log({ foundUsers });

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

## `InMemoryEntity`
## `InMemoryDBEntity`

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.

```typescript
export interface InMemoryDBEntity {
id: number;
id: string;
}
```
185 changes: 185 additions & 0 deletions API_V1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# API Documentation - V1

## `InMemoryDBV1Service<T extends InMemoryDBV1Entity>`

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.

### Public Methods

**`public create(record: Partial<T>): T`**

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`.

Example Usage:

```typescript
const newUser = this.userService.create({
firstName: 'Some',
lastName: 'Person',
});

console.log({ newUser });

// logs out
// {
// newUser: {
// id: 1,
// firstName: 'Some',
// lastName: 'Person,
// }
// }
```

**`public createMany(records: Array<Partial<T>>): T[]`**

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.

Example Usage:

```typescript
const recordsToCreate = [
{
firstName: 'Some',
lastName: 'Person',
},
{
firstName: 'Other',
lastName: 'Person',
},
];

const newUsers = this.userService.createMany(recordsToCreate);

console.log({ newUsers });

// logs out
// {
// newUsers: [{
// id: 1,
// firstName: 'Some',
// lastName: 'Person,
// },
// {
// id: 2,
// firstName: 'Other',
// lastName: 'Person,
// }]
// }
```

**`public update(record: T): void`**

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.

Example Usage:

```typescript
this.userService.update({
id: 1,
firstName: 'Other',
lastName: 'Person',
});
```

**`public delete(id: number): void`**

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.

Example Usage:

```typescript
this.userService.delete(1);
```

**`public get(id: number): T`**

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

Example Usage:

```typescript
const foundUser = this.userService.get(1);

console.log({ foundUser });

// logs out
// {
// foundUser: {
// id:1,
// firstName: 'Some',
// lastName: 'Person'
// }
// }
```

**`public getAll(): T[]`**

This method has no parameters and returns the all from the `records` array.

Example Usage:

```typescript
const allUsers = this.userService.getAll();

console.log({ allUsers });

// logs out
// {
// allUsers: [
// {
// id: 1,
// firstName: 'Some',
// lastName: 'Person'
// },
// {
// id: 2,
// firstName: 'Other',
// lastName: 'Person'
// }
// ];
// }
```

**`public query(predicate: (record: T) => boolean): T[]`**

This method has takes in a `record: T` predicate and returns all from the `records` array that meet that predicate's requirements.

Example Usage:

```typescript
const foundUsers = this.userService.query(
(record) => record.lastName === 'Person',
);

console.log({ foundUsers });

// logs out
// {
// allUsers: [
// {
// id: 1,
// firstName: 'Some',
// lastName: 'Person'
// },
// {
// id: 2,
// firstName: 'Other',
// lastName: 'Person'
// }
// ];
// }
```

### Public Properties

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

## `InMemoryDBV1Entity`

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.

```typescript
export interface InMemoryDBV1Entity {
id: number;
}
```
Loading

0 comments on commit bf68214

Please sign in to comment.