diff --git a/README.md b/README.md index e0d81ae..bc0c5dd 100644 --- a/README.md +++ b/README.md @@ -101,23 +101,64 @@ This is the service that provides the in-memory database. All methods interact w #### Public Methods -**`public create(record: Partial): number`** +**`public create(record: Partial): T`** -This method takes in a `Partial` 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` 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>): T[]`** + +This method takes in an array of `Partial` 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, +// }] // } ``` diff --git a/lib/services/in-memory-db.service.spec.ts b/lib/services/in-memory-db.service.spec.ts index 1dc0eaa..bb1bc0f 100644 --- a/lib/services/in-memory-db.service.spec.ts +++ b/lib/services/in-memory-db.service.spec.ts @@ -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 = { 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 = { someField: 'Test' }; + const item2ToAdd: Partial = { 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 = { someField: 'Test' }; + const item2ToAdd: Partial = { 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', () => { diff --git a/lib/services/in-memory-db.service.ts b/lib/services/in-memory-db.service.ts index 4746d44..1318f2d 100644 --- a/lib/services/in-memory-db.service.ts +++ b/lib/services/in-memory-db.service.ts @@ -7,7 +7,8 @@ export class InMemoryDBService { /** * 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 @@ -51,23 +52,43 @@ export class InMemoryDBService { } /** - * 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): number { + public create(record: Partial): 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>): 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 { @@ -78,7 +99,7 @@ export class InMemoryDBService { } /** - * 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 {