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

Commit

Permalink
feat: implement createMany (#11)
Browse files Browse the repository at this point in the history
* feat: implement createMany method

Implement new createMany for bulk record creation

impl #9
  • Loading branch information
wesleygrimes authored Aug 4, 2019
1 parent 8b20c16 commit 1f9762d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,64 @@ This is the service that provides the in-memory database. All methods interact w

#### Public Methods

**`public create(record: Partial<T>): number`**
**`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 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`.

Example Usage:

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

console.log({ newUserId });
console.log({ newUser });

// logs out
// {
// newUserId: 1
// 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,
// }]
// }
```

Expand Down
48 changes: 42 additions & 6 deletions lib/services/in-memory-db.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,59 @@ describe('In Memory DB Service', () => {

// act
service.create(itemToAdd);
const actualRecords = service.records;

// assert
expect(actualRecords).toEqual(expectedRecords);
expect(service.records).toEqual(expectedRecords);
});
it('should return generated id', () => {
// arrange
service.records = [];
const itemToAdd: Partial<TestEntity> = { someField: 'Test' };
const expectedGeneratedId = 1;
const expectedRecords = [...[{ ...itemToAdd, id: expectedGeneratedId }]];
const expectedRecord = { ...itemToAdd, id: 1 };

// act
const actualRecord = service.create(itemToAdd);

// assert
expect(actualRecord).toEqual(expectedRecord);
});
});
describe('createMany', () => {
it('should update records with correct items', () => {
// arrange
service.records = [];
const item1ToAdd: Partial<TestEntity> = { someField: 'Test' };
const item2ToAdd: Partial<TestEntity> = { someField: 'Another' };
const expectedRecords = [
...[{ ...item1ToAdd, id: 1 }, { ...item2ToAdd, id: 2 }],
];

// act
const createdRecords = service.createMany([item1ToAdd, item2ToAdd]);

// assert
expect(service.records).toEqual(expectedRecords);
expect(createdRecords).toEqual(expectedRecords);
});
it('should return generated ids', () => {
// arrange
service.records = [];
const item1ToAdd: Partial<TestEntity> = { someField: 'Test' };
const item2ToAdd: Partial<TestEntity> = { someField: 'Another' };

const expectedGeneratedRecords = [
{ ...item1ToAdd, id: 1 },
{ ...item2ToAdd, id: 2 },
];

// act
const actualGeneratedId = service.create(itemToAdd);
const actualGeneratedRecords = service.createMany([
item1ToAdd,
item2ToAdd,
]);

// assert
expect(actualGeneratedId).toEqual(expectedGeneratedId);
expect(actualGeneratedRecords).toEqual(expectedGeneratedRecords);
});
});
describe('update', () => {
Expand Down
35 changes: 28 additions & 7 deletions lib/services/in-memory-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {

/**
* Given the array of records of type `T`, reduce the array into a dictionary object of
* type `{ [id: number]: T }`. Set the value of the `recordMap` to this reduced input array.
* type `{ [id: number]: T }`. Set the value of the in-memory data store
* to this reduced input array.
* Example:
*
* - input array
Expand Down Expand Up @@ -51,23 +52,43 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
}

/**
* Add the supplied `record` partial to the `recordMap` in-memory data store of records.
* Add the supplied `record` partial to the in-memory data store of records.
* Get the `id` of the record by getting the next available `id` value.
* Returns the `id` of the newly added record.
* Returns the updated record with the newly generated `id`.
* @param record the partial record of type `T` to create
*/
public create(record: Partial<T>): number {
public create(record: Partial<T>): T {
const id = record.id || this.getNextId();
const newRecord: T = { ...record, id } as T;
this.recordMap = {
...this.recordMap,
[id]: newRecord,
};
return newRecord.id;
return newRecord;
}

/**
* Update a record in the `recordMap` of type `T` using the supplied record.
* Add the supplied `records` partials array to in-memory data store of records.
* Get the `id` of the record by getting the next available `id` value.
* Returns a sequential array of the records with the newly generated `ids`.
* @param records any array of partial records of type `T` to create
*/
public createMany(records: Array<Partial<T>>): T[] {
const newRecords = records.map(record => {
const id = record.id || this.getNextId();
const newRecord: T = { ...record, id } as T;
this.recordMap = {
...this.recordMap,
[id]: newRecord,
};
return newRecord;
});

return newRecords;
}

/**
* Update a record in the in-memory data store of type `T` using the supplied record.
* @param record the record of type `T` to update
*/
public update(record: T): void {
Expand All @@ -78,7 +99,7 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
}

/**
* Remove the record of type `T` from the `recordMap` using the supplied PK id.
* Remove the record of type `T` from the in-memory data store using the supplied PK id.
* @param id the PK id of the record
*/
public delete(id: number): void {
Expand Down

0 comments on commit 1f9762d

Please sign in to comment.