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

Commit b992ab4

Browse files
authored
feat: implement updatemany, deletemany, getmany (#15)
* feat: implement updatemany, deletemany, getmany Implemented new updateMany, deleteMany, getMany methods on InMemoryDBService impl #14
1 parent ac7b4a1 commit b992ab4

File tree

2 files changed

+107
-14
lines changed

2 files changed

+107
-14
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('In Memory DB Service', () => {
1111
const sampleRecords: TestEntity[] = [
1212
{ id: 1, someField: 'AAA' },
1313
{ id: 2, someField: 'BBB' },
14+
{ id: 3, someField: 'CCC' },
1415
];
1516

1617
beforeEach(() => {
@@ -42,6 +43,31 @@ describe('In Memory DB Service', () => {
4243
expect(actualRecord).toEqual(expectedRecord);
4344
});
4445
});
46+
describe('getMany', () => {
47+
it('should return expected records if given valid ids', () => {
48+
// arrange
49+
service.records = [...sampleRecords];
50+
const expectedRecords = [...[sampleRecords[0], sampleRecords[1]]];
51+
52+
// act
53+
const actualRecords = service.getMany([1, 2]);
54+
55+
// assert
56+
expect(actualRecords).toEqual(expectedRecords);
57+
});
58+
59+
it('should return only expected records if given an invalid id', () => {
60+
// arrange
61+
service.records = [...sampleRecords];
62+
const expectedRecords = [sampleRecords[0]];
63+
64+
// act
65+
const actualRecords = service.getMany([-1, 1]);
66+
67+
// assert
68+
expect(actualRecords).toEqual(expectedRecords);
69+
});
70+
});
4571
describe('getAll', () => {
4672
it('should return all expected records', () => {
4773
// arrange
@@ -148,6 +174,31 @@ describe('In Memory DB Service', () => {
148174
expect(actualUpdatedRecord).toEqual(expectedUpdatedRecord);
149175
});
150176
});
177+
describe('updateMany', () => {
178+
it('should update records as expected', () => {
179+
// arrange
180+
const originalRecords: TestEntity[] = [
181+
{ id: 1, someField: 'AAA' },
182+
{ id: 2, someField: 'BBB' },
183+
{ id: 3, someField: 'CCC' },
184+
];
185+
const expectedUpdatedRecords: TestEntity[] = [
186+
{ id: 1, someField: 'YYY' },
187+
{ id: 2, someField: 'ZZZ' },
188+
];
189+
service.records = [...originalRecords];
190+
191+
// act
192+
service.updateMany(expectedUpdatedRecords);
193+
194+
// assert
195+
const actualUpdatedRecords = service.records.filter(record =>
196+
expectedUpdatedRecords.map(o => o.id).includes(record.id),
197+
);
198+
199+
expect(actualUpdatedRecords).toEqual(expectedUpdatedRecords);
200+
});
201+
});
151202
describe('delete', () => {
152203
it('should remove record as expected', () => {
153204
// arrange
@@ -165,6 +216,24 @@ describe('In Memory DB Service', () => {
165216
expect(service.records.length).toEqual(1);
166217
});
167218
});
219+
describe('deleteMany', () => {
220+
it('should remove records as expected', () => {
221+
// arrange
222+
service.records = [
223+
{ id: 1, someField: 'AAA' },
224+
{ id: 2, someField: 'BBB' },
225+
{ id: 3, someField: 'CCC' },
226+
];
227+
228+
// act
229+
service.deleteMany([1, 2]);
230+
231+
// assert
232+
const thirdRecord = service.records[0];
233+
expect(thirdRecord).toEqual({ id: 3, someField: 'CCC' });
234+
expect(service.records.length).toEqual(1);
235+
});
236+
});
168237
describe('query', () => {
169238
it('should return expected records for given predicate', () => {
170239
// arrange

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,10 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
7171
* Add the supplied `records` partials array to in-memory data store of records.
7272
* Get the `id` of the record by getting the next available `id` value.
7373
* 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
74+
* @param records an array of partial records of type `T` to create
7575
*/
7676
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;
77+
return records.map(record => this.create(record));
8878
}
8979

9080
/**
@@ -94,10 +84,20 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
9484
public update(record: T): void {
9585
this.recordMap = {
9686
...this.recordMap,
97-
[record.id]: record,
87+
[record.id]: { ...record },
9888
};
9989
}
10090

91+
/**
92+
* Update records in the in-memory data store of type `T` using the supplied records.
93+
* @param records an array of records of type `T` to update
94+
*/
95+
public updateMany(records: T[]): void {
96+
for (const record of records) {
97+
this.update(record);
98+
}
99+
}
100+
101101
/**
102102
* Remove the record of type `T` from the in-memory data store using the supplied PK id.
103103
* @param id the PK id of the record
@@ -110,13 +110,37 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
110110
}
111111

112112
/**
113-
* Get a single record of type `T` with the supplid id value.
113+
* Remove the records of type `T` from the in-memory data store using the supplied PK ids.
114+
* @param ids the PK ids of the records
115+
*/
116+
public deleteMany(ids: number[]): void {
117+
for (const id of ids) {
118+
this.delete(id);
119+
}
120+
}
121+
122+
/**
123+
* Get a single record of type `T` with the supplied id value.
114124
* @param id the PK id of the record
115125
*/
116126
public get(id: number): T {
117127
return this.recordMap[id];
118128
}
119129

130+
/**
131+
* Get records of type `T` with the supplied id values.
132+
* @param ids the PK ids of the records
133+
*/
134+
public getMany(ids: number[]): T[] {
135+
const records = ids
136+
.filter(id => this.recordMap[id])
137+
.map(id => {
138+
return this.recordMap[id];
139+
});
140+
141+
return records;
142+
}
143+
120144
/**
121145
* Return all of the records of type `T`.
122146
*/

0 commit comments

Comments
 (0)