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

Commit 1f9762d

Browse files
authored
feat: implement createMany (#11)
* feat: implement createMany method Implement new createMany for bulk record creation impl #9
1 parent 8b20c16 commit 1f9762d

File tree

3 files changed

+116
-18
lines changed

3 files changed

+116
-18
lines changed

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,64 @@ This is the service that provides the in-memory database. All methods interact w
101101

102102
#### Public Methods
103103

104-
**`public create(record: Partial<T>): number`**
104+
**`public create(record: Partial<T>): T`**
105105

106-
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`.
106+
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`.
107107

108108
Example Usage:
109109

110110
```typescript
111-
const newUserId = this.userService.create({
111+
const newUser = this.userService.create({
112112
firstName: 'Some',
113113
lastName: 'Person',
114114
});
115115

116-
console.log({ newUserId });
116+
console.log({ newUser });
117117

118118
// logs out
119119
// {
120-
// newUserId: 1
120+
// newUser: {
121+
// id: 1,
122+
// firstName: 'Some',
123+
// lastName: 'Person,
124+
// }
125+
// }
126+
```
127+
128+
**`public createMany(records: Array<Partial<T>>): T[]`**
129+
130+
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.
131+
132+
Example Usage:
133+
134+
```typescript
135+
const recordsToCreate = [
136+
{
137+
firstName: 'Some',
138+
lastName: 'Person',
139+
},
140+
{
141+
firstName: 'Other',
142+
lastName: 'Person',
143+
},
144+
];
145+
146+
const newUsers = this.userService.createMany(recordsToCreate);
147+
148+
console.log({ newUsers });
149+
150+
// logs out
151+
// {
152+
// newUsers: [{
153+
// id: 1,
154+
// firstName: 'Some',
155+
// lastName: 'Person,
156+
// },
157+
// {
158+
// id: 2,
159+
// firstName: 'Other',
160+
// lastName: 'Person,
161+
// }]
121162
// }
122163
```
123164

lib/services/in-memory-db.service.spec.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,59 @@ describe('In Memory DB Service', () => {
7575

7676
// act
7777
service.create(itemToAdd);
78-
const actualRecords = service.records;
7978

8079
// assert
81-
expect(actualRecords).toEqual(expectedRecords);
80+
expect(service.records).toEqual(expectedRecords);
8281
});
8382
it('should return generated id', () => {
8483
// arrange
8584
service.records = [];
8685
const itemToAdd: Partial<TestEntity> = { someField: 'Test' };
87-
const expectedGeneratedId = 1;
88-
const expectedRecords = [...[{ ...itemToAdd, id: expectedGeneratedId }]];
86+
const expectedRecord = { ...itemToAdd, id: 1 };
87+
88+
// act
89+
const actualRecord = service.create(itemToAdd);
90+
91+
// assert
92+
expect(actualRecord).toEqual(expectedRecord);
93+
});
94+
});
95+
describe('createMany', () => {
96+
it('should update records with correct items', () => {
97+
// arrange
98+
service.records = [];
99+
const item1ToAdd: Partial<TestEntity> = { someField: 'Test' };
100+
const item2ToAdd: Partial<TestEntity> = { someField: 'Another' };
101+
const expectedRecords = [
102+
...[{ ...item1ToAdd, id: 1 }, { ...item2ToAdd, id: 2 }],
103+
];
104+
105+
// act
106+
const createdRecords = service.createMany([item1ToAdd, item2ToAdd]);
107+
108+
// assert
109+
expect(service.records).toEqual(expectedRecords);
110+
expect(createdRecords).toEqual(expectedRecords);
111+
});
112+
it('should return generated ids', () => {
113+
// arrange
114+
service.records = [];
115+
const item1ToAdd: Partial<TestEntity> = { someField: 'Test' };
116+
const item2ToAdd: Partial<TestEntity> = { someField: 'Another' };
117+
118+
const expectedGeneratedRecords = [
119+
{ ...item1ToAdd, id: 1 },
120+
{ ...item2ToAdd, id: 2 },
121+
];
89122

90123
// act
91-
const actualGeneratedId = service.create(itemToAdd);
124+
const actualGeneratedRecords = service.createMany([
125+
item1ToAdd,
126+
item2ToAdd,
127+
]);
92128

93129
// assert
94-
expect(actualGeneratedId).toEqual(expectedGeneratedId);
130+
expect(actualGeneratedRecords).toEqual(expectedGeneratedRecords);
95131
});
96132
});
97133
describe('update', () => {

lib/services/in-memory-db.service.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
77

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

5354
/**
54-
* Add the supplied `record` partial to the `recordMap` in-memory data store of records.
55+
* Add the supplied `record` partial to the in-memory data store of records.
5556
* Get the `id` of the record by getting the next available `id` value.
56-
* Returns the `id` of the newly added record.
57+
* Returns the updated record with the newly generated `id`.
5758
* @param record the partial record of type `T` to create
5859
*/
59-
public create(record: Partial<T>): number {
60+
public create(record: Partial<T>): T {
6061
const id = record.id || this.getNextId();
6162
const newRecord: T = { ...record, id } as T;
6263
this.recordMap = {
6364
...this.recordMap,
6465
[id]: newRecord,
6566
};
66-
return newRecord.id;
67+
return newRecord;
6768
}
6869

6970
/**
70-
* Update a record in the `recordMap` of type `T` using the supplied record.
71+
* Add the supplied `records` partials array to in-memory data store of records.
72+
* Get the `id` of the record by getting the next available `id` value.
73+
* Returns a sequential array of the records with the newly generated `ids`.
74+
* @param records any array of partial records of type `T` to create
75+
*/
76+
public createMany(records: Array<Partial<T>>): T[] {
77+
const newRecords = records.map(record => {
78+
const id = record.id || this.getNextId();
79+
const newRecord: T = { ...record, id } as T;
80+
this.recordMap = {
81+
...this.recordMap,
82+
[id]: newRecord,
83+
};
84+
return newRecord;
85+
});
86+
87+
return newRecords;
88+
}
89+
90+
/**
91+
* Update a record in the in-memory data store of type `T` using the supplied record.
7192
* @param record the record of type `T` to update
7293
*/
7394
public update(record: T): void {
@@ -78,7 +99,7 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
7899
}
79100

80101
/**
81-
* Remove the record of type `T` from the `recordMap` using the supplied PK id.
102+
* Remove the record of type `T` from the in-memory data store using the supplied PK id.
82103
* @param id the PK id of the record
83104
*/
84105
public delete(id: number): void {

0 commit comments

Comments
 (0)