Skip to content

Commit

Permalink
fix: improved error messages for failed database to entity transforma…
Browse files Browse the repository at this point in the history
…tion (#1453)
  • Loading branch information
TheSlimvReal authored Sep 23, 2022
1 parent 86cdb17 commit d1f9de3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
40 changes: 25 additions & 15 deletions src/app/core/entity/entity-mapper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { EntitySchemaService } from "./schema/entity-schema.service";
import { waitForAsync } from "@angular/core/testing";
import { PouchDatabase } from "../database/pouch-database";
import { DatabaseEntity, entityRegistry } from "./database-entity.decorator";
import { Child } from "../../child-dev-project/children/model/child";

describe("EntityMapperService", () => {
let entityMapper: EntityMapperService;
Expand All @@ -38,21 +39,19 @@ describe("EntityMapperService", () => {
label: "entity 2 from database",
};

beforeEach(
waitForAsync(() => {
testDatabase = PouchDatabase.create();
entityMapper = new EntityMapperService(
testDatabase,
new EntitySchemaService(),
entityRegistry
);

return Promise.all([
testDatabase.put(existingEntity),
testDatabase.put(existingEntity2),
]);
})
);
beforeEach(waitForAsync(() => {
testDatabase = PouchDatabase.create();
entityMapper = new EntityMapperService(
testDatabase,
new EntitySchemaService(),
entityRegistry
);

return Promise.all([
testDatabase.put(existingEntity),
testDatabase.put(existingEntity2),
]);
}));

afterEach(async () => {
await testDatabase.destroy();
Expand Down Expand Up @@ -261,6 +260,17 @@ describe("EntityMapperService", () => {
]);
});

it("should include _id field in transformation errors", (done) => {
const doc = { _id: "Child:test", dateOfBirth: "invalidDate" };
testDatabase
.put(doc)
.then(() => entityMapper.load(Child, "Child:test"))
.catch((err) => {
expect(err.message).toContain("Child:test");
done();
});
});

function receiveUpdatesAndTestTypeAndId(type?: string, entityId?: string) {
return new Promise<void>((resolve) => {
entityMapper.receiveUpdates(Entity).subscribe((e) => {
Expand Down
37 changes: 19 additions & 18 deletions src/app/core/entity/entity-mapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ export class EntityMapperService {
id: string
): Promise<T> {
const ctor = this.resolveConstructor(entityType);
const resultEntity = new ctor("");
const result = await this._db.get(
Entity.createPrefixedId(resultEntity.getType(), id)
);
this.entitySchemaService.loadDataIntoEntity(resultEntity, result);
return resultEntity;
const entityId = Entity.createPrefixedId(ctor.ENTITY_TYPE, id);
const result = await this._db.get(entityId);
return this.transformToEntityFormat(result, ctor);
}

/**
Expand All @@ -74,19 +71,24 @@ export class EntityMapperService {
public async loadType<T extends Entity>(
entityType: EntityConstructor<T> | string
): Promise<T[]> {
const resultArray: Array<T> = [];
const ctor = this.resolveConstructor(entityType);
const allRecordsOfType = await this._db.getAll(
new ctor("").getType() + ":"
);
const records = await this._db.getAll(ctor.ENTITY_TYPE + ":");
return records.map((rec) => this.transformToEntityFormat(rec, ctor));
}

for (const record of allRecordsOfType) {
const entity = new ctor("");
private transformToEntityFormat<T extends Entity>(
record: any,
ctor: EntityConstructor<T>
): T {
const entity = new ctor("");
try {
this.entitySchemaService.loadDataIntoEntity(entity, record);
resultArray.push(entity);
} catch (e) {
// add _id information to error message
e.message = `Could not transform entity "${record._id}": ${e.message}`;
throw e;
}

return resultArray;
return entity;
}

/**
Expand Down Expand Up @@ -134,9 +136,8 @@ export class EntityMapperService {
entity: T,
forceUpdate: boolean = false
): Promise<any> {
const rawData = this.entitySchemaService.transformEntityToDatabaseFormat(
entity
);
const rawData =
this.entitySchemaService.transformEntityToDatabaseFormat(entity);
const result = await this._db.put(rawData, forceUpdate);
if (result?.ok) {
entity._rev = result.rev;
Expand Down

0 comments on commit d1f9de3

Please sign in to comment.